1 ///////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
5 // Modified by: Robin Dunn, Vadim Zeitlin, Santiago Palacios
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
25 #include "wx/dcclient.h"
26 #include "wx/settings.h"
28 #include "wx/textctrl.h"
29 #include "wx/checkbox.h"
30 #include "wx/combobox.h"
31 #include "wx/valtext.h"
34 #include "wx/listbox.h"
37 #include "wx/textfile.h"
38 #include "wx/spinctrl.h"
39 #include "wx/tokenzr.h"
40 #include "wx/renderer.h"
42 #include "wx/generic/gridsel.h"
44 const wxChar wxGridNameStr
[] = wxT("grid");
46 #if defined(__WXMOTIF__)
47 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
49 #define WXUNUSED_MOTIF(identifier) identifier
52 #if defined(__WXGTK__)
53 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
55 #define WXUNUSED_GTK(identifier) identifier
58 // Required for wxIs... functions
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr
*, wxArrayAttrs
,
66 class WXDLLIMPEXP_ADV
);
68 struct wxGridCellWithAttr
70 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
71 : coords(row
, col
), attr(attr_
)
80 wxGridCellCoords coords
;
84 // DECLARE_NO_COPY_CLASS(wxGridCellWithAttr)
85 // without rewriting the macros, which require a public copy constructor.
88 WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr
, wxGridCellWithAttrArray
,
89 class WXDLLIMPEXP_ADV
);
91 #include "wx/arrimpl.cpp"
93 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
94 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
100 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
101 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
102 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
103 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
104 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_BEGIN_DRAG
)
105 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
106 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
107 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
108 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
109 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
110 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
111 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_MOVE
)
112 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
113 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
114 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
115 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
116 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
117 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
119 // ----------------------------------------------------------------------------
121 // ----------------------------------------------------------------------------
123 // common base class for various grid subwindows
124 class WXDLLIMPEXP_ADV wxGridSubwindow
: public wxWindow
127 wxGridSubwindow() { m_owner
= NULL
; }
128 wxGridSubwindow(wxGrid
*owner
,
132 int additionalStyle
= 0,
133 const wxString
& name
= wxPanelNameStr
)
134 : wxWindow(owner
, id
, pos
, size
,
135 wxBORDER_NONE
| additionalStyle
,
141 virtual bool AcceptsFocus() const { return false; }
143 wxGrid
*GetOwner() { return m_owner
; }
146 void OnMouseCaptureLost(wxMouseCaptureLostEvent
& event
);
150 DECLARE_EVENT_TABLE()
151 DECLARE_NO_COPY_CLASS(wxGridSubwindow
)
154 class WXDLLIMPEXP_ADV wxGridRowLabelWindow
: public wxGridSubwindow
157 wxGridRowLabelWindow() { }
158 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
159 const wxPoint
&pos
, const wxSize
&size
);
162 void OnPaint( wxPaintEvent
& event
);
163 void OnMouseEvent( wxMouseEvent
& event
);
164 void OnMouseWheel( wxMouseEvent
& event
);
166 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
167 DECLARE_EVENT_TABLE()
168 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow
)
172 class WXDLLIMPEXP_ADV wxGridColLabelWindow
: public wxGridSubwindow
175 wxGridColLabelWindow() { }
176 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
177 const wxPoint
&pos
, const wxSize
&size
);
180 void OnPaint( wxPaintEvent
& event
);
181 void OnMouseEvent( wxMouseEvent
& event
);
182 void OnMouseWheel( wxMouseEvent
& event
);
184 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
185 DECLARE_EVENT_TABLE()
186 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow
)
190 class WXDLLIMPEXP_ADV wxGridCornerLabelWindow
: public wxGridSubwindow
193 wxGridCornerLabelWindow() { }
194 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
195 const wxPoint
&pos
, const wxSize
&size
);
198 void OnMouseEvent( wxMouseEvent
& event
);
199 void OnMouseWheel( wxMouseEvent
& event
);
200 void OnPaint( wxPaintEvent
& event
);
202 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
203 DECLARE_EVENT_TABLE()
204 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow
)
207 class WXDLLIMPEXP_ADV wxGridWindow
: public wxGridSubwindow
212 m_rowLabelWin
= NULL
;
213 m_colLabelWin
= NULL
;
216 wxGridWindow( wxGrid
*parent
,
217 wxGridRowLabelWindow
*rowLblWin
,
218 wxGridColLabelWindow
*colLblWin
,
219 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
221 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
223 virtual bool AcceptsFocus() const { return true; }
226 wxGridRowLabelWindow
*m_rowLabelWin
;
227 wxGridColLabelWindow
*m_colLabelWin
;
229 void OnPaint( wxPaintEvent
&event
);
230 void OnMouseWheel( wxMouseEvent
& event
);
231 void OnMouseEvent( wxMouseEvent
& event
);
232 void OnKeyDown( wxKeyEvent
& );
233 void OnKeyUp( wxKeyEvent
& );
234 void OnChar( wxKeyEvent
& );
235 void OnEraseBackground( wxEraseEvent
& );
236 void OnFocus( wxFocusEvent
& );
238 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
239 DECLARE_EVENT_TABLE()
240 DECLARE_NO_COPY_CLASS(wxGridWindow
)
244 class wxGridCellEditorEvtHandler
: public wxEvtHandler
247 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
254 void OnKillFocus(wxFocusEvent
& event
);
255 void OnKeyDown(wxKeyEvent
& event
);
256 void OnChar(wxKeyEvent
& event
);
258 void SetInSetFocus(bool inSetFocus
) { m_inSetFocus
= inSetFocus
; }
262 wxGridCellEditor
*m_editor
;
264 // Work around the fact that a focus kill event can be sent to
265 // a combobox within a set focus event.
268 DECLARE_EVENT_TABLE()
269 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
270 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler
)
274 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler
, wxEvtHandler
)
276 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
277 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus
)
278 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
279 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
283 // ----------------------------------------------------------------------------
284 // the internal data representation used by wxGridCellAttrProvider
285 // ----------------------------------------------------------------------------
287 // this class stores attributes set for cells
288 class WXDLLIMPEXP_ADV wxGridCellAttrData
291 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
292 wxGridCellAttr
*GetAttr(int row
, int col
) const;
293 void UpdateAttrRows( size_t pos
, int numRows
);
294 void UpdateAttrCols( size_t pos
, int numCols
);
297 // searches for the attr for given cell, returns wxNOT_FOUND if not found
298 int FindIndex(int row
, int col
) const;
300 wxGridCellWithAttrArray m_attrs
;
303 // this class stores attributes set for rows or columns
304 class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
307 // empty ctor to suppress warnings
308 wxGridRowOrColAttrData() {}
309 ~wxGridRowOrColAttrData();
311 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
312 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
313 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
316 wxArrayInt m_rowsOrCols
;
317 wxArrayAttrs m_attrs
;
320 // NB: this is just a wrapper around 3 objects: one which stores cell
321 // attributes, and 2 others for row/col ones
322 class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
325 wxGridCellAttrData m_cellAttrs
;
326 wxGridRowOrColAttrData m_rowAttrs
,
331 // ----------------------------------------------------------------------------
332 // data structures used for the data type registry
333 // ----------------------------------------------------------------------------
335 struct wxGridDataTypeInfo
337 wxGridDataTypeInfo(const wxString
& typeName
,
338 wxGridCellRenderer
* renderer
,
339 wxGridCellEditor
* editor
)
340 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
343 ~wxGridDataTypeInfo()
345 wxSafeDecRef(m_renderer
);
346 wxSafeDecRef(m_editor
);
350 wxGridCellRenderer
* m_renderer
;
351 wxGridCellEditor
* m_editor
;
353 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo
)
357 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
,
358 class WXDLLIMPEXP_ADV
);
361 class WXDLLIMPEXP_ADV wxGridTypeRegistry
364 wxGridTypeRegistry() {}
365 ~wxGridTypeRegistry();
367 void RegisterDataType(const wxString
& typeName
,
368 wxGridCellRenderer
* renderer
,
369 wxGridCellEditor
* editor
);
371 // find one of already registered data types
372 int FindRegisteredDataType(const wxString
& typeName
);
374 // try to FindRegisteredDataType(), if this fails and typeName is one of
375 // standard typenames, register it and return its index
376 int FindDataType(const wxString
& typeName
);
378 // try to FindDataType(), if it fails see if it is not one of already
379 // registered data types with some params in which case clone the
380 // registered data type and set params for it
381 int FindOrCloneDataType(const wxString
& typeName
);
383 wxGridCellRenderer
* GetRenderer(int index
);
384 wxGridCellEditor
* GetEditor(int index
);
387 wxGridDataTypeInfoArray m_typeinfo
;
391 // ----------------------------------------------------------------------------
392 // conditional compilation
393 // ----------------------------------------------------------------------------
395 #ifndef WXGRID_DRAW_LINES
396 #define WXGRID_DRAW_LINES 1
399 // ----------------------------------------------------------------------------
401 // ----------------------------------------------------------------------------
403 //#define DEBUG_ATTR_CACHE
404 #ifdef DEBUG_ATTR_CACHE
405 static size_t gs_nAttrCacheHits
= 0;
406 static size_t gs_nAttrCacheMisses
= 0;
409 // ----------------------------------------------------------------------------
411 // ----------------------------------------------------------------------------
413 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
414 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
417 // TODO: this doesn't work at all, grid cells have different sizes and approx
418 // calculations don't work as because of the size mismatch scrollbars
419 // sometimes fail to be shown when they should be or vice versa
421 // The scroll bars may be a little flakey once in a while, but that is
422 // surely much less horrible than having scroll lines of only 1!!!
425 // Well, it's still seriously broken so it might be better but needs
428 static const size_t GRID_SCROLL_LINE_X
= 15; // 1;
429 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
431 // the size of hash tables used a bit everywhere (the max number of elements
432 // in these hash tables is the number of rows/columns)
433 static const int GRID_HASH_SIZE
= 100;
436 // ----------------------------------------------------------------------------
438 // ----------------------------------------------------------------------------
440 static inline int GetScrollX(int x
)
442 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
445 static inline int GetScrollY(int y
)
447 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
451 // ============================================================================
453 // ============================================================================
455 // ----------------------------------------------------------------------------
457 // ----------------------------------------------------------------------------
459 wxGridCellEditor::wxGridCellEditor()
465 wxGridCellEditor::~wxGridCellEditor()
470 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
471 wxWindowID
WXUNUSED(id
),
472 wxEvtHandler
* evtHandler
)
475 m_control
->PushEventHandler(evtHandler
);
478 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
479 wxGridCellAttr
*attr
)
481 // erase the background because we might not fill the cell
482 wxClientDC
dc(m_control
->GetParent());
483 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
485 gridWindow
->GetOwner()->PrepareDC(dc
);
487 dc
.SetPen(*wxTRANSPARENT_PEN
);
488 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxBRUSHSTYLE_SOLID
));
489 dc
.DrawRectangle(rectCell
);
491 // redraw the control we just painted over
492 m_control
->Refresh();
495 void wxGridCellEditor::Destroy()
499 m_control
->PopEventHandler( true /* delete it*/ );
501 m_control
->Destroy();
506 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
508 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
510 m_control
->Show(show
);
514 // set the colours/fonts if we have any
517 m_colFgOld
= m_control
->GetForegroundColour();
518 m_control
->SetForegroundColour(attr
->GetTextColour());
520 m_colBgOld
= m_control
->GetBackgroundColour();
521 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
523 // Workaround for GTK+1 font setting problem on some platforms
524 #if !defined(__WXGTK__) || defined(__WXGTK20__)
525 m_fontOld
= m_control
->GetFont();
526 m_control
->SetFont(attr
->GetFont());
529 // can't do anything more in the base class version, the other
530 // attributes may only be used by the derived classes
535 // restore the standard colours fonts
536 if ( m_colFgOld
.Ok() )
538 m_control
->SetForegroundColour(m_colFgOld
);
539 m_colFgOld
= wxNullColour
;
542 if ( m_colBgOld
.Ok() )
544 m_control
->SetBackgroundColour(m_colBgOld
);
545 m_colBgOld
= wxNullColour
;
548 // Workaround for GTK+1 font setting problem on some platforms
549 #if !defined(__WXGTK__) || defined(__WXGTK20__)
550 if ( m_fontOld
.Ok() )
552 m_control
->SetFont(m_fontOld
);
553 m_fontOld
= wxNullFont
;
559 void wxGridCellEditor::SetSize(const wxRect
& rect
)
561 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
563 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
566 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
571 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
573 bool ctrl
= event
.ControlDown();
574 bool alt
= event
.AltDown();
577 // On the Mac the Alt key is more like shift and is used for entry of
578 // valid characters, so check for Ctrl and Meta instead.
579 alt
= event
.MetaDown();
582 // Assume it's not a valid char if ctrl or alt is down, but if both are
583 // down then it may be because of an AltGr key combination, so let them
584 // through in that case.
585 if ((ctrl
|| alt
) && !(ctrl
&& alt
))
592 // If it's a F-Key or other special key then it shouldn't start the
594 if (event
.GetKeyCode() >= WXK_START
)
598 // if the unicode key code is not really a unicode character (it may
599 // be a function key or etc., the platforms appear to always give us a
600 // small value in this case) then fallback to the ASCII key code but
601 // don't do anything for function keys or etc.
602 key
= event
.GetUnicodeKey();
605 key
= event
.GetKeyCode();
606 keyOk
= (key
<= 127);
609 key
= event
.GetKeyCode();
610 keyOk
= (key
<= 255);
616 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
621 void wxGridCellEditor::StartingClick()
627 // ----------------------------------------------------------------------------
628 // wxGridCellTextEditor
629 // ----------------------------------------------------------------------------
631 wxGridCellTextEditor::wxGridCellTextEditor()
636 void wxGridCellTextEditor::Create(wxWindow
* parent
,
638 wxEvtHandler
* evtHandler
)
640 DoCreate(parent
, id
, evtHandler
);
643 void wxGridCellTextEditor::DoCreate(wxWindow
* parent
,
645 wxEvtHandler
* evtHandler
,
648 style
|= wxTE_PROCESS_ENTER
|
653 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
654 wxDefaultPosition
, wxDefaultSize
,
657 // set max length allowed in the textctrl, if the parameter was set
658 if ( m_maxChars
!= 0 )
660 Text()->SetMaxLength(m_maxChars
);
663 wxGridCellEditor::Create(parent
, id
, evtHandler
);
666 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
667 wxGridCellAttr
* WXUNUSED(attr
))
669 // as we fill the entire client area,
670 // don't do anything here to minimize flicker
673 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
675 wxRect
rect(rectOrig
);
677 // Make the edit control large enough to allow for internal margins
679 // TODO: remove this if the text ctrl sizing is improved esp. for unix
681 #if defined(__WXGTK__)
689 #elif defined(__WXMSW__)
703 int extra_x
= ( rect
.x
> 2 ) ? 2 : 1;
704 int extra_y
= ( rect
.y
> 2 ) ? 2 : 1;
706 #if defined(__WXMOTIF__)
711 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
712 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
713 rect
.SetRight( rect
.GetRight() + 2 * extra_x
);
714 rect
.SetBottom( rect
.GetBottom() + 2 * extra_y
);
717 wxGridCellEditor::SetSize(rect
);
720 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
722 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
724 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
726 DoBeginEdit(m_startValue
);
729 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
731 Text()->SetValue(startValue
);
732 Text()->SetInsertionPointEnd();
733 Text()->SetSelection(-1, -1);
737 bool wxGridCellTextEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
739 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
741 bool changed
= false;
742 wxString value
= Text()->GetValue();
743 if (value
!= m_startValue
)
747 grid
->GetTable()->SetValue(row
, col
, value
);
749 m_startValue
= wxEmptyString
;
751 // No point in setting the text of the hidden control
752 //Text()->SetValue(m_startValue);
757 void wxGridCellTextEditor::Reset()
759 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
761 DoReset(m_startValue
);
764 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
766 Text()->SetValue(startValue
);
767 Text()->SetInsertionPointEnd();
770 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
772 return wxGridCellEditor::IsAcceptedKey(event
);
775 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
777 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
778 // longer an appropriate way to get the character into the text control.
779 // Do it ourselves instead. We know that if we get this far that we have
780 // a valid character, so not a whole lot of testing needs to be done.
782 wxTextCtrl
* tc
= Text();
787 ch
= event
.GetUnicodeKey();
789 ch
= (wxChar
)event
.GetKeyCode();
791 ch
= (wxChar
)event
.GetKeyCode();
797 // delete the character at the cursor
798 pos
= tc
->GetInsertionPoint();
799 if (pos
< tc
->GetLastPosition())
800 tc
->Remove(pos
, pos
+ 1);
804 // delete the character before the cursor
805 pos
= tc
->GetInsertionPoint();
807 tc
->Remove(pos
- 1, pos
);
816 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
817 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
819 #if defined(__WXMOTIF__) || defined(__WXGTK__)
820 // wxMotif needs a little extra help...
821 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
822 wxString
s( Text()->GetValue() );
823 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
825 Text()->SetInsertionPoint( pos
);
827 // the other ports can handle a Return key press
833 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
843 if ( params
.ToLong(&tmp
) )
845 m_maxChars
= (size_t)tmp
;
849 wxLogDebug( _T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str() );
854 // return the value in the text control
855 wxString
wxGridCellTextEditor::GetValue() const
857 return Text()->GetValue();
860 // ----------------------------------------------------------------------------
861 // wxGridCellNumberEditor
862 // ----------------------------------------------------------------------------
864 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
870 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
872 wxEvtHandler
* evtHandler
)
877 // create a spin ctrl
878 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
879 wxDefaultPosition
, wxDefaultSize
,
883 wxGridCellEditor::Create(parent
, id
, evtHandler
);
888 // just a text control
889 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
892 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
897 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
899 // first get the value
900 wxGridTableBase
*table
= grid
->GetTable();
901 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
903 m_valueOld
= table
->GetValueAsLong(row
, col
);
908 wxString sValue
= table
->GetValue(row
, col
);
909 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty())
911 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
919 Spin()->SetValue((int)m_valueOld
);
925 DoBeginEdit(GetString());
929 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
939 value
= Spin()->GetValue();
940 changed
= value
!= m_valueOld
;
942 text
= wxString::Format(wxT("%ld"), value
);
947 text
= Text()->GetValue();
948 changed
= (text
.empty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
953 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
954 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
956 grid
->GetTable()->SetValue(row
, col
, text
);
962 void wxGridCellNumberEditor::Reset()
967 Spin()->SetValue((int)m_valueOld
);
972 DoReset(GetString());
976 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
978 if ( wxGridCellEditor::IsAcceptedKey(event
) )
980 int keycode
= event
.GetKeyCode();
981 if ( (keycode
< 128) &&
982 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
991 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
993 int keycode
= event
.GetKeyCode();
996 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
998 wxGridCellTextEditor::StartingKey(event
);
1000 // skip Skip() below
1007 if ( wxIsdigit(keycode
) )
1009 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
1010 spin
->SetValue(keycode
- '0');
1011 spin
->SetSelection(1,1);
1020 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
1031 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1035 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1039 // skip the error message below
1044 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1048 // return the value in the spin control if it is there (the text control otherwise)
1049 wxString
wxGridCellNumberEditor::GetValue() const
1056 long value
= Spin()->GetValue();
1057 s
.Printf(wxT("%ld"), value
);
1062 s
= Text()->GetValue();
1068 // ----------------------------------------------------------------------------
1069 // wxGridCellFloatEditor
1070 // ----------------------------------------------------------------------------
1072 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1075 m_precision
= precision
;
1078 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1080 wxEvtHandler
* evtHandler
)
1082 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1084 #if wxUSE_VALIDATORS
1085 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1089 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1091 // first get the value
1092 wxGridTableBase
*table
= grid
->GetTable();
1093 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1095 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1100 wxString sValue
= table
->GetValue(row
, col
);
1101 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.empty())
1103 wxFAIL_MSG( _T("this cell doesn't have float value") );
1108 DoBeginEdit(GetString());
1111 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1115 wxString
text(Text()->GetValue());
1117 if ( (text
.empty() || text
.ToDouble(&value
)) &&
1118 !wxIsSameDouble(value
, m_valueOld
) )
1120 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1121 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1123 grid
->GetTable()->SetValue(row
, col
, text
);
1131 void wxGridCellFloatEditor::Reset()
1133 DoReset(GetString());
1136 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1138 int keycode
= event
.GetKeyCode();
1140 tmpbuf
[0] = (char) keycode
;
1142 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1145 bool is_decimal_point
= ( strbuf
==
1146 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1148 bool is_decimal_point
= ( strbuf
== _T(".") );
1151 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1152 || is_decimal_point
)
1154 wxGridCellTextEditor::StartingKey(event
);
1156 // skip Skip() below
1163 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1174 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1178 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1180 m_precision
= (int)tmp
;
1182 // skip the error message below
1187 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1191 wxString
wxGridCellFloatEditor::GetString() const
1194 if ( m_precision
== -1 && m_width
!= -1)
1196 // default precision
1197 fmt
.Printf(_T("%%%d.f"), m_width
);
1199 else if ( m_precision
!= -1 && m_width
== -1)
1202 fmt
.Printf(_T("%%.%df"), m_precision
);
1204 else if ( m_precision
!= -1 && m_width
!= -1 )
1206 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1210 // default width/precision
1214 return wxString::Format(fmt
, m_valueOld
);
1217 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1219 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1221 const int keycode
= event
.GetKeyCode();
1222 if ( isascii(keycode
) )
1225 tmpbuf
[0] = (char) keycode
;
1227 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1230 const wxString decimalPoint
=
1231 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
1233 const wxString
decimalPoint(_T('.'));
1236 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1237 if ( wxIsdigit(keycode
) ||
1238 tolower(keycode
) == 'e' ||
1239 keycode
== decimalPoint
||
1251 #endif // wxUSE_TEXTCTRL
1255 // ----------------------------------------------------------------------------
1256 // wxGridCellBoolEditor
1257 // ----------------------------------------------------------------------------
1259 // the default values for GetValue()
1260 wxString
wxGridCellBoolEditor::ms_stringValues
[2] = { _T(""), _T("1") };
1262 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1264 wxEvtHandler
* evtHandler
)
1266 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1267 wxDefaultPosition
, wxDefaultSize
,
1270 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1273 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1275 bool resize
= false;
1276 wxSize size
= m_control
->GetSize();
1277 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1279 // check if the checkbox is not too big/small for this cell
1280 wxSize sizeBest
= m_control
->GetBestSize();
1281 if ( !(size
== sizeBest
) )
1283 // reset to default size if it had been made smaller
1289 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1291 // leave 1 pixel margin
1292 size
.x
= size
.y
= minSize
- 2;
1299 m_control
->SetSize(size
);
1302 // position it in the centre of the rectangle (TODO: support alignment?)
1304 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1305 // the checkbox without label still has some space to the right in wxGTK,
1306 // so shift it to the right
1308 #elif defined(__WXMSW__)
1309 // here too, but in other way
1314 int hAlign
= wxALIGN_CENTRE
;
1315 int vAlign
= wxALIGN_CENTRE
;
1317 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1320 if (hAlign
== wxALIGN_LEFT
)
1328 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1330 else if (hAlign
== wxALIGN_RIGHT
)
1332 x
= r
.x
+ r
.width
- size
.x
- 2;
1333 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1335 else if (hAlign
== wxALIGN_CENTRE
)
1337 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1338 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1341 m_control
->Move(x
, y
);
1344 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1346 m_control
->Show(show
);
1350 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1351 CBox()->SetBackgroundColour(colBg
);
1355 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1357 wxASSERT_MSG(m_control
,
1358 wxT("The wxGridCellEditor must be created first!"));
1360 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1362 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1366 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1368 if ( cellval
== ms_stringValues
[false] )
1369 m_startValue
= false;
1370 else if ( cellval
== ms_stringValues
[true] )
1371 m_startValue
= true;
1374 // do not try to be smart here and convert it to true or false
1375 // because we'll still overwrite it with something different and
1376 // this risks to be very surprising for the user code, let them
1378 wxFAIL_MSG( _T("invalid value for a cell with bool editor!") );
1382 CBox()->SetValue(m_startValue
);
1386 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1389 wxASSERT_MSG(m_control
,
1390 wxT("The wxGridCellEditor must be created first!"));
1392 bool changed
= false;
1393 bool value
= CBox()->GetValue();
1394 if ( value
!= m_startValue
)
1399 wxGridTableBase
* const table
= grid
->GetTable();
1400 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1401 table
->SetValueAsBool(row
, col
, value
);
1403 table
->SetValue(row
, col
, GetValue());
1409 void wxGridCellBoolEditor::Reset()
1411 wxASSERT_MSG(m_control
,
1412 wxT("The wxGridCellEditor must be created first!"));
1414 CBox()->SetValue(m_startValue
);
1417 void wxGridCellBoolEditor::StartingClick()
1419 CBox()->SetValue(!CBox()->GetValue());
1422 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1424 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1426 int keycode
= event
.GetKeyCode();
1439 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1441 int keycode
= event
.GetKeyCode();
1445 CBox()->SetValue(!CBox()->GetValue());
1449 CBox()->SetValue(true);
1453 CBox()->SetValue(false);
1458 wxString
wxGridCellBoolEditor::GetValue() const
1460 return ms_stringValues
[CBox()->GetValue()];
1464 wxGridCellBoolEditor::UseStringValues(const wxString
& valueTrue
,
1465 const wxString
& valueFalse
)
1467 ms_stringValues
[false] = valueFalse
;
1468 ms_stringValues
[true] = valueTrue
;
1472 wxGridCellBoolEditor::IsTrueValue(const wxString
& value
)
1474 return value
== ms_stringValues
[true];
1477 #endif // wxUSE_CHECKBOX
1481 // ----------------------------------------------------------------------------
1482 // wxGridCellChoiceEditor
1483 // ----------------------------------------------------------------------------
1485 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1487 : m_choices(choices
),
1488 m_allowOthers(allowOthers
) { }
1490 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1491 const wxString choices
[],
1493 : m_allowOthers(allowOthers
)
1497 m_choices
.Alloc(count
);
1498 for ( size_t n
= 0; n
< count
; n
++ )
1500 m_choices
.Add(choices
[n
]);
1505 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1507 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1508 editor
->m_allowOthers
= m_allowOthers
;
1509 editor
->m_choices
= m_choices
;
1514 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1516 wxEvtHandler
* evtHandler
)
1518 int style
= wxTE_PROCESS_ENTER
|
1522 if ( !m_allowOthers
)
1523 style
|= wxCB_READONLY
;
1524 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1525 wxDefaultPosition
, wxDefaultSize
,
1529 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1532 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1533 wxGridCellAttr
* attr
)
1535 // as we fill the entire client area, don't do anything here to minimize
1538 // TODO: It doesn't actually fill the client area since the height of a
1539 // combo always defaults to the standard. Until someone has time to
1540 // figure out the right rectangle to paint, just do it the normal way.
1541 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1544 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1546 wxASSERT_MSG(m_control
,
1547 wxT("The wxGridCellEditor must be created first!"));
1549 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1551 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1553 // Don't immediately end if we get a kill focus event within BeginEdit
1555 evtHandler
->SetInSetFocus(true);
1557 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1561 Combo()->SetValue(m_startValue
);
1562 Combo()->SetInsertionPointEnd();
1564 else // the combobox is read-only
1566 // find the right position, or default to the first if not found
1567 int pos
= Combo()->FindString(m_startValue
);
1568 if (pos
== wxNOT_FOUND
)
1570 Combo()->SetSelection(pos
);
1573 Combo()->SetFocus();
1577 // When dropping down the menu, a kill focus event
1578 // happens after this point, so we can't reset the flag yet.
1579 #if !defined(__WXGTK20__)
1580 evtHandler
->SetInSetFocus(false);
1585 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1588 wxString value
= Combo()->GetValue();
1589 if ( value
== m_startValue
)
1592 grid
->GetTable()->SetValue(row
, col
, value
);
1597 void wxGridCellChoiceEditor::Reset()
1599 Combo()->SetValue(m_startValue
);
1600 Combo()->SetInsertionPointEnd();
1603 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1613 wxStringTokenizer
tk(params
, _T(','));
1614 while ( tk
.HasMoreTokens() )
1616 m_choices
.Add(tk
.GetNextToken());
1620 // return the value in the text control
1621 wxString
wxGridCellChoiceEditor::GetValue() const
1623 return Combo()->GetValue();
1626 #endif // wxUSE_COMBOBOX
1628 // ----------------------------------------------------------------------------
1629 // wxGridCellEditorEvtHandler
1630 // ----------------------------------------------------------------------------
1632 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
1634 // Don't disable the cell if we're just starting to edit it
1639 m_grid
->DisableCellEditControl();
1644 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1646 switch ( event
.GetKeyCode() )
1650 m_grid
->DisableCellEditControl();
1654 m_grid
->GetEventHandler()->ProcessEvent( event
);
1658 case WXK_NUMPAD_ENTER
:
1659 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1660 m_editor
->HandleReturn(event
);
1669 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1671 int row
= m_grid
->GetGridCursorRow();
1672 int col
= m_grid
->GetGridCursorCol();
1673 wxRect rect
= m_grid
->CellToRect( row
, col
);
1675 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
1677 // if cell width is smaller than grid client area, cell is wholly visible
1678 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
1680 switch ( event
.GetKeyCode() )
1685 case WXK_NUMPAD_ENTER
:
1690 if ( wholeCellVisible
)
1692 // no special processing needed...
1697 // do special processing for partly visible cell...
1699 // get the widths of all cells previous to this one
1701 for ( int i
= 0; i
< col
; i
++ )
1703 colXPos
+= m_grid
->GetColSize(i
);
1706 int xUnit
= 1, yUnit
= 1;
1707 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1710 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1714 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
1722 if ( wholeCellVisible
)
1724 // no special processing needed...
1729 // do special processing for partly visible cell...
1732 wxString value
= m_grid
->GetCellValue(row
, col
);
1733 if ( wxEmptyString
!= value
)
1735 // get width of cell CONTENTS (text)
1737 wxFont font
= m_grid
->GetCellFont(row
, col
);
1738 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
1740 // try to RIGHT align the text by scrolling
1741 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
1743 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
1744 // otherwise the last part of the cell content might be hidden below the scroll bar
1745 // FIXME: maybe there is a more suitable correction?
1746 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
1747 if ( textWidth
< 0 )
1753 // get the widths of all cells previous to this one
1755 for ( int i
= 0; i
< col
; i
++ )
1757 colXPos
+= m_grid
->GetColSize(i
);
1760 // and add the (modified) text width of the cell contents
1761 // as we'd like to see the last part of the cell contents
1762 colXPos
+= textWidth
;
1764 int xUnit
= 1, yUnit
= 1;
1765 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1766 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1777 // ----------------------------------------------------------------------------
1778 // wxGridCellWorker is an (almost) empty common base class for
1779 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1780 // ----------------------------------------------------------------------------
1782 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1787 wxGridCellWorker::~wxGridCellWorker()
1791 // ============================================================================
1793 // ============================================================================
1795 // ----------------------------------------------------------------------------
1796 // wxGridCellRenderer
1797 // ----------------------------------------------------------------------------
1799 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1800 wxGridCellAttr
& attr
,
1803 int WXUNUSED(row
), int WXUNUSED(col
),
1806 dc
.SetBackgroundMode( wxBRUSHSTYLE_SOLID
);
1808 // grey out fields if the grid is disabled
1809 if ( grid
.IsEnabled() )
1814 if ( grid
.HasFocus() )
1815 clr
= grid
.GetSelectionBackground();
1817 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
1818 dc
.SetBrush( wxBrush(clr
, wxBRUSHSTYLE_SOLID
) );
1822 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxBRUSHSTYLE_SOLID
) );
1827 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
), wxBRUSHSTYLE_SOLID
));
1830 dc
.SetPen( *wxTRANSPARENT_PEN
);
1831 dc
.DrawRectangle(rect
);
1834 // ----------------------------------------------------------------------------
1835 // wxGridCellStringRenderer
1836 // ----------------------------------------------------------------------------
1838 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
1839 const wxGridCellAttr
& attr
,
1843 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
1845 // TODO some special colours for attr.IsReadOnly() case?
1847 // different coloured text when the grid is disabled
1848 if ( grid
.IsEnabled() )
1853 if ( grid
.HasFocus() )
1854 clr
= grid
.GetSelectionBackground();
1856 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
1857 dc
.SetTextBackground( clr
);
1858 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1862 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1863 dc
.SetTextForeground( attr
.GetTextColour() );
1868 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1869 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
1872 dc
.SetFont( attr
.GetFont() );
1875 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
1877 const wxString
& text
)
1879 wxCoord x
= 0, y
= 0, max_x
= 0;
1880 dc
.SetFont(attr
.GetFont());
1881 wxStringTokenizer
tk(text
, _T('\n'));
1882 while ( tk
.HasMoreTokens() )
1884 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1885 max_x
= wxMax(max_x
, x
);
1888 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1890 return wxSize(max_x
, y
);
1893 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1894 wxGridCellAttr
& attr
,
1898 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1901 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1902 wxGridCellAttr
& attr
,
1904 const wxRect
& rectCell
,
1908 wxRect rect
= rectCell
;
1911 // erase only this cells background, overflow cells should have been erased
1912 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1915 attr
.GetAlignment(&hAlign
, &vAlign
);
1917 int overflowCols
= 0;
1919 if (attr
.GetOverflow())
1921 int cols
= grid
.GetNumberCols();
1922 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1923 int cell_rows
, cell_cols
;
1924 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <= 0
1925 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1927 int i
, c_cols
, c_rows
;
1928 for (i
= col
+cell_cols
; i
< cols
; i
++)
1930 bool is_empty
= true;
1931 for (int j
=row
; j
< row
+ cell_rows
; j
++)
1933 // check w/ anchor cell for multicell block
1934 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1937 if (!grid
.GetTable()->IsEmptyCell(j
+ c_rows
, i
))
1946 rect
.width
+= grid
.GetColSize(i
);
1954 if (rect
.width
>= best_width
)
1958 overflowCols
= i
- col
- cell_cols
+ 1;
1959 if (overflowCols
>= cols
)
1960 overflowCols
= cols
- 1;
1963 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1965 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1967 clip
.x
+= rectCell
.width
;
1968 // draw each overflow cell individually
1969 int col_end
= col
+ cell_cols
+ overflowCols
;
1970 if (col_end
>= grid
.GetNumberCols())
1971 col_end
= grid
.GetNumberCols() - 1;
1972 for (int i
= col
+ cell_cols
; i
<= col_end
; i
++)
1974 clip
.width
= grid
.GetColSize(i
) - 1;
1975 dc
.DestroyClippingRegion();
1976 dc
.SetClippingRegion(clip
);
1978 SetTextColoursAndFont(grid
, attr
, dc
,
1979 grid
.IsInSelection(row
,i
));
1981 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1982 rect
, hAlign
, vAlign
);
1983 clip
.x
+= grid
.GetColSize(i
) - 1;
1989 dc
.DestroyClippingRegion();
1993 // now we only have to draw the text
1994 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1996 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1997 rect
, hAlign
, vAlign
);
2000 // ----------------------------------------------------------------------------
2001 // wxGridCellNumberRenderer
2002 // ----------------------------------------------------------------------------
2004 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2006 wxGridTableBase
*table
= grid
.GetTable();
2008 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
2010 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
2014 text
= table
->GetValue(row
, col
);
2020 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
2021 wxGridCellAttr
& attr
,
2023 const wxRect
& rectCell
,
2027 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2029 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2031 // draw the text right aligned by default
2033 attr
.GetAlignment(&hAlign
, &vAlign
);
2034 hAlign
= wxALIGN_RIGHT
;
2036 wxRect rect
= rectCell
;
2039 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2042 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
2043 wxGridCellAttr
& attr
,
2047 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2050 // ----------------------------------------------------------------------------
2051 // wxGridCellFloatRenderer
2052 // ----------------------------------------------------------------------------
2054 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
2057 SetPrecision(precision
);
2060 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
2062 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
2063 renderer
->m_width
= m_width
;
2064 renderer
->m_precision
= m_precision
;
2065 renderer
->m_format
= m_format
;
2070 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2072 wxGridTableBase
*table
= grid
.GetTable();
2077 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
2079 val
= table
->GetValueAsDouble(row
, col
);
2084 text
= table
->GetValue(row
, col
);
2085 hasDouble
= text
.ToDouble(&val
);
2092 if ( m_width
== -1 )
2094 if ( m_precision
== -1 )
2096 // default width/precision
2097 m_format
= _T("%f");
2101 m_format
.Printf(_T("%%.%df"), m_precision
);
2104 else if ( m_precision
== -1 )
2106 // default precision
2107 m_format
.Printf(_T("%%%d.f"), m_width
);
2111 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
2115 text
.Printf(m_format
, val
);
2118 //else: text already contains the string
2123 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
2124 wxGridCellAttr
& attr
,
2126 const wxRect
& rectCell
,
2130 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2132 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2134 // draw the text right aligned by default
2136 attr
.GetAlignment(&hAlign
, &vAlign
);
2137 hAlign
= wxALIGN_RIGHT
;
2139 wxRect rect
= rectCell
;
2142 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2145 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
2146 wxGridCellAttr
& attr
,
2150 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2153 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
2157 // reset to defaults
2163 wxString tmp
= params
.BeforeFirst(_T(','));
2167 if ( tmp
.ToLong(&width
) )
2169 SetWidth((int)width
);
2173 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
2177 tmp
= params
.AfterFirst(_T(','));
2181 if ( tmp
.ToLong(&precision
) )
2183 SetPrecision((int)precision
);
2187 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
2193 // ----------------------------------------------------------------------------
2194 // wxGridCellBoolRenderer
2195 // ----------------------------------------------------------------------------
2197 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
2199 // FIXME these checkbox size calculations are really ugly...
2201 // between checkmark and box
2202 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2204 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2205 wxGridCellAttr
& WXUNUSED(attr
),
2210 // compute it only once (no locks for MT safeness in GUI thread...)
2211 if ( !ms_sizeCheckMark
.x
)
2213 // get checkbox size
2214 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2215 wxSize size
= checkbox
->GetBestSize();
2216 wxCoord checkSize
= size
.y
+ 2 * wxGRID_CHECKMARK_MARGIN
;
2218 #if defined(__WXMOTIF__)
2219 checkSize
-= size
.y
/ 2;
2224 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2227 return ms_sizeCheckMark
;
2230 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2231 wxGridCellAttr
& attr
,
2237 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2239 // draw a check mark in the centre (ignoring alignment - TODO)
2240 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2242 // don't draw outside the cell
2243 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2244 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2246 // and even leave (at least) 1 pixel margin
2247 size
.x
= size
.y
= minSize
;
2250 // draw a border around checkmark
2252 attr
.GetAlignment(&hAlign
, &vAlign
);
2255 if (hAlign
== wxALIGN_CENTRE
)
2257 rectBorder
.x
= rect
.x
+ rect
.width
/ 2 - size
.x
/ 2;
2258 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2259 rectBorder
.width
= size
.x
;
2260 rectBorder
.height
= size
.y
;
2262 else if (hAlign
== wxALIGN_LEFT
)
2264 rectBorder
.x
= rect
.x
+ 2;
2265 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2266 rectBorder
.width
= size
.x
;
2267 rectBorder
.height
= size
.y
;
2269 else if (hAlign
== wxALIGN_RIGHT
)
2271 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2272 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2273 rectBorder
.width
= size
.x
;
2274 rectBorder
.height
= size
.y
;
2278 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2280 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2284 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2285 value
= wxGridCellBoolEditor::IsTrueValue(cellval
);
2290 flags
|= wxCONTROL_CHECKED
;
2292 wxRendererNative::Get().DrawCheckBox( &grid
, dc
, rectBorder
, flags
);
2295 // ----------------------------------------------------------------------------
2297 // ----------------------------------------------------------------------------
2299 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2303 m_isReadOnly
= Unset
;
2308 m_attrkind
= wxGridCellAttr::Cell
;
2310 m_sizeRows
= m_sizeCols
= 1;
2311 m_overflow
= UnsetOverflow
;
2313 SetDefAttr(attrDefault
);
2316 wxGridCellAttr
*wxGridCellAttr::Clone() const
2318 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2320 if ( HasTextColour() )
2321 attr
->SetTextColour(GetTextColour());
2322 if ( HasBackgroundColour() )
2323 attr
->SetBackgroundColour(GetBackgroundColour());
2325 attr
->SetFont(GetFont());
2326 if ( HasAlignment() )
2327 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2329 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2333 attr
->SetRenderer(m_renderer
);
2334 m_renderer
->IncRef();
2338 attr
->SetEditor(m_editor
);
2343 attr
->SetReadOnly();
2345 attr
->SetOverflow( m_overflow
== Overflow
);
2346 attr
->SetKind( m_attrkind
);
2351 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2353 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2354 SetTextColour(mergefrom
->GetTextColour());
2355 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2356 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2357 if ( !HasFont() && mergefrom
->HasFont() )
2358 SetFont(mergefrom
->GetFont());
2359 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2362 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2363 SetAlignment(hAlign
, vAlign
);
2365 if ( !HasSize() && mergefrom
->HasSize() )
2366 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2368 // Directly access member functions as GetRender/Editor don't just return
2369 // m_renderer/m_editor
2371 // Maybe add support for merge of Render and Editor?
2372 if (!HasRenderer() && mergefrom
->HasRenderer() )
2374 m_renderer
= mergefrom
->m_renderer
;
2375 m_renderer
->IncRef();
2377 if ( !HasEditor() && mergefrom
->HasEditor() )
2379 m_editor
= mergefrom
->m_editor
;
2382 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2383 SetReadOnly(mergefrom
->IsReadOnly());
2385 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2386 SetOverflow(mergefrom
->GetOverflow());
2388 SetDefAttr(mergefrom
->m_defGridAttr
);
2391 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2393 // The size of a cell is normally 1,1
2395 // If this cell is larger (2,2) then this is the top left cell
2396 // the other cells that will be covered (lower right cells) must be
2397 // set to negative or zero values such that
2398 // row + num_rows of the covered cell points to the larger cell (this cell)
2399 // same goes for the col + num_cols.
2401 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2403 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
2404 !((num_rows
<= 0) && (num_cols
> 0)) ||
2405 !((num_rows
== 0) && (num_cols
== 0))),
2406 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2408 m_sizeRows
= num_rows
;
2409 m_sizeCols
= num_cols
;
2412 const wxColour
& wxGridCellAttr::GetTextColour() const
2414 if (HasTextColour())
2418 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2420 return m_defGridAttr
->GetTextColour();
2424 wxFAIL_MSG(wxT("Missing default cell attribute"));
2425 return wxNullColour
;
2429 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2431 if (HasBackgroundColour())
2435 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2437 return m_defGridAttr
->GetBackgroundColour();
2441 wxFAIL_MSG(wxT("Missing default cell attribute"));
2442 return wxNullColour
;
2446 const wxFont
& wxGridCellAttr::GetFont() const
2452 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2454 return m_defGridAttr
->GetFont();
2458 wxFAIL_MSG(wxT("Missing default cell attribute"));
2463 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2472 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2474 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2478 wxFAIL_MSG(wxT("Missing default cell attribute"));
2482 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2485 *num_rows
= m_sizeRows
;
2487 *num_cols
= m_sizeCols
;
2490 // GetRenderer and GetEditor use a slightly different decision path about
2491 // which attribute to use. If a non-default attr object has one then it is
2492 // used, otherwise the default editor or renderer is fetched from the grid and
2493 // used. It should be the default for the data type of the cell. If it is
2494 // NULL (because the table has a type that the grid does not have in its
2495 // registry), then the grid's default editor or renderer is used.
2497 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(const wxGrid
* grid
, int row
, int col
) const
2499 wxGridCellRenderer
*renderer
= NULL
;
2501 if ( m_renderer
&& this != m_defGridAttr
)
2503 // use the cells renderer if it has one
2504 renderer
= m_renderer
;
2507 else // no non-default cell renderer
2509 // get default renderer for the data type
2512 // GetDefaultRendererForCell() will do IncRef() for us
2513 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2516 if ( renderer
== NULL
)
2518 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2520 // if we still don't have one then use the grid default
2521 // (no need for IncRef() here neither)
2522 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2524 else // default grid attr
2526 // use m_renderer which we had decided not to use initially
2527 renderer
= m_renderer
;
2534 // we're supposed to always find something
2535 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2540 // same as above, except for s/renderer/editor/g
2541 wxGridCellEditor
* wxGridCellAttr::GetEditor(const wxGrid
* grid
, int row
, int col
) const
2543 wxGridCellEditor
*editor
= NULL
;
2545 if ( m_editor
&& this != m_defGridAttr
)
2547 // use the cells editor if it has one
2551 else // no non default cell editor
2553 // get default editor for the data type
2556 // GetDefaultEditorForCell() will do IncRef() for us
2557 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2560 if ( editor
== NULL
)
2562 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2564 // if we still don't have one then use the grid default
2565 // (no need for IncRef() here neither)
2566 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2568 else // default grid attr
2570 // use m_editor which we had decided not to use initially
2578 // we're supposed to always find something
2579 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2584 // ----------------------------------------------------------------------------
2585 // wxGridCellAttrData
2586 // ----------------------------------------------------------------------------
2588 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2590 int n
= FindIndex(row
, col
);
2591 if ( n
== wxNOT_FOUND
)
2595 // add the attribute
2596 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2598 //else: nothing to do
2602 // free the old attribute
2603 m_attrs
[(size_t)n
].attr
->DecRef();
2607 // change the attribute
2608 m_attrs
[(size_t)n
].attr
= attr
;
2612 // remove this attribute
2613 m_attrs
.RemoveAt((size_t)n
);
2618 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2620 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2622 int n
= FindIndex(row
, col
);
2623 if ( n
!= wxNOT_FOUND
)
2625 attr
= m_attrs
[(size_t)n
].attr
;
2632 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2634 size_t count
= m_attrs
.GetCount();
2635 for ( size_t n
= 0; n
< count
; n
++ )
2637 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2638 wxCoord row
= coords
.GetRow();
2639 if ((size_t)row
>= pos
)
2643 // If rows inserted, include row counter where necessary
2644 coords
.SetRow(row
+ numRows
);
2646 else if (numRows
< 0)
2648 // If rows deleted ...
2649 if ((size_t)row
>= pos
- numRows
)
2651 // ...either decrement row counter (if row still exists)...
2652 coords
.SetRow(row
+ numRows
);
2656 // ...or remove the attribute
2657 // No need to DecRef the attribute itself since this is
2658 // done be wxGridCellWithAttr's destructor!
2659 m_attrs
.RemoveAt(n
);
2668 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2670 size_t count
= m_attrs
.GetCount();
2671 for ( size_t n
= 0; n
< count
; n
++ )
2673 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2674 wxCoord col
= coords
.GetCol();
2675 if ( (size_t)col
>= pos
)
2679 // If rows inserted, include row counter where necessary
2680 coords
.SetCol(col
+ numCols
);
2682 else if (numCols
< 0)
2684 // If rows deleted ...
2685 if ((size_t)col
>= pos
- numCols
)
2687 // ...either decrement row counter (if row still exists)...
2688 coords
.SetCol(col
+ numCols
);
2692 // ...or remove the attribute
2693 // No need to DecRef the attribute itself since this is
2694 // done be wxGridCellWithAttr's destructor!
2695 m_attrs
.RemoveAt(n
);
2704 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2706 size_t count
= m_attrs
.GetCount();
2707 for ( size_t n
= 0; n
< count
; n
++ )
2709 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2710 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2719 // ----------------------------------------------------------------------------
2720 // wxGridRowOrColAttrData
2721 // ----------------------------------------------------------------------------
2723 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2725 size_t count
= m_attrs
.GetCount();
2726 for ( size_t n
= 0; n
< count
; n
++ )
2728 m_attrs
[n
]->DecRef();
2732 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2734 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2736 int n
= m_rowsOrCols
.Index(rowOrCol
);
2737 if ( n
!= wxNOT_FOUND
)
2739 attr
= m_attrs
[(size_t)n
];
2746 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2748 int i
= m_rowsOrCols
.Index(rowOrCol
);
2749 if ( i
== wxNOT_FOUND
)
2753 // add the attribute
2754 m_rowsOrCols
.Add(rowOrCol
);
2757 // nothing to remove
2761 size_t n
= (size_t)i
;
2764 // change the attribute
2765 m_attrs
[n
]->DecRef();
2770 // remove this attribute
2771 m_attrs
[n
]->DecRef();
2772 m_rowsOrCols
.RemoveAt(n
);
2773 m_attrs
.RemoveAt(n
);
2778 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2780 size_t count
= m_attrs
.GetCount();
2781 for ( size_t n
= 0; n
< count
; n
++ )
2783 int & rowOrCol
= m_rowsOrCols
[n
];
2784 if ( (size_t)rowOrCol
>= pos
)
2786 if ( numRowsOrCols
> 0 )
2788 // If rows inserted, include row counter where necessary
2789 rowOrCol
+= numRowsOrCols
;
2791 else if ( numRowsOrCols
< 0)
2793 // If rows deleted, either decrement row counter (if row still exists)
2794 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2795 rowOrCol
+= numRowsOrCols
;
2798 m_rowsOrCols
.RemoveAt(n
);
2799 m_attrs
[n
]->DecRef();
2800 m_attrs
.RemoveAt(n
);
2809 // ----------------------------------------------------------------------------
2810 // wxGridCellAttrProvider
2811 // ----------------------------------------------------------------------------
2813 wxGridCellAttrProvider::wxGridCellAttrProvider()
2815 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2818 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2823 void wxGridCellAttrProvider::InitData()
2825 m_data
= new wxGridCellAttrProviderData
;
2828 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2829 wxGridCellAttr::wxAttrKind kind
) const
2831 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2836 case (wxGridCellAttr::Any
):
2837 // Get cached merge attributes.
2838 // Currently not used as no cache implemented as not mutable
2839 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2842 // Basically implement old version.
2843 // Also check merge cache, so we don't have to re-merge every time..
2844 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2845 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2846 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2848 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
2850 // Two or more are non NULL
2851 attr
= new wxGridCellAttr
;
2852 attr
->SetKind(wxGridCellAttr::Merged
);
2854 // Order is important..
2857 attr
->MergeWith(attrcell
);
2862 attr
->MergeWith(attrcol
);
2867 attr
->MergeWith(attrrow
);
2871 // store merge attr if cache implemented
2873 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2877 // one or none is non null return it or null.
2896 case (wxGridCellAttr::Cell
):
2897 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2900 case (wxGridCellAttr::Col
):
2901 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2904 case (wxGridCellAttr::Row
):
2905 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2910 // (wxGridCellAttr::Default):
2911 // (wxGridCellAttr::Merged):
2919 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2925 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2928 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2933 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2936 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2941 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2944 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2948 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2950 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2954 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2958 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2960 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2964 // ----------------------------------------------------------------------------
2965 // wxGridTypeRegistry
2966 // ----------------------------------------------------------------------------
2968 wxGridTypeRegistry::~wxGridTypeRegistry()
2970 size_t count
= m_typeinfo
.GetCount();
2971 for ( size_t i
= 0; i
< count
; i
++ )
2972 delete m_typeinfo
[i
];
2975 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2976 wxGridCellRenderer
* renderer
,
2977 wxGridCellEditor
* editor
)
2979 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2981 // is it already registered?
2982 int loc
= FindRegisteredDataType(typeName
);
2983 if ( loc
!= wxNOT_FOUND
)
2985 delete m_typeinfo
[loc
];
2986 m_typeinfo
[loc
] = info
;
2990 m_typeinfo
.Add(info
);
2994 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2996 size_t count
= m_typeinfo
.GetCount();
2997 for ( size_t i
= 0; i
< count
; i
++ )
2999 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
3008 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
3010 int index
= FindRegisteredDataType(typeName
);
3011 if ( index
== wxNOT_FOUND
)
3013 // check whether this is one of the standard ones, in which case
3014 // register it "on the fly"
3016 if ( typeName
== wxGRID_VALUE_STRING
)
3018 RegisterDataType(wxGRID_VALUE_STRING
,
3019 new wxGridCellStringRenderer
,
3020 new wxGridCellTextEditor
);
3023 #endif // wxUSE_TEXTCTRL
3025 if ( typeName
== wxGRID_VALUE_BOOL
)
3027 RegisterDataType(wxGRID_VALUE_BOOL
,
3028 new wxGridCellBoolRenderer
,
3029 new wxGridCellBoolEditor
);
3032 #endif // wxUSE_CHECKBOX
3034 if ( typeName
== wxGRID_VALUE_NUMBER
)
3036 RegisterDataType(wxGRID_VALUE_NUMBER
,
3037 new wxGridCellNumberRenderer
,
3038 new wxGridCellNumberEditor
);
3040 else if ( typeName
== wxGRID_VALUE_FLOAT
)
3042 RegisterDataType(wxGRID_VALUE_FLOAT
,
3043 new wxGridCellFloatRenderer
,
3044 new wxGridCellFloatEditor
);
3047 #endif // wxUSE_TEXTCTRL
3049 if ( typeName
== wxGRID_VALUE_CHOICE
)
3051 RegisterDataType(wxGRID_VALUE_CHOICE
,
3052 new wxGridCellStringRenderer
,
3053 new wxGridCellChoiceEditor
);
3056 #endif // wxUSE_COMBOBOX
3061 // we get here only if just added the entry for this type, so return
3063 index
= m_typeinfo
.GetCount() - 1;
3069 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
3071 int index
= FindDataType(typeName
);
3072 if ( index
== wxNOT_FOUND
)
3074 // the first part of the typename is the "real" type, anything after ':'
3075 // are the parameters for the renderer
3076 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
3077 if ( index
== wxNOT_FOUND
)
3082 wxGridCellRenderer
*renderer
= GetRenderer(index
);
3083 wxGridCellRenderer
*rendererOld
= renderer
;
3084 renderer
= renderer
->Clone();
3085 rendererOld
->DecRef();
3087 wxGridCellEditor
*editor
= GetEditor(index
);
3088 wxGridCellEditor
*editorOld
= editor
;
3089 editor
= editor
->Clone();
3090 editorOld
->DecRef();
3092 // do it even if there are no parameters to reset them to defaults
3093 wxString params
= typeName
.AfterFirst(_T(':'));
3094 renderer
->SetParameters(params
);
3095 editor
->SetParameters(params
);
3097 // register the new typename
3098 RegisterDataType(typeName
, renderer
, editor
);
3100 // we just registered it, it's the last one
3101 index
= m_typeinfo
.GetCount() - 1;
3107 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
3109 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
3116 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
3118 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
3125 // ----------------------------------------------------------------------------
3127 // ----------------------------------------------------------------------------
3129 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
3131 wxGridTableBase::wxGridTableBase()
3133 m_view
= (wxGrid
*) NULL
;
3134 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
3137 wxGridTableBase::~wxGridTableBase()
3139 delete m_attrProvider
;
3142 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
3144 delete m_attrProvider
;
3145 m_attrProvider
= attrProvider
;
3148 bool wxGridTableBase::CanHaveAttributes()
3150 if ( ! GetAttrProvider() )
3152 // use the default attr provider by default
3153 SetAttrProvider(new wxGridCellAttrProvider
);
3159 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
3161 if ( m_attrProvider
)
3162 return m_attrProvider
->GetAttr(row
, col
, kind
);
3164 return (wxGridCellAttr
*)NULL
;
3167 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
3169 if ( m_attrProvider
)
3171 attr
->SetKind(wxGridCellAttr::Cell
);
3172 m_attrProvider
->SetAttr(attr
, row
, col
);
3176 // as we take ownership of the pointer and don't store it, we must
3182 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3184 if ( m_attrProvider
)
3186 attr
->SetKind(wxGridCellAttr::Row
);
3187 m_attrProvider
->SetRowAttr(attr
, row
);
3191 // as we take ownership of the pointer and don't store it, we must
3197 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3199 if ( m_attrProvider
)
3201 attr
->SetKind(wxGridCellAttr::Col
);
3202 m_attrProvider
->SetColAttr(attr
, col
);
3206 // as we take ownership of the pointer and don't store it, we must
3212 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3213 size_t WXUNUSED(numRows
) )
3215 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3220 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3222 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3227 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3228 size_t WXUNUSED(numRows
) )
3230 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3235 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3236 size_t WXUNUSED(numCols
) )
3238 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3243 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3245 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3250 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3251 size_t WXUNUSED(numCols
) )
3253 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3258 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3262 // RD: Starting the rows at zero confuses users,
3263 // no matter how much it makes sense to us geeks.
3269 wxString
wxGridTableBase::GetColLabelValue( int col
)
3271 // default col labels are:
3272 // cols 0 to 25 : A-Z
3273 // cols 26 to 675 : AA-ZZ
3278 for ( n
= 1; ; n
++ )
3280 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
3286 // reverse the string...
3288 for ( i
= 0; i
< n
; i
++ )
3296 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3298 return wxGRID_VALUE_STRING
;
3301 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3302 const wxString
& typeName
)
3304 return typeName
== wxGRID_VALUE_STRING
;
3307 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3309 return CanGetValueAs(row
, col
, typeName
);
3312 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3317 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3322 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3327 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3328 long WXUNUSED(value
) )
3332 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3333 double WXUNUSED(value
) )
3337 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3338 bool WXUNUSED(value
) )
3342 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3343 const wxString
& WXUNUSED(typeName
) )
3348 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3349 const wxString
& WXUNUSED(typeName
),
3350 void* WXUNUSED(value
) )
3354 //////////////////////////////////////////////////////////////////////
3356 // Message class for the grid table to send requests and notifications
3360 wxGridTableMessage::wxGridTableMessage()
3362 m_table
= (wxGridTableBase
*) NULL
;
3368 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3369 int commandInt1
, int commandInt2
)
3373 m_comInt1
= commandInt1
;
3374 m_comInt2
= commandInt2
;
3377 //////////////////////////////////////////////////////////////////////
3379 // A basic grid table for string data. An object of this class will
3380 // created by wxGrid if you don't specify an alternative table class.
3383 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3385 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3387 wxGridStringTable::wxGridStringTable()
3392 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3395 m_data
.Alloc( numRows
);
3398 sa
.Alloc( numCols
);
3399 sa
.Add( wxEmptyString
, numCols
);
3401 m_data
.Add( sa
, numRows
);
3404 wxGridStringTable::~wxGridStringTable()
3408 int wxGridStringTable::GetNumberRows()
3410 return m_data
.GetCount();
3413 int wxGridStringTable::GetNumberCols()
3415 if ( m_data
.GetCount() > 0 )
3416 return m_data
[0].GetCount();
3421 wxString
wxGridStringTable::GetValue( int row
, int col
)
3423 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3425 _T("invalid row or column index in wxGridStringTable") );
3427 return m_data
[row
][col
];
3430 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3432 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3433 _T("invalid row or column index in wxGridStringTable") );
3435 m_data
[row
][col
] = value
;
3438 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3440 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3442 _T("invalid row or column index in wxGridStringTable") );
3444 return (m_data
[row
][col
] == wxEmptyString
);
3447 void wxGridStringTable::Clear()
3450 int numRows
, numCols
;
3452 numRows
= m_data
.GetCount();
3455 numCols
= m_data
[0].GetCount();
3457 for ( row
= 0; row
< numRows
; row
++ )
3459 for ( col
= 0; col
< numCols
; col
++ )
3461 m_data
[row
][col
] = wxEmptyString
;
3467 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3469 size_t curNumRows
= m_data
.GetCount();
3470 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3471 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3473 if ( pos
>= curNumRows
)
3475 return AppendRows( numRows
);
3479 sa
.Alloc( curNumCols
);
3480 sa
.Add( wxEmptyString
, curNumCols
);
3481 m_data
.Insert( sa
, pos
, numRows
);
3485 wxGridTableMessage
msg( this,
3486 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3490 GetView()->ProcessTableMessage( msg
);
3496 bool wxGridStringTable::AppendRows( size_t numRows
)
3498 size_t curNumRows
= m_data
.GetCount();
3499 size_t curNumCols
= ( curNumRows
> 0
3500 ? m_data
[0].GetCount()
3501 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3504 if ( curNumCols
> 0 )
3506 sa
.Alloc( curNumCols
);
3507 sa
.Add( wxEmptyString
, curNumCols
);
3510 m_data
.Add( sa
, numRows
);
3514 wxGridTableMessage
msg( this,
3515 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3518 GetView()->ProcessTableMessage( msg
);
3524 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3526 size_t curNumRows
= m_data
.GetCount();
3528 if ( pos
>= curNumRows
)
3530 wxFAIL_MSG( wxString::Format
3532 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3534 (unsigned long)numRows
,
3535 (unsigned long)curNumRows
3541 if ( numRows
> curNumRows
- pos
)
3543 numRows
= curNumRows
- pos
;
3546 if ( numRows
>= curNumRows
)
3552 m_data
.RemoveAt( pos
, numRows
);
3557 wxGridTableMessage
msg( this,
3558 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3562 GetView()->ProcessTableMessage( msg
);
3568 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3572 size_t curNumRows
= m_data
.GetCount();
3573 size_t curNumCols
= ( curNumRows
> 0
3574 ? m_data
[0].GetCount()
3575 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3577 if ( pos
>= curNumCols
)
3579 return AppendCols( numCols
);
3582 if ( !m_colLabels
.IsEmpty() )
3584 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
3587 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
3588 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
3591 for ( row
= 0; row
< curNumRows
; row
++ )
3593 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3595 m_data
[row
].Insert( wxEmptyString
, col
);
3601 wxGridTableMessage
msg( this,
3602 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3606 GetView()->ProcessTableMessage( msg
);
3612 bool wxGridStringTable::AppendCols( size_t numCols
)
3616 size_t curNumRows
= m_data
.GetCount();
3621 // TODO: something better than this ?
3623 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3628 for ( row
= 0; row
< curNumRows
; row
++ )
3630 m_data
[row
].Add( wxEmptyString
, numCols
);
3635 wxGridTableMessage
msg( this,
3636 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3639 GetView()->ProcessTableMessage( msg
);
3645 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3649 size_t curNumRows
= m_data
.GetCount();
3650 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3651 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3653 if ( pos
>= curNumCols
)
3655 wxFAIL_MSG( wxString::Format
3657 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3659 (unsigned long)numCols
,
3660 (unsigned long)curNumCols
3667 colID
= GetView()->GetColAt( pos
);
3671 if ( numCols
> curNumCols
- colID
)
3673 numCols
= curNumCols
- colID
;
3676 if ( !m_colLabels
.IsEmpty() )
3678 // m_colLabels stores just as many elements as it needs, e.g. if only
3679 // the label of the first column had been set it would have only one
3680 // element and not numCols, so account for it
3681 int nToRm
= m_colLabels
.size() - colID
;
3683 m_colLabels
.RemoveAt( colID
, nToRm
);
3686 for ( row
= 0; row
< curNumRows
; row
++ )
3688 if ( numCols
>= curNumCols
)
3690 m_data
[row
].Clear();
3694 m_data
[row
].RemoveAt( colID
, numCols
);
3700 wxGridTableMessage
msg( this,
3701 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3705 GetView()->ProcessTableMessage( msg
);
3711 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3713 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3715 // using default label
3717 return wxGridTableBase::GetRowLabelValue( row
);
3721 return m_rowLabels
[row
];
3725 wxString
wxGridStringTable::GetColLabelValue( int col
)
3727 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3729 // using default label
3731 return wxGridTableBase::GetColLabelValue( col
);
3735 return m_colLabels
[col
];
3739 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3741 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3743 int n
= m_rowLabels
.GetCount();
3746 for ( i
= n
; i
<= row
; i
++ )
3748 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3752 m_rowLabels
[row
] = value
;
3755 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3757 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3759 int n
= m_colLabels
.GetCount();
3762 for ( i
= n
; i
<= col
; i
++ )
3764 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3768 m_colLabels
[col
] = value
;
3772 //////////////////////////////////////////////////////////////////////
3773 //////////////////////////////////////////////////////////////////////
3775 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
)
3776 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost
)
3779 void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
3781 m_owner
->CancelMouseCapture();
3784 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3786 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow
)
3787 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3788 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3789 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3792 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3794 const wxPoint
&pos
, const wxSize
&size
)
3795 : wxGridSubwindow(parent
, id
, pos
, size
)
3800 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3804 // NO - don't do this because it will set both the x and y origin
3805 // coords to match the parent scrolled window and we just want to
3806 // set the y coord - MB
3808 // m_owner->PrepareDC( dc );
3811 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3812 wxPoint pt
= dc
.GetDeviceOrigin();
3813 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
3815 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3816 m_owner
->DrawRowLabels( dc
, rows
);
3819 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3821 m_owner
->ProcessRowLabelMouseEvent( event
);
3824 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3826 m_owner
->GetEventHandler()->ProcessEvent( event
);
3829 //////////////////////////////////////////////////////////////////////
3831 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3833 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow
)
3834 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3835 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3836 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3839 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3841 const wxPoint
&pos
, const wxSize
&size
)
3842 : wxGridSubwindow(parent
, id
, pos
, size
)
3847 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3851 // NO - don't do this because it will set both the x and y origin
3852 // coords to match the parent scrolled window and we just want to
3853 // set the x coord - MB
3855 // m_owner->PrepareDC( dc );
3858 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3859 wxPoint pt
= dc
.GetDeviceOrigin();
3860 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3861 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
3863 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
3865 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3866 m_owner
->DrawColLabels( dc
, cols
);
3869 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3871 m_owner
->ProcessColLabelMouseEvent( event
);
3874 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3876 m_owner
->GetEventHandler()->ProcessEvent( event
);
3879 //////////////////////////////////////////////////////////////////////
3881 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3883 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow
)
3884 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3885 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3886 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3889 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3891 const wxPoint
&pos
, const wxSize
&size
)
3892 : wxGridSubwindow(parent
, id
, pos
, size
)
3897 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3901 int client_height
= 0;
3902 int client_width
= 0;
3903 GetClientSize( &client_width
, &client_height
);
3905 // VZ: any reason for this ifdef? (FIXME)
3911 rect
.SetWidth( client_width
- 2 );
3912 rect
.SetHeight( client_height
- 2 );
3914 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3916 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxPENSTYLE_SOLID
) );
3917 dc
.DrawLine( client_width
- 1, client_height
- 1, client_width
- 1, 0 );
3918 dc
.DrawLine( client_width
- 1, client_height
- 1, 0, client_height
- 1 );
3919 dc
.DrawLine( 0, 0, client_width
, 0 );
3920 dc
.DrawLine( 0, 0, 0, client_height
);
3922 dc
.SetPen( *wxWHITE_PEN
);
3923 dc
.DrawLine( 1, 1, client_width
- 1, 1 );
3924 dc
.DrawLine( 1, 1, 1, client_height
- 1 );
3928 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3930 m_owner
->ProcessCornerLabelMouseEvent( event
);
3933 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3935 m_owner
->GetEventHandler()->ProcessEvent(event
);
3938 //////////////////////////////////////////////////////////////////////
3940 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3942 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow
)
3943 EVT_PAINT( wxGridWindow::OnPaint
)
3944 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3945 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3946 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3947 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3948 EVT_CHAR( wxGridWindow::OnChar
)
3949 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3950 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3951 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3954 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3955 wxGridRowLabelWindow
*rowLblWin
,
3956 wxGridColLabelWindow
*colLblWin
,
3959 const wxSize
&size
)
3960 : wxGridSubwindow(parent
, id
, pos
, size
,
3961 wxWANTS_CHARS
| wxCLIP_CHILDREN
,
3962 wxT("grid window") )
3965 m_rowLabelWin
= rowLblWin
;
3966 m_colLabelWin
= colLblWin
;
3969 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3971 wxPaintDC
dc( this );
3972 m_owner
->PrepareDC( dc
);
3973 wxRegion reg
= GetUpdateRegion();
3974 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
3975 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
3977 #if WXGRID_DRAW_LINES
3978 m_owner
->DrawAllGridLines( dc
, reg
);
3981 m_owner
->DrawGridSpace( dc
);
3982 m_owner
->DrawHighlight( dc
, dirtyCells
);
3985 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3987 wxWindow::ScrollWindow( dx
, dy
, rect
);
3988 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3989 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3992 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3994 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
3997 m_owner
->ProcessGridCellMouseEvent( event
);
4000 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
4002 m_owner
->GetEventHandler()->ProcessEvent( event
);
4005 // This seems to be required for wxMotif/wxGTK otherwise the mouse
4006 // cursor must be in the cell edit control to get key events
4008 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
4010 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4014 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
4016 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4020 void wxGridWindow::OnChar( wxKeyEvent
& event
)
4022 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4026 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
4030 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
4032 // and if we have any selection, it has to be repainted, because it
4033 // uses different colour when the grid is not focused:
4034 if ( m_owner
->IsSelection() )
4040 // NB: Note that this code is in "else" branch only because the other
4041 // branch refreshes everything and so there's no point in calling
4042 // Refresh() again, *not* because it should only be done if
4043 // !IsSelection(). If the above code is ever optimized to refresh
4044 // only selected area, this needs to be moved out of the "else"
4045 // branch so that it's always executed.
4047 // current cell cursor {dis,re}appears on focus change:
4048 const wxGridCellCoords
cursorCoords(m_owner
->GetGridCursorRow(),
4049 m_owner
->GetGridCursorCol());
4050 const wxRect cursor
=
4051 m_owner
->BlockToDeviceRect(cursorCoords
, cursorCoords
);
4052 Refresh(true, &cursor
);
4055 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4059 //////////////////////////////////////////////////////////////////////
4061 // Internal Helper function for computing row or column from some
4062 // (unscrolled) coordinate value, using either
4063 // m_defaultRowHeight/m_defaultColWidth or binary search on array
4064 // of m_rowBottoms/m_ColRights to speed up the search!
4066 // Internal helper macros for simpler use of that function
4068 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
4069 const wxArrayInt
& BorderArray
, int nMax
,
4072 #define internalXToCol(x) XToCol(x, true)
4073 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
4074 m_minAcceptableRowHeight, \
4075 m_rowBottoms, m_numRows, true)
4077 /////////////////////////////////////////////////////////////////////
4079 #if wxUSE_EXTENDED_RTTI
4080 WX_DEFINE_FLAGS( wxGridStyle
)
4082 wxBEGIN_FLAGS( wxGridStyle
)
4083 // new style border flags, we put them first to
4084 // use them for streaming out
4085 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4086 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4087 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4088 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4089 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4090 wxFLAGS_MEMBER(wxBORDER_NONE
)
4092 // old style border flags
4093 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4094 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4095 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4096 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4097 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4098 wxFLAGS_MEMBER(wxBORDER
)
4100 // standard window styles
4101 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4102 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4103 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4104 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4105 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4106 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4107 wxFLAGS_MEMBER(wxVSCROLL
)
4108 wxFLAGS_MEMBER(wxHSCROLL
)
4110 wxEND_FLAGS( wxGridStyle
)
4112 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4114 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4115 wxHIDE_PROPERTY( Children
)
4116 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4117 wxEND_PROPERTIES_TABLE()
4119 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4120 wxEND_HANDLERS_TABLE()
4122 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4125 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
4128 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4131 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4132 EVT_PAINT( wxGrid::OnPaint
)
4133 EVT_SIZE( wxGrid::OnSize
)
4134 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
4135 EVT_KEY_UP( wxGrid::OnKeyUp
)
4136 EVT_CHAR ( wxGrid::OnChar
)
4137 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
4145 wxGrid::wxGrid( wxWindow
*parent
,
4150 const wxString
& name
)
4153 Create(parent
, id
, pos
, size
, style
, name
);
4156 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
4157 const wxPoint
& pos
, const wxSize
& size
,
4158 long style
, const wxString
& name
)
4160 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
4161 style
| wxWANTS_CHARS
, name
))
4164 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4165 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4168 SetInitialSize(size
);
4176 // Must do this or ~wxScrollHelper will pop the wrong event handler
4177 SetTargetWindow(this);
4179 wxSafeDecRef(m_defaultCellAttr
);
4181 #ifdef DEBUG_ATTR_CACHE
4182 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4183 wxPrintf(_T("wxGrid attribute cache statistics: "
4184 "total: %u, hits: %u (%u%%)\n"),
4185 total
, gs_nAttrCacheHits
,
4186 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4189 // if we own the table, just delete it, otherwise at least don't leave it
4190 // with dangling view pointer
4193 else if ( m_table
&& m_table
->GetView() == this )
4194 m_table
->SetView(NULL
);
4196 delete m_typeRegistry
;
4201 // ----- internal init and update functions
4204 // NOTE: If using the default visual attributes works everywhere then this can
4205 // be removed as well as the #else cases below.
4206 #define _USE_VISATTR 0
4208 void wxGrid::Create()
4210 // create the type registry
4211 m_typeRegistry
= new wxGridTypeRegistry
;
4213 m_cellEditCtrlEnabled
= false;
4215 m_defaultCellAttr
= new wxGridCellAttr();
4217 // Set default cell attributes
4218 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4219 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4220 m_defaultCellAttr
->SetFont(GetFont());
4221 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4222 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4223 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4226 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4227 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4229 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4230 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4233 m_defaultCellAttr
->SetTextColour(
4234 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4235 m_defaultCellAttr
->SetBackgroundColour(
4236 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4241 m_currentCellCoords
= wxGridNoCellCoords
;
4243 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4244 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4246 // subwindow components that make up the wxGrid
4247 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4252 m_colLabelWin
= new wxGridColLabelWindow( this,
4257 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4262 m_gridWin
= new wxGridWindow( this,
4269 SetTargetWindow( m_gridWin
);
4272 wxColour gfg
= gva
.colFg
;
4273 wxColour gbg
= gva
.colBg
;
4274 wxColour lfg
= lva
.colFg
;
4275 wxColour lbg
= lva
.colBg
;
4277 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4278 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4279 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4280 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4283 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4284 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4285 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4286 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4287 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4288 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4290 m_gridWin
->SetOwnForegroundColour(gfg
);
4291 m_gridWin
->SetOwnBackgroundColour(gbg
);
4296 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4297 wxGrid::wxGridSelectionModes selmode
)
4299 wxCHECK_MSG( !m_created
,
4301 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4303 m_numRows
= numRows
;
4304 m_numCols
= numCols
;
4306 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4307 m_table
->SetView( this );
4309 m_selection
= new wxGridSelection( this, selmode
);
4318 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4320 wxCHECK_RET( m_created
,
4321 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4323 m_selection
->SetSelectionMode( selmode
);
4326 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4328 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4329 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4331 return m_selection
->GetSelectionMode();
4334 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4335 wxGrid::wxGridSelectionModes selmode
)
4337 bool checkSelection
= false;
4340 // stop all processing
4345 m_table
->SetView(0);
4357 checkSelection
= true;
4359 // kill row and column size arrays
4360 m_colWidths
.Empty();
4361 m_colRights
.Empty();
4362 m_rowHeights
.Empty();
4363 m_rowBottoms
.Empty();
4368 m_numRows
= table
->GetNumberRows();
4369 m_numCols
= table
->GetNumberCols();
4372 m_table
->SetView( this );
4373 m_ownTable
= takeOwnership
;
4374 m_selection
= new wxGridSelection( this, selmode
);
4377 // If the newly set table is smaller than the
4378 // original one current cell and selection regions
4379 // might be invalid,
4380 m_selectingKeyboard
= wxGridNoCellCoords
;
4381 m_currentCellCoords
=
4382 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
4383 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
4384 if (m_selectingTopLeft
.GetRow() >= m_numRows
||
4385 m_selectingTopLeft
.GetCol() >= m_numCols
)
4387 m_selectingTopLeft
= wxGridNoCellCoords
;
4388 m_selectingBottomRight
= wxGridNoCellCoords
;
4391 m_selectingBottomRight
=
4392 wxGridCellCoords(wxMin(m_numRows
,
4393 m_selectingBottomRight
.GetRow()),
4395 m_selectingBottomRight
.GetCol()));
4405 void wxGrid::InitVars()
4409 m_cornerLabelWin
= NULL
;
4410 m_rowLabelWin
= NULL
;
4411 m_colLabelWin
= NULL
;
4418 m_defaultCellAttr
= NULL
;
4419 m_typeRegistry
= NULL
;
4420 m_winCapture
= NULL
;
4425 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4426 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4428 if ( m_rowLabelWin
)
4430 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4434 m_labelBackgroundColour
= *wxWHITE
;
4437 m_labelTextColour
= *wxBLACK
;
4440 m_attrCache
.row
= -1;
4441 m_attrCache
.col
= -1;
4442 m_attrCache
.attr
= NULL
;
4444 // TODO: something better than this ?
4446 m_labelFont
= this->GetFont();
4447 m_labelFont
.SetWeight( wxBOLD
);
4449 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4450 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4452 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4453 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4454 m_colLabelTextOrientation
= wxHORIZONTAL
;
4456 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4457 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4459 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4460 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4462 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4463 m_defaultRowHeight
+= 8;
4465 m_defaultRowHeight
+= 4;
4468 m_gridLineColour
= wxColour( 192,192,192 );
4469 m_gridLinesEnabled
= true;
4470 m_cellHighlightColour
= *wxBLACK
;
4471 m_cellHighlightPenWidth
= 2;
4472 m_cellHighlightROPenWidth
= 1;
4474 m_canDragColMove
= false;
4476 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4477 m_winCapture
= (wxWindow
*)NULL
;
4478 m_canDragRowSize
= true;
4479 m_canDragColSize
= true;
4480 m_canDragGridSize
= true;
4481 m_canDragCell
= false;
4483 m_dragRowOrCol
= -1;
4484 m_isDragging
= false;
4485 m_startDragPos
= wxDefaultPosition
;
4486 m_nativeColumnLabels
= false;
4488 m_waitForSlowClick
= false;
4490 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4491 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4493 m_currentCellCoords
= wxGridNoCellCoords
;
4497 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4498 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4500 m_editable
= true; // default for whole grid
4502 m_inOnKeyDown
= false;
4508 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4509 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4512 // ----------------------------------------------------------------------------
4513 // the idea is to call these functions only when necessary because they create
4514 // quite big arrays which eat memory mostly unnecessary - in particular, if
4515 // default widths/heights are used for all rows/columns, we may not use these
4518 // with some extra code, it should be possible to only store the widths/heights
4519 // different from default ones (resulting in space savings for huge grids) but
4520 // this is not done currently
4521 // ----------------------------------------------------------------------------
4523 void wxGrid::InitRowHeights()
4525 m_rowHeights
.Empty();
4526 m_rowBottoms
.Empty();
4528 m_rowHeights
.Alloc( m_numRows
);
4529 m_rowBottoms
.Alloc( m_numRows
);
4531 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4534 for ( int i
= 0; i
< m_numRows
; i
++ )
4536 rowBottom
+= m_defaultRowHeight
;
4537 m_rowBottoms
.Add( rowBottom
);
4541 void wxGrid::InitColWidths()
4543 m_colWidths
.Empty();
4544 m_colRights
.Empty();
4546 m_colWidths
.Alloc( m_numCols
);
4547 m_colRights
.Alloc( m_numCols
);
4549 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4552 for ( int i
= 0; i
< m_numCols
; i
++ )
4554 colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
4555 m_colRights
.Add( colRight
);
4559 int wxGrid::GetColWidth(int col
) const
4561 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4564 int wxGrid::GetColLeft(int col
) const
4566 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
4567 : m_colRights
[col
] - m_colWidths
[col
];
4570 int wxGrid::GetColRight(int col
) const
4572 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
4576 int wxGrid::GetRowHeight(int row
) const
4578 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4581 int wxGrid::GetRowTop(int row
) const
4583 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4584 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4587 int wxGrid::GetRowBottom(int row
) const
4589 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4590 : m_rowBottoms
[row
];
4593 void wxGrid::CalcDimensions()
4595 // compute the size of the scrollable area
4596 int w
= m_numCols
> 0 ? GetColRight(GetColAt(m_numCols
- 1)) : 0;
4597 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
4602 // take into account editor if shown
4603 if ( IsCellEditControlShown() )
4606 int r
= m_currentCellCoords
.GetRow();
4607 int c
= m_currentCellCoords
.GetCol();
4608 int x
= GetColLeft(c
);
4609 int y
= GetRowTop(r
);
4611 // how big is the editor
4612 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4613 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4614 editor
->GetControl()->GetSize(&w2
, &h2
);
4625 // preserve (more or less) the previous position
4627 GetViewStart( &x
, &y
);
4629 // ensure the position is valid for the new scroll ranges
4631 x
= wxMax( w
- 1, 0 );
4633 y
= wxMax( h
- 1, 0 );
4635 // do set scrollbar parameters
4636 SetScrollbars( m_scrollLineX
, m_scrollLineY
,
4637 GetScrollX(w
), GetScrollY(h
),
4639 GetBatchCount() != 0);
4641 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4642 // still must reposition the children
4646 void wxGrid::CalcWindowSizes()
4648 // escape if the window is has not been fully created yet
4650 if ( m_cornerLabelWin
== NULL
)
4654 GetClientSize( &cw
, &ch
);
4656 // this block of code tries to work around the following problem: the grid
4657 // could have been just resized to have enough space to show the full grid
4658 // window contents without the scrollbars, but its client size could be
4659 // not big enough because the grid has the scrollbars right now and so the
4660 // scrollbars would remain even though we don't need them any more
4662 // to prevent this from happening, check if we have enough space for
4663 // everything without the scrollbars and explicitly disable them then
4664 wxSize size
= GetSize() - GetWindowBorderSize();
4665 if ( size
!= wxSize(cw
, ch
) )
4667 // check if we have enough space for grid window after accounting for
4668 // the fixed size elements
4669 size
.x
-= m_rowLabelWidth
;
4670 size
.y
-= m_colLabelHeight
;
4672 const wxSize vsize
= m_gridWin
->GetVirtualSize();
4674 if ( size
.x
>= vsize
.x
&& size
.y
>= vsize
.y
)
4676 // yes, we do, so remove the scrollbars and use the new client size
4677 // (which should be the same as full window size - borders now)
4678 SetScrollbars(0, 0, 0, 0);
4679 GetClientSize(&cw
, &ch
);
4683 // the grid may be too small to have enough space for the labels yet, don't
4684 // size the windows to negative sizes in this case
4685 int gw
= cw
- m_rowLabelWidth
;
4686 int gh
= ch
- m_colLabelHeight
;
4692 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4693 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4695 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4696 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, gw
, m_colLabelHeight
);
4698 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4699 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, gh
);
4701 if ( m_gridWin
&& m_gridWin
->IsShown() )
4702 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, gw
, gh
);
4705 // this is called when the grid table sends a message
4706 // to indicate that it has been redimensioned
4708 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4711 bool result
= false;
4713 // Clear the attribute cache as the attribute might refer to a different
4714 // cell than stored in the cache after adding/removing rows/columns.
4717 // By the same reasoning, the editor should be dismissed if columns are
4718 // added or removed. And for consistency, it should IMHO always be
4719 // removed, not only if the cell "underneath" it actually changes.
4720 // For now, I intentionally do not save the editor's content as the
4721 // cell it might want to save that stuff to might no longer exist.
4722 HideCellEditControl();
4725 // if we were using the default widths/heights so far, we must change them
4727 if ( m_colWidths
.IsEmpty() )
4732 if ( m_rowHeights
.IsEmpty() )
4738 switch ( msg
.GetId() )
4740 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4742 size_t pos
= msg
.GetCommandInt();
4743 int numRows
= msg
.GetCommandInt2();
4745 m_numRows
+= numRows
;
4747 if ( !m_rowHeights
.IsEmpty() )
4749 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4750 m_rowBottoms
.Insert( 0, pos
, numRows
);
4754 bottom
= m_rowBottoms
[pos
- 1];
4756 for ( i
= pos
; i
< m_numRows
; i
++ )
4758 bottom
+= m_rowHeights
[i
];
4759 m_rowBottoms
[i
] = bottom
;
4763 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4765 // if we have just inserted cols into an empty grid the current
4766 // cell will be undefined...
4768 SetCurrentCell( 0, 0 );
4772 m_selection
->UpdateRows( pos
, numRows
);
4773 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4775 attrProvider
->UpdateAttrRows( pos
, numRows
);
4777 if ( !GetBatchCount() )
4780 m_rowLabelWin
->Refresh();
4786 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4788 int numRows
= msg
.GetCommandInt();
4789 int oldNumRows
= m_numRows
;
4790 m_numRows
+= numRows
;
4792 if ( !m_rowHeights
.IsEmpty() )
4794 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4795 m_rowBottoms
.Add( 0, numRows
);
4798 if ( oldNumRows
> 0 )
4799 bottom
= m_rowBottoms
[oldNumRows
- 1];
4801 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4803 bottom
+= m_rowHeights
[i
];
4804 m_rowBottoms
[i
] = bottom
;
4808 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4810 // if we have just inserted cols into an empty grid the current
4811 // cell will be undefined...
4813 SetCurrentCell( 0, 0 );
4816 if ( !GetBatchCount() )
4819 m_rowLabelWin
->Refresh();
4825 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4827 size_t pos
= msg
.GetCommandInt();
4828 int numRows
= msg
.GetCommandInt2();
4829 m_numRows
-= numRows
;
4831 if ( !m_rowHeights
.IsEmpty() )
4833 m_rowHeights
.RemoveAt( pos
, numRows
);
4834 m_rowBottoms
.RemoveAt( pos
, numRows
);
4837 for ( i
= 0; i
< m_numRows
; i
++ )
4839 h
+= m_rowHeights
[i
];
4840 m_rowBottoms
[i
] = h
;
4846 m_currentCellCoords
= wxGridNoCellCoords
;
4850 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4851 m_currentCellCoords
.Set( 0, 0 );
4855 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4856 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4859 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4861 // ifdef'd out following patch from Paul Gammans
4863 // No need to touch column attributes, unless we
4864 // removed _all_ rows, in this case, we remove
4865 // all column attributes.
4866 // I hate to do this here, but the
4867 // needed data is not available inside UpdateAttrRows.
4868 if ( !GetNumberRows() )
4869 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4873 if ( !GetBatchCount() )
4876 m_rowLabelWin
->Refresh();
4882 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4884 size_t pos
= msg
.GetCommandInt();
4885 int numCols
= msg
.GetCommandInt2();
4886 m_numCols
+= numCols
;
4888 if ( !m_colAt
.IsEmpty() )
4890 //Shift the column IDs
4892 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
4894 if ( m_colAt
[i
] >= (int)pos
)
4895 m_colAt
[i
] += numCols
;
4898 m_colAt
.Insert( pos
, pos
, numCols
);
4900 //Set the new columns' positions
4901 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
4907 if ( !m_colWidths
.IsEmpty() )
4909 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4910 m_colRights
.Insert( 0, pos
, numCols
);
4914 right
= m_colRights
[GetColAt( pos
- 1 )];
4917 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
4919 i
= GetColAt( colPos
);
4921 right
+= m_colWidths
[i
];
4922 m_colRights
[i
] = right
;
4926 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4928 // if we have just inserted cols into an empty grid the current
4929 // cell will be undefined...
4931 SetCurrentCell( 0, 0 );
4935 m_selection
->UpdateCols( pos
, numCols
);
4936 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4938 attrProvider
->UpdateAttrCols( pos
, numCols
);
4939 if ( !GetBatchCount() )
4942 m_colLabelWin
->Refresh();
4948 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4950 int numCols
= msg
.GetCommandInt();
4951 int oldNumCols
= m_numCols
;
4952 m_numCols
+= numCols
;
4954 if ( !m_colAt
.IsEmpty() )
4956 m_colAt
.Add( 0, numCols
);
4958 //Set the new columns' positions
4960 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4966 if ( !m_colWidths
.IsEmpty() )
4968 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4969 m_colRights
.Add( 0, numCols
);
4972 if ( oldNumCols
> 0 )
4973 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
4976 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
4978 i
= GetColAt( colPos
);
4980 right
+= m_colWidths
[i
];
4981 m_colRights
[i
] = right
;
4985 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4987 // if we have just inserted cols into an empty grid the current
4988 // cell will be undefined...
4990 SetCurrentCell( 0, 0 );
4992 if ( !GetBatchCount() )
4995 m_colLabelWin
->Refresh();
5001 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5003 size_t pos
= msg
.GetCommandInt();
5004 int numCols
= msg
.GetCommandInt2();
5005 m_numCols
-= numCols
;
5007 if ( !m_colAt
.IsEmpty() )
5009 int colID
= GetColAt( pos
);
5011 m_colAt
.RemoveAt( pos
, numCols
);
5013 //Shift the column IDs
5015 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5017 if ( m_colAt
[colPos
] > colID
)
5018 m_colAt
[colPos
] -= numCols
;
5022 if ( !m_colWidths
.IsEmpty() )
5024 m_colWidths
.RemoveAt( pos
, numCols
);
5025 m_colRights
.RemoveAt( pos
, numCols
);
5029 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5031 i
= GetColAt( colPos
);
5033 w
+= m_colWidths
[i
];
5040 m_currentCellCoords
= wxGridNoCellCoords
;
5044 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
5045 m_currentCellCoords
.Set( 0, 0 );
5049 m_selection
->UpdateCols( pos
, -((int)numCols
) );
5050 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5053 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
5055 // ifdef'd out following patch from Paul Gammans
5057 // No need to touch row attributes, unless we
5058 // removed _all_ columns, in this case, we remove
5059 // all row attributes.
5060 // I hate to do this here, but the
5061 // needed data is not available inside UpdateAttrCols.
5062 if ( !GetNumberCols() )
5063 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
5067 if ( !GetBatchCount() )
5070 m_colLabelWin
->Refresh();
5077 if (result
&& !GetBatchCount() )
5078 m_gridWin
->Refresh();
5083 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
) const
5085 wxRegionIterator
iter( reg
);
5088 wxArrayInt rowlabels
;
5095 // TODO: remove this when we can...
5096 // There is a bug in wxMotif that gives garbage update
5097 // rectangles if you jump-scroll a long way by clicking the
5098 // scrollbar with middle button. This is a work-around
5100 #if defined(__WXMOTIF__)
5102 m_gridWin
->GetClientSize( &cw
, &ch
);
5103 if ( r
.GetTop() > ch
)
5105 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5108 // logical bounds of update region
5111 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
5112 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
5114 // find the row labels within these bounds
5117 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5119 if ( GetRowBottom(row
) < top
)
5122 if ( GetRowTop(row
) > bottom
)
5125 rowlabels
.Add( row
);
5134 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
) const
5136 wxRegionIterator
iter( reg
);
5139 wxArrayInt colLabels
;
5146 // TODO: remove this when we can...
5147 // There is a bug in wxMotif that gives garbage update
5148 // rectangles if you jump-scroll a long way by clicking the
5149 // scrollbar with middle button. This is a work-around
5151 #if defined(__WXMOTIF__)
5153 m_gridWin
->GetClientSize( &cw
, &ch
);
5154 if ( r
.GetLeft() > cw
)
5156 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5159 // logical bounds of update region
5162 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
5163 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
5165 // find the cells within these bounds
5169 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5171 col
= GetColAt( colPos
);
5173 if ( GetColRight(col
) < left
)
5176 if ( GetColLeft(col
) > right
)
5179 colLabels
.Add( col
);
5188 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
) const
5190 wxRegionIterator
iter( reg
);
5193 wxGridCellCoordsArray cellsExposed
;
5195 int left
, top
, right
, bottom
;
5200 // TODO: remove this when we can...
5201 // There is a bug in wxMotif that gives garbage update
5202 // rectangles if you jump-scroll a long way by clicking the
5203 // scrollbar with middle button. This is a work-around
5205 #if defined(__WXMOTIF__)
5207 m_gridWin
->GetClientSize( &cw
, &ch
);
5208 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5209 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5210 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5211 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5214 // logical bounds of update region
5216 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5217 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5219 // find the cells within these bounds
5222 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5224 if ( GetRowBottom(row
) <= top
)
5227 if ( GetRowTop(row
) > bottom
)
5231 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5233 col
= GetColAt( colPos
);
5235 if ( GetColRight(col
) <= left
)
5238 if ( GetColLeft(col
) > right
)
5241 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
5248 return cellsExposed
;
5252 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5255 wxPoint
pos( event
.GetPosition() );
5256 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5258 if ( event
.Dragging() )
5262 m_isDragging
= true;
5263 m_rowLabelWin
->CaptureMouse();
5266 if ( event
.LeftIsDown() )
5268 switch ( m_cursorMode
)
5270 case WXGRID_CURSOR_RESIZE_ROW
:
5272 int cw
, ch
, left
, dummy
;
5273 m_gridWin
->GetClientSize( &cw
, &ch
);
5274 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5276 wxClientDC
dc( m_gridWin
);
5279 GetRowTop(m_dragRowOrCol
) +
5280 GetRowMinimalHeight(m_dragRowOrCol
) );
5281 dc
.SetLogicalFunction(wxINVERT
);
5282 if ( m_dragLastPos
>= 0 )
5284 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5286 dc
.DrawLine( left
, y
, left
+cw
, y
);
5291 case WXGRID_CURSOR_SELECT_ROW
:
5293 if ( (row
= YToRow( y
)) >= 0 )
5297 m_selection
->SelectRow( row
,
5298 event
.ControlDown(),
5307 // default label to suppress warnings about "enumeration value
5308 // 'xxx' not handled in switch
5316 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5321 if (m_rowLabelWin
->HasCapture())
5322 m_rowLabelWin
->ReleaseMouse();
5323 m_isDragging
= false;
5326 // ------------ Entering or leaving the window
5328 if ( event
.Entering() || event
.Leaving() )
5330 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5333 // ------------ Left button pressed
5335 else if ( event
.LeftDown() )
5337 // don't send a label click event for a hit on the
5338 // edge of the row label - this is probably the user
5339 // wanting to resize the row
5341 if ( YToEdgeOfRow(y
) < 0 )
5345 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5347 if ( !event
.ShiftDown() && !event
.CmdDown() )
5351 if ( event
.ShiftDown() )
5353 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5356 GetNumberCols() - 1,
5357 event
.ControlDown(),
5364 m_selection
->SelectRow( row
,
5365 event
.ControlDown(),
5372 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5377 // starting to drag-resize a row
5378 if ( CanDragRowSize() )
5379 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5383 // ------------ Left double click
5385 else if (event
.LeftDClick() )
5387 row
= YToEdgeOfRow(y
);
5392 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5394 // no default action at the moment
5399 // adjust row height depending on label text
5400 AutoSizeRowLabelSize( row
);
5402 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5407 // ------------ Left button released
5409 else if ( event
.LeftUp() )
5411 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5413 DoEndDragResizeRow();
5415 // Note: we are ending the event *after* doing
5416 // default processing in this case
5418 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5421 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5425 // ------------ Right button down
5427 else if ( event
.RightDown() )
5431 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5433 // no default action at the moment
5437 // ------------ Right double click
5439 else if ( event
.RightDClick() )
5443 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5445 // no default action at the moment
5449 // ------------ No buttons down and mouse moving
5451 else if ( event
.Moving() )
5453 m_dragRowOrCol
= YToEdgeOfRow( y
);
5454 if ( m_dragRowOrCol
>= 0 )
5456 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5458 // don't capture the mouse yet
5459 if ( CanDragRowSize() )
5460 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5463 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5465 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5470 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5473 wxPoint
pos( event
.GetPosition() );
5474 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5476 if ( event
.Dragging() )
5480 m_isDragging
= true;
5481 m_colLabelWin
->CaptureMouse();
5483 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
)
5484 m_dragRowOrCol
= XToCol( x
);
5487 if ( event
.LeftIsDown() )
5489 switch ( m_cursorMode
)
5491 case WXGRID_CURSOR_RESIZE_COL
:
5493 int cw
, ch
, dummy
, top
;
5494 m_gridWin
->GetClientSize( &cw
, &ch
);
5495 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5497 wxClientDC
dc( m_gridWin
);
5500 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5501 GetColMinimalWidth(m_dragRowOrCol
));
5502 dc
.SetLogicalFunction(wxINVERT
);
5503 if ( m_dragLastPos
>= 0 )
5505 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5507 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5512 case WXGRID_CURSOR_SELECT_COL
:
5514 if ( (col
= XToCol( x
)) >= 0 )
5518 m_selection
->SelectCol( col
,
5519 event
.ControlDown(),
5528 case WXGRID_CURSOR_MOVE_COL
:
5531 m_moveToCol
= GetColAt( 0 );
5533 m_moveToCol
= XToCol( x
);
5537 if ( m_moveToCol
< 0 )
5538 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5540 markerX
= GetColLeft( m_moveToCol
);
5542 if ( markerX
!= m_dragLastPos
)
5544 wxClientDC
dc( m_colLabelWin
);
5547 m_colLabelWin
->GetClientSize( &cw
, &ch
);
5551 //Clean up the last indicator
5552 if ( m_dragLastPos
>= 0 )
5554 wxPen
pen( m_colLabelWin
->GetBackgroundColour(), 2 );
5556 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
5557 dc
.SetPen(wxNullPen
);
5559 if ( XToCol( m_dragLastPos
) != -1 )
5560 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
5563 //Moving to the same place? Don't draw a marker
5564 if ( (m_moveToCol
== m_dragRowOrCol
)
5565 || (GetColPos( m_moveToCol
) == GetColPos( m_dragRowOrCol
) + 1)
5566 || (m_moveToCol
< 0 && m_dragRowOrCol
== GetColAt( m_numCols
- 1 )))
5573 wxPen
pen( *wxBLUE
, 2 );
5576 dc
.DrawLine( markerX
, 0, markerX
, ch
);
5578 dc
.SetPen(wxNullPen
);
5580 m_dragLastPos
= markerX
- 1;
5585 // default label to suppress warnings about "enumeration value
5586 // 'xxx' not handled in switch
5594 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5599 if (m_colLabelWin
->HasCapture())
5600 m_colLabelWin
->ReleaseMouse();
5601 m_isDragging
= false;
5604 // ------------ Entering or leaving the window
5606 if ( event
.Entering() || event
.Leaving() )
5608 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5611 // ------------ Left button pressed
5613 else if ( event
.LeftDown() )
5615 // don't send a label click event for a hit on the
5616 // edge of the col label - this is probably the user
5617 // wanting to resize the col
5619 if ( XToEdgeOfCol(x
) < 0 )
5623 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5625 if ( m_canDragColMove
)
5627 //Show button as pressed
5628 wxClientDC
dc( m_colLabelWin
);
5629 int colLeft
= GetColLeft( col
);
5630 int colRight
= GetColRight( col
) - 1;
5631 dc
.SetPen( wxPen( m_colLabelWin
->GetBackgroundColour(), 1 ) );
5632 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
5633 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
5635 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, m_colLabelWin
);
5639 if ( !event
.ShiftDown() && !event
.CmdDown() )
5643 if ( event
.ShiftDown() )
5645 m_selection
->SelectBlock( 0,
5646 m_currentCellCoords
.GetCol(),
5647 GetNumberRows() - 1, col
,
5648 event
.ControlDown(),
5655 m_selection
->SelectCol( col
,
5656 event
.ControlDown(),
5663 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5669 // starting to drag-resize a col
5671 if ( CanDragColSize() )
5672 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5676 // ------------ Left double click
5678 if ( event
.LeftDClick() )
5680 col
= XToEdgeOfCol(x
);
5685 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5687 // no default action at the moment
5692 // adjust column width depending on label text
5693 AutoSizeColLabelSize( col
);
5695 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5700 // ------------ Left button released
5702 else if ( event
.LeftUp() )
5704 switch ( m_cursorMode
)
5706 case WXGRID_CURSOR_RESIZE_COL
:
5707 DoEndDragResizeCol();
5709 // Note: we are ending the event *after* doing
5710 // default processing in this case
5712 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5715 case WXGRID_CURSOR_MOVE_COL
:
5718 SendEvent( wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
, event
);
5721 case WXGRID_CURSOR_SELECT_COL
:
5722 case WXGRID_CURSOR_SELECT_CELL
:
5723 case WXGRID_CURSOR_RESIZE_ROW
:
5724 case WXGRID_CURSOR_SELECT_ROW
:
5725 // nothing to do (?)
5729 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5733 // ------------ Right button down
5735 else if ( event
.RightDown() )
5739 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5741 // no default action at the moment
5745 // ------------ Right double click
5747 else if ( event
.RightDClick() )
5751 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5753 // no default action at the moment
5757 // ------------ No buttons down and mouse moving
5759 else if ( event
.Moving() )
5761 m_dragRowOrCol
= XToEdgeOfCol( x
);
5762 if ( m_dragRowOrCol
>= 0 )
5764 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5766 // don't capture the cursor yet
5767 if ( CanDragColSize() )
5768 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5771 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5773 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5778 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5780 if ( event
.LeftDown() )
5782 // indicate corner label by having both row and
5785 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5790 else if ( event
.LeftDClick() )
5792 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5794 else if ( event
.RightDown() )
5796 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5798 // no default action at the moment
5801 else if ( event
.RightDClick() )
5803 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5805 // no default action at the moment
5810 void wxGrid::CancelMouseCapture()
5812 // cancel operation currently in progress, whatever it is
5815 m_isDragging
= false;
5816 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
5817 m_winCapture
->SetCursor( *wxSTANDARD_CURSOR
);
5818 m_winCapture
= NULL
;
5820 // remove traces of whatever we drew on screen
5825 void wxGrid::ChangeCursorMode(CursorMode mode
,
5830 static const wxChar
*cursorModes
[] =
5840 wxLogTrace(_T("grid"),
5841 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5842 win
== m_colLabelWin
? _T("colLabelWin")
5843 : win
? _T("rowLabelWin")
5845 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5848 if ( mode
== m_cursorMode
&&
5849 win
== m_winCapture
&&
5850 captureMouse
== (m_winCapture
!= NULL
))
5855 // by default use the grid itself
5861 if (m_winCapture
->HasCapture())
5862 m_winCapture
->ReleaseMouse();
5863 m_winCapture
= (wxWindow
*)NULL
;
5866 m_cursorMode
= mode
;
5868 switch ( m_cursorMode
)
5870 case WXGRID_CURSOR_RESIZE_ROW
:
5871 win
->SetCursor( m_rowResizeCursor
);
5874 case WXGRID_CURSOR_RESIZE_COL
:
5875 win
->SetCursor( m_colResizeCursor
);
5878 case WXGRID_CURSOR_MOVE_COL
:
5879 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
5883 win
->SetCursor( *wxSTANDARD_CURSOR
);
5887 // we need to capture mouse when resizing
5888 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5889 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5891 if ( captureMouse
&& resize
)
5893 win
->CaptureMouse();
5898 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5901 wxPoint
pos( event
.GetPosition() );
5902 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5904 wxGridCellCoords coords
;
5905 XYToCell( x
, y
, coords
);
5907 int cell_rows
, cell_cols
;
5908 bool isFirstDrag
= !m_isDragging
;
5909 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5910 if ((cell_rows
< 0) || (cell_cols
< 0))
5912 coords
.SetRow(coords
.GetRow() + cell_rows
);
5913 coords
.SetCol(coords
.GetCol() + cell_cols
);
5916 if ( event
.Dragging() )
5918 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5920 // Don't start doing anything until the mouse has been dragged at
5921 // least 3 pixels in any direction...
5924 if (m_startDragPos
== wxDefaultPosition
)
5926 m_startDragPos
= pos
;
5929 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5933 m_isDragging
= true;
5934 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5936 // Hide the edit control, so it
5937 // won't interfere with drag-shrinking.
5938 if ( IsCellEditControlShown() )
5940 HideCellEditControl();
5941 SaveEditControlValue();
5944 if ( coords
!= wxGridNoCellCoords
)
5946 if ( event
.CmdDown() )
5948 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5949 m_selectingKeyboard
= coords
;
5950 HighlightBlock( m_selectingKeyboard
, coords
);
5952 else if ( CanDragCell() )
5956 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5957 m_selectingKeyboard
= coords
;
5959 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5968 if ( !IsSelection() )
5970 HighlightBlock( coords
, coords
);
5974 HighlightBlock( m_currentCellCoords
, coords
);
5978 if (! IsVisible(coords
))
5980 MakeCellVisible(coords
);
5981 // TODO: need to introduce a delay or something here. The
5982 // scrolling is way to fast, at least on MSW - also on GTK.
5985 // Have we captured the mouse yet?
5988 m_winCapture
= m_gridWin
;
5989 m_winCapture
->CaptureMouse();
5994 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5996 int cw
, ch
, left
, dummy
;
5997 m_gridWin
->GetClientSize( &cw
, &ch
);
5998 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6000 wxClientDC
dc( m_gridWin
);
6002 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
6003 GetRowMinimalHeight(m_dragRowOrCol
) );
6004 dc
.SetLogicalFunction(wxINVERT
);
6005 if ( m_dragLastPos
>= 0 )
6007 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
6009 dc
.DrawLine( left
, y
, left
+cw
, y
);
6012 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6014 int cw
, ch
, dummy
, top
;
6015 m_gridWin
->GetClientSize( &cw
, &ch
);
6016 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6018 wxClientDC
dc( m_gridWin
);
6020 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
6021 GetColMinimalWidth(m_dragRowOrCol
) );
6022 dc
.SetLogicalFunction(wxINVERT
);
6023 if ( m_dragLastPos
>= 0 )
6025 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6027 dc
.DrawLine( x
, top
, x
, top
+ ch
);
6034 m_isDragging
= false;
6035 m_startDragPos
= wxDefaultPosition
;
6037 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
6038 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
6041 if ( event
.Entering() || event
.Leaving() )
6043 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6044 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
6049 // ------------ Left button pressed
6051 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
6053 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
6058 if ( !event
.CmdDown() )
6060 if ( event
.ShiftDown() )
6064 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
6065 m_currentCellCoords
.GetCol(),
6068 event
.ControlDown(),
6074 else if ( XToEdgeOfCol(x
) < 0 &&
6075 YToEdgeOfRow(y
) < 0 )
6077 DisableCellEditControl();
6078 MakeCellVisible( coords
);
6080 if ( event
.CmdDown() )
6084 m_selection
->ToggleCellSelection( coords
.GetRow(),
6086 event
.ControlDown(),
6091 m_selectingTopLeft
= wxGridNoCellCoords
;
6092 m_selectingBottomRight
= wxGridNoCellCoords
;
6093 m_selectingKeyboard
= coords
;
6097 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
6098 SetCurrentCell( coords
);
6101 if ( m_selection
->GetSelectionMode() !=
6102 wxGrid::wxGridSelectCells
)
6104 HighlightBlock( coords
, coords
);
6112 // ------------ Left double click
6114 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
6116 DisableCellEditControl();
6118 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
6120 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
6125 // we want double click to select a cell and start editing
6126 // (i.e. to behave in same way as sequence of two slow clicks):
6127 m_waitForSlowClick
= true;
6132 // ------------ Left button released
6134 else if ( event
.LeftUp() )
6136 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6140 if (m_winCapture
->HasCapture())
6141 m_winCapture
->ReleaseMouse();
6142 m_winCapture
= NULL
;
6145 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
6148 EnableCellEditControl();
6150 wxGridCellAttr
*attr
= GetCellAttr(coords
);
6151 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
6152 editor
->StartingClick();
6156 m_waitForSlowClick
= false;
6158 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6159 m_selectingBottomRight
!= wxGridNoCellCoords
)
6163 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6164 m_selectingTopLeft
.GetCol(),
6165 m_selectingBottomRight
.GetRow(),
6166 m_selectingBottomRight
.GetCol(),
6167 event
.ControlDown(),
6173 m_selectingTopLeft
= wxGridNoCellCoords
;
6174 m_selectingBottomRight
= wxGridNoCellCoords
;
6176 // Show the edit control, if it has been hidden for
6178 ShowCellEditControl();
6181 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6183 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6184 DoEndDragResizeRow();
6186 // Note: we are ending the event *after* doing
6187 // default processing in this case
6189 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
6191 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6193 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6194 DoEndDragResizeCol();
6196 // Note: we are ending the event *after* doing
6197 // default processing in this case
6199 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
6205 // ------------ Right button down
6207 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
6209 DisableCellEditControl();
6210 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
6215 // no default action at the moment
6219 // ------------ Right double click
6221 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
6223 DisableCellEditControl();
6224 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
6229 // no default action at the moment
6233 // ------------ Moving and no button action
6235 else if ( event
.Moving() && !event
.IsButton() )
6237 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
6239 // out of grid cell area
6240 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6244 int dragRow
= YToEdgeOfRow( y
);
6245 int dragCol
= XToEdgeOfCol( x
);
6247 // Dragging on the corner of a cell to resize in both
6248 // directions is not implemented yet...
6250 if ( dragRow
>= 0 && dragCol
>= 0 )
6252 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6258 m_dragRowOrCol
= dragRow
;
6260 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6262 if ( CanDragRowSize() && CanDragGridSize() )
6263 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false);
6266 else if ( dragCol
>= 0 )
6268 m_dragRowOrCol
= dragCol
;
6270 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6272 if ( CanDragColSize() && CanDragGridSize() )
6273 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false);
6276 else // Neither on a row or col edge
6278 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6280 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6286 void wxGrid::DoEndDragResizeRow()
6288 if ( m_dragLastPos
>= 0 )
6290 // erase the last line and resize the row
6292 int cw
, ch
, left
, dummy
;
6293 m_gridWin
->GetClientSize( &cw
, &ch
);
6294 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6296 wxClientDC
dc( m_gridWin
);
6298 dc
.SetLogicalFunction( wxINVERT
);
6299 dc
.DrawLine( left
, m_dragLastPos
, left
+ cw
, m_dragLastPos
);
6300 HideCellEditControl();
6301 SaveEditControlValue();
6303 int rowTop
= GetRowTop(m_dragRowOrCol
);
6304 SetRowSize( m_dragRowOrCol
,
6305 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
6307 if ( !GetBatchCount() )
6309 // Only needed to get the correct rect.y:
6310 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
6312 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6313 rect
.width
= m_rowLabelWidth
;
6314 rect
.height
= ch
- rect
.y
;
6315 m_rowLabelWin
->Refresh( true, &rect
);
6318 // if there is a multicell block, paint all of it
6321 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
6322 int leftCol
= XToCol(left
);
6323 int rightCol
= internalXToCol(left
+ cw
);
6326 for (i
=leftCol
; i
<rightCol
; i
++)
6328 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
6329 if (cell_rows
< subtract_rows
)
6330 subtract_rows
= cell_rows
;
6332 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
6333 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6334 rect
.height
= ch
- rect
.y
;
6337 m_gridWin
->Refresh( false, &rect
);
6340 ShowCellEditControl();
6345 void wxGrid::DoEndDragResizeCol()
6347 if ( m_dragLastPos
>= 0 )
6349 // erase the last line and resize the col
6351 int cw
, ch
, dummy
, top
;
6352 m_gridWin
->GetClientSize( &cw
, &ch
);
6353 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6355 wxClientDC
dc( m_gridWin
);
6357 dc
.SetLogicalFunction( wxINVERT
);
6358 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6359 HideCellEditControl();
6360 SaveEditControlValue();
6362 int colLeft
= GetColLeft(m_dragRowOrCol
);
6363 SetColSize( m_dragRowOrCol
,
6364 wxMax( m_dragLastPos
- colLeft
,
6365 GetColMinimalWidth(m_dragRowOrCol
) ) );
6367 if ( !GetBatchCount() )
6369 // Only needed to get the correct rect.x:
6370 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
6372 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6373 rect
.width
= cw
- rect
.x
;
6374 rect
.height
= m_colLabelHeight
;
6375 m_colLabelWin
->Refresh( true, &rect
);
6378 // if there is a multicell block, paint all of it
6381 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
6382 int topRow
= YToRow(top
);
6383 int bottomRow
= internalYToRow(top
+ cw
);
6386 for (i
=topRow
; i
<bottomRow
; i
++)
6388 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
6389 if (cell_cols
< subtract_cols
)
6390 subtract_cols
= cell_cols
;
6393 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
6394 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6395 rect
.width
= cw
- rect
.x
;
6399 m_gridWin
->Refresh( false, &rect
);
6402 ShowCellEditControl();
6406 void wxGrid::DoEndDragMoveCol()
6408 //The user clicked on the column but didn't actually drag
6409 if ( m_dragLastPos
< 0 )
6411 m_colLabelWin
->Refresh(); //Do this to "unpress" the column
6416 if ( m_moveToCol
== -1 )
6417 newPos
= m_numCols
- 1;
6420 newPos
= GetColPos( m_moveToCol
);
6421 if ( newPos
> GetColPos( m_dragRowOrCol
) )
6425 SetColPos( m_dragRowOrCol
, newPos
);
6428 void wxGrid::SetColPos( int colID
, int newPos
)
6430 if ( m_colAt
.IsEmpty() )
6432 m_colAt
.Alloc( m_numCols
);
6435 for ( i
= 0; i
< m_numCols
; i
++ )
6441 int oldPos
= GetColPos( colID
);
6443 //Reshuffle the m_colAt array
6444 if ( newPos
> oldPos
)
6447 for ( i
= oldPos
; i
< newPos
; i
++ )
6449 m_colAt
[i
] = m_colAt
[i
+1];
6455 for ( i
= oldPos
; i
> newPos
; i
-- )
6457 m_colAt
[i
] = m_colAt
[i
-1];
6461 m_colAt
[newPos
] = colID
;
6463 //Recalculate the column rights
6464 if ( !m_colWidths
.IsEmpty() )
6468 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6470 int colID
= GetColAt( colPos
);
6472 colRight
+= m_colWidths
[colID
];
6473 m_colRights
[colID
] = colRight
;
6477 m_colLabelWin
->Refresh();
6478 m_gridWin
->Refresh();
6483 void wxGrid::EnableDragColMove( bool enable
)
6485 if ( m_canDragColMove
== enable
)
6488 m_canDragColMove
= enable
;
6490 if ( !m_canDragColMove
)
6494 //Recalculate the column rights
6495 if ( !m_colWidths
.IsEmpty() )
6499 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6501 colRight
+= m_colWidths
[colPos
];
6502 m_colRights
[colPos
] = colRight
;
6506 m_colLabelWin
->Refresh();
6507 m_gridWin
->Refresh();
6513 // ------ interaction with data model
6515 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6517 switch ( msg
.GetId() )
6519 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6520 return GetModelValues();
6522 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6523 return SetModelValues();
6525 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6526 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6527 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6528 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6529 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6530 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6531 return Redimension( msg
);
6538 // The behaviour of this function depends on the grid table class
6539 // Clear() function. For the default wxGridStringTable class the
6540 // behavious is to replace all cell contents with wxEmptyString but
6541 // not to change the number of rows or cols.
6543 void wxGrid::ClearGrid()
6547 if (IsCellEditControlEnabled())
6548 DisableCellEditControl();
6551 if (!GetBatchCount())
6552 m_gridWin
->Refresh();
6556 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6558 // TODO: something with updateLabels flag
6562 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6568 if (IsCellEditControlEnabled())
6569 DisableCellEditControl();
6571 bool done
= m_table
->InsertRows( pos
, numRows
);
6574 // the table will have sent the results of the insert row
6575 // operation to this view object as a grid table message
6581 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6583 // TODO: something with updateLabels flag
6587 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6593 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6596 // the table will have sent the results of the append row
6597 // operation to this view object as a grid table message
6603 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6605 // TODO: something with updateLabels flag
6609 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6615 if (IsCellEditControlEnabled())
6616 DisableCellEditControl();
6618 bool done
= m_table
->DeleteRows( pos
, numRows
);
6620 // the table will have sent the results of the delete row
6621 // operation to this view object as a grid table message
6627 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6629 // TODO: something with updateLabels flag
6633 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6639 if (IsCellEditControlEnabled())
6640 DisableCellEditControl();
6642 bool done
= m_table
->InsertCols( pos
, numCols
);
6644 // the table will have sent the results of the insert col
6645 // operation to this view object as a grid table message
6651 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6653 // TODO: something with updateLabels flag
6657 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6663 bool done
= m_table
->AppendCols( numCols
);
6665 // the table will have sent the results of the append col
6666 // operation to this view object as a grid table message
6672 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6674 // TODO: something with updateLabels flag
6678 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6684 if (IsCellEditControlEnabled())
6685 DisableCellEditControl();
6687 bool done
= m_table
->DeleteCols( pos
, numCols
);
6689 // the table will have sent the results of the delete col
6690 // operation to this view object as a grid table message
6697 // ----- event handlers
6700 // Generate a grid event based on a mouse event and
6701 // return the result of ProcessEvent()
6703 int wxGrid::SendEvent( const wxEventType type
,
6705 wxMouseEvent
& mouseEv
)
6707 bool claimed
, vetoed
;
6709 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6711 int rowOrCol
= (row
== -1 ? col
: row
);
6713 wxGridSizeEvent
gridEvt( GetId(),
6717 mouseEv
.GetX() + GetRowLabelSize(),
6718 mouseEv
.GetY() + GetColLabelSize(),
6719 mouseEv
.ControlDown(),
6720 mouseEv
.ShiftDown(),
6722 mouseEv
.MetaDown() );
6724 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6725 vetoed
= !gridEvt
.IsAllowed();
6727 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6729 // Right now, it should _never_ end up here!
6730 wxGridRangeSelectEvent
gridEvt( GetId(),
6734 m_selectingBottomRight
,
6736 mouseEv
.ControlDown(),
6737 mouseEv
.ShiftDown(),
6739 mouseEv
.MetaDown() );
6741 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6742 vetoed
= !gridEvt
.IsAllowed();
6744 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
6745 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
6746 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
6747 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
6749 wxPoint pos
= mouseEv
.GetPosition();
6751 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
6752 pos
.y
+= GetColLabelSize();
6753 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
6754 pos
.x
+= GetRowLabelSize();
6756 wxGridEvent
gridEvt( GetId(),
6763 mouseEv
.ControlDown(),
6764 mouseEv
.ShiftDown(),
6766 mouseEv
.MetaDown() );
6767 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6768 vetoed
= !gridEvt
.IsAllowed();
6772 wxGridEvent
gridEvt( GetId(),
6776 mouseEv
.GetX() + GetRowLabelSize(),
6777 mouseEv
.GetY() + GetColLabelSize(),
6779 mouseEv
.ControlDown(),
6780 mouseEv
.ShiftDown(),
6782 mouseEv
.MetaDown() );
6783 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6784 vetoed
= !gridEvt
.IsAllowed();
6787 // A Veto'd event may not be `claimed' so test this first
6791 return claimed
? 1 : 0;
6794 // Generate a grid event of specified type and return the result
6795 // of ProcessEvent().
6797 int wxGrid::SendEvent( const wxEventType type
,
6800 bool claimed
, vetoed
;
6802 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6804 int rowOrCol
= (row
== -1 ? col
: row
);
6806 wxGridSizeEvent
gridEvt( GetId(), type
, this, rowOrCol
);
6808 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6809 vetoed
= !gridEvt
.IsAllowed();
6813 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
6815 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6816 vetoed
= !gridEvt
.IsAllowed();
6819 // A Veto'd event may not be `claimed' so test this first
6823 return claimed
? 1 : 0;
6826 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6828 // needed to prevent zillions of paint events on MSW
6832 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6834 // Don't do anything if between Begin/EndBatch...
6835 // EndBatch() will do all this on the last nested one anyway.
6836 if ( m_created
&& !GetBatchCount() )
6838 // Refresh to get correct scrolled position:
6839 wxScrolledWindow::Refresh(eraseb
, rect
);
6843 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6844 int width_label
, width_cell
, height_label
, height_cell
;
6847 // Copy rectangle can get scroll offsets..
6848 rect_x
= rect
->GetX();
6849 rect_y
= rect
->GetY();
6850 rectWidth
= rect
->GetWidth();
6851 rectHeight
= rect
->GetHeight();
6853 width_label
= m_rowLabelWidth
- rect_x
;
6854 if (width_label
> rectWidth
)
6855 width_label
= rectWidth
;
6857 height_label
= m_colLabelHeight
- rect_y
;
6858 if (height_label
> rectHeight
)
6859 height_label
= rectHeight
;
6861 if (rect_x
> m_rowLabelWidth
)
6863 x
= rect_x
- m_rowLabelWidth
;
6864 width_cell
= rectWidth
;
6869 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6872 if (rect_y
> m_colLabelHeight
)
6874 y
= rect_y
- m_colLabelHeight
;
6875 height_cell
= rectHeight
;
6880 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6883 // Paint corner label part intersecting rect.
6884 if ( width_label
> 0 && height_label
> 0 )
6886 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6887 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6890 // Paint col labels part intersecting rect.
6891 if ( width_cell
> 0 && height_label
> 0 )
6893 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6894 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6897 // Paint row labels part intersecting rect.
6898 if ( width_label
> 0 && height_cell
> 0 )
6900 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6901 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6904 // Paint cell area part intersecting rect.
6905 if ( width_cell
> 0 && height_cell
> 0 )
6907 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6908 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6913 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6914 m_colLabelWin
->Refresh(eraseb
, NULL
);
6915 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6916 m_gridWin
->Refresh(eraseb
, NULL
);
6921 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
))
6923 if (m_targetWindow
!= this) // check whether initialisation has been done
6925 // update our children window positions and scrollbars
6930 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6932 if ( m_inOnKeyDown
)
6934 // shouldn't be here - we are going round in circles...
6936 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6939 m_inOnKeyDown
= true;
6941 // propagate the event up and see if it gets processed
6942 wxWindow
*parent
= GetParent();
6943 wxKeyEvent
keyEvt( event
);
6944 keyEvt
.SetEventObject( parent
);
6946 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6948 if (GetLayoutDirection() == wxLayout_RightToLeft
)
6950 if (event
.GetKeyCode() == WXK_RIGHT
)
6951 event
.m_keyCode
= WXK_LEFT
;
6952 else if (event
.GetKeyCode() == WXK_LEFT
)
6953 event
.m_keyCode
= WXK_RIGHT
;
6956 // try local handlers
6957 switch ( event
.GetKeyCode() )
6960 if ( event
.ControlDown() )
6961 MoveCursorUpBlock( event
.ShiftDown() );
6963 MoveCursorUp( event
.ShiftDown() );
6967 if ( event
.ControlDown() )
6968 MoveCursorDownBlock( event
.ShiftDown() );
6970 MoveCursorDown( event
.ShiftDown() );
6974 if ( event
.ControlDown() )
6975 MoveCursorLeftBlock( event
.ShiftDown() );
6977 MoveCursorLeft( event
.ShiftDown() );
6981 if ( event
.ControlDown() )
6982 MoveCursorRightBlock( event
.ShiftDown() );
6984 MoveCursorRight( event
.ShiftDown() );
6988 case WXK_NUMPAD_ENTER
:
6989 if ( event
.ControlDown() )
6991 event
.Skip(); // to let the edit control have the return
6995 if ( GetGridCursorRow() < GetNumberRows()-1 )
6997 MoveCursorDown( event
.ShiftDown() );
7001 // at the bottom of a column
7002 DisableCellEditControl();
7012 if (event
.ShiftDown())
7014 if ( GetGridCursorCol() > 0 )
7016 MoveCursorLeft( false );
7021 DisableCellEditControl();
7026 if ( GetGridCursorCol() < GetNumberCols() - 1 )
7028 MoveCursorRight( false );
7033 DisableCellEditControl();
7039 if ( event
.ControlDown() )
7041 MakeCellVisible( 0, 0 );
7042 SetCurrentCell( 0, 0 );
7051 if ( event
.ControlDown() )
7053 MakeCellVisible( m_numRows
- 1, m_numCols
- 1 );
7054 SetCurrentCell( m_numRows
- 1, m_numCols
- 1 );
7071 if ( event
.ControlDown() )
7075 m_selection
->ToggleCellSelection(
7076 m_currentCellCoords
.GetRow(),
7077 m_currentCellCoords
.GetCol(),
7078 event
.ControlDown(),
7086 if ( !IsEditable() )
7087 MoveCursorRight( false );
7098 m_inOnKeyDown
= false;
7101 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
7103 // try local handlers
7105 if ( event
.GetKeyCode() == WXK_SHIFT
)
7107 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
7108 m_selectingBottomRight
!= wxGridNoCellCoords
)
7112 m_selection
->SelectBlock(
7113 m_selectingTopLeft
.GetRow(),
7114 m_selectingTopLeft
.GetCol(),
7115 m_selectingBottomRight
.GetRow(),
7116 m_selectingBottomRight
.GetCol(),
7117 event
.ControlDown(),
7124 m_selectingTopLeft
= wxGridNoCellCoords
;
7125 m_selectingBottomRight
= wxGridNoCellCoords
;
7126 m_selectingKeyboard
= wxGridNoCellCoords
;
7130 void wxGrid::OnChar( wxKeyEvent
& event
)
7132 // is it possible to edit the current cell at all?
7133 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7135 // yes, now check whether the cells editor accepts the key
7136 int row
= m_currentCellCoords
.GetRow();
7137 int col
= m_currentCellCoords
.GetCol();
7138 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7139 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7141 // <F2> is special and will always start editing, for
7142 // other keys - ask the editor itself
7143 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
7144 || editor
->IsAcceptedKey(event
) )
7146 // ensure cell is visble
7147 MakeCellVisible(row
, col
);
7148 EnableCellEditControl();
7150 // a problem can arise if the cell is not completely
7151 // visible (even after calling MakeCellVisible the
7152 // control is not created and calling StartingKey will
7154 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
7155 editor
->StartingKey(event
);
7171 void wxGrid::OnEraseBackground(wxEraseEvent
&)
7175 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
7177 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
7179 // the event has been intercepted - do nothing
7183 #if !defined(__WXMAC__)
7184 wxClientDC
dc( m_gridWin
);
7188 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
7190 DisableCellEditControl();
7192 if ( IsVisible( m_currentCellCoords
, false ) )
7195 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
7196 if ( !m_gridLinesEnabled
)
7204 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
7206 // Otherwise refresh redraws the highlight!
7207 m_currentCellCoords
= coords
;
7209 #if defined(__WXMAC__)
7210 m_gridWin
->Refresh(true /*, & r */);
7212 DrawGridCellArea( dc
, cells
);
7213 DrawAllGridLines( dc
, r
);
7218 m_currentCellCoords
= coords
;
7220 wxGridCellAttr
*attr
= GetCellAttr( coords
);
7221 #if !defined(__WXMAC__)
7222 DrawCellHighlight( dc
, attr
);
7227 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7230 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7234 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
7237 rightCol
= GetNumberCols() - 1;
7239 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
7242 bottomRow
= GetNumberRows() - 1;
7246 if ( topRow
> bottomRow
)
7253 if ( leftCol
> rightCol
)
7260 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7261 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7263 // First the case that we selected a completely new area
7264 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
7265 m_selectingBottomRight
== wxGridNoCellCoords
)
7268 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
7269 wxGridCellCoords ( bottomRow
, rightCol
) );
7270 m_gridWin
->Refresh( false, &rect
);
7273 // Now handle changing an existing selection area.
7274 else if ( m_selectingTopLeft
!= updateTopLeft
||
7275 m_selectingBottomRight
!= updateBottomRight
)
7277 // Compute two optimal update rectangles:
7278 // Either one rectangle is a real subset of the
7279 // other, or they are (almost) disjoint!
7281 bool need_refresh
[4];
7285 need_refresh
[3] = false;
7288 // Store intermediate values
7289 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
7290 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
7291 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
7292 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
7294 // Determine the outer/inner coordinates.
7295 if (oldLeft
> leftCol
)
7301 if (oldTop
> topRow
)
7307 if (oldRight
< rightCol
)
7310 oldRight
= rightCol
;
7313 if (oldBottom
< bottomRow
)
7316 oldBottom
= bottomRow
;
7320 // Now, either the stuff marked old is the outer
7321 // rectangle or we don't have a situation where one
7322 // is contained in the other.
7324 if ( oldLeft
< leftCol
)
7326 // Refresh the newly selected or deselected
7327 // area to the left of the old or new selection.
7328 need_refresh
[0] = true;
7329 rect
[0] = BlockToDeviceRect(
7330 wxGridCellCoords( oldTop
, oldLeft
),
7331 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
7334 if ( oldTop
< topRow
)
7336 // Refresh the newly selected or deselected
7337 // area above the old or new selection.
7338 need_refresh
[1] = true;
7339 rect
[1] = BlockToDeviceRect(
7340 wxGridCellCoords( oldTop
, leftCol
),
7341 wxGridCellCoords( topRow
- 1, rightCol
) );
7344 if ( oldRight
> rightCol
)
7346 // Refresh the newly selected or deselected
7347 // area to the right of the old or new selection.
7348 need_refresh
[2] = true;
7349 rect
[2] = BlockToDeviceRect(
7350 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
7351 wxGridCellCoords( oldBottom
, oldRight
) );
7354 if ( oldBottom
> bottomRow
)
7356 // Refresh the newly selected or deselected
7357 // area below the old or new selection.
7358 need_refresh
[3] = true;
7359 rect
[3] = BlockToDeviceRect(
7360 wxGridCellCoords( bottomRow
+ 1, leftCol
),
7361 wxGridCellCoords( oldBottom
, rightCol
) );
7364 // various Refresh() calls
7365 for (i
= 0; i
< 4; i
++ )
7366 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7367 m_gridWin
->Refresh( false, &(rect
[i
]) );
7371 m_selectingTopLeft
= updateTopLeft
;
7372 m_selectingBottomRight
= updateBottomRight
;
7376 // ------ functions to get/send data (see also public functions)
7379 bool wxGrid::GetModelValues()
7381 // Hide the editor, so it won't hide a changed value.
7382 HideCellEditControl();
7386 // all we need to do is repaint the grid
7388 m_gridWin
->Refresh();
7395 bool wxGrid::SetModelValues()
7399 // Disable the editor, so it won't hide a changed value.
7400 // Do we also want to save the current value of the editor first?
7402 DisableCellEditControl();
7406 for ( row
= 0; row
< m_numRows
; row
++ )
7408 for ( col
= 0; col
< m_numCols
; col
++ )
7410 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
7420 // Note - this function only draws cells that are in the list of
7421 // exposed cells (usually set from the update region by
7422 // CalcExposedCells)
7424 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7426 if ( !m_numRows
|| !m_numCols
)
7429 int i
, numCells
= cells
.GetCount();
7430 int row
, col
, cell_rows
, cell_cols
;
7431 wxGridCellCoordsArray redrawCells
;
7433 for ( i
= numCells
- 1; i
>= 0; i
-- )
7435 row
= cells
[i
].GetRow();
7436 col
= cells
[i
].GetCol();
7437 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7439 // If this cell is part of a multicell block, find owner for repaint
7440 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7442 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
7443 bool marked
= false;
7444 for ( int j
= 0; j
< numCells
; j
++ )
7446 if ( cell
== cells
[j
] )
7455 int count
= redrawCells
.GetCount();
7456 for (int j
= 0; j
< count
; j
++)
7458 if ( cell
== redrawCells
[j
] )
7466 redrawCells
.Add( cell
);
7469 // don't bother drawing this cell
7473 // If this cell is empty, find cell to left that might want to overflow
7474 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7476 for ( int l
= 0; l
< cell_rows
; l
++ )
7478 // find a cell in this row to leave already marked for repaint
7480 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7481 if ((redrawCells
[k
].GetCol() < left
) &&
7482 (redrawCells
[k
].GetRow() == row
))
7484 left
= redrawCells
[k
].GetCol();
7488 left
= 0; // oh well
7490 for (int j
= col
- 1; j
>= left
; j
--)
7492 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7494 if (GetCellOverflow(row
+ l
, j
))
7496 wxGridCellCoords
cell(row
+ l
, j
);
7497 bool marked
= false;
7499 for (int k
= 0; k
< numCells
; k
++)
7501 if ( cell
== cells
[k
] )
7510 int count
= redrawCells
.GetCount();
7511 for (int k
= 0; k
< count
; k
++)
7513 if ( cell
== redrawCells
[k
] )
7520 redrawCells
.Add( cell
);
7529 DrawCell( dc
, cells
[i
] );
7532 numCells
= redrawCells
.GetCount();
7534 for ( i
= numCells
- 1; i
>= 0; i
-- )
7536 DrawCell( dc
, redrawCells
[i
] );
7540 void wxGrid::DrawGridSpace( wxDC
& dc
)
7543 m_gridWin
->GetClientSize( &cw
, &ch
);
7546 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7548 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
7549 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7551 if ( right
> rightCol
|| bottom
> bottomRow
)
7554 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7556 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxBRUSHSTYLE_SOLID
) );
7557 dc
.SetPen( *wxTRANSPARENT_PEN
);
7559 if ( right
> rightCol
)
7561 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7564 if ( bottom
> bottomRow
)
7566 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7571 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7573 int row
= coords
.GetRow();
7574 int col
= coords
.GetCol();
7576 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7579 // we draw the cell border ourselves
7580 #if !WXGRID_DRAW_LINES
7581 if ( m_gridLinesEnabled
)
7582 DrawCellBorder( dc
, coords
);
7585 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7587 bool isCurrent
= coords
== m_currentCellCoords
;
7589 wxRect rect
= CellToRect( row
, col
);
7591 // if the editor is shown, we should use it and not the renderer
7592 // Note: However, only if it is really _shown_, i.e. not hidden!
7593 if ( isCurrent
&& IsCellEditControlShown() )
7595 // NB: this "#if..." is temporary and fixes a problem where the
7596 // edit control is erased by this code after being rendered.
7597 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7598 // implicitly, causing this out-of order render.
7599 #if !defined(__WXMAC__)
7600 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7601 editor
->PaintBackground(rect
, attr
);
7607 // but all the rest is drawn by the cell renderer and hence may be customized
7608 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7609 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7616 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7618 // don't show highlight when the grid doesn't have focus
7622 int row
= m_currentCellCoords
.GetRow();
7623 int col
= m_currentCellCoords
.GetCol();
7625 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7628 wxRect rect
= CellToRect(row
, col
);
7630 // hmmm... what could we do here to show that the cell is disabled?
7631 // for now, I just draw a thinner border than for the other ones, but
7632 // it doesn't look really good
7634 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7638 // The center of the drawn line is where the position/width/height of
7639 // the rectangle is actually at (on wxMSW at least), so the
7640 // size of the rectangle is reduced to compensate for the thickness of
7641 // the line. If this is too strange on non-wxMSW platforms then
7642 // please #ifdef this appropriately.
7643 rect
.x
+= penWidth
/ 2;
7644 rect
.y
+= penWidth
/ 2;
7645 rect
.width
-= penWidth
- 1;
7646 rect
.height
-= penWidth
- 1;
7648 // Now draw the rectangle
7649 // use the cellHighlightColour if the cell is inside a selection, this
7650 // will ensure the cell is always visible.
7651 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
: m_cellHighlightColour
, penWidth
, wxPENSTYLE_SOLID
));
7652 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7653 dc
.DrawRectangle(rect
);
7657 // VZ: my experiments with 3D borders...
7659 // how to properly set colours for arbitrary bg?
7660 wxCoord x1
= rect
.x
,
7662 x2
= rect
.x
+ rect
.width
- 1,
7663 y2
= rect
.y
+ rect
.height
- 1;
7665 dc
.SetPen(*wxWHITE_PEN
);
7666 dc
.DrawLine(x1
, y1
, x2
, y1
);
7667 dc
.DrawLine(x1
, y1
, x1
, y2
);
7669 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7670 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7672 dc
.SetPen(*wxBLACK_PEN
);
7673 dc
.DrawLine(x1
, y2
, x2
, y2
);
7674 dc
.DrawLine(x2
, y1
, x2
, y2
+ 1);
7678 wxPen
wxGrid::GetDefaultGridLinePen()
7680 return wxPen(GetGridLineColour(), 1, wxPENSTYLE_SOLID
);
7683 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
7685 return GetDefaultGridLinePen();
7688 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
7690 return GetDefaultGridLinePen();
7693 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7695 int row
= coords
.GetRow();
7696 int col
= coords
.GetCol();
7697 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7701 wxRect rect
= CellToRect( row
, col
);
7703 // right hand border
7704 dc
.SetPen( GetColGridLinePen(col
) );
7705 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7706 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7709 dc
.SetPen( GetRowGridLinePen(row
) );
7710 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7711 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7714 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7716 // This if block was previously in wxGrid::OnPaint but that doesn't
7717 // seem to get called under wxGTK - MB
7719 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7720 m_numRows
&& m_numCols
)
7722 m_currentCellCoords
.Set(0, 0);
7725 if ( IsCellEditControlShown() )
7727 // don't show highlight when the edit control is shown
7731 // if the active cell was repainted, repaint its highlight too because it
7732 // might have been damaged by the grid lines
7733 size_t count
= cells
.GetCount();
7734 for ( size_t n
= 0; n
< count
; n
++ )
7736 if ( cells
[n
] == m_currentCellCoords
)
7738 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7739 DrawCellHighlight(dc
, attr
);
7747 // TODO: remove this ???
7748 // This is used to redraw all grid lines e.g. when the grid line colour
7751 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7753 #if !WXGRID_DRAW_LINES
7757 if ( !m_gridLinesEnabled
|| !m_numRows
|| !m_numCols
)
7760 int top
, bottom
, left
, right
;
7762 #if 0 //#ifndef __WXGTK__
7766 m_gridWin
->GetClientSize(&cw
, &ch
);
7768 // virtual coords of visible area
7770 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7771 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7776 reg
.GetBox(x
, y
, w
, h
);
7777 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7778 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7782 m_gridWin
->GetClientSize(&cw
, &ch
);
7783 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7784 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7787 // avoid drawing grid lines past the last row and col
7789 right
= wxMin( right
, GetColRight(GetColAt( m_numCols
- 1 )) );
7790 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7792 // no gridlines inside multicells, clip them out
7793 int leftCol
= GetColPos( internalXToCol(left
) );
7794 int topRow
= internalYToRow(top
);
7795 int rightCol
= GetColPos( internalXToCol(right
) );
7796 int bottomRow
= internalYToRow(bottom
);
7798 wxRegion
clippedcells(0, 0, cw
, ch
);
7800 int i
, j
, cell_rows
, cell_cols
;
7803 for (j
=topRow
; j
<=bottomRow
; j
++)
7806 for (colPos
=leftCol
; colPos
<=rightCol
; colPos
++)
7808 i
= GetColAt( colPos
);
7810 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7811 if ((cell_rows
> 1) || (cell_cols
> 1))
7813 rect
= CellToRect(j
,i
);
7814 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7815 clippedcells
.Subtract(rect
);
7817 else if ((cell_rows
< 0) || (cell_cols
< 0))
7819 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7820 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7821 clippedcells
.Subtract(rect
);
7826 dc
.SetDeviceClippingRegion( clippedcells
);
7829 // horizontal grid lines
7831 // already declared above - int i;
7832 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7834 int bot
= GetRowBottom(i
) - 1;
7843 dc
.SetPen( GetRowGridLinePen(i
) );
7844 dc
.DrawLine( left
, bot
, right
, bot
);
7848 // vertical grid lines
7851 for ( colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
7853 i
= GetColAt( colPos
);
7855 int colRight
= GetColRight(i
);
7857 if (GetLayoutDirection() != wxLayout_RightToLeft
)
7861 if ( colRight
> right
)
7866 if ( colRight
>= left
)
7868 dc
.SetPen( GetColGridLinePen(i
) );
7869 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7873 dc
.DestroyClippingRegion();
7876 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
7882 size_t numLabels
= rows
.GetCount();
7884 for ( i
= 0; i
< numLabels
; i
++ )
7886 DrawRowLabel( dc
, rows
[i
] );
7890 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7892 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7897 int rowTop
= GetRowTop(row
),
7898 rowBottom
= GetRowBottom(row
) - 1;
7900 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxPENSTYLE_SOLID
) );
7901 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
7902 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7903 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7905 dc
.SetPen( *wxWHITE_PEN
);
7906 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7907 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
7909 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
7910 dc
.SetTextForeground( GetLabelTextColour() );
7911 dc
.SetFont( GetLabelFont() );
7914 GetRowLabelAlignment( &hAlign
, &vAlign
);
7917 rect
.SetY( GetRowTop(row
) + 2 );
7918 rect
.SetWidth( m_rowLabelWidth
- 4 );
7919 rect
.SetHeight( GetRowHeight(row
) - 4 );
7920 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7923 void wxGrid::SetUseNativeColLabels( bool native
)
7925 m_nativeColumnLabels
= native
;
7928 int height
= wxRendererNative::Get().GetHeaderButtonHeight( this );
7929 SetColLabelSize( height
);
7932 m_colLabelWin
->Refresh();
7935 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7941 size_t numLabels
= cols
.GetCount();
7943 for ( i
= 0; i
< numLabels
; i
++ )
7945 DrawColLabel( dc
, cols
[i
] );
7949 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7951 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7954 int colLeft
= GetColLeft(col
);
7958 if (m_nativeColumnLabels
)
7960 rect
.SetX( colLeft
);
7962 rect
.SetWidth( GetColWidth(col
));
7963 rect
.SetHeight( m_colLabelHeight
);
7965 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7966 wxRendererNative::Get().DrawHeaderButton( win_dc
->GetWindow(), dc
, rect
, 0 );
7970 int colRight
= GetColRight(col
) - 1;
7972 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxPENSTYLE_SOLID
) );
7973 dc
.DrawLine( colRight
, 0, colRight
, m_colLabelHeight
- 1 );
7974 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7975 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
7976 colRight
+ 1, m_colLabelHeight
- 1 );
7978 dc
.SetPen( *wxWHITE_PEN
);
7979 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
7980 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7983 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
7984 dc
.SetTextForeground( GetLabelTextColour() );
7985 dc
.SetFont( GetLabelFont() );
7987 int hAlign
, vAlign
, orient
;
7988 GetColLabelAlignment( &hAlign
, &vAlign
);
7989 orient
= GetColLabelTextOrientation();
7991 rect
.SetX( colLeft
+ 2 );
7993 rect
.SetWidth( GetColWidth(col
) - 4 );
7994 rect
.SetHeight( m_colLabelHeight
- 4 );
7995 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7998 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7999 const wxString
& value
,
8003 int textOrientation
)
8005 wxArrayString lines
;
8007 StringToLines( value
, lines
);
8009 // Forward to new API.
8010 DrawTextRectangle( dc
,
8018 // VZ: this should be replaced with wxDC::DrawLabel() to which we just have to
8019 // add textOrientation support
8020 void wxGrid::DrawTextRectangle(wxDC
& dc
,
8021 const wxArrayString
& lines
,
8025 int textOrientation
)
8027 if ( lines
.empty() )
8030 wxDCClipper
clip(dc
, rect
);
8035 if ( textOrientation
== wxHORIZONTAL
)
8036 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
8038 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
8042 switch ( vertAlign
)
8044 case wxALIGN_BOTTOM
:
8045 if ( textOrientation
== wxHORIZONTAL
)
8046 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
8048 x
= rect
.x
+ rect
.width
- textWidth
;
8051 case wxALIGN_CENTRE
:
8052 if ( textOrientation
== wxHORIZONTAL
)
8053 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
8055 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
8060 if ( textOrientation
== wxHORIZONTAL
)
8067 // Align each line of a multi-line label
8068 size_t nLines
= lines
.GetCount();
8069 for ( size_t l
= 0; l
< nLines
; l
++ )
8071 const wxString
& line
= lines
[l
];
8075 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
8079 wxCoord lineWidth
= 0,
8081 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
8083 switch ( horizAlign
)
8086 if ( textOrientation
== wxHORIZONTAL
)
8087 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
8089 y
= rect
.y
+ lineWidth
+ 1;
8092 case wxALIGN_CENTRE
:
8093 if ( textOrientation
== wxHORIZONTAL
)
8094 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
8096 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
8101 if ( textOrientation
== wxHORIZONTAL
)
8104 y
= rect
.y
+ rect
.height
- 1;
8108 if ( textOrientation
== wxHORIZONTAL
)
8110 dc
.DrawText( line
, x
, y
);
8115 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
8121 // Split multi-line text up into an array of strings.
8122 // Any existing contents of the string array are preserved.
8124 // TODO: refactor wxTextFile::Read() and reuse the same code from here
8125 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
8129 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
8130 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
8132 while ( startPos
< (int)tVal
.length() )
8134 pos
= tVal
.Mid(startPos
).Find( eol
);
8139 else if ( pos
== 0 )
8141 lines
.Add( wxEmptyString
);
8145 lines
.Add( tVal
.Mid(startPos
, pos
) );
8148 startPos
+= pos
+ 1;
8151 if ( startPos
< (int)tVal
.length() )
8153 lines
.Add( tVal
.Mid( startPos
) );
8157 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
8158 const wxArrayString
& lines
,
8159 long *width
, long *height
) const
8163 wxCoord lineW
= 0, lineH
= 0;
8166 for ( i
= 0; i
< lines
.GetCount(); i
++ )
8168 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
8169 w
= wxMax( w
, lineW
);
8178 // ------ Batch processing.
8180 void wxGrid::EndBatch()
8182 if ( m_batchCount
> 0 )
8185 if ( !m_batchCount
)
8188 m_rowLabelWin
->Refresh();
8189 m_colLabelWin
->Refresh();
8190 m_cornerLabelWin
->Refresh();
8191 m_gridWin
->Refresh();
8196 // Use this, rather than wxWindow::Refresh(), to force an immediate
8197 // repainting of the grid. Has no effect if you are already inside a
8198 // BeginBatch / EndBatch block.
8200 void wxGrid::ForceRefresh()
8206 bool wxGrid::Enable(bool enable
)
8208 if ( !wxScrolledWindow::Enable(enable
) )
8211 // redraw in the new state
8212 m_gridWin
->Refresh();
8218 // ------ Edit control functions
8221 void wxGrid::EnableEditing( bool edit
)
8223 // TODO: improve this ?
8225 if ( edit
!= m_editable
)
8228 EnableCellEditControl(edit
);
8233 void wxGrid::EnableCellEditControl( bool enable
)
8238 if ( enable
!= m_cellEditCtrlEnabled
)
8242 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
8245 // this should be checked by the caller!
8246 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8248 // do it before ShowCellEditControl()
8249 m_cellEditCtrlEnabled
= enable
;
8251 ShowCellEditControl();
8255 //FIXME:add veto support
8256 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
8258 HideCellEditControl();
8259 SaveEditControlValue();
8261 // do it after HideCellEditControl()
8262 m_cellEditCtrlEnabled
= enable
;
8267 bool wxGrid::IsCurrentCellReadOnly() const
8270 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
8271 bool readonly
= attr
->IsReadOnly();
8277 bool wxGrid::CanEnableCellControl() const
8279 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
8280 !IsCurrentCellReadOnly();
8283 bool wxGrid::IsCellEditControlEnabled() const
8285 // the cell edit control might be disable for all cells or just for the
8286 // current one if it's read only
8287 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
8290 bool wxGrid::IsCellEditControlShown() const
8292 bool isShown
= false;
8294 if ( m_cellEditCtrlEnabled
)
8296 int row
= m_currentCellCoords
.GetRow();
8297 int col
= m_currentCellCoords
.GetCol();
8298 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8299 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
8304 if ( editor
->IsCreated() )
8306 isShown
= editor
->GetControl()->IsShown();
8316 void wxGrid::ShowCellEditControl()
8318 if ( IsCellEditControlEnabled() )
8320 if ( !IsVisible( m_currentCellCoords
, false ) )
8322 m_cellEditCtrlEnabled
= false;
8327 wxRect rect
= CellToRect( m_currentCellCoords
);
8328 int row
= m_currentCellCoords
.GetRow();
8329 int col
= m_currentCellCoords
.GetCol();
8331 // if this is part of a multicell, find owner (topleft)
8332 int cell_rows
, cell_cols
;
8333 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8334 if ( cell_rows
<= 0 || cell_cols
<= 0 )
8338 m_currentCellCoords
.SetRow( row
);
8339 m_currentCellCoords
.SetCol( col
);
8342 // erase the highlight and the cell contents because the editor
8343 // might not cover the entire cell
8344 wxClientDC
dc( m_gridWin
);
8346 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8347 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxBRUSHSTYLE_SOLID
));
8348 dc
.SetPen(*wxTRANSPARENT_PEN
);
8349 dc
.DrawRectangle(rect
);
8351 // convert to scrolled coords
8352 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8358 // cell is shifted by one pixel
8359 // However, don't allow x or y to become negative
8360 // since the SetSize() method interprets that as
8367 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8368 if ( !editor
->IsCreated() )
8370 editor
->Create(m_gridWin
, wxID_ANY
,
8371 new wxGridCellEditorEvtHandler(this, editor
));
8373 wxGridEditorCreatedEvent
evt(GetId(),
8374 wxEVT_GRID_EDITOR_CREATED
,
8378 editor
->GetControl());
8379 GetEventHandler()->ProcessEvent(evt
);
8382 // resize editor to overflow into righthand cells if allowed
8383 int maxWidth
= rect
.width
;
8384 wxString value
= GetCellValue(row
, col
);
8385 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
8388 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
8389 if (maxWidth
< rect
.width
)
8390 maxWidth
= rect
.width
;
8393 int client_right
= m_gridWin
->GetClientSize().GetWidth();
8394 if (rect
.x
+ maxWidth
> client_right
)
8395 maxWidth
= client_right
- rect
.x
;
8397 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
8399 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8400 // may have changed earlier
8401 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
8404 GetCellSize( row
, i
, &c_rows
, &c_cols
);
8406 // looks weird going over a multicell
8407 if (m_table
->IsEmptyCell( row
, i
) &&
8408 (rect
.width
< maxWidth
) && (c_rows
== 1))
8410 rect
.width
+= GetColWidth( i
);
8416 if (rect
.GetRight() > client_right
)
8417 rect
.SetRight( client_right
- 1 );
8420 editor
->SetCellAttr( attr
);
8421 editor
->SetSize( rect
);
8423 editor
->GetControl()->Move(
8424 editor
->GetControl()->GetPosition().x
+ nXMove
,
8425 editor
->GetControl()->GetPosition().y
);
8426 editor
->Show( true, attr
);
8428 // recalc dimensions in case we need to
8429 // expand the scrolled window to account for editor
8432 editor
->BeginEdit(row
, col
, this);
8433 editor
->SetCellAttr(NULL
);
8441 void wxGrid::HideCellEditControl()
8443 if ( IsCellEditControlEnabled() )
8445 int row
= m_currentCellCoords
.GetRow();
8446 int col
= m_currentCellCoords
.GetCol();
8448 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8449 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
8450 editor
->Show( false );
8454 m_gridWin
->SetFocus();
8456 // refresh whole row to the right
8457 wxRect
rect( CellToRect(row
, col
) );
8458 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8459 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8462 // ensure that the pixels under the focus ring get refreshed as well
8463 rect
.Inflate(10, 10);
8466 m_gridWin
->Refresh( false, &rect
);
8470 void wxGrid::SaveEditControlValue()
8472 if ( IsCellEditControlEnabled() )
8474 int row
= m_currentCellCoords
.GetRow();
8475 int col
= m_currentCellCoords
.GetCol();
8477 wxString oldval
= GetCellValue(row
, col
);
8479 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8480 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8481 bool changed
= editor
->EndEdit(row
, col
, this);
8488 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
8489 m_currentCellCoords
.GetRow(),
8490 m_currentCellCoords
.GetCol() ) < 0 )
8492 // Event has been vetoed, set the data back.
8493 SetCellValue(row
, col
, oldval
);
8500 // ------ Grid location functions
8501 // Note that all of these functions work with the logical coordinates of
8502 // grid cells and labels so you will need to convert from device
8503 // coordinates for mouse events etc.
8506 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
) const
8508 int row
= YToRow(y
);
8509 int col
= XToCol(x
);
8511 if ( row
== -1 || col
== -1 )
8513 coords
= wxGridNoCellCoords
;
8517 coords
.Set( row
, col
);
8521 // Internal Helper function for computing row or column from some
8522 // (unscrolled) coordinate value, using either
8523 // m_defaultRowHeight/m_defaultColWidth or binary search on array
8524 // of m_rowBottoms/m_ColRights to speed up the search!
8526 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
8527 const wxArrayInt
& BorderArray
, int nMax
,
8531 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
8536 size_t i_max
= coord
/ defaultDist
,
8539 if (BorderArray
.IsEmpty())
8541 if ((int) i_max
< nMax
)
8543 return clipToMinMax
? nMax
- 1 : -1;
8546 if ( i_max
>= BorderArray
.GetCount())
8548 i_max
= BorderArray
.GetCount() - 1;
8552 if ( coord
>= BorderArray
[i_max
])
8556 i_max
= coord
/ minDist
;
8558 i_max
= BorderArray
.GetCount() - 1;
8561 if ( i_max
>= BorderArray
.GetCount())
8562 i_max
= BorderArray
.GetCount() - 1;
8565 if ( coord
>= BorderArray
[i_max
])
8566 return clipToMinMax
? (int)i_max
: -1;
8567 if ( coord
< BorderArray
[0] )
8570 while ( i_max
- i_min
> 0 )
8572 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
8573 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8574 if (coord
>= BorderArray
[ i_max
- 1])
8578 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
8579 if (coord
< BorderArray
[median
])
8588 int wxGrid::YToRow( int y
) const
8590 return CoordToRowOrCol(y
, m_defaultRowHeight
,
8591 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8594 int wxGrid::XToCol( int x
, bool clipToMinMax
) const
8597 return clipToMinMax
&& (m_numCols
> 0) ? GetColAt( 0 ) : -1;
8599 wxASSERT_MSG(m_defaultColWidth
> 0, wxT("Default column width can not be zero"));
8601 int maxPos
= x
/ m_defaultColWidth
;
8604 if (m_colRights
.IsEmpty())
8606 if(maxPos
< m_numCols
)
8607 return GetColAt( maxPos
);
8608 return clipToMinMax
? GetColAt( m_numCols
- 1 ) : -1;
8611 if ( maxPos
>= m_numCols
)
8612 maxPos
= m_numCols
- 1;
8615 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8618 if (m_minAcceptableColWidth
)
8619 maxPos
= x
/ m_minAcceptableColWidth
;
8621 maxPos
= m_numCols
- 1;
8623 if ( maxPos
>= m_numCols
)
8624 maxPos
= m_numCols
- 1;
8627 //X is beyond the last column
8628 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8629 return clipToMinMax
? GetColAt( maxPos
) : -1;
8631 //X is before the first column
8632 if ( x
< m_colRights
[GetColAt( 0 )] )
8633 return GetColAt( 0 );
8635 //Perform a binary search
8636 while ( maxPos
- minPos
> 0 )
8638 wxCHECK_MSG(m_colRights
[GetColAt( minPos
)] <= x
&& x
< m_colRights
[GetColAt( maxPos
)],
8639 0, _T("wxGrid: internal error in XToCol"));
8641 if (x
>= m_colRights
[GetColAt( maxPos
- 1 )])
8642 return GetColAt( maxPos
);
8645 int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
8646 if (x
< m_colRights
[GetColAt( median
)])
8651 return GetColAt( maxPos
);
8654 // return the row number that that the y coord is near
8655 // the edge of, or -1 if not near an edge.
8656 // coords can only possibly be near an edge if
8657 // (a) the row/column is large enough to still allow for an "inner" area
8658 // that is _not_ nead the edge (i.e., if the height/width is smaller
8659 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8662 // (b) resizing rows/columns (the thing for which edge detection is
8663 // relevant at all) is enabled.
8665 int wxGrid::YToEdgeOfRow( int y
) const
8668 i
= internalYToRow(y
);
8670 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragRowSize() )
8672 // We know that we are in row i, test whether we are
8673 // close enough to lower or upper border, respectively.
8674 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8676 else if ( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8683 // return the col number that that the x coord is near the edge of, or
8684 // -1 if not near an edge
8685 // See comment at YToEdgeOfRow for conditions on edge detection.
8687 int wxGrid::XToEdgeOfCol( int x
) const
8690 i
= internalXToCol(x
);
8692 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragColSize() )
8694 // We know that we are in column i; test whether we are
8695 // close enough to right or left border, respectively.
8696 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8698 else if ( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8705 wxRect
wxGrid::CellToRect( int row
, int col
) const
8707 wxRect
rect( -1, -1, -1, -1 );
8709 if ( row
>= 0 && row
< m_numRows
&&
8710 col
>= 0 && col
< m_numCols
)
8712 int i
, cell_rows
, cell_cols
;
8713 rect
.width
= rect
.height
= 0;
8714 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8715 // if negative then find multicell owner
8720 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8722 rect
.x
= GetColLeft(col
);
8723 rect
.y
= GetRowTop(row
);
8724 for (i
=col
; i
< col
+ cell_cols
; i
++)
8725 rect
.width
+= GetColWidth(i
);
8726 for (i
=row
; i
< row
+ cell_rows
; i
++)
8727 rect
.height
+= GetRowHeight(i
);
8730 // if grid lines are enabled, then the area of the cell is a bit smaller
8731 if (m_gridLinesEnabled
)
8740 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
8742 // get the cell rectangle in logical coords
8744 wxRect
r( CellToRect( row
, col
) );
8746 // convert to device coords
8748 int left
, top
, right
, bottom
;
8749 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8750 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8752 // check against the client area of the grid window
8754 m_gridWin
->GetClientSize( &cw
, &ch
);
8756 if ( wholeCellVisible
)
8758 // is the cell wholly visible ?
8759 return ( left
>= 0 && right
<= cw
&&
8760 top
>= 0 && bottom
<= ch
);
8764 // is the cell partly visible ?
8766 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8767 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8771 // make the specified cell location visible by doing a minimal amount
8774 void wxGrid::MakeCellVisible( int row
, int col
)
8777 int xpos
= -1, ypos
= -1;
8779 if ( row
>= 0 && row
< m_numRows
&&
8780 col
>= 0 && col
< m_numCols
)
8782 // get the cell rectangle in logical coords
8783 wxRect
r( CellToRect( row
, col
) );
8785 // convert to device coords
8786 int left
, top
, right
, bottom
;
8787 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8788 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8791 m_gridWin
->GetClientSize( &cw
, &ch
);
8797 else if ( bottom
> ch
)
8799 int h
= r
.GetHeight();
8801 for ( i
= row
- 1; i
>= 0; i
-- )
8803 int rowHeight
= GetRowHeight(i
);
8804 if ( h
+ rowHeight
> ch
)
8811 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8812 // have rounding errors (this is important, because if we do,
8813 // we might not scroll at all and some cells won't be redrawn)
8815 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
8816 // so just add a full scroll unit...
8817 ypos
+= m_scrollLineY
;
8820 // special handling for wide cells - show always left part of the cell!
8821 // Otherwise, e.g. when stepping from row to row, it would jump between
8822 // left and right part of the cell on every step!
8824 if ( left
< 0 || (right
- left
) >= cw
)
8828 else if ( right
> cw
)
8830 // position the view so that the cell is on the right
8832 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8833 xpos
= x0
+ (right
- cw
);
8835 // see comment for ypos above
8836 xpos
+= m_scrollLineX
;
8839 if ( xpos
!= -1 || ypos
!= -1 )
8842 xpos
/= m_scrollLineX
;
8844 ypos
/= m_scrollLineY
;
8845 Scroll( xpos
, ypos
);
8852 // ------ Grid cursor movement functions
8855 bool wxGrid::MoveCursorUp( bool expandSelection
)
8857 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8858 m_currentCellCoords
.GetRow() >= 0 )
8860 if ( expandSelection
)
8862 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8863 m_selectingKeyboard
= m_currentCellCoords
;
8864 if ( m_selectingKeyboard
.GetRow() > 0 )
8866 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8867 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8868 m_selectingKeyboard
.GetCol() );
8869 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8872 else if ( m_currentCellCoords
.GetRow() > 0 )
8874 int row
= m_currentCellCoords
.GetRow() - 1;
8875 int col
= m_currentCellCoords
.GetCol();
8877 MakeCellVisible( row
, col
);
8878 SetCurrentCell( row
, col
);
8889 bool wxGrid::MoveCursorDown( bool expandSelection
)
8891 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8892 m_currentCellCoords
.GetRow() < m_numRows
)
8894 if ( expandSelection
)
8896 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8897 m_selectingKeyboard
= m_currentCellCoords
;
8898 if ( m_selectingKeyboard
.GetRow() < m_numRows
- 1 )
8900 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8901 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8902 m_selectingKeyboard
.GetCol() );
8903 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8906 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8908 int row
= m_currentCellCoords
.GetRow() + 1;
8909 int col
= m_currentCellCoords
.GetCol();
8911 MakeCellVisible( row
, col
);
8912 SetCurrentCell( row
, col
);
8923 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8925 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8926 m_currentCellCoords
.GetCol() >= 0 )
8928 if ( expandSelection
)
8930 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8931 m_selectingKeyboard
= m_currentCellCoords
;
8932 if ( m_selectingKeyboard
.GetCol() > 0 )
8934 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8935 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8936 m_selectingKeyboard
.GetCol() );
8937 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8940 else if ( GetColPos( m_currentCellCoords
.GetCol() ) > 0 )
8942 int row
= m_currentCellCoords
.GetRow();
8943 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) - 1 );
8946 MakeCellVisible( row
, col
);
8947 SetCurrentCell( row
, col
);
8958 bool wxGrid::MoveCursorRight( bool expandSelection
)
8960 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8961 m_currentCellCoords
.GetCol() < m_numCols
)
8963 if ( expandSelection
)
8965 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8966 m_selectingKeyboard
= m_currentCellCoords
;
8967 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8969 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8970 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8971 m_selectingKeyboard
.GetCol() );
8972 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8975 else if ( GetColPos( m_currentCellCoords
.GetCol() ) < m_numCols
- 1 )
8977 int row
= m_currentCellCoords
.GetRow();
8978 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) + 1 );
8981 MakeCellVisible( row
, col
);
8982 SetCurrentCell( row
, col
);
8993 bool wxGrid::MovePageUp()
8995 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8998 int row
= m_currentCellCoords
.GetRow();
9002 m_gridWin
->GetClientSize( &cw
, &ch
);
9004 int y
= GetRowTop(row
);
9005 int newRow
= internalYToRow( y
- ch
+ 1 );
9007 if ( newRow
== row
)
9009 // row > 0, so newRow can never be less than 0 here.
9013 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
9014 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
9022 bool wxGrid::MovePageDown()
9024 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9027 int row
= m_currentCellCoords
.GetRow();
9028 if ( (row
+ 1) < m_numRows
)
9031 m_gridWin
->GetClientSize( &cw
, &ch
);
9033 int y
= GetRowTop(row
);
9034 int newRow
= internalYToRow( y
+ ch
);
9035 if ( newRow
== row
)
9037 // row < m_numRows, so newRow can't overflow here.
9041 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
9042 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
9050 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
9053 m_currentCellCoords
!= wxGridNoCellCoords
&&
9054 m_currentCellCoords
.GetRow() > 0 )
9056 int row
= m_currentCellCoords
.GetRow();
9057 int col
= m_currentCellCoords
.GetCol();
9059 if ( m_table
->IsEmptyCell(row
, col
) )
9061 // starting in an empty cell: find the next block of
9067 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9071 else if ( m_table
->IsEmptyCell(row
- 1, col
) )
9073 // starting at the top of a block: find the next block
9079 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9085 // starting within a block: find the top of the block
9090 if ( m_table
->IsEmptyCell(row
, col
) )
9098 MakeCellVisible( row
, col
);
9099 if ( expandSelection
)
9101 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9102 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9107 SetCurrentCell( row
, col
);
9116 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
9119 m_currentCellCoords
!= wxGridNoCellCoords
&&
9120 m_currentCellCoords
.GetRow() < m_numRows
- 1 )
9122 int row
= m_currentCellCoords
.GetRow();
9123 int col
= m_currentCellCoords
.GetCol();
9125 if ( m_table
->IsEmptyCell(row
, col
) )
9127 // starting in an empty cell: find the next block of
9130 while ( row
< m_numRows
- 1 )
9133 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9137 else if ( m_table
->IsEmptyCell(row
+ 1, col
) )
9139 // starting at the bottom of a block: find the next block
9142 while ( row
< m_numRows
- 1 )
9145 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9151 // starting within a block: find the bottom of the block
9153 while ( row
< m_numRows
- 1 )
9156 if ( m_table
->IsEmptyCell(row
, col
) )
9164 MakeCellVisible( row
, col
);
9165 if ( expandSelection
)
9167 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9168 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9173 SetCurrentCell( row
, col
);
9182 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
9185 m_currentCellCoords
!= wxGridNoCellCoords
&&
9186 m_currentCellCoords
.GetCol() > 0 )
9188 int row
= m_currentCellCoords
.GetRow();
9189 int col
= m_currentCellCoords
.GetCol();
9191 if ( m_table
->IsEmptyCell(row
, col
) )
9193 // starting in an empty cell: find the next block of
9199 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9203 else if ( m_table
->IsEmptyCell(row
, col
- 1) )
9205 // starting at the left of a block: find the next block
9211 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9217 // starting within a block: find the left of the block
9222 if ( m_table
->IsEmptyCell(row
, col
) )
9230 MakeCellVisible( row
, col
);
9231 if ( expandSelection
)
9233 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9234 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9239 SetCurrentCell( row
, col
);
9248 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
9251 m_currentCellCoords
!= wxGridNoCellCoords
&&
9252 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
9254 int row
= m_currentCellCoords
.GetRow();
9255 int col
= m_currentCellCoords
.GetCol();
9257 if ( m_table
->IsEmptyCell(row
, col
) )
9259 // starting in an empty cell: find the next block of
9262 while ( col
< m_numCols
- 1 )
9265 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9269 else if ( m_table
->IsEmptyCell(row
, col
+ 1) )
9271 // starting at the right of a block: find the next block
9274 while ( col
< m_numCols
- 1 )
9277 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9283 // starting within a block: find the right of the block
9285 while ( col
< m_numCols
- 1 )
9288 if ( m_table
->IsEmptyCell(row
, col
) )
9296 MakeCellVisible( row
, col
);
9297 if ( expandSelection
)
9299 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9300 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9305 SetCurrentCell( row
, col
);
9315 // ------ Label values and formatting
9318 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
9321 *horiz
= m_rowLabelHorizAlign
;
9323 *vert
= m_rowLabelVertAlign
;
9326 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
9329 *horiz
= m_colLabelHorizAlign
;
9331 *vert
= m_colLabelVertAlign
;
9334 int wxGrid::GetColLabelTextOrientation() const
9336 return m_colLabelTextOrientation
;
9339 wxString
wxGrid::GetRowLabelValue( int row
) const
9343 return m_table
->GetRowLabelValue( row
);
9353 wxString
wxGrid::GetColLabelValue( int col
) const
9357 return m_table
->GetColLabelValue( col
);
9367 void wxGrid::SetRowLabelSize( int width
)
9369 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
9371 if ( width
== wxGRID_AUTOSIZE
)
9373 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
9376 if ( width
!= m_rowLabelWidth
)
9380 m_rowLabelWin
->Show( false );
9381 m_cornerLabelWin
->Show( false );
9383 else if ( m_rowLabelWidth
== 0 )
9385 m_rowLabelWin
->Show( true );
9386 if ( m_colLabelHeight
> 0 )
9387 m_cornerLabelWin
->Show( true );
9390 m_rowLabelWidth
= width
;
9392 wxScrolledWindow::Refresh( true );
9396 void wxGrid::SetColLabelSize( int height
)
9398 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
9400 if ( height
== wxGRID_AUTOSIZE
)
9402 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
9405 if ( height
!= m_colLabelHeight
)
9409 m_colLabelWin
->Show( false );
9410 m_cornerLabelWin
->Show( false );
9412 else if ( m_colLabelHeight
== 0 )
9414 m_colLabelWin
->Show( true );
9415 if ( m_rowLabelWidth
> 0 )
9416 m_cornerLabelWin
->Show( true );
9419 m_colLabelHeight
= height
;
9421 wxScrolledWindow::Refresh( true );
9425 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
9427 if ( m_labelBackgroundColour
!= colour
)
9429 m_labelBackgroundColour
= colour
;
9430 m_rowLabelWin
->SetBackgroundColour( colour
);
9431 m_colLabelWin
->SetBackgroundColour( colour
);
9432 m_cornerLabelWin
->SetBackgroundColour( colour
);
9434 if ( !GetBatchCount() )
9436 m_rowLabelWin
->Refresh();
9437 m_colLabelWin
->Refresh();
9438 m_cornerLabelWin
->Refresh();
9443 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
9445 if ( m_labelTextColour
!= colour
)
9447 m_labelTextColour
= colour
;
9448 if ( !GetBatchCount() )
9450 m_rowLabelWin
->Refresh();
9451 m_colLabelWin
->Refresh();
9456 void wxGrid::SetLabelFont( const wxFont
& font
)
9459 if ( !GetBatchCount() )
9461 m_rowLabelWin
->Refresh();
9462 m_colLabelWin
->Refresh();
9466 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
9468 // allow old (incorrect) defs to be used
9471 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9472 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9473 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9478 case wxTOP
: vert
= wxALIGN_TOP
; break;
9479 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9480 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9483 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9485 m_rowLabelHorizAlign
= horiz
;
9488 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9490 m_rowLabelVertAlign
= vert
;
9493 if ( !GetBatchCount() )
9495 m_rowLabelWin
->Refresh();
9499 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
9501 // allow old (incorrect) defs to be used
9504 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9505 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9506 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9511 case wxTOP
: vert
= wxALIGN_TOP
; break;
9512 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9513 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9516 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9518 m_colLabelHorizAlign
= horiz
;
9521 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9523 m_colLabelVertAlign
= vert
;
9526 if ( !GetBatchCount() )
9528 m_colLabelWin
->Refresh();
9532 // Note: under MSW, the default column label font must be changed because it
9533 // does not support vertical printing
9535 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9536 // pGrid->SetLabelFont(font);
9537 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9539 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9541 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9542 m_colLabelTextOrientation
= textOrientation
;
9544 if ( !GetBatchCount() )
9545 m_colLabelWin
->Refresh();
9548 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9552 m_table
->SetRowLabelValue( row
, s
);
9553 if ( !GetBatchCount() )
9555 wxRect rect
= CellToRect( row
, 0 );
9556 if ( rect
.height
> 0 )
9558 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9560 rect
.width
= m_rowLabelWidth
;
9561 m_rowLabelWin
->Refresh( true, &rect
);
9567 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9571 m_table
->SetColLabelValue( col
, s
);
9572 if ( !GetBatchCount() )
9574 wxRect rect
= CellToRect( 0, col
);
9575 if ( rect
.width
> 0 )
9577 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9579 rect
.height
= m_colLabelHeight
;
9580 m_colLabelWin
->Refresh( true, &rect
);
9586 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9588 if ( m_gridLineColour
!= colour
)
9590 m_gridLineColour
= colour
;
9592 wxClientDC
dc( m_gridWin
);
9594 DrawAllGridLines( dc
, wxRegion() );
9598 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9600 if ( m_cellHighlightColour
!= colour
)
9602 m_cellHighlightColour
= colour
;
9604 wxClientDC
dc( m_gridWin
);
9606 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9607 DrawCellHighlight(dc
, attr
);
9612 void wxGrid::SetCellHighlightPenWidth(int width
)
9614 if (m_cellHighlightPenWidth
!= width
)
9616 m_cellHighlightPenWidth
= width
;
9618 // Just redrawing the cell highlight is not enough since that won't
9619 // make any visible change if the the thickness is getting smaller.
9620 int row
= m_currentCellCoords
.GetRow();
9621 int col
= m_currentCellCoords
.GetCol();
9622 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9625 wxRect rect
= CellToRect(row
, col
);
9626 m_gridWin
->Refresh(true, &rect
);
9630 void wxGrid::SetCellHighlightROPenWidth(int width
)
9632 if (m_cellHighlightROPenWidth
!= width
)
9634 m_cellHighlightROPenWidth
= width
;
9636 // Just redrawing the cell highlight is not enough since that won't
9637 // make any visible change if the the thickness is getting smaller.
9638 int row
= m_currentCellCoords
.GetRow();
9639 int col
= m_currentCellCoords
.GetCol();
9640 if ( row
== -1 || col
== -1 ||
9641 GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9644 wxRect rect
= CellToRect(row
, col
);
9645 m_gridWin
->Refresh(true, &rect
);
9649 void wxGrid::EnableGridLines( bool enable
)
9651 if ( enable
!= m_gridLinesEnabled
)
9653 m_gridLinesEnabled
= enable
;
9655 if ( !GetBatchCount() )
9659 wxClientDC
dc( m_gridWin
);
9661 DrawAllGridLines( dc
, wxRegion() );
9665 m_gridWin
->Refresh();
9671 int wxGrid::GetDefaultRowSize() const
9673 return m_defaultRowHeight
;
9676 int wxGrid::GetRowSize( int row
) const
9678 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9680 return GetRowHeight(row
);
9683 int wxGrid::GetDefaultColSize() const
9685 return m_defaultColWidth
;
9688 int wxGrid::GetColSize( int col
) const
9690 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9692 return GetColWidth(col
);
9695 // ============================================================================
9696 // access to the grid attributes: each of them has a default value in the grid
9697 // itself and may be overidden on a per-cell basis
9698 // ============================================================================
9700 // ----------------------------------------------------------------------------
9701 // setting default attributes
9702 // ----------------------------------------------------------------------------
9704 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9706 m_defaultCellAttr
->SetBackgroundColour(col
);
9708 m_gridWin
->SetBackgroundColour(col
);
9712 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9714 m_defaultCellAttr
->SetTextColour(col
);
9717 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9719 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9722 void wxGrid::SetDefaultCellOverflow( bool allow
)
9724 m_defaultCellAttr
->SetOverflow(allow
);
9727 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9729 m_defaultCellAttr
->SetFont(font
);
9732 // For editors and renderers the type registry takes precedence over the
9733 // default attr, so we need to register the new editor/renderer for the string
9734 // data type in order to make setting a default editor/renderer appear to
9737 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9739 RegisterDataType(wxGRID_VALUE_STRING
,
9741 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9744 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9746 RegisterDataType(wxGRID_VALUE_STRING
,
9747 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9751 // ----------------------------------------------------------------------------
9752 // access to the default attributes
9753 // ----------------------------------------------------------------------------
9755 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
9757 return m_defaultCellAttr
->GetBackgroundColour();
9760 wxColour
wxGrid::GetDefaultCellTextColour() const
9762 return m_defaultCellAttr
->GetTextColour();
9765 wxFont
wxGrid::GetDefaultCellFont() const
9767 return m_defaultCellAttr
->GetFont();
9770 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
9772 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9775 bool wxGrid::GetDefaultCellOverflow() const
9777 return m_defaultCellAttr
->GetOverflow();
9780 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9782 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9785 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9787 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9790 // ----------------------------------------------------------------------------
9791 // access to cell attributes
9792 // ----------------------------------------------------------------------------
9794 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
9796 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9797 wxColour colour
= attr
->GetBackgroundColour();
9803 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
9805 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9806 wxColour colour
= attr
->GetTextColour();
9812 wxFont
wxGrid::GetCellFont( int row
, int col
) const
9814 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9815 wxFont font
= attr
->GetFont();
9821 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
9823 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9824 attr
->GetAlignment(horiz
, vert
);
9828 bool wxGrid::GetCellOverflow( int row
, int col
) const
9830 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9831 bool allow
= attr
->GetOverflow();
9837 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
9839 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9840 attr
->GetSize( num_rows
, num_cols
);
9844 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
9846 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9847 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9853 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
9855 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9856 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9862 bool wxGrid::IsReadOnly(int row
, int col
) const
9864 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9865 bool isReadOnly
= attr
->IsReadOnly();
9871 // ----------------------------------------------------------------------------
9872 // attribute support: cache, automatic provider creation, ...
9873 // ----------------------------------------------------------------------------
9875 bool wxGrid::CanHaveAttributes() const
9882 return m_table
->CanHaveAttributes();
9885 void wxGrid::ClearAttrCache()
9887 if ( m_attrCache
.row
!= -1 )
9889 wxSafeDecRef(m_attrCache
.attr
);
9890 m_attrCache
.attr
= NULL
;
9891 m_attrCache
.row
= -1;
9895 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9899 wxGrid
*self
= (wxGrid
*)this; // const_cast
9901 self
->ClearAttrCache();
9902 self
->m_attrCache
.row
= row
;
9903 self
->m_attrCache
.col
= col
;
9904 self
->m_attrCache
.attr
= attr
;
9909 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9911 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9913 *attr
= m_attrCache
.attr
;
9914 wxSafeIncRef(m_attrCache
.attr
);
9916 #ifdef DEBUG_ATTR_CACHE
9917 gs_nAttrCacheHits
++;
9924 #ifdef DEBUG_ATTR_CACHE
9925 gs_nAttrCacheMisses
++;
9932 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9934 wxGridCellAttr
*attr
= NULL
;
9935 // Additional test to avoid looking at the cache e.g. for
9936 // wxNoCellCoords, as this will confuse memory management.
9939 if ( !LookupAttr(row
, col
, &attr
) )
9941 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9942 : (wxGridCellAttr
*)NULL
;
9943 CacheAttr(row
, col
, attr
);
9949 attr
->SetDefAttr(m_defaultCellAttr
);
9953 attr
= m_defaultCellAttr
;
9960 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9962 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9963 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9965 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9966 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9968 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9971 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9973 // artificially inc the ref count to match DecRef() in caller
9975 m_table
->SetAttr(attr
, row
, col
);
9981 // ----------------------------------------------------------------------------
9982 // setting column attributes (wrappers around SetColAttr)
9983 // ----------------------------------------------------------------------------
9985 void wxGrid::SetColFormatBool(int col
)
9987 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9990 void wxGrid::SetColFormatNumber(int col
)
9992 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9995 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9997 wxString typeName
= wxGRID_VALUE_FLOAT
;
9998 if ( (width
!= -1) || (precision
!= -1) )
10000 typeName
<< _T(':') << width
<< _T(',') << precision
;
10003 SetColFormatCustom(col
, typeName
);
10006 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
10008 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
10010 attr
= new wxGridCellAttr
;
10011 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
10012 attr
->SetRenderer(renderer
);
10014 SetColAttr(col
, attr
);
10018 // ----------------------------------------------------------------------------
10019 // setting cell attributes: this is forwarded to the table
10020 // ----------------------------------------------------------------------------
10022 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
10024 if ( CanHaveAttributes() )
10026 m_table
->SetAttr(attr
, row
, col
);
10031 wxSafeDecRef(attr
);
10035 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
10037 if ( CanHaveAttributes() )
10039 m_table
->SetRowAttr(attr
, row
);
10044 wxSafeDecRef(attr
);
10048 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
10050 if ( CanHaveAttributes() )
10052 m_table
->SetColAttr(attr
, col
);
10057 wxSafeDecRef(attr
);
10061 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
10063 if ( CanHaveAttributes() )
10065 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10066 attr
->SetBackgroundColour(colour
);
10071 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
10073 if ( CanHaveAttributes() )
10075 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10076 attr
->SetTextColour(colour
);
10081 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
10083 if ( CanHaveAttributes() )
10085 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10086 attr
->SetFont(font
);
10091 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
10093 if ( CanHaveAttributes() )
10095 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10096 attr
->SetAlignment(horiz
, vert
);
10101 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
10103 if ( CanHaveAttributes() )
10105 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10106 attr
->SetOverflow(allow
);
10111 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
10113 if ( CanHaveAttributes() )
10115 int cell_rows
, cell_cols
;
10117 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10118 attr
->GetSize(&cell_rows
, &cell_cols
);
10119 attr
->SetSize(num_rows
, num_cols
);
10122 // Cannot set the size of a cell to 0 or negative values
10123 // While it is perfectly legal to do that, this function cannot
10124 // handle all the possibilies, do it by hand by getting the CellAttr.
10125 // You can only set the size of a cell to 1,1 or greater with this fn
10126 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
10127 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10128 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
10129 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10131 // if this was already a multicell then "turn off" the other cells first
10132 if ((cell_rows
> 1) || (cell_rows
> 1))
10135 for (j
=row
; j
< row
+ cell_rows
; j
++)
10137 for (i
=col
; i
< col
+ cell_cols
; i
++)
10139 if ((i
!= col
) || (j
!= row
))
10141 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10142 attr_stub
->SetSize( 1, 1 );
10143 attr_stub
->DecRef();
10149 // mark the cells that will be covered by this cell to
10150 // negative or zero values to point back at this cell
10151 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
10154 for (j
=row
; j
< row
+ num_rows
; j
++)
10156 for (i
=col
; i
< col
+ num_cols
; i
++)
10158 if ((i
!= col
) || (j
!= row
))
10160 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10161 attr_stub
->SetSize( row
- j
, col
- i
);
10162 attr_stub
->DecRef();
10170 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
10172 if ( CanHaveAttributes() )
10174 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10175 attr
->SetRenderer(renderer
);
10180 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
10182 if ( CanHaveAttributes() )
10184 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10185 attr
->SetEditor(editor
);
10190 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
10192 if ( CanHaveAttributes() )
10194 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10195 attr
->SetReadOnly(isReadOnly
);
10200 // ----------------------------------------------------------------------------
10201 // Data type registration
10202 // ----------------------------------------------------------------------------
10204 void wxGrid::RegisterDataType(const wxString
& typeName
,
10205 wxGridCellRenderer
* renderer
,
10206 wxGridCellEditor
* editor
)
10208 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
10212 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
10214 wxString typeName
= m_table
->GetTypeName(row
, col
);
10215 return GetDefaultEditorForType(typeName
);
10218 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
10220 wxString typeName
= m_table
->GetTypeName(row
, col
);
10221 return GetDefaultRendererForType(typeName
);
10224 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
10226 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10227 if ( index
== wxNOT_FOUND
)
10229 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10234 return m_typeRegistry
->GetEditor(index
);
10237 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
10239 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10240 if ( index
== wxNOT_FOUND
)
10242 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10247 return m_typeRegistry
->GetRenderer(index
);
10250 // ----------------------------------------------------------------------------
10252 // ----------------------------------------------------------------------------
10254 void wxGrid::EnableDragRowSize( bool enable
)
10256 m_canDragRowSize
= enable
;
10259 void wxGrid::EnableDragColSize( bool enable
)
10261 m_canDragColSize
= enable
;
10264 void wxGrid::EnableDragGridSize( bool enable
)
10266 m_canDragGridSize
= enable
;
10269 void wxGrid::EnableDragCell( bool enable
)
10271 m_canDragCell
= enable
;
10274 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
10276 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
10278 if ( resizeExistingRows
)
10280 // since we are resizing all rows to the default row size,
10281 // we can simply clear the row heights and row bottoms
10282 // arrays (which also allows us to take advantage of
10283 // some speed optimisations)
10284 m_rowHeights
.Empty();
10285 m_rowBottoms
.Empty();
10286 if ( !GetBatchCount() )
10291 void wxGrid::SetRowSize( int row
, int height
)
10293 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
10295 // See comment in SetColSize
10296 if ( height
< GetRowMinimalAcceptableHeight())
10299 if ( m_rowHeights
.IsEmpty() )
10301 // need to really create the array
10305 int h
= wxMax( 0, height
);
10306 int diff
= h
- m_rowHeights
[row
];
10308 m_rowHeights
[row
] = h
;
10309 for ( int i
= row
; i
< m_numRows
; i
++ )
10311 m_rowBottoms
[i
] += diff
;
10314 if ( !GetBatchCount() )
10318 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
10320 // we dont allow zero default column width
10321 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
10323 if ( resizeExistingCols
)
10325 // since we are resizing all columns to the default column size,
10326 // we can simply clear the col widths and col rights
10327 // arrays (which also allows us to take advantage of
10328 // some speed optimisations)
10329 m_colWidths
.Empty();
10330 m_colRights
.Empty();
10331 if ( !GetBatchCount() )
10336 void wxGrid::SetColSize( int col
, int width
)
10338 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
10340 // should we check that it's bigger than GetColMinimalWidth(col) here?
10342 // No, because it is reasonable to assume the library user know's
10343 // what he is doing. However we should test against the weaker
10344 // constraint of minimalAcceptableWidth, as this breaks rendering
10346 // This test then fixes sf.net bug #645734
10348 if ( width
< GetColMinimalAcceptableWidth() )
10351 if ( m_colWidths
.IsEmpty() )
10353 // need to really create the array
10357 // if < 0 then calculate new width from label
10361 wxArrayString lines
;
10362 wxClientDC
dc(m_colLabelWin
);
10363 dc
.SetFont(GetLabelFont());
10364 StringToLines(GetColLabelValue(col
), lines
);
10365 GetTextBoxSize(dc
, lines
, &w
, &h
);
10369 int w
= wxMax( 0, width
);
10370 int diff
= w
- m_colWidths
[col
];
10371 m_colWidths
[col
] = w
;
10373 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
10375 m_colRights
[GetColAt(colPos
)] += diff
;
10378 if ( !GetBatchCount() )
10382 void wxGrid::SetColMinimalWidth( int col
, int width
)
10384 if (width
> GetColMinimalAcceptableWidth())
10386 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10387 m_colMinWidths
[key
] = width
;
10391 void wxGrid::SetRowMinimalHeight( int row
, int width
)
10393 if (width
> GetRowMinimalAcceptableHeight())
10395 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10396 m_rowMinHeights
[key
] = width
;
10400 int wxGrid::GetColMinimalWidth(int col
) const
10402 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10403 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
10405 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
10408 int wxGrid::GetRowMinimalHeight(int row
) const
10410 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10411 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
10413 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
10416 void wxGrid::SetColMinimalAcceptableWidth( int width
)
10418 // We do allow a width of 0 since this gives us
10419 // an easy way to temporarily hiding columns.
10421 m_minAcceptableColWidth
= width
;
10424 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
10426 // We do allow a height of 0 since this gives us
10427 // an easy way to temporarily hiding rows.
10429 m_minAcceptableRowHeight
= height
;
10432 int wxGrid::GetColMinimalAcceptableWidth() const
10434 return m_minAcceptableColWidth
;
10437 int wxGrid::GetRowMinimalAcceptableHeight() const
10439 return m_minAcceptableRowHeight
;
10442 // ----------------------------------------------------------------------------
10444 // ----------------------------------------------------------------------------
10447 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
10449 const bool column
= direction
== wxGRID_COLUMN
;
10451 wxClientDC
dc(m_gridWin
);
10453 // cancel editing of cell
10454 HideCellEditControl();
10455 SaveEditControlValue();
10457 // init both of them to avoid compiler warnings, even if we only need one
10465 wxCoord extent
, extentMax
= 0;
10466 int max
= column
? m_numRows
: m_numCols
;
10467 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
10474 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
10475 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
10478 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
10479 extent
= column
? size
.x
: size
.y
;
10480 if ( extent
> extentMax
)
10481 extentMax
= extent
;
10483 renderer
->DecRef();
10489 // now also compare with the column label extent
10491 dc
.SetFont( GetLabelFont() );
10495 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
10496 if ( GetColLabelTextOrientation() == wxVERTICAL
)
10500 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
10502 extent
= column
? w
: h
;
10503 if ( extent
> extentMax
)
10504 extentMax
= extent
;
10508 // empty column - give default extent (notice that if extentMax is less
10509 // than default extent but != 0, it's OK)
10510 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
10515 // leave some space around text
10523 // Ensure automatic width is not less than minimal width. See the
10524 // comment in SetColSize() for explanation of why this isn't done
10525 // in SetColSize().
10527 extentMax
= wxMax(extentMax
, GetColMinimalWidth(col
));
10529 SetColSize( col
, extentMax
);
10530 if ( !GetBatchCount() )
10533 m_gridWin
->GetClientSize( &cw
, &ch
);
10534 wxRect
rect ( CellToRect( 0, col
) );
10536 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10537 rect
.width
= cw
- rect
.x
;
10538 rect
.height
= m_colLabelHeight
;
10539 m_colLabelWin
->Refresh( true, &rect
);
10544 // Ensure automatic width is not less than minimal height. See the
10545 // comment in SetColSize() for explanation of why this isn't done
10546 // in SetRowSize().
10548 extentMax
= wxMax(extentMax
, GetRowMinimalHeight(row
));
10550 SetRowSize(row
, extentMax
);
10551 if ( !GetBatchCount() )
10554 m_gridWin
->GetClientSize( &cw
, &ch
);
10555 wxRect
rect( CellToRect( row
, 0 ) );
10557 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10558 rect
.width
= m_rowLabelWidth
;
10559 rect
.height
= ch
- rect
.y
;
10560 m_rowLabelWin
->Refresh( true, &rect
);
10567 SetColMinimalWidth(col
, extentMax
);
10569 SetRowMinimalHeight(row
, extentMax
);
10573 wxCoord
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
10575 // calculate size for the rows or columns?
10576 const bool calcRows
= direction
== wxGRID_ROW
;
10578 wxClientDC
dc(calcRows
? GetGridRowLabelWindow()
10579 : GetGridColLabelWindow());
10580 dc
.SetFont(GetLabelFont());
10582 // which dimension should we take into account for calculations?
10584 // for columns, the text can be only horizontal so it's easy but for rows
10585 // we also have to take into account the text orientation
10587 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
10589 wxArrayString lines
;
10590 wxCoord extentMax
= 0;
10592 const int numRowsOrCols
= calcRows
? m_numRows
: m_numCols
;
10593 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
10597 wxString label
= calcRows
? GetRowLabelValue(rowOrCol
)
10598 : GetColLabelValue(rowOrCol
);
10599 StringToLines(label
, lines
);
10602 GetTextBoxSize(dc
, lines
, &w
, &h
);
10604 const wxCoord extent
= useWidth
? w
: h
;
10605 if ( extent
> extentMax
)
10606 extentMax
= extent
;
10611 // empty column - give default extent (notice that if extentMax is less
10612 // than default extent but != 0, it's OK)
10613 extentMax
= calcRows
? GetDefaultRowLabelSize()
10614 : GetDefaultColLabelSize();
10617 // leave some space around text (taken from AutoSizeColOrRow)
10626 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10628 int width
= m_rowLabelWidth
;
10630 wxGridUpdateLocker locker
;
10632 locker
.Create(this);
10634 for ( int col
= 0; col
< m_numCols
; col
++ )
10637 AutoSizeColumn(col
, setAsMin
);
10639 width
+= GetColWidth(col
);
10645 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10647 int height
= m_colLabelHeight
;
10649 wxGridUpdateLocker locker
;
10651 locker
.Create(this);
10653 for ( int row
= 0; row
< m_numRows
; row
++ )
10656 AutoSizeRow(row
, setAsMin
);
10658 height
+= GetRowHeight(row
);
10664 void wxGrid::AutoSize()
10666 wxGridUpdateLocker
locker(this);
10668 // we need to round up the size of the scrollable area to a multiple of
10669 // scroll step to ensure that we don't get the scrollbars when we're sized
10670 // exactly to fit our contents
10671 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
10672 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
10673 wxSize
sizeFit(GetScrollX(size
.x
) * GetScrollLineX(),
10674 GetScrollY(size
.y
) * GetScrollLineY());
10676 // distribute the extra space between the columns/rows to avoid having
10677 // extra white space
10678 wxCoord diff
= sizeFit
.x
- size
.x
;
10679 if ( diff
&& m_numCols
)
10681 // try to resize the columns uniformly
10682 wxCoord diffPerCol
= diff
/ m_numCols
;
10685 for ( int col
= 0; col
< m_numCols
; col
++ )
10687 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
10691 // add remaining amount to the last columns
10692 diff
-= diffPerCol
* m_numCols
;
10695 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
10697 SetColSize(col
, GetColWidth(col
) + 1);
10703 diff
= sizeFit
.y
- size
.y
;
10704 if ( diff
&& m_numRows
)
10706 // try to resize the columns uniformly
10707 wxCoord diffPerRow
= diff
/ m_numRows
;
10710 for ( int row
= 0; row
< m_numRows
; row
++ )
10712 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
10716 // add remaining amount to the last rows
10717 diff
-= diffPerRow
* m_numRows
;
10720 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
10722 SetRowSize(row
, GetRowHeight(row
) + 1);
10727 // we know that we're not going to have scrollbars so disable them now to
10728 // avoid trouble in SetClientSize() which can otherwise set the correct
10729 // client size but also leave space for (not needed any more) scrollbars
10730 SetScrollbars(0, 0, 0, 0, 0, 0, true);
10731 SetClientSize(sizeFit
.x
+ m_rowLabelWidth
, sizeFit
.y
+ m_colLabelHeight
);
10734 void wxGrid::AutoSizeRowLabelSize( int row
)
10736 wxArrayString lines
;
10739 // Hide the edit control, so it
10740 // won't interfere with drag-shrinking.
10741 if ( IsCellEditControlShown() )
10743 HideCellEditControl();
10744 SaveEditControlValue();
10747 // autosize row height depending on label text
10748 StringToLines( GetRowLabelValue( row
), lines
);
10749 wxClientDC
dc( m_rowLabelWin
);
10750 GetTextBoxSize( dc
, lines
, &w
, &h
);
10751 if ( h
< m_defaultRowHeight
)
10752 h
= m_defaultRowHeight
;
10753 SetRowSize(row
, h
);
10757 void wxGrid::AutoSizeColLabelSize( int col
)
10759 wxArrayString lines
;
10762 // Hide the edit control, so it
10763 // won't interfere with drag-shrinking.
10764 if ( IsCellEditControlShown() )
10766 HideCellEditControl();
10767 SaveEditControlValue();
10770 // autosize column width depending on label text
10771 StringToLines( GetColLabelValue( col
), lines
);
10772 wxClientDC
dc( m_colLabelWin
);
10773 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10774 GetTextBoxSize( dc
, lines
, &w
, &h
);
10776 GetTextBoxSize( dc
, lines
, &h
, &w
);
10777 if ( w
< m_defaultColWidth
)
10778 w
= m_defaultColWidth
;
10779 SetColSize(col
, w
);
10783 wxSize
wxGrid::DoGetBestSize() const
10785 wxGrid
*self
= (wxGrid
*)this; // const_cast
10787 // we do the same as in AutoSize() here with the exception that we don't
10788 // change the column/row sizes, only calculate them
10789 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
10790 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
10791 wxSize
sizeFit(GetScrollX(size
.x
) * GetScrollLineX(),
10792 GetScrollY(size
.y
) * GetScrollLineY());
10794 // NOTE: This size should be cached, but first we need to add calls to
10795 // InvalidateBestSize everywhere that could change the results of this
10797 // CacheBestSize(size);
10799 return wxSize(sizeFit
.x
+ m_rowLabelWidth
, sizeFit
.y
+ m_colLabelHeight
)
10800 + GetWindowBorderSize();
10808 wxPen
& wxGrid::GetDividerPen() const
10813 // ----------------------------------------------------------------------------
10814 // cell value accessor functions
10815 // ----------------------------------------------------------------------------
10817 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10821 m_table
->SetValue( row
, col
, s
);
10822 if ( !GetBatchCount() )
10825 wxRect
rect( CellToRect( row
, col
) );
10827 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10828 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10829 m_gridWin
->Refresh( false, &rect
);
10832 if ( m_currentCellCoords
.GetRow() == row
&&
10833 m_currentCellCoords
.GetCol() == col
&&
10834 IsCellEditControlShown())
10835 // Note: If we are using IsCellEditControlEnabled,
10836 // this interacts badly with calling SetCellValue from
10837 // an EVT_GRID_CELL_CHANGE handler.
10839 HideCellEditControl();
10840 ShowCellEditControl(); // will reread data from table
10845 // ----------------------------------------------------------------------------
10846 // block, row and column selection
10847 // ----------------------------------------------------------------------------
10849 void wxGrid::SelectRow( int row
, bool addToSelected
)
10851 if ( IsSelection() && !addToSelected
)
10855 m_selection
->SelectRow( row
, false, addToSelected
);
10858 void wxGrid::SelectCol( int col
, bool addToSelected
)
10860 if ( IsSelection() && !addToSelected
)
10864 m_selection
->SelectCol( col
, false, addToSelected
);
10867 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10868 bool addToSelected
)
10870 if ( IsSelection() && !addToSelected
)
10874 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10875 false, addToSelected
);
10878 void wxGrid::SelectAll()
10880 if ( m_numRows
> 0 && m_numCols
> 0 )
10883 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10887 // ----------------------------------------------------------------------------
10888 // cell, row and col deselection
10889 // ----------------------------------------------------------------------------
10891 void wxGrid::DeselectRow( int row
)
10893 if ( !m_selection
)
10896 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10898 if ( m_selection
->IsInSelection(row
, 0 ) )
10899 m_selection
->ToggleCellSelection(row
, 0);
10903 int nCols
= GetNumberCols();
10904 for ( int i
= 0; i
< nCols
; i
++ )
10906 if ( m_selection
->IsInSelection(row
, i
) )
10907 m_selection
->ToggleCellSelection(row
, i
);
10912 void wxGrid::DeselectCol( int col
)
10914 if ( !m_selection
)
10917 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10919 if ( m_selection
->IsInSelection(0, col
) )
10920 m_selection
->ToggleCellSelection(0, col
);
10924 int nRows
= GetNumberRows();
10925 for ( int i
= 0; i
< nRows
; i
++ )
10927 if ( m_selection
->IsInSelection(i
, col
) )
10928 m_selection
->ToggleCellSelection(i
, col
);
10933 void wxGrid::DeselectCell( int row
, int col
)
10935 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10936 m_selection
->ToggleCellSelection(row
, col
);
10939 bool wxGrid::IsSelection() const
10941 return ( m_selection
&& (m_selection
->IsSelection() ||
10942 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10943 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10946 bool wxGrid::IsInSelection( int row
, int col
) const
10948 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10949 ( row
>= m_selectingTopLeft
.GetRow() &&
10950 col
>= m_selectingTopLeft
.GetCol() &&
10951 row
<= m_selectingBottomRight
.GetRow() &&
10952 col
<= m_selectingBottomRight
.GetCol() )) );
10955 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10959 wxGridCellCoordsArray a
;
10963 return m_selection
->m_cellSelection
;
10966 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10970 wxGridCellCoordsArray a
;
10974 return m_selection
->m_blockSelectionTopLeft
;
10977 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10981 wxGridCellCoordsArray a
;
10985 return m_selection
->m_blockSelectionBottomRight
;
10988 wxArrayInt
wxGrid::GetSelectedRows() const
10996 return m_selection
->m_rowSelection
;
10999 wxArrayInt
wxGrid::GetSelectedCols() const
11007 return m_selection
->m_colSelection
;
11010 void wxGrid::ClearSelection()
11012 m_selectingTopLeft
=
11013 m_selectingBottomRight
=
11014 m_selectingKeyboard
= wxGridNoCellCoords
;
11016 m_selection
->ClearSelection();
11019 // This function returns the rectangle that encloses the given block
11020 // in device coords clipped to the client size of the grid window.
11022 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
11023 const wxGridCellCoords
& bottomRight
) const
11026 wxRect tempCellRect
= CellToRect(topLeft
);
11027 if ( tempCellRect
!= wxGridNoCellRect
)
11029 resultRect
= tempCellRect
;
11033 resultRect
= wxRect(0, 0, 0, 0);
11036 tempCellRect
= CellToRect(bottomRight
);
11037 if ( tempCellRect
!= wxGridNoCellRect
)
11039 resultRect
+= tempCellRect
;
11043 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
11044 return wxGridNoCellRect
;
11047 // Ensure that left/right and top/bottom pairs are in order.
11048 int left
= resultRect
.GetLeft();
11049 int top
= resultRect
.GetTop();
11050 int right
= resultRect
.GetRight();
11051 int bottom
= resultRect
.GetBottom();
11053 int leftCol
= topLeft
.GetCol();
11054 int topRow
= topLeft
.GetRow();
11055 int rightCol
= bottomRight
.GetCol();
11056 int bottomRow
= bottomRight
.GetRow();
11065 leftCol
= rightCol
;
11076 topRow
= bottomRow
;
11080 // The following loop is ONLY necessary to detect and handle merged cells.
11082 m_gridWin
->GetClientSize( &cw
, &ch
);
11084 // Get the origin coordinates: notice that they will be negative if the
11085 // grid is scrolled downwards/to the right.
11086 int gridOriginX
= 0;
11087 int gridOriginY
= 0;
11088 CalcScrolledPosition(gridOriginX
, gridOriginY
, &gridOriginX
, &gridOriginY
);
11090 int onScreenLeftmostCol
= internalXToCol(-gridOriginX
);
11091 int onScreenUppermostRow
= internalYToRow(-gridOriginY
);
11093 int onScreenRightmostCol
= internalXToCol(-gridOriginX
+ cw
);
11094 int onScreenBottommostRow
= internalYToRow(-gridOriginY
+ ch
);
11096 // Bound our loop so that we only examine the portion of the selected block
11097 // that is shown on screen. Therefore, we compare the Top-Left block values
11098 // to the Top-Left screen values, and the Bottom-Right block values to the
11099 // Bottom-Right screen values, choosing appropriately.
11100 const int visibleTopRow
= wxMax(topRow
, onScreenUppermostRow
);
11101 const int visibleBottomRow
= wxMin(bottomRow
, onScreenBottommostRow
);
11102 const int visibleLeftCol
= wxMax(leftCol
, onScreenLeftmostCol
);
11103 const int visibleRightCol
= wxMin(rightCol
, onScreenRightmostCol
);
11105 for ( int j
= visibleTopRow
; j
<= visibleBottomRow
; j
++ )
11107 for ( int i
= visibleLeftCol
; i
<= visibleRightCol
; i
++ )
11109 if ( (j
== visibleTopRow
) || (j
== visibleBottomRow
) ||
11110 (i
== visibleLeftCol
) || (i
== visibleRightCol
) )
11112 tempCellRect
= CellToRect( j
, i
);
11114 if (tempCellRect
.x
< left
)
11115 left
= tempCellRect
.x
;
11116 if (tempCellRect
.y
< top
)
11117 top
= tempCellRect
.y
;
11118 if (tempCellRect
.x
+ tempCellRect
.width
> right
)
11119 right
= tempCellRect
.x
+ tempCellRect
.width
;
11120 if (tempCellRect
.y
+ tempCellRect
.height
> bottom
)
11121 bottom
= tempCellRect
.y
+ tempCellRect
.height
;
11125 i
= visibleRightCol
; // jump over inner cells.
11130 // Convert to scrolled coords
11131 CalcScrolledPosition( left
, top
, &left
, &top
);
11132 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
11134 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
11135 return wxRect(0,0,0,0);
11137 resultRect
.SetLeft( wxMax(0, left
) );
11138 resultRect
.SetTop( wxMax(0, top
) );
11139 resultRect
.SetRight( wxMin(cw
, right
) );
11140 resultRect
.SetBottom( wxMin(ch
, bottom
) );
11145 // ----------------------------------------------------------------------------
11147 // ----------------------------------------------------------------------------
11149 #if wxUSE_DRAG_AND_DROP
11151 // this allow setting drop target directly on wxGrid
11152 void wxGrid::SetDropTarget(wxDropTarget
*dropTarget
)
11154 GetGridWindow()->SetDropTarget(dropTarget
);
11157 #endif // wxUSE_DRAG_AND_DROP
11159 // ----------------------------------------------------------------------------
11160 // grid event classes
11161 // ----------------------------------------------------------------------------
11163 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
11165 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
11166 int row
, int col
, int x
, int y
, bool sel
,
11167 bool control
, bool shift
, bool alt
, bool meta
)
11168 : wxNotifyEvent( type
, id
)
11175 m_control
= control
;
11180 SetEventObject(obj
);
11184 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
11186 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
11187 int rowOrCol
, int x
, int y
,
11188 bool control
, bool shift
, bool alt
, bool meta
)
11189 : wxNotifyEvent( type
, id
)
11191 m_rowOrCol
= rowOrCol
;
11194 m_control
= control
;
11199 SetEventObject(obj
);
11203 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
11205 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
11206 const wxGridCellCoords
& topLeft
,
11207 const wxGridCellCoords
& bottomRight
,
11208 bool sel
, bool control
,
11209 bool shift
, bool alt
, bool meta
)
11210 : wxNotifyEvent( type
, id
)
11212 m_topLeft
= topLeft
;
11213 m_bottomRight
= bottomRight
;
11215 m_control
= control
;
11220 SetEventObject(obj
);
11224 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
11226 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
11227 wxObject
* obj
, int row
,
11228 int col
, wxControl
* ctrl
)
11229 : wxCommandEvent(type
, id
)
11231 SetEventObject(obj
);
11237 #endif // wxUSE_GRID