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
)
2593 // add the attribute
2594 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2598 // free the old attribute
2599 m_attrs
[(size_t)n
].attr
->DecRef();
2603 // change the attribute
2604 m_attrs
[(size_t)n
].attr
= attr
;
2608 // remove this attribute
2609 m_attrs
.RemoveAt((size_t)n
);
2614 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2616 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2618 int n
= FindIndex(row
, col
);
2619 if ( n
!= wxNOT_FOUND
)
2621 attr
= m_attrs
[(size_t)n
].attr
;
2628 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2630 size_t count
= m_attrs
.GetCount();
2631 for ( size_t n
= 0; n
< count
; n
++ )
2633 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2634 wxCoord row
= coords
.GetRow();
2635 if ((size_t)row
>= pos
)
2639 // If rows inserted, include row counter where necessary
2640 coords
.SetRow(row
+ numRows
);
2642 else if (numRows
< 0)
2644 // If rows deleted ...
2645 if ((size_t)row
>= pos
- numRows
)
2647 // ...either decrement row counter (if row still exists)...
2648 coords
.SetRow(row
+ numRows
);
2652 // ...or remove the attribute
2653 // No need to DecRef the attribute itself since this is
2654 // done be wxGridCellWithAttr's destructor!
2655 m_attrs
.RemoveAt(n
);
2664 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2666 size_t count
= m_attrs
.GetCount();
2667 for ( size_t n
= 0; n
< count
; n
++ )
2669 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2670 wxCoord col
= coords
.GetCol();
2671 if ( (size_t)col
>= pos
)
2675 // If rows inserted, include row counter where necessary
2676 coords
.SetCol(col
+ numCols
);
2678 else if (numCols
< 0)
2680 // If rows deleted ...
2681 if ((size_t)col
>= pos
- numCols
)
2683 // ...either decrement row counter (if row still exists)...
2684 coords
.SetCol(col
+ numCols
);
2688 // ...or remove the attribute
2689 // No need to DecRef the attribute itself since this is
2690 // done be wxGridCellWithAttr's destructor!
2691 m_attrs
.RemoveAt(n
);
2700 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2702 size_t count
= m_attrs
.GetCount();
2703 for ( size_t n
= 0; n
< count
; n
++ )
2705 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2706 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2715 // ----------------------------------------------------------------------------
2716 // wxGridRowOrColAttrData
2717 // ----------------------------------------------------------------------------
2719 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2721 size_t count
= m_attrs
.GetCount();
2722 for ( size_t n
= 0; n
< count
; n
++ )
2724 m_attrs
[n
]->DecRef();
2728 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2730 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2732 int n
= m_rowsOrCols
.Index(rowOrCol
);
2733 if ( n
!= wxNOT_FOUND
)
2735 attr
= m_attrs
[(size_t)n
];
2742 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2744 int i
= m_rowsOrCols
.Index(rowOrCol
);
2745 if ( i
== wxNOT_FOUND
)
2749 // add the attribute
2750 m_rowsOrCols
.Add(rowOrCol
);
2753 // nothing to remove
2757 size_t n
= (size_t)i
;
2760 // change the attribute
2761 m_attrs
[n
]->DecRef();
2766 // remove this attribute
2767 m_attrs
[n
]->DecRef();
2768 m_rowsOrCols
.RemoveAt(n
);
2769 m_attrs
.RemoveAt(n
);
2774 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2776 size_t count
= m_attrs
.GetCount();
2777 for ( size_t n
= 0; n
< count
; n
++ )
2779 int & rowOrCol
= m_rowsOrCols
[n
];
2780 if ( (size_t)rowOrCol
>= pos
)
2782 if ( numRowsOrCols
> 0 )
2784 // If rows inserted, include row counter where necessary
2785 rowOrCol
+= numRowsOrCols
;
2787 else if ( numRowsOrCols
< 0)
2789 // If rows deleted, either decrement row counter (if row still exists)
2790 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2791 rowOrCol
+= numRowsOrCols
;
2794 m_rowsOrCols
.RemoveAt(n
);
2795 m_attrs
[n
]->DecRef();
2796 m_attrs
.RemoveAt(n
);
2805 // ----------------------------------------------------------------------------
2806 // wxGridCellAttrProvider
2807 // ----------------------------------------------------------------------------
2809 wxGridCellAttrProvider::wxGridCellAttrProvider()
2811 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2814 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2819 void wxGridCellAttrProvider::InitData()
2821 m_data
= new wxGridCellAttrProviderData
;
2824 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2825 wxGridCellAttr::wxAttrKind kind
) const
2827 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2832 case (wxGridCellAttr::Any
):
2833 // Get cached merge attributes.
2834 // Currently not used as no cache implemented as not mutable
2835 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2838 // Basically implement old version.
2839 // Also check merge cache, so we don't have to re-merge every time..
2840 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2841 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2842 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2844 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
2846 // Two or more are non NULL
2847 attr
= new wxGridCellAttr
;
2848 attr
->SetKind(wxGridCellAttr::Merged
);
2850 // Order is important..
2853 attr
->MergeWith(attrcell
);
2858 attr
->MergeWith(attrcol
);
2863 attr
->MergeWith(attrrow
);
2867 // store merge attr if cache implemented
2869 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2873 // one or none is non null return it or null.
2892 case (wxGridCellAttr::Cell
):
2893 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2896 case (wxGridCellAttr::Col
):
2897 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2900 case (wxGridCellAttr::Row
):
2901 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2906 // (wxGridCellAttr::Default):
2907 // (wxGridCellAttr::Merged):
2915 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2921 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2924 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2929 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2932 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2937 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2940 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2944 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2946 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2950 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2954 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2956 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2960 // ----------------------------------------------------------------------------
2961 // wxGridTypeRegistry
2962 // ----------------------------------------------------------------------------
2964 wxGridTypeRegistry::~wxGridTypeRegistry()
2966 size_t count
= m_typeinfo
.GetCount();
2967 for ( size_t i
= 0; i
< count
; i
++ )
2968 delete m_typeinfo
[i
];
2971 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2972 wxGridCellRenderer
* renderer
,
2973 wxGridCellEditor
* editor
)
2975 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2977 // is it already registered?
2978 int loc
= FindRegisteredDataType(typeName
);
2979 if ( loc
!= wxNOT_FOUND
)
2981 delete m_typeinfo
[loc
];
2982 m_typeinfo
[loc
] = info
;
2986 m_typeinfo
.Add(info
);
2990 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2992 size_t count
= m_typeinfo
.GetCount();
2993 for ( size_t i
= 0; i
< count
; i
++ )
2995 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
3004 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
3006 int index
= FindRegisteredDataType(typeName
);
3007 if ( index
== wxNOT_FOUND
)
3009 // check whether this is one of the standard ones, in which case
3010 // register it "on the fly"
3012 if ( typeName
== wxGRID_VALUE_STRING
)
3014 RegisterDataType(wxGRID_VALUE_STRING
,
3015 new wxGridCellStringRenderer
,
3016 new wxGridCellTextEditor
);
3019 #endif // wxUSE_TEXTCTRL
3021 if ( typeName
== wxGRID_VALUE_BOOL
)
3023 RegisterDataType(wxGRID_VALUE_BOOL
,
3024 new wxGridCellBoolRenderer
,
3025 new wxGridCellBoolEditor
);
3028 #endif // wxUSE_CHECKBOX
3030 if ( typeName
== wxGRID_VALUE_NUMBER
)
3032 RegisterDataType(wxGRID_VALUE_NUMBER
,
3033 new wxGridCellNumberRenderer
,
3034 new wxGridCellNumberEditor
);
3036 else if ( typeName
== wxGRID_VALUE_FLOAT
)
3038 RegisterDataType(wxGRID_VALUE_FLOAT
,
3039 new wxGridCellFloatRenderer
,
3040 new wxGridCellFloatEditor
);
3043 #endif // wxUSE_TEXTCTRL
3045 if ( typeName
== wxGRID_VALUE_CHOICE
)
3047 RegisterDataType(wxGRID_VALUE_CHOICE
,
3048 new wxGridCellStringRenderer
,
3049 new wxGridCellChoiceEditor
);
3052 #endif // wxUSE_COMBOBOX
3057 // we get here only if just added the entry for this type, so return
3059 index
= m_typeinfo
.GetCount() - 1;
3065 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
3067 int index
= FindDataType(typeName
);
3068 if ( index
== wxNOT_FOUND
)
3070 // the first part of the typename is the "real" type, anything after ':'
3071 // are the parameters for the renderer
3072 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
3073 if ( index
== wxNOT_FOUND
)
3078 wxGridCellRenderer
*renderer
= GetRenderer(index
);
3079 wxGridCellRenderer
*rendererOld
= renderer
;
3080 renderer
= renderer
->Clone();
3081 rendererOld
->DecRef();
3083 wxGridCellEditor
*editor
= GetEditor(index
);
3084 wxGridCellEditor
*editorOld
= editor
;
3085 editor
= editor
->Clone();
3086 editorOld
->DecRef();
3088 // do it even if there are no parameters to reset them to defaults
3089 wxString params
= typeName
.AfterFirst(_T(':'));
3090 renderer
->SetParameters(params
);
3091 editor
->SetParameters(params
);
3093 // register the new typename
3094 RegisterDataType(typeName
, renderer
, editor
);
3096 // we just registered it, it's the last one
3097 index
= m_typeinfo
.GetCount() - 1;
3103 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
3105 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
3112 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
3114 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
3121 // ----------------------------------------------------------------------------
3123 // ----------------------------------------------------------------------------
3125 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
3127 wxGridTableBase::wxGridTableBase()
3129 m_view
= (wxGrid
*) NULL
;
3130 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
3133 wxGridTableBase::~wxGridTableBase()
3135 delete m_attrProvider
;
3138 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
3140 delete m_attrProvider
;
3141 m_attrProvider
= attrProvider
;
3144 bool wxGridTableBase::CanHaveAttributes()
3146 if ( ! GetAttrProvider() )
3148 // use the default attr provider by default
3149 SetAttrProvider(new wxGridCellAttrProvider
);
3155 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
3157 if ( m_attrProvider
)
3158 return m_attrProvider
->GetAttr(row
, col
, kind
);
3160 return (wxGridCellAttr
*)NULL
;
3163 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
3165 if ( m_attrProvider
)
3167 attr
->SetKind(wxGridCellAttr::Cell
);
3168 m_attrProvider
->SetAttr(attr
, row
, col
);
3172 // as we take ownership of the pointer and don't store it, we must
3178 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3180 if ( m_attrProvider
)
3182 attr
->SetKind(wxGridCellAttr::Row
);
3183 m_attrProvider
->SetRowAttr(attr
, row
);
3187 // as we take ownership of the pointer and don't store it, we must
3193 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3195 if ( m_attrProvider
)
3197 attr
->SetKind(wxGridCellAttr::Col
);
3198 m_attrProvider
->SetColAttr(attr
, col
);
3202 // as we take ownership of the pointer and don't store it, we must
3208 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3209 size_t WXUNUSED(numRows
) )
3211 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3216 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3218 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3223 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3224 size_t WXUNUSED(numRows
) )
3226 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3231 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3232 size_t WXUNUSED(numCols
) )
3234 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3239 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3241 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3246 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3247 size_t WXUNUSED(numCols
) )
3249 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3254 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3258 // RD: Starting the rows at zero confuses users,
3259 // no matter how much it makes sense to us geeks.
3265 wxString
wxGridTableBase::GetColLabelValue( int col
)
3267 // default col labels are:
3268 // cols 0 to 25 : A-Z
3269 // cols 26 to 675 : AA-ZZ
3274 for ( n
= 1; ; n
++ )
3276 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
3282 // reverse the string...
3284 for ( i
= 0; i
< n
; i
++ )
3292 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3294 return wxGRID_VALUE_STRING
;
3297 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3298 const wxString
& typeName
)
3300 return typeName
== wxGRID_VALUE_STRING
;
3303 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3305 return CanGetValueAs(row
, col
, typeName
);
3308 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3313 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3318 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3323 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3324 long WXUNUSED(value
) )
3328 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3329 double WXUNUSED(value
) )
3333 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3334 bool WXUNUSED(value
) )
3338 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3339 const wxString
& WXUNUSED(typeName
) )
3344 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3345 const wxString
& WXUNUSED(typeName
),
3346 void* WXUNUSED(value
) )
3350 //////////////////////////////////////////////////////////////////////
3352 // Message class for the grid table to send requests and notifications
3356 wxGridTableMessage::wxGridTableMessage()
3358 m_table
= (wxGridTableBase
*) NULL
;
3364 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3365 int commandInt1
, int commandInt2
)
3369 m_comInt1
= commandInt1
;
3370 m_comInt2
= commandInt2
;
3373 //////////////////////////////////////////////////////////////////////
3375 // A basic grid table for string data. An object of this class will
3376 // created by wxGrid if you don't specify an alternative table class.
3379 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3381 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3383 wxGridStringTable::wxGridStringTable()
3388 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3391 m_data
.Alloc( numRows
);
3394 sa
.Alloc( numCols
);
3395 sa
.Add( wxEmptyString
, numCols
);
3397 m_data
.Add( sa
, numRows
);
3400 wxGridStringTable::~wxGridStringTable()
3404 int wxGridStringTable::GetNumberRows()
3406 return m_data
.GetCount();
3409 int wxGridStringTable::GetNumberCols()
3411 if ( m_data
.GetCount() > 0 )
3412 return m_data
[0].GetCount();
3417 wxString
wxGridStringTable::GetValue( int row
, int col
)
3419 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3421 _T("invalid row or column index in wxGridStringTable") );
3423 return m_data
[row
][col
];
3426 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3428 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3429 _T("invalid row or column index in wxGridStringTable") );
3431 m_data
[row
][col
] = value
;
3434 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3436 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3438 _T("invalid row or column index in wxGridStringTable") );
3440 return (m_data
[row
][col
] == wxEmptyString
);
3443 void wxGridStringTable::Clear()
3446 int numRows
, numCols
;
3448 numRows
= m_data
.GetCount();
3451 numCols
= m_data
[0].GetCount();
3453 for ( row
= 0; row
< numRows
; row
++ )
3455 for ( col
= 0; col
< numCols
; col
++ )
3457 m_data
[row
][col
] = wxEmptyString
;
3463 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3465 size_t curNumRows
= m_data
.GetCount();
3466 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3467 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3469 if ( pos
>= curNumRows
)
3471 return AppendRows( numRows
);
3475 sa
.Alloc( curNumCols
);
3476 sa
.Add( wxEmptyString
, curNumCols
);
3477 m_data
.Insert( sa
, pos
, numRows
);
3481 wxGridTableMessage
msg( this,
3482 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3486 GetView()->ProcessTableMessage( msg
);
3492 bool wxGridStringTable::AppendRows( size_t numRows
)
3494 size_t curNumRows
= m_data
.GetCount();
3495 size_t curNumCols
= ( curNumRows
> 0
3496 ? m_data
[0].GetCount()
3497 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3500 if ( curNumCols
> 0 )
3502 sa
.Alloc( curNumCols
);
3503 sa
.Add( wxEmptyString
, curNumCols
);
3506 m_data
.Add( sa
, numRows
);
3510 wxGridTableMessage
msg( this,
3511 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3514 GetView()->ProcessTableMessage( msg
);
3520 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3522 size_t curNumRows
= m_data
.GetCount();
3524 if ( pos
>= curNumRows
)
3526 wxFAIL_MSG( wxString::Format
3528 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3530 (unsigned long)numRows
,
3531 (unsigned long)curNumRows
3537 if ( numRows
> curNumRows
- pos
)
3539 numRows
= curNumRows
- pos
;
3542 if ( numRows
>= curNumRows
)
3548 m_data
.RemoveAt( pos
, numRows
);
3553 wxGridTableMessage
msg( this,
3554 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3558 GetView()->ProcessTableMessage( msg
);
3564 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3568 size_t curNumRows
= m_data
.GetCount();
3569 size_t curNumCols
= ( curNumRows
> 0
3570 ? m_data
[0].GetCount()
3571 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3573 if ( pos
>= curNumCols
)
3575 return AppendCols( numCols
);
3578 if ( !m_colLabels
.IsEmpty() )
3580 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
3583 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
3584 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
3587 for ( row
= 0; row
< curNumRows
; row
++ )
3589 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3591 m_data
[row
].Insert( wxEmptyString
, col
);
3597 wxGridTableMessage
msg( this,
3598 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3602 GetView()->ProcessTableMessage( msg
);
3608 bool wxGridStringTable::AppendCols( size_t numCols
)
3612 size_t curNumRows
= m_data
.GetCount();
3617 // TODO: something better than this ?
3619 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3624 for ( row
= 0; row
< curNumRows
; row
++ )
3626 m_data
[row
].Add( wxEmptyString
, numCols
);
3631 wxGridTableMessage
msg( this,
3632 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3635 GetView()->ProcessTableMessage( msg
);
3641 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3645 size_t curNumRows
= m_data
.GetCount();
3646 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3647 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3649 if ( pos
>= curNumCols
)
3651 wxFAIL_MSG( wxString::Format
3653 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3655 (unsigned long)numCols
,
3656 (unsigned long)curNumCols
3663 colID
= GetView()->GetColAt( pos
);
3667 if ( numCols
> curNumCols
- colID
)
3669 numCols
= curNumCols
- colID
;
3672 if ( !m_colLabels
.IsEmpty() )
3674 // m_colLabels stores just as many elements as it needs, e.g. if only
3675 // the label of the first column had been set it would have only one
3676 // element and not numCols, so account for it
3677 int nToRm
= m_colLabels
.size() - colID
;
3679 m_colLabels
.RemoveAt( colID
, nToRm
);
3682 for ( row
= 0; row
< curNumRows
; row
++ )
3684 if ( numCols
>= curNumCols
)
3686 m_data
[row
].Clear();
3690 m_data
[row
].RemoveAt( colID
, numCols
);
3696 wxGridTableMessage
msg( this,
3697 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3701 GetView()->ProcessTableMessage( msg
);
3707 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3709 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3711 // using default label
3713 return wxGridTableBase::GetRowLabelValue( row
);
3717 return m_rowLabels
[row
];
3721 wxString
wxGridStringTable::GetColLabelValue( int col
)
3723 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3725 // using default label
3727 return wxGridTableBase::GetColLabelValue( col
);
3731 return m_colLabels
[col
];
3735 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3737 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3739 int n
= m_rowLabels
.GetCount();
3742 for ( i
= n
; i
<= row
; i
++ )
3744 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3748 m_rowLabels
[row
] = value
;
3751 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3753 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3755 int n
= m_colLabels
.GetCount();
3758 for ( i
= n
; i
<= col
; i
++ )
3760 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3764 m_colLabels
[col
] = value
;
3768 //////////////////////////////////////////////////////////////////////
3769 //////////////////////////////////////////////////////////////////////
3771 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
)
3772 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost
)
3775 void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
3777 m_owner
->CancelMouseCapture();
3780 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3782 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow
)
3783 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3784 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3785 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3788 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3790 const wxPoint
&pos
, const wxSize
&size
)
3791 : wxGridSubwindow(parent
, id
, pos
, size
)
3796 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3800 // NO - don't do this because it will set both the x and y origin
3801 // coords to match the parent scrolled window and we just want to
3802 // set the y coord - MB
3804 // m_owner->PrepareDC( dc );
3807 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3808 wxPoint pt
= dc
.GetDeviceOrigin();
3809 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
3811 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3812 m_owner
->DrawRowLabels( dc
, rows
);
3815 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3817 m_owner
->ProcessRowLabelMouseEvent( event
);
3820 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3822 m_owner
->GetEventHandler()->ProcessEvent( event
);
3825 //////////////////////////////////////////////////////////////////////
3827 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3829 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow
)
3830 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3831 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3832 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3835 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3837 const wxPoint
&pos
, const wxSize
&size
)
3838 : wxGridSubwindow(parent
, id
, pos
, size
)
3843 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3847 // NO - don't do this because it will set both the x and y origin
3848 // coords to match the parent scrolled window and we just want to
3849 // set the x coord - MB
3851 // m_owner->PrepareDC( dc );
3854 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3855 wxPoint pt
= dc
.GetDeviceOrigin();
3856 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3857 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
3859 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
3861 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3862 m_owner
->DrawColLabels( dc
, cols
);
3865 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3867 m_owner
->ProcessColLabelMouseEvent( event
);
3870 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3872 m_owner
->GetEventHandler()->ProcessEvent( event
);
3875 //////////////////////////////////////////////////////////////////////
3877 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3879 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow
)
3880 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3881 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3882 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3885 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3887 const wxPoint
&pos
, const wxSize
&size
)
3888 : wxGridSubwindow(parent
, id
, pos
, size
)
3893 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3897 int client_height
= 0;
3898 int client_width
= 0;
3899 GetClientSize( &client_width
, &client_height
);
3901 // VZ: any reason for this ifdef? (FIXME)
3907 rect
.SetWidth( client_width
- 2 );
3908 rect
.SetHeight( client_height
- 2 );
3910 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3912 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxPENSTYLE_SOLID
) );
3913 dc
.DrawLine( client_width
- 1, client_height
- 1, client_width
- 1, 0 );
3914 dc
.DrawLine( client_width
- 1, client_height
- 1, 0, client_height
- 1 );
3915 dc
.DrawLine( 0, 0, client_width
, 0 );
3916 dc
.DrawLine( 0, 0, 0, client_height
);
3918 dc
.SetPen( *wxWHITE_PEN
);
3919 dc
.DrawLine( 1, 1, client_width
- 1, 1 );
3920 dc
.DrawLine( 1, 1, 1, client_height
- 1 );
3924 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3926 m_owner
->ProcessCornerLabelMouseEvent( event
);
3929 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3931 m_owner
->GetEventHandler()->ProcessEvent(event
);
3934 //////////////////////////////////////////////////////////////////////
3936 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3938 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow
)
3939 EVT_PAINT( wxGridWindow::OnPaint
)
3940 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3941 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3942 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3943 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3944 EVT_CHAR( wxGridWindow::OnChar
)
3945 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3946 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3947 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3950 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3951 wxGridRowLabelWindow
*rowLblWin
,
3952 wxGridColLabelWindow
*colLblWin
,
3955 const wxSize
&size
)
3956 : wxGridSubwindow(parent
, id
, pos
, size
,
3957 wxWANTS_CHARS
| wxCLIP_CHILDREN
,
3958 wxT("grid window") )
3961 m_rowLabelWin
= rowLblWin
;
3962 m_colLabelWin
= colLblWin
;
3965 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3967 wxPaintDC
dc( this );
3968 m_owner
->PrepareDC( dc
);
3969 wxRegion reg
= GetUpdateRegion();
3970 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
3971 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
3973 #if WXGRID_DRAW_LINES
3974 m_owner
->DrawAllGridLines( dc
, reg
);
3977 m_owner
->DrawGridSpace( dc
);
3978 m_owner
->DrawHighlight( dc
, dirtyCells
);
3981 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3983 wxWindow::ScrollWindow( dx
, dy
, rect
);
3984 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3985 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3988 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3990 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
3993 m_owner
->ProcessGridCellMouseEvent( event
);
3996 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3998 m_owner
->GetEventHandler()->ProcessEvent( event
);
4001 // This seems to be required for wxMotif/wxGTK otherwise the mouse
4002 // cursor must be in the cell edit control to get key events
4004 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
4006 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4010 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
4012 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4016 void wxGridWindow::OnChar( wxKeyEvent
& event
)
4018 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4022 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
4026 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
4028 // and if we have any selection, it has to be repainted, because it
4029 // uses different colour when the grid is not focused:
4030 if ( m_owner
->IsSelection() )
4036 // NB: Note that this code is in "else" branch only because the other
4037 // branch refreshes everything and so there's no point in calling
4038 // Refresh() again, *not* because it should only be done if
4039 // !IsSelection(). If the above code is ever optimized to refresh
4040 // only selected area, this needs to be moved out of the "else"
4041 // branch so that it's always executed.
4043 // current cell cursor {dis,re}appears on focus change:
4044 const wxGridCellCoords
cursorCoords(m_owner
->GetGridCursorRow(),
4045 m_owner
->GetGridCursorCol());
4046 const wxRect cursor
=
4047 m_owner
->BlockToDeviceRect(cursorCoords
, cursorCoords
);
4048 Refresh(true, &cursor
);
4051 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4055 //////////////////////////////////////////////////////////////////////
4057 // Internal Helper function for computing row or column from some
4058 // (unscrolled) coordinate value, using either
4059 // m_defaultRowHeight/m_defaultColWidth or binary search on array
4060 // of m_rowBottoms/m_ColRights to speed up the search!
4062 // Internal helper macros for simpler use of that function
4064 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
4065 const wxArrayInt
& BorderArray
, int nMax
,
4068 #define internalXToCol(x) XToCol(x, true)
4069 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
4070 m_minAcceptableRowHeight, \
4071 m_rowBottoms, m_numRows, true)
4073 /////////////////////////////////////////////////////////////////////
4075 #if wxUSE_EXTENDED_RTTI
4076 WX_DEFINE_FLAGS( wxGridStyle
)
4078 wxBEGIN_FLAGS( wxGridStyle
)
4079 // new style border flags, we put them first to
4080 // use them for streaming out
4081 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4082 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4083 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4084 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4085 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4086 wxFLAGS_MEMBER(wxBORDER_NONE
)
4088 // old style border flags
4089 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4090 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4091 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4092 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4093 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4094 wxFLAGS_MEMBER(wxBORDER
)
4096 // standard window styles
4097 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4098 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4099 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4100 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4101 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4102 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4103 wxFLAGS_MEMBER(wxVSCROLL
)
4104 wxFLAGS_MEMBER(wxHSCROLL
)
4106 wxEND_FLAGS( wxGridStyle
)
4108 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4110 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4111 wxHIDE_PROPERTY( Children
)
4112 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4113 wxEND_PROPERTIES_TABLE()
4115 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4116 wxEND_HANDLERS_TABLE()
4118 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4121 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
4124 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4127 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4128 EVT_PAINT( wxGrid::OnPaint
)
4129 EVT_SIZE( wxGrid::OnSize
)
4130 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
4131 EVT_KEY_UP( wxGrid::OnKeyUp
)
4132 EVT_CHAR ( wxGrid::OnChar
)
4133 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
4141 wxGrid::wxGrid( wxWindow
*parent
,
4146 const wxString
& name
)
4149 Create(parent
, id
, pos
, size
, style
, name
);
4152 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
4153 const wxPoint
& pos
, const wxSize
& size
,
4154 long style
, const wxString
& name
)
4156 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
4157 style
| wxWANTS_CHARS
, name
))
4160 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4161 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4164 SetInitialSize(size
);
4172 // Must do this or ~wxScrollHelper will pop the wrong event handler
4173 SetTargetWindow(this);
4175 wxSafeDecRef(m_defaultCellAttr
);
4177 #ifdef DEBUG_ATTR_CACHE
4178 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4179 wxPrintf(_T("wxGrid attribute cache statistics: "
4180 "total: %u, hits: %u (%u%%)\n"),
4181 total
, gs_nAttrCacheHits
,
4182 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4185 // if we own the table, just delete it, otherwise at least don't leave it
4186 // with dangling view pointer
4189 else if ( m_table
&& m_table
->GetView() == this )
4190 m_table
->SetView(NULL
);
4192 delete m_typeRegistry
;
4197 // ----- internal init and update functions
4200 // NOTE: If using the default visual attributes works everywhere then this can
4201 // be removed as well as the #else cases below.
4202 #define _USE_VISATTR 0
4204 void wxGrid::Create()
4206 // create the type registry
4207 m_typeRegistry
= new wxGridTypeRegistry
;
4209 m_cellEditCtrlEnabled
= false;
4211 m_defaultCellAttr
= new wxGridCellAttr();
4213 // Set default cell attributes
4214 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4215 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4216 m_defaultCellAttr
->SetFont(GetFont());
4217 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4218 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4219 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4222 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4223 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4225 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4226 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4229 m_defaultCellAttr
->SetTextColour(
4230 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4231 m_defaultCellAttr
->SetBackgroundColour(
4232 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4237 m_currentCellCoords
= wxGridNoCellCoords
;
4239 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4240 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4242 // subwindow components that make up the wxGrid
4243 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4248 m_colLabelWin
= new wxGridColLabelWindow( this,
4253 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4258 m_gridWin
= new wxGridWindow( this,
4265 SetTargetWindow( m_gridWin
);
4268 wxColour gfg
= gva
.colFg
;
4269 wxColour gbg
= gva
.colBg
;
4270 wxColour lfg
= lva
.colFg
;
4271 wxColour lbg
= lva
.colBg
;
4273 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4274 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4275 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4276 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4279 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4280 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4281 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4282 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4283 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4284 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4286 m_gridWin
->SetOwnForegroundColour(gfg
);
4287 m_gridWin
->SetOwnBackgroundColour(gbg
);
4292 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4293 wxGrid::wxGridSelectionModes selmode
)
4295 wxCHECK_MSG( !m_created
,
4297 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4299 m_numRows
= numRows
;
4300 m_numCols
= numCols
;
4302 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4303 m_table
->SetView( this );
4305 m_selection
= new wxGridSelection( this, selmode
);
4314 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4316 wxCHECK_RET( m_created
,
4317 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4319 m_selection
->SetSelectionMode( selmode
);
4322 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4324 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4325 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4327 return m_selection
->GetSelectionMode();
4330 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4331 wxGrid::wxGridSelectionModes selmode
)
4333 bool checkSelection
= false;
4336 // stop all processing
4341 m_table
->SetView(0);
4353 checkSelection
= true;
4355 // kill row and column size arrays
4356 m_colWidths
.Empty();
4357 m_colRights
.Empty();
4358 m_rowHeights
.Empty();
4359 m_rowBottoms
.Empty();
4364 m_numRows
= table
->GetNumberRows();
4365 m_numCols
= table
->GetNumberCols();
4368 m_table
->SetView( this );
4369 m_ownTable
= takeOwnership
;
4370 m_selection
= new wxGridSelection( this, selmode
);
4373 // If the newly set table is smaller than the
4374 // original one current cell and selection regions
4375 // might be invalid,
4376 m_selectingKeyboard
= wxGridNoCellCoords
;
4377 m_currentCellCoords
=
4378 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
4379 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
4380 if (m_selectingTopLeft
.GetRow() >= m_numRows
||
4381 m_selectingTopLeft
.GetCol() >= m_numCols
)
4383 m_selectingTopLeft
= wxGridNoCellCoords
;
4384 m_selectingBottomRight
= wxGridNoCellCoords
;
4387 m_selectingBottomRight
=
4388 wxGridCellCoords(wxMin(m_numRows
,
4389 m_selectingBottomRight
.GetRow()),
4391 m_selectingBottomRight
.GetCol()));
4401 void wxGrid::InitVars()
4405 m_cornerLabelWin
= NULL
;
4406 m_rowLabelWin
= NULL
;
4407 m_colLabelWin
= NULL
;
4414 m_defaultCellAttr
= NULL
;
4415 m_typeRegistry
= NULL
;
4416 m_winCapture
= NULL
;
4421 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4422 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4424 if ( m_rowLabelWin
)
4426 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4430 m_labelBackgroundColour
= *wxWHITE
;
4433 m_labelTextColour
= *wxBLACK
;
4436 m_attrCache
.row
= -1;
4437 m_attrCache
.col
= -1;
4438 m_attrCache
.attr
= NULL
;
4440 // TODO: something better than this ?
4442 m_labelFont
= this->GetFont();
4443 m_labelFont
.SetWeight( wxBOLD
);
4445 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4446 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4448 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4449 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4450 m_colLabelTextOrientation
= wxHORIZONTAL
;
4452 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4453 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4455 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4456 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4458 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4459 m_defaultRowHeight
+= 8;
4461 m_defaultRowHeight
+= 4;
4464 m_gridLineColour
= wxColour( 192,192,192 );
4465 m_gridLinesEnabled
= true;
4466 m_cellHighlightColour
= *wxBLACK
;
4467 m_cellHighlightPenWidth
= 2;
4468 m_cellHighlightROPenWidth
= 1;
4470 m_canDragColMove
= false;
4472 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4473 m_winCapture
= (wxWindow
*)NULL
;
4474 m_canDragRowSize
= true;
4475 m_canDragColSize
= true;
4476 m_canDragGridSize
= true;
4477 m_canDragCell
= false;
4479 m_dragRowOrCol
= -1;
4480 m_isDragging
= false;
4481 m_startDragPos
= wxDefaultPosition
;
4482 m_nativeColumnLabels
= false;
4484 m_waitForSlowClick
= false;
4486 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4487 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4489 m_currentCellCoords
= wxGridNoCellCoords
;
4493 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4494 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4496 m_editable
= true; // default for whole grid
4498 m_inOnKeyDown
= false;
4504 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4505 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4508 // ----------------------------------------------------------------------------
4509 // the idea is to call these functions only when necessary because they create
4510 // quite big arrays which eat memory mostly unnecessary - in particular, if
4511 // default widths/heights are used for all rows/columns, we may not use these
4514 // with some extra code, it should be possible to only store the widths/heights
4515 // different from default ones (resulting in space savings for huge grids) but
4516 // this is not done currently
4517 // ----------------------------------------------------------------------------
4519 void wxGrid::InitRowHeights()
4521 m_rowHeights
.Empty();
4522 m_rowBottoms
.Empty();
4524 m_rowHeights
.Alloc( m_numRows
);
4525 m_rowBottoms
.Alloc( m_numRows
);
4527 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4530 for ( int i
= 0; i
< m_numRows
; i
++ )
4532 rowBottom
+= m_defaultRowHeight
;
4533 m_rowBottoms
.Add( rowBottom
);
4537 void wxGrid::InitColWidths()
4539 m_colWidths
.Empty();
4540 m_colRights
.Empty();
4542 m_colWidths
.Alloc( m_numCols
);
4543 m_colRights
.Alloc( m_numCols
);
4545 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4548 for ( int i
= 0; i
< m_numCols
; i
++ )
4550 colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
4551 m_colRights
.Add( colRight
);
4555 int wxGrid::GetColWidth(int col
) const
4557 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4560 int wxGrid::GetColLeft(int col
) const
4562 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
4563 : m_colRights
[col
] - m_colWidths
[col
];
4566 int wxGrid::GetColRight(int col
) const
4568 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
4572 int wxGrid::GetRowHeight(int row
) const
4574 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4577 int wxGrid::GetRowTop(int row
) const
4579 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4580 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4583 int wxGrid::GetRowBottom(int row
) const
4585 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4586 : m_rowBottoms
[row
];
4589 void wxGrid::CalcDimensions()
4591 // compute the size of the scrollable area
4592 int w
= m_numCols
> 0 ? GetColRight(GetColAt(m_numCols
- 1)) : 0;
4593 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
4598 // take into account editor if shown
4599 if ( IsCellEditControlShown() )
4602 int r
= m_currentCellCoords
.GetRow();
4603 int c
= m_currentCellCoords
.GetCol();
4604 int x
= GetColLeft(c
);
4605 int y
= GetRowTop(r
);
4607 // how big is the editor
4608 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4609 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4610 editor
->GetControl()->GetSize(&w2
, &h2
);
4621 // preserve (more or less) the previous position
4623 GetViewStart( &x
, &y
);
4625 // ensure the position is valid for the new scroll ranges
4627 x
= wxMax( w
- 1, 0 );
4629 y
= wxMax( h
- 1, 0 );
4631 // do set scrollbar parameters
4632 SetScrollbars( m_scrollLineX
, m_scrollLineY
,
4633 GetScrollX(w
), GetScrollY(h
),
4635 GetBatchCount() != 0);
4637 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4638 // still must reposition the children
4642 void wxGrid::CalcWindowSizes()
4644 // escape if the window is has not been fully created yet
4646 if ( m_cornerLabelWin
== NULL
)
4650 GetClientSize( &cw
, &ch
);
4652 // this block of code tries to work around the following problem: the grid
4653 // could have been just resized to have enough space to show the full grid
4654 // window contents without the scrollbars, but its client size could be
4655 // not big enough because the grid has the scrollbars right now and so the
4656 // scrollbars would remain even though we don't need them any more
4658 // to prevent this from happening, check if we have enough space for
4659 // everything without the scrollbars and explicitly disable them then
4660 wxSize size
= GetSize() - GetWindowBorderSize();
4661 if ( size
!= wxSize(cw
, ch
) )
4663 // check if we have enough space for grid window after accounting for
4664 // the fixed size elements
4665 size
.x
-= m_rowLabelWidth
;
4666 size
.y
-= m_colLabelHeight
;
4668 const wxSize vsize
= m_gridWin
->GetVirtualSize();
4670 if ( size
.x
>= vsize
.x
&& size
.y
>= vsize
.y
)
4672 // yes, we do, so remove the scrollbars and use the new client size
4673 // (which should be the same as full window size - borders now)
4674 SetScrollbars(0, 0, 0, 0);
4675 GetClientSize(&cw
, &ch
);
4679 // the grid may be too small to have enough space for the labels yet, don't
4680 // size the windows to negative sizes in this case
4681 int gw
= cw
- m_rowLabelWidth
;
4682 int gh
= ch
- m_colLabelHeight
;
4688 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4689 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4691 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4692 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, gw
, m_colLabelHeight
);
4694 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4695 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, gh
);
4697 if ( m_gridWin
&& m_gridWin
->IsShown() )
4698 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, gw
, gh
);
4701 // this is called when the grid table sends a message
4702 // to indicate that it has been redimensioned
4704 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4707 bool result
= false;
4709 // Clear the attribute cache as the attribute might refer to a different
4710 // cell than stored in the cache after adding/removing rows/columns.
4713 // By the same reasoning, the editor should be dismissed if columns are
4714 // added or removed. And for consistency, it should IMHO always be
4715 // removed, not only if the cell "underneath" it actually changes.
4716 // For now, I intentionally do not save the editor's content as the
4717 // cell it might want to save that stuff to might no longer exist.
4718 HideCellEditControl();
4721 // if we were using the default widths/heights so far, we must change them
4723 if ( m_colWidths
.IsEmpty() )
4728 if ( m_rowHeights
.IsEmpty() )
4734 switch ( msg
.GetId() )
4736 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4738 size_t pos
= msg
.GetCommandInt();
4739 int numRows
= msg
.GetCommandInt2();
4741 m_numRows
+= numRows
;
4743 if ( !m_rowHeights
.IsEmpty() )
4745 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4746 m_rowBottoms
.Insert( 0, pos
, numRows
);
4750 bottom
= m_rowBottoms
[pos
- 1];
4752 for ( i
= pos
; i
< m_numRows
; i
++ )
4754 bottom
+= m_rowHeights
[i
];
4755 m_rowBottoms
[i
] = bottom
;
4759 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4761 // if we have just inserted cols into an empty grid the current
4762 // cell will be undefined...
4764 SetCurrentCell( 0, 0 );
4768 m_selection
->UpdateRows( pos
, numRows
);
4769 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4771 attrProvider
->UpdateAttrRows( pos
, numRows
);
4773 if ( !GetBatchCount() )
4776 m_rowLabelWin
->Refresh();
4782 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4784 int numRows
= msg
.GetCommandInt();
4785 int oldNumRows
= m_numRows
;
4786 m_numRows
+= numRows
;
4788 if ( !m_rowHeights
.IsEmpty() )
4790 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4791 m_rowBottoms
.Add( 0, numRows
);
4794 if ( oldNumRows
> 0 )
4795 bottom
= m_rowBottoms
[oldNumRows
- 1];
4797 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4799 bottom
+= m_rowHeights
[i
];
4800 m_rowBottoms
[i
] = bottom
;
4804 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4806 // if we have just inserted cols into an empty grid the current
4807 // cell will be undefined...
4809 SetCurrentCell( 0, 0 );
4812 if ( !GetBatchCount() )
4815 m_rowLabelWin
->Refresh();
4821 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4823 size_t pos
= msg
.GetCommandInt();
4824 int numRows
= msg
.GetCommandInt2();
4825 m_numRows
-= numRows
;
4827 if ( !m_rowHeights
.IsEmpty() )
4829 m_rowHeights
.RemoveAt( pos
, numRows
);
4830 m_rowBottoms
.RemoveAt( pos
, numRows
);
4833 for ( i
= 0; i
< m_numRows
; i
++ )
4835 h
+= m_rowHeights
[i
];
4836 m_rowBottoms
[i
] = h
;
4842 m_currentCellCoords
= wxGridNoCellCoords
;
4846 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4847 m_currentCellCoords
.Set( 0, 0 );
4851 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4852 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4855 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4857 // ifdef'd out following patch from Paul Gammans
4859 // No need to touch column attributes, unless we
4860 // removed _all_ rows, in this case, we remove
4861 // all column attributes.
4862 // I hate to do this here, but the
4863 // needed data is not available inside UpdateAttrRows.
4864 if ( !GetNumberRows() )
4865 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4869 if ( !GetBatchCount() )
4872 m_rowLabelWin
->Refresh();
4878 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4880 size_t pos
= msg
.GetCommandInt();
4881 int numCols
= msg
.GetCommandInt2();
4882 m_numCols
+= numCols
;
4884 if ( !m_colAt
.IsEmpty() )
4886 //Shift the column IDs
4888 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
4890 if ( m_colAt
[i
] >= (int)pos
)
4891 m_colAt
[i
] += numCols
;
4894 m_colAt
.Insert( pos
, pos
, numCols
);
4896 //Set the new columns' positions
4897 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
4903 if ( !m_colWidths
.IsEmpty() )
4905 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4906 m_colRights
.Insert( 0, pos
, numCols
);
4910 right
= m_colRights
[GetColAt( pos
- 1 )];
4913 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
4915 i
= GetColAt( colPos
);
4917 right
+= m_colWidths
[i
];
4918 m_colRights
[i
] = right
;
4922 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4924 // if we have just inserted cols into an empty grid the current
4925 // cell will be undefined...
4927 SetCurrentCell( 0, 0 );
4931 m_selection
->UpdateCols( pos
, numCols
);
4932 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4934 attrProvider
->UpdateAttrCols( pos
, numCols
);
4935 if ( !GetBatchCount() )
4938 m_colLabelWin
->Refresh();
4944 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4946 int numCols
= msg
.GetCommandInt();
4947 int oldNumCols
= m_numCols
;
4948 m_numCols
+= numCols
;
4950 if ( !m_colAt
.IsEmpty() )
4952 m_colAt
.Add( 0, numCols
);
4954 //Set the new columns' positions
4956 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4962 if ( !m_colWidths
.IsEmpty() )
4964 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4965 m_colRights
.Add( 0, numCols
);
4968 if ( oldNumCols
> 0 )
4969 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
4972 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
4974 i
= GetColAt( colPos
);
4976 right
+= m_colWidths
[i
];
4977 m_colRights
[i
] = right
;
4981 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4983 // if we have just inserted cols into an empty grid the current
4984 // cell will be undefined...
4986 SetCurrentCell( 0, 0 );
4988 if ( !GetBatchCount() )
4991 m_colLabelWin
->Refresh();
4997 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4999 size_t pos
= msg
.GetCommandInt();
5000 int numCols
= msg
.GetCommandInt2();
5001 m_numCols
-= numCols
;
5003 if ( !m_colAt
.IsEmpty() )
5005 int colID
= GetColAt( pos
);
5007 m_colAt
.RemoveAt( pos
, numCols
);
5009 //Shift the column IDs
5011 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5013 if ( m_colAt
[colPos
] > colID
)
5014 m_colAt
[colPos
] -= numCols
;
5018 if ( !m_colWidths
.IsEmpty() )
5020 m_colWidths
.RemoveAt( pos
, numCols
);
5021 m_colRights
.RemoveAt( pos
, numCols
);
5025 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5027 i
= GetColAt( colPos
);
5029 w
+= m_colWidths
[i
];
5036 m_currentCellCoords
= wxGridNoCellCoords
;
5040 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
5041 m_currentCellCoords
.Set( 0, 0 );
5045 m_selection
->UpdateCols( pos
, -((int)numCols
) );
5046 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5049 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
5051 // ifdef'd out following patch from Paul Gammans
5053 // No need to touch row attributes, unless we
5054 // removed _all_ columns, in this case, we remove
5055 // all row attributes.
5056 // I hate to do this here, but the
5057 // needed data is not available inside UpdateAttrCols.
5058 if ( !GetNumberCols() )
5059 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
5063 if ( !GetBatchCount() )
5066 m_colLabelWin
->Refresh();
5073 if (result
&& !GetBatchCount() )
5074 m_gridWin
->Refresh();
5079 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
) const
5081 wxRegionIterator
iter( reg
);
5084 wxArrayInt rowlabels
;
5091 // TODO: remove this when we can...
5092 // There is a bug in wxMotif that gives garbage update
5093 // rectangles if you jump-scroll a long way by clicking the
5094 // scrollbar with middle button. This is a work-around
5096 #if defined(__WXMOTIF__)
5098 m_gridWin
->GetClientSize( &cw
, &ch
);
5099 if ( r
.GetTop() > ch
)
5101 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5104 // logical bounds of update region
5107 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
5108 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
5110 // find the row labels within these bounds
5113 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5115 if ( GetRowBottom(row
) < top
)
5118 if ( GetRowTop(row
) > bottom
)
5121 rowlabels
.Add( row
);
5130 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
) const
5132 wxRegionIterator
iter( reg
);
5135 wxArrayInt colLabels
;
5142 // TODO: remove this when we can...
5143 // There is a bug in wxMotif that gives garbage update
5144 // rectangles if you jump-scroll a long way by clicking the
5145 // scrollbar with middle button. This is a work-around
5147 #if defined(__WXMOTIF__)
5149 m_gridWin
->GetClientSize( &cw
, &ch
);
5150 if ( r
.GetLeft() > cw
)
5152 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5155 // logical bounds of update region
5158 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
5159 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
5161 // find the cells within these bounds
5165 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5167 col
= GetColAt( colPos
);
5169 if ( GetColRight(col
) < left
)
5172 if ( GetColLeft(col
) > right
)
5175 colLabels
.Add( col
);
5184 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
) const
5186 wxRegionIterator
iter( reg
);
5189 wxGridCellCoordsArray cellsExposed
;
5191 int left
, top
, right
, bottom
;
5196 // TODO: remove this when we can...
5197 // There is a bug in wxMotif that gives garbage update
5198 // rectangles if you jump-scroll a long way by clicking the
5199 // scrollbar with middle button. This is a work-around
5201 #if defined(__WXMOTIF__)
5203 m_gridWin
->GetClientSize( &cw
, &ch
);
5204 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5205 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5206 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5207 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5210 // logical bounds of update region
5212 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5213 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5215 // find the cells within these bounds
5218 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5220 if ( GetRowBottom(row
) <= top
)
5223 if ( GetRowTop(row
) > bottom
)
5227 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5229 col
= GetColAt( colPos
);
5231 if ( GetColRight(col
) <= left
)
5234 if ( GetColLeft(col
) > right
)
5237 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
5244 return cellsExposed
;
5248 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5251 wxPoint
pos( event
.GetPosition() );
5252 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5254 if ( event
.Dragging() )
5258 m_isDragging
= true;
5259 m_rowLabelWin
->CaptureMouse();
5262 if ( event
.LeftIsDown() )
5264 switch ( m_cursorMode
)
5266 case WXGRID_CURSOR_RESIZE_ROW
:
5268 int cw
, ch
, left
, dummy
;
5269 m_gridWin
->GetClientSize( &cw
, &ch
);
5270 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5272 wxClientDC
dc( m_gridWin
);
5275 GetRowTop(m_dragRowOrCol
) +
5276 GetRowMinimalHeight(m_dragRowOrCol
) );
5277 dc
.SetLogicalFunction(wxINVERT
);
5278 if ( m_dragLastPos
>= 0 )
5280 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5282 dc
.DrawLine( left
, y
, left
+cw
, y
);
5287 case WXGRID_CURSOR_SELECT_ROW
:
5289 if ( (row
= YToRow( y
)) >= 0 )
5293 m_selection
->SelectRow( row
,
5294 event
.ControlDown(),
5303 // default label to suppress warnings about "enumeration value
5304 // 'xxx' not handled in switch
5312 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5317 if (m_rowLabelWin
->HasCapture())
5318 m_rowLabelWin
->ReleaseMouse();
5319 m_isDragging
= false;
5322 // ------------ Entering or leaving the window
5324 if ( event
.Entering() || event
.Leaving() )
5326 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5329 // ------------ Left button pressed
5331 else if ( event
.LeftDown() )
5333 // don't send a label click event for a hit on the
5334 // edge of the row label - this is probably the user
5335 // wanting to resize the row
5337 if ( YToEdgeOfRow(y
) < 0 )
5341 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5343 if ( !event
.ShiftDown() && !event
.CmdDown() )
5347 if ( event
.ShiftDown() )
5349 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5352 GetNumberCols() - 1,
5353 event
.ControlDown(),
5360 m_selection
->SelectRow( row
,
5361 event
.ControlDown(),
5368 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5373 // starting to drag-resize a row
5374 if ( CanDragRowSize() )
5375 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5379 // ------------ Left double click
5381 else if (event
.LeftDClick() )
5383 row
= YToEdgeOfRow(y
);
5388 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5390 // no default action at the moment
5395 // adjust row height depending on label text
5396 AutoSizeRowLabelSize( row
);
5398 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5403 // ------------ Left button released
5405 else if ( event
.LeftUp() )
5407 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5409 DoEndDragResizeRow();
5411 // Note: we are ending the event *after* doing
5412 // default processing in this case
5414 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5417 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5421 // ------------ Right button down
5423 else if ( event
.RightDown() )
5427 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5429 // no default action at the moment
5433 // ------------ Right double click
5435 else if ( event
.RightDClick() )
5439 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5441 // no default action at the moment
5445 // ------------ No buttons down and mouse moving
5447 else if ( event
.Moving() )
5449 m_dragRowOrCol
= YToEdgeOfRow( y
);
5450 if ( m_dragRowOrCol
>= 0 )
5452 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5454 // don't capture the mouse yet
5455 if ( CanDragRowSize() )
5456 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5459 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5461 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5466 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5469 wxPoint
pos( event
.GetPosition() );
5470 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5472 if ( event
.Dragging() )
5476 m_isDragging
= true;
5477 m_colLabelWin
->CaptureMouse();
5479 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
)
5480 m_dragRowOrCol
= XToCol( x
);
5483 if ( event
.LeftIsDown() )
5485 switch ( m_cursorMode
)
5487 case WXGRID_CURSOR_RESIZE_COL
:
5489 int cw
, ch
, dummy
, top
;
5490 m_gridWin
->GetClientSize( &cw
, &ch
);
5491 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5493 wxClientDC
dc( m_gridWin
);
5496 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5497 GetColMinimalWidth(m_dragRowOrCol
));
5498 dc
.SetLogicalFunction(wxINVERT
);
5499 if ( m_dragLastPos
>= 0 )
5501 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5503 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5508 case WXGRID_CURSOR_SELECT_COL
:
5510 if ( (col
= XToCol( x
)) >= 0 )
5514 m_selection
->SelectCol( col
,
5515 event
.ControlDown(),
5524 case WXGRID_CURSOR_MOVE_COL
:
5527 m_moveToCol
= GetColAt( 0 );
5529 m_moveToCol
= XToCol( x
);
5533 if ( m_moveToCol
< 0 )
5534 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5536 markerX
= GetColLeft( m_moveToCol
);
5538 if ( markerX
!= m_dragLastPos
)
5540 wxClientDC
dc( m_colLabelWin
);
5543 m_colLabelWin
->GetClientSize( &cw
, &ch
);
5547 //Clean up the last indicator
5548 if ( m_dragLastPos
>= 0 )
5550 wxPen
pen( m_colLabelWin
->GetBackgroundColour(), 2 );
5552 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
5553 dc
.SetPen(wxNullPen
);
5555 if ( XToCol( m_dragLastPos
) != -1 )
5556 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
5559 //Moving to the same place? Don't draw a marker
5560 if ( (m_moveToCol
== m_dragRowOrCol
)
5561 || (GetColPos( m_moveToCol
) == GetColPos( m_dragRowOrCol
) + 1)
5562 || (m_moveToCol
< 0 && m_dragRowOrCol
== GetColAt( m_numCols
- 1 )))
5569 wxPen
pen( *wxBLUE
, 2 );
5572 dc
.DrawLine( markerX
, 0, markerX
, ch
);
5574 dc
.SetPen(wxNullPen
);
5576 m_dragLastPos
= markerX
- 1;
5581 // default label to suppress warnings about "enumeration value
5582 // 'xxx' not handled in switch
5590 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5595 if (m_colLabelWin
->HasCapture())
5596 m_colLabelWin
->ReleaseMouse();
5597 m_isDragging
= false;
5600 // ------------ Entering or leaving the window
5602 if ( event
.Entering() || event
.Leaving() )
5604 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5607 // ------------ Left button pressed
5609 else if ( event
.LeftDown() )
5611 // don't send a label click event for a hit on the
5612 // edge of the col label - this is probably the user
5613 // wanting to resize the col
5615 if ( XToEdgeOfCol(x
) < 0 )
5619 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5621 if ( m_canDragColMove
)
5623 //Show button as pressed
5624 wxClientDC
dc( m_colLabelWin
);
5625 int colLeft
= GetColLeft( col
);
5626 int colRight
= GetColRight( col
) - 1;
5627 dc
.SetPen( wxPen( m_colLabelWin
->GetBackgroundColour(), 1 ) );
5628 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
5629 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
5631 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, m_colLabelWin
);
5635 if ( !event
.ShiftDown() && !event
.CmdDown() )
5639 if ( event
.ShiftDown() )
5641 m_selection
->SelectBlock( 0,
5642 m_currentCellCoords
.GetCol(),
5643 GetNumberRows() - 1, col
,
5644 event
.ControlDown(),
5651 m_selection
->SelectCol( col
,
5652 event
.ControlDown(),
5659 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5665 // starting to drag-resize a col
5667 if ( CanDragColSize() )
5668 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5672 // ------------ Left double click
5674 if ( event
.LeftDClick() )
5676 col
= XToEdgeOfCol(x
);
5681 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5683 // no default action at the moment
5688 // adjust column width depending on label text
5689 AutoSizeColLabelSize( col
);
5691 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5696 // ------------ Left button released
5698 else if ( event
.LeftUp() )
5700 switch ( m_cursorMode
)
5702 case WXGRID_CURSOR_RESIZE_COL
:
5703 DoEndDragResizeCol();
5705 // Note: we are ending the event *after* doing
5706 // default processing in this case
5708 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5711 case WXGRID_CURSOR_MOVE_COL
:
5714 SendEvent( wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
, event
);
5717 case WXGRID_CURSOR_SELECT_COL
:
5718 case WXGRID_CURSOR_SELECT_CELL
:
5719 case WXGRID_CURSOR_RESIZE_ROW
:
5720 case WXGRID_CURSOR_SELECT_ROW
:
5721 // nothing to do (?)
5725 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5729 // ------------ Right button down
5731 else if ( event
.RightDown() )
5735 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5737 // no default action at the moment
5741 // ------------ Right double click
5743 else if ( event
.RightDClick() )
5747 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5749 // no default action at the moment
5753 // ------------ No buttons down and mouse moving
5755 else if ( event
.Moving() )
5757 m_dragRowOrCol
= XToEdgeOfCol( x
);
5758 if ( m_dragRowOrCol
>= 0 )
5760 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5762 // don't capture the cursor yet
5763 if ( CanDragColSize() )
5764 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5767 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5769 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5774 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5776 if ( event
.LeftDown() )
5778 // indicate corner label by having both row and
5781 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5786 else if ( event
.LeftDClick() )
5788 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5790 else if ( event
.RightDown() )
5792 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5794 // no default action at the moment
5797 else if ( event
.RightDClick() )
5799 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5801 // no default action at the moment
5806 void wxGrid::CancelMouseCapture()
5808 // cancel operation currently in progress, whatever it is
5811 m_isDragging
= false;
5812 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
5813 m_winCapture
->SetCursor( *wxSTANDARD_CURSOR
);
5814 m_winCapture
= NULL
;
5816 // remove traces of whatever we drew on screen
5821 void wxGrid::ChangeCursorMode(CursorMode mode
,
5826 static const wxChar
*cursorModes
[] =
5836 wxLogTrace(_T("grid"),
5837 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5838 win
== m_colLabelWin
? _T("colLabelWin")
5839 : win
? _T("rowLabelWin")
5841 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5844 if ( mode
== m_cursorMode
&&
5845 win
== m_winCapture
&&
5846 captureMouse
== (m_winCapture
!= NULL
))
5851 // by default use the grid itself
5857 if (m_winCapture
->HasCapture())
5858 m_winCapture
->ReleaseMouse();
5859 m_winCapture
= (wxWindow
*)NULL
;
5862 m_cursorMode
= mode
;
5864 switch ( m_cursorMode
)
5866 case WXGRID_CURSOR_RESIZE_ROW
:
5867 win
->SetCursor( m_rowResizeCursor
);
5870 case WXGRID_CURSOR_RESIZE_COL
:
5871 win
->SetCursor( m_colResizeCursor
);
5874 case WXGRID_CURSOR_MOVE_COL
:
5875 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
5879 win
->SetCursor( *wxSTANDARD_CURSOR
);
5883 // we need to capture mouse when resizing
5884 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5885 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5887 if ( captureMouse
&& resize
)
5889 win
->CaptureMouse();
5894 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5897 wxPoint
pos( event
.GetPosition() );
5898 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5900 wxGridCellCoords coords
;
5901 XYToCell( x
, y
, coords
);
5903 int cell_rows
, cell_cols
;
5904 bool isFirstDrag
= !m_isDragging
;
5905 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5906 if ((cell_rows
< 0) || (cell_cols
< 0))
5908 coords
.SetRow(coords
.GetRow() + cell_rows
);
5909 coords
.SetCol(coords
.GetCol() + cell_cols
);
5912 if ( event
.Dragging() )
5914 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5916 // Don't start doing anything until the mouse has been dragged at
5917 // least 3 pixels in any direction...
5920 if (m_startDragPos
== wxDefaultPosition
)
5922 m_startDragPos
= pos
;
5925 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5929 m_isDragging
= true;
5930 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5932 // Hide the edit control, so it
5933 // won't interfere with drag-shrinking.
5934 if ( IsCellEditControlShown() )
5936 HideCellEditControl();
5937 SaveEditControlValue();
5940 if ( coords
!= wxGridNoCellCoords
)
5942 if ( event
.CmdDown() )
5944 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5945 m_selectingKeyboard
= coords
;
5946 HighlightBlock( m_selectingKeyboard
, coords
);
5948 else if ( CanDragCell() )
5952 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5953 m_selectingKeyboard
= coords
;
5955 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5964 if ( !IsSelection() )
5966 HighlightBlock( coords
, coords
);
5970 HighlightBlock( m_currentCellCoords
, coords
);
5974 if (! IsVisible(coords
))
5976 MakeCellVisible(coords
);
5977 // TODO: need to introduce a delay or something here. The
5978 // scrolling is way to fast, at least on MSW - also on GTK.
5981 // Have we captured the mouse yet?
5984 m_winCapture
= m_gridWin
;
5985 m_winCapture
->CaptureMouse();
5990 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5992 int cw
, ch
, left
, dummy
;
5993 m_gridWin
->GetClientSize( &cw
, &ch
);
5994 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5996 wxClientDC
dc( m_gridWin
);
5998 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5999 GetRowMinimalHeight(m_dragRowOrCol
) );
6000 dc
.SetLogicalFunction(wxINVERT
);
6001 if ( m_dragLastPos
>= 0 )
6003 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
6005 dc
.DrawLine( left
, y
, left
+cw
, y
);
6008 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6010 int cw
, ch
, dummy
, top
;
6011 m_gridWin
->GetClientSize( &cw
, &ch
);
6012 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6014 wxClientDC
dc( m_gridWin
);
6016 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
6017 GetColMinimalWidth(m_dragRowOrCol
) );
6018 dc
.SetLogicalFunction(wxINVERT
);
6019 if ( m_dragLastPos
>= 0 )
6021 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6023 dc
.DrawLine( x
, top
, x
, top
+ ch
);
6030 m_isDragging
= false;
6031 m_startDragPos
= wxDefaultPosition
;
6033 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
6034 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
6037 if ( event
.Entering() || event
.Leaving() )
6039 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6040 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
6045 // ------------ Left button pressed
6047 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
6049 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
6054 if ( !event
.CmdDown() )
6056 if ( event
.ShiftDown() )
6060 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
6061 m_currentCellCoords
.GetCol(),
6064 event
.ControlDown(),
6070 else if ( XToEdgeOfCol(x
) < 0 &&
6071 YToEdgeOfRow(y
) < 0 )
6073 DisableCellEditControl();
6074 MakeCellVisible( coords
);
6076 if ( event
.CmdDown() )
6080 m_selection
->ToggleCellSelection( coords
.GetRow(),
6082 event
.ControlDown(),
6087 m_selectingTopLeft
= wxGridNoCellCoords
;
6088 m_selectingBottomRight
= wxGridNoCellCoords
;
6089 m_selectingKeyboard
= coords
;
6093 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
6094 SetCurrentCell( coords
);
6097 if ( m_selection
->GetSelectionMode() !=
6098 wxGrid::wxGridSelectCells
)
6100 HighlightBlock( coords
, coords
);
6108 // ------------ Left double click
6110 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
6112 DisableCellEditControl();
6114 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
6116 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
6121 // we want double click to select a cell and start editing
6122 // (i.e. to behave in same way as sequence of two slow clicks):
6123 m_waitForSlowClick
= true;
6128 // ------------ Left button released
6130 else if ( event
.LeftUp() )
6132 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6136 if (m_winCapture
->HasCapture())
6137 m_winCapture
->ReleaseMouse();
6138 m_winCapture
= NULL
;
6141 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
6144 EnableCellEditControl();
6146 wxGridCellAttr
*attr
= GetCellAttr(coords
);
6147 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
6148 editor
->StartingClick();
6152 m_waitForSlowClick
= false;
6154 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6155 m_selectingBottomRight
!= wxGridNoCellCoords
)
6159 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6160 m_selectingTopLeft
.GetCol(),
6161 m_selectingBottomRight
.GetRow(),
6162 m_selectingBottomRight
.GetCol(),
6163 event
.ControlDown(),
6169 m_selectingTopLeft
= wxGridNoCellCoords
;
6170 m_selectingBottomRight
= wxGridNoCellCoords
;
6172 // Show the edit control, if it has been hidden for
6174 ShowCellEditControl();
6177 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6179 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6180 DoEndDragResizeRow();
6182 // Note: we are ending the event *after* doing
6183 // default processing in this case
6185 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
6187 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6189 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6190 DoEndDragResizeCol();
6192 // Note: we are ending the event *after* doing
6193 // default processing in this case
6195 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
6201 // ------------ Right button down
6203 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
6205 DisableCellEditControl();
6206 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
6211 // no default action at the moment
6215 // ------------ Right double click
6217 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
6219 DisableCellEditControl();
6220 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
6225 // no default action at the moment
6229 // ------------ Moving and no button action
6231 else if ( event
.Moving() && !event
.IsButton() )
6233 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
6235 // out of grid cell area
6236 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6240 int dragRow
= YToEdgeOfRow( y
);
6241 int dragCol
= XToEdgeOfCol( x
);
6243 // Dragging on the corner of a cell to resize in both
6244 // directions is not implemented yet...
6246 if ( dragRow
>= 0 && dragCol
>= 0 )
6248 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6254 m_dragRowOrCol
= dragRow
;
6256 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6258 if ( CanDragRowSize() && CanDragGridSize() )
6259 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false);
6262 else if ( dragCol
>= 0 )
6264 m_dragRowOrCol
= dragCol
;
6266 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6268 if ( CanDragColSize() && CanDragGridSize() )
6269 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false);
6272 else // Neither on a row or col edge
6274 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6276 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6282 void wxGrid::DoEndDragResizeRow()
6284 if ( m_dragLastPos
>= 0 )
6286 // erase the last line and resize the row
6288 int cw
, ch
, left
, dummy
;
6289 m_gridWin
->GetClientSize( &cw
, &ch
);
6290 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6292 wxClientDC
dc( m_gridWin
);
6294 dc
.SetLogicalFunction( wxINVERT
);
6295 dc
.DrawLine( left
, m_dragLastPos
, left
+ cw
, m_dragLastPos
);
6296 HideCellEditControl();
6297 SaveEditControlValue();
6299 int rowTop
= GetRowTop(m_dragRowOrCol
);
6300 SetRowSize( m_dragRowOrCol
,
6301 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
6303 if ( !GetBatchCount() )
6305 // Only needed to get the correct rect.y:
6306 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
6308 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6309 rect
.width
= m_rowLabelWidth
;
6310 rect
.height
= ch
- rect
.y
;
6311 m_rowLabelWin
->Refresh( true, &rect
);
6314 // if there is a multicell block, paint all of it
6317 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
6318 int leftCol
= XToCol(left
);
6319 int rightCol
= internalXToCol(left
+ cw
);
6322 for (i
=leftCol
; i
<rightCol
; i
++)
6324 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
6325 if (cell_rows
< subtract_rows
)
6326 subtract_rows
= cell_rows
;
6328 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
6329 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6330 rect
.height
= ch
- rect
.y
;
6333 m_gridWin
->Refresh( false, &rect
);
6336 ShowCellEditControl();
6341 void wxGrid::DoEndDragResizeCol()
6343 if ( m_dragLastPos
>= 0 )
6345 // erase the last line and resize the col
6347 int cw
, ch
, dummy
, top
;
6348 m_gridWin
->GetClientSize( &cw
, &ch
);
6349 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6351 wxClientDC
dc( m_gridWin
);
6353 dc
.SetLogicalFunction( wxINVERT
);
6354 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6355 HideCellEditControl();
6356 SaveEditControlValue();
6358 int colLeft
= GetColLeft(m_dragRowOrCol
);
6359 SetColSize( m_dragRowOrCol
,
6360 wxMax( m_dragLastPos
- colLeft
,
6361 GetColMinimalWidth(m_dragRowOrCol
) ) );
6363 if ( !GetBatchCount() )
6365 // Only needed to get the correct rect.x:
6366 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
6368 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6369 rect
.width
= cw
- rect
.x
;
6370 rect
.height
= m_colLabelHeight
;
6371 m_colLabelWin
->Refresh( true, &rect
);
6374 // if there is a multicell block, paint all of it
6377 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
6378 int topRow
= YToRow(top
);
6379 int bottomRow
= internalYToRow(top
+ cw
);
6382 for (i
=topRow
; i
<bottomRow
; i
++)
6384 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
6385 if (cell_cols
< subtract_cols
)
6386 subtract_cols
= cell_cols
;
6389 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
6390 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6391 rect
.width
= cw
- rect
.x
;
6395 m_gridWin
->Refresh( false, &rect
);
6398 ShowCellEditControl();
6402 void wxGrid::DoEndDragMoveCol()
6404 //The user clicked on the column but didn't actually drag
6405 if ( m_dragLastPos
< 0 )
6407 m_colLabelWin
->Refresh(); //Do this to "unpress" the column
6412 if ( m_moveToCol
== -1 )
6413 newPos
= m_numCols
- 1;
6416 newPos
= GetColPos( m_moveToCol
);
6417 if ( newPos
> GetColPos( m_dragRowOrCol
) )
6421 SetColPos( m_dragRowOrCol
, newPos
);
6424 void wxGrid::SetColPos( int colID
, int newPos
)
6426 if ( m_colAt
.IsEmpty() )
6428 m_colAt
.Alloc( m_numCols
);
6431 for ( i
= 0; i
< m_numCols
; i
++ )
6437 int oldPos
= GetColPos( colID
);
6439 //Reshuffle the m_colAt array
6440 if ( newPos
> oldPos
)
6443 for ( i
= oldPos
; i
< newPos
; i
++ )
6445 m_colAt
[i
] = m_colAt
[i
+1];
6451 for ( i
= oldPos
; i
> newPos
; i
-- )
6453 m_colAt
[i
] = m_colAt
[i
-1];
6457 m_colAt
[newPos
] = colID
;
6459 //Recalculate the column rights
6460 if ( !m_colWidths
.IsEmpty() )
6464 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6466 int colID
= GetColAt( colPos
);
6468 colRight
+= m_colWidths
[colID
];
6469 m_colRights
[colID
] = colRight
;
6473 m_colLabelWin
->Refresh();
6474 m_gridWin
->Refresh();
6479 void wxGrid::EnableDragColMove( bool enable
)
6481 if ( m_canDragColMove
== enable
)
6484 m_canDragColMove
= enable
;
6486 if ( !m_canDragColMove
)
6490 //Recalculate the column rights
6491 if ( !m_colWidths
.IsEmpty() )
6495 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6497 colRight
+= m_colWidths
[colPos
];
6498 m_colRights
[colPos
] = colRight
;
6502 m_colLabelWin
->Refresh();
6503 m_gridWin
->Refresh();
6509 // ------ interaction with data model
6511 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6513 switch ( msg
.GetId() )
6515 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6516 return GetModelValues();
6518 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6519 return SetModelValues();
6521 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6522 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6523 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6524 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6525 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6526 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6527 return Redimension( msg
);
6534 // The behaviour of this function depends on the grid table class
6535 // Clear() function. For the default wxGridStringTable class the
6536 // behavious is to replace all cell contents with wxEmptyString but
6537 // not to change the number of rows or cols.
6539 void wxGrid::ClearGrid()
6543 if (IsCellEditControlEnabled())
6544 DisableCellEditControl();
6547 if (!GetBatchCount())
6548 m_gridWin
->Refresh();
6552 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6554 // TODO: something with updateLabels flag
6558 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6564 if (IsCellEditControlEnabled())
6565 DisableCellEditControl();
6567 bool done
= m_table
->InsertRows( pos
, numRows
);
6570 // the table will have sent the results of the insert row
6571 // operation to this view object as a grid table message
6577 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6579 // TODO: something with updateLabels flag
6583 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6589 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6592 // the table will have sent the results of the append row
6593 // operation to this view object as a grid table message
6599 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6601 // TODO: something with updateLabels flag
6605 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6611 if (IsCellEditControlEnabled())
6612 DisableCellEditControl();
6614 bool done
= m_table
->DeleteRows( pos
, numRows
);
6616 // the table will have sent the results of the delete row
6617 // operation to this view object as a grid table message
6623 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6625 // TODO: something with updateLabels flag
6629 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6635 if (IsCellEditControlEnabled())
6636 DisableCellEditControl();
6638 bool done
= m_table
->InsertCols( pos
, numCols
);
6640 // the table will have sent the results of the insert col
6641 // operation to this view object as a grid table message
6647 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6649 // TODO: something with updateLabels flag
6653 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6659 bool done
= m_table
->AppendCols( numCols
);
6661 // the table will have sent the results of the append col
6662 // operation to this view object as a grid table message
6668 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6670 // TODO: something with updateLabels flag
6674 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6680 if (IsCellEditControlEnabled())
6681 DisableCellEditControl();
6683 bool done
= m_table
->DeleteCols( pos
, numCols
);
6685 // the table will have sent the results of the delete col
6686 // operation to this view object as a grid table message
6693 // ----- event handlers
6696 // Generate a grid event based on a mouse event and
6697 // return the result of ProcessEvent()
6699 int wxGrid::SendEvent( const wxEventType type
,
6701 wxMouseEvent
& mouseEv
)
6703 bool claimed
, vetoed
;
6705 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6707 int rowOrCol
= (row
== -1 ? col
: row
);
6709 wxGridSizeEvent
gridEvt( GetId(),
6713 mouseEv
.GetX() + GetRowLabelSize(),
6714 mouseEv
.GetY() + GetColLabelSize(),
6715 mouseEv
.ControlDown(),
6716 mouseEv
.ShiftDown(),
6718 mouseEv
.MetaDown() );
6720 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6721 vetoed
= !gridEvt
.IsAllowed();
6723 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6725 // Right now, it should _never_ end up here!
6726 wxGridRangeSelectEvent
gridEvt( GetId(),
6730 m_selectingBottomRight
,
6732 mouseEv
.ControlDown(),
6733 mouseEv
.ShiftDown(),
6735 mouseEv
.MetaDown() );
6737 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6738 vetoed
= !gridEvt
.IsAllowed();
6740 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
6741 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
6742 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
6743 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
6745 wxPoint pos
= mouseEv
.GetPosition();
6747 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
6748 pos
.y
+= GetColLabelSize();
6749 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
6750 pos
.x
+= GetRowLabelSize();
6752 wxGridEvent
gridEvt( GetId(),
6759 mouseEv
.ControlDown(),
6760 mouseEv
.ShiftDown(),
6762 mouseEv
.MetaDown() );
6763 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6764 vetoed
= !gridEvt
.IsAllowed();
6768 wxGridEvent
gridEvt( GetId(),
6772 mouseEv
.GetX() + GetRowLabelSize(),
6773 mouseEv
.GetY() + GetColLabelSize(),
6775 mouseEv
.ControlDown(),
6776 mouseEv
.ShiftDown(),
6778 mouseEv
.MetaDown() );
6779 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6780 vetoed
= !gridEvt
.IsAllowed();
6783 // A Veto'd event may not be `claimed' so test this first
6787 return claimed
? 1 : 0;
6790 // Generate a grid event of specified type and return the result
6791 // of ProcessEvent().
6793 int wxGrid::SendEvent( const wxEventType type
,
6796 bool claimed
, vetoed
;
6798 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6800 int rowOrCol
= (row
== -1 ? col
: row
);
6802 wxGridSizeEvent
gridEvt( GetId(), type
, this, rowOrCol
);
6804 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6805 vetoed
= !gridEvt
.IsAllowed();
6809 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
6811 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6812 vetoed
= !gridEvt
.IsAllowed();
6815 // A Veto'd event may not be `claimed' so test this first
6819 return claimed
? 1 : 0;
6822 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6824 // needed to prevent zillions of paint events on MSW
6828 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6830 // Don't do anything if between Begin/EndBatch...
6831 // EndBatch() will do all this on the last nested one anyway.
6832 if ( m_created
&& !GetBatchCount() )
6834 // Refresh to get correct scrolled position:
6835 wxScrolledWindow::Refresh(eraseb
, rect
);
6839 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6840 int width_label
, width_cell
, height_label
, height_cell
;
6843 // Copy rectangle can get scroll offsets..
6844 rect_x
= rect
->GetX();
6845 rect_y
= rect
->GetY();
6846 rectWidth
= rect
->GetWidth();
6847 rectHeight
= rect
->GetHeight();
6849 width_label
= m_rowLabelWidth
- rect_x
;
6850 if (width_label
> rectWidth
)
6851 width_label
= rectWidth
;
6853 height_label
= m_colLabelHeight
- rect_y
;
6854 if (height_label
> rectHeight
)
6855 height_label
= rectHeight
;
6857 if (rect_x
> m_rowLabelWidth
)
6859 x
= rect_x
- m_rowLabelWidth
;
6860 width_cell
= rectWidth
;
6865 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6868 if (rect_y
> m_colLabelHeight
)
6870 y
= rect_y
- m_colLabelHeight
;
6871 height_cell
= rectHeight
;
6876 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6879 // Paint corner label part intersecting rect.
6880 if ( width_label
> 0 && height_label
> 0 )
6882 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6883 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6886 // Paint col labels part intersecting rect.
6887 if ( width_cell
> 0 && height_label
> 0 )
6889 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6890 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6893 // Paint row labels part intersecting rect.
6894 if ( width_label
> 0 && height_cell
> 0 )
6896 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6897 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6900 // Paint cell area part intersecting rect.
6901 if ( width_cell
> 0 && height_cell
> 0 )
6903 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6904 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6909 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6910 m_colLabelWin
->Refresh(eraseb
, NULL
);
6911 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6912 m_gridWin
->Refresh(eraseb
, NULL
);
6917 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
))
6919 if (m_targetWindow
!= this) // check whether initialisation has been done
6921 // update our children window positions and scrollbars
6926 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6928 if ( m_inOnKeyDown
)
6930 // shouldn't be here - we are going round in circles...
6932 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6935 m_inOnKeyDown
= true;
6937 // propagate the event up and see if it gets processed
6938 wxWindow
*parent
= GetParent();
6939 wxKeyEvent
keyEvt( event
);
6940 keyEvt
.SetEventObject( parent
);
6942 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6944 if (GetLayoutDirection() == wxLayout_RightToLeft
)
6946 if (event
.GetKeyCode() == WXK_RIGHT
)
6947 event
.m_keyCode
= WXK_LEFT
;
6948 else if (event
.GetKeyCode() == WXK_LEFT
)
6949 event
.m_keyCode
= WXK_RIGHT
;
6952 // try local handlers
6953 switch ( event
.GetKeyCode() )
6956 if ( event
.ControlDown() )
6957 MoveCursorUpBlock( event
.ShiftDown() );
6959 MoveCursorUp( event
.ShiftDown() );
6963 if ( event
.ControlDown() )
6964 MoveCursorDownBlock( event
.ShiftDown() );
6966 MoveCursorDown( event
.ShiftDown() );
6970 if ( event
.ControlDown() )
6971 MoveCursorLeftBlock( event
.ShiftDown() );
6973 MoveCursorLeft( event
.ShiftDown() );
6977 if ( event
.ControlDown() )
6978 MoveCursorRightBlock( event
.ShiftDown() );
6980 MoveCursorRight( event
.ShiftDown() );
6984 case WXK_NUMPAD_ENTER
:
6985 if ( event
.ControlDown() )
6987 event
.Skip(); // to let the edit control have the return
6991 if ( GetGridCursorRow() < GetNumberRows()-1 )
6993 MoveCursorDown( event
.ShiftDown() );
6997 // at the bottom of a column
6998 DisableCellEditControl();
7008 if (event
.ShiftDown())
7010 if ( GetGridCursorCol() > 0 )
7012 MoveCursorLeft( false );
7017 DisableCellEditControl();
7022 if ( GetGridCursorCol() < GetNumberCols() - 1 )
7024 MoveCursorRight( false );
7029 DisableCellEditControl();
7035 if ( event
.ControlDown() )
7037 MakeCellVisible( 0, 0 );
7038 SetCurrentCell( 0, 0 );
7047 if ( event
.ControlDown() )
7049 MakeCellVisible( m_numRows
- 1, m_numCols
- 1 );
7050 SetCurrentCell( m_numRows
- 1, m_numCols
- 1 );
7067 if ( event
.ControlDown() )
7071 m_selection
->ToggleCellSelection(
7072 m_currentCellCoords
.GetRow(),
7073 m_currentCellCoords
.GetCol(),
7074 event
.ControlDown(),
7082 if ( !IsEditable() )
7083 MoveCursorRight( false );
7094 m_inOnKeyDown
= false;
7097 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
7099 // try local handlers
7101 if ( event
.GetKeyCode() == WXK_SHIFT
)
7103 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
7104 m_selectingBottomRight
!= wxGridNoCellCoords
)
7108 m_selection
->SelectBlock(
7109 m_selectingTopLeft
.GetRow(),
7110 m_selectingTopLeft
.GetCol(),
7111 m_selectingBottomRight
.GetRow(),
7112 m_selectingBottomRight
.GetCol(),
7113 event
.ControlDown(),
7120 m_selectingTopLeft
= wxGridNoCellCoords
;
7121 m_selectingBottomRight
= wxGridNoCellCoords
;
7122 m_selectingKeyboard
= wxGridNoCellCoords
;
7126 void wxGrid::OnChar( wxKeyEvent
& event
)
7128 // is it possible to edit the current cell at all?
7129 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7131 // yes, now check whether the cells editor accepts the key
7132 int row
= m_currentCellCoords
.GetRow();
7133 int col
= m_currentCellCoords
.GetCol();
7134 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7135 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7137 // <F2> is special and will always start editing, for
7138 // other keys - ask the editor itself
7139 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
7140 || editor
->IsAcceptedKey(event
) )
7142 // ensure cell is visble
7143 MakeCellVisible(row
, col
);
7144 EnableCellEditControl();
7146 // a problem can arise if the cell is not completely
7147 // visible (even after calling MakeCellVisible the
7148 // control is not created and calling StartingKey will
7150 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
7151 editor
->StartingKey(event
);
7167 void wxGrid::OnEraseBackground(wxEraseEvent
&)
7171 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
7173 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
7175 // the event has been intercepted - do nothing
7179 #if !defined(__WXMAC__)
7180 wxClientDC
dc( m_gridWin
);
7184 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
7186 DisableCellEditControl();
7188 if ( IsVisible( m_currentCellCoords
, false ) )
7191 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
7192 if ( !m_gridLinesEnabled
)
7200 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
7202 // Otherwise refresh redraws the highlight!
7203 m_currentCellCoords
= coords
;
7205 #if defined(__WXMAC__)
7206 m_gridWin
->Refresh(true /*, & r */);
7208 DrawGridCellArea( dc
, cells
);
7209 DrawAllGridLines( dc
, r
);
7214 m_currentCellCoords
= coords
;
7216 wxGridCellAttr
*attr
= GetCellAttr( coords
);
7217 #if !defined(__WXMAC__)
7218 DrawCellHighlight( dc
, attr
);
7223 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7226 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7230 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
7233 rightCol
= GetNumberCols() - 1;
7235 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
7238 bottomRow
= GetNumberRows() - 1;
7242 if ( topRow
> bottomRow
)
7249 if ( leftCol
> rightCol
)
7256 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7257 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7259 // First the case that we selected a completely new area
7260 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
7261 m_selectingBottomRight
== wxGridNoCellCoords
)
7264 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
7265 wxGridCellCoords ( bottomRow
, rightCol
) );
7266 m_gridWin
->Refresh( false, &rect
);
7269 // Now handle changing an existing selection area.
7270 else if ( m_selectingTopLeft
!= updateTopLeft
||
7271 m_selectingBottomRight
!= updateBottomRight
)
7273 // Compute two optimal update rectangles:
7274 // Either one rectangle is a real subset of the
7275 // other, or they are (almost) disjoint!
7277 bool need_refresh
[4];
7281 need_refresh
[3] = false;
7284 // Store intermediate values
7285 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
7286 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
7287 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
7288 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
7290 // Determine the outer/inner coordinates.
7291 if (oldLeft
> leftCol
)
7297 if (oldTop
> topRow
)
7303 if (oldRight
< rightCol
)
7306 oldRight
= rightCol
;
7309 if (oldBottom
< bottomRow
)
7312 oldBottom
= bottomRow
;
7316 // Now, either the stuff marked old is the outer
7317 // rectangle or we don't have a situation where one
7318 // is contained in the other.
7320 if ( oldLeft
< leftCol
)
7322 // Refresh the newly selected or deselected
7323 // area to the left of the old or new selection.
7324 need_refresh
[0] = true;
7325 rect
[0] = BlockToDeviceRect(
7326 wxGridCellCoords( oldTop
, oldLeft
),
7327 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
7330 if ( oldTop
< topRow
)
7332 // Refresh the newly selected or deselected
7333 // area above the old or new selection.
7334 need_refresh
[1] = true;
7335 rect
[1] = BlockToDeviceRect(
7336 wxGridCellCoords( oldTop
, leftCol
),
7337 wxGridCellCoords( topRow
- 1, rightCol
) );
7340 if ( oldRight
> rightCol
)
7342 // Refresh the newly selected or deselected
7343 // area to the right of the old or new selection.
7344 need_refresh
[2] = true;
7345 rect
[2] = BlockToDeviceRect(
7346 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
7347 wxGridCellCoords( oldBottom
, oldRight
) );
7350 if ( oldBottom
> bottomRow
)
7352 // Refresh the newly selected or deselected
7353 // area below the old or new selection.
7354 need_refresh
[3] = true;
7355 rect
[3] = BlockToDeviceRect(
7356 wxGridCellCoords( bottomRow
+ 1, leftCol
),
7357 wxGridCellCoords( oldBottom
, rightCol
) );
7360 // various Refresh() calls
7361 for (i
= 0; i
< 4; i
++ )
7362 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7363 m_gridWin
->Refresh( false, &(rect
[i
]) );
7367 m_selectingTopLeft
= updateTopLeft
;
7368 m_selectingBottomRight
= updateBottomRight
;
7372 // ------ functions to get/send data (see also public functions)
7375 bool wxGrid::GetModelValues()
7377 // Hide the editor, so it won't hide a changed value.
7378 HideCellEditControl();
7382 // all we need to do is repaint the grid
7384 m_gridWin
->Refresh();
7391 bool wxGrid::SetModelValues()
7395 // Disable the editor, so it won't hide a changed value.
7396 // Do we also want to save the current value of the editor first?
7398 DisableCellEditControl();
7402 for ( row
= 0; row
< m_numRows
; row
++ )
7404 for ( col
= 0; col
< m_numCols
; col
++ )
7406 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
7416 // Note - this function only draws cells that are in the list of
7417 // exposed cells (usually set from the update region by
7418 // CalcExposedCells)
7420 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7422 if ( !m_numRows
|| !m_numCols
)
7425 int i
, numCells
= cells
.GetCount();
7426 int row
, col
, cell_rows
, cell_cols
;
7427 wxGridCellCoordsArray redrawCells
;
7429 for ( i
= numCells
- 1; i
>= 0; i
-- )
7431 row
= cells
[i
].GetRow();
7432 col
= cells
[i
].GetCol();
7433 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7435 // If this cell is part of a multicell block, find owner for repaint
7436 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7438 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
7439 bool marked
= false;
7440 for ( int j
= 0; j
< numCells
; j
++ )
7442 if ( cell
== cells
[j
] )
7451 int count
= redrawCells
.GetCount();
7452 for (int j
= 0; j
< count
; j
++)
7454 if ( cell
== redrawCells
[j
] )
7462 redrawCells
.Add( cell
);
7465 // don't bother drawing this cell
7469 // If this cell is empty, find cell to left that might want to overflow
7470 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7472 for ( int l
= 0; l
< cell_rows
; l
++ )
7474 // find a cell in this row to leave already marked for repaint
7476 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7477 if ((redrawCells
[k
].GetCol() < left
) &&
7478 (redrawCells
[k
].GetRow() == row
))
7480 left
= redrawCells
[k
].GetCol();
7484 left
= 0; // oh well
7486 for (int j
= col
- 1; j
>= left
; j
--)
7488 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7490 if (GetCellOverflow(row
+ l
, j
))
7492 wxGridCellCoords
cell(row
+ l
, j
);
7493 bool marked
= false;
7495 for (int k
= 0; k
< numCells
; k
++)
7497 if ( cell
== cells
[k
] )
7506 int count
= redrawCells
.GetCount();
7507 for (int k
= 0; k
< count
; k
++)
7509 if ( cell
== redrawCells
[k
] )
7516 redrawCells
.Add( cell
);
7525 DrawCell( dc
, cells
[i
] );
7528 numCells
= redrawCells
.GetCount();
7530 for ( i
= numCells
- 1; i
>= 0; i
-- )
7532 DrawCell( dc
, redrawCells
[i
] );
7536 void wxGrid::DrawGridSpace( wxDC
& dc
)
7539 m_gridWin
->GetClientSize( &cw
, &ch
);
7542 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7544 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
7545 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7547 if ( right
> rightCol
|| bottom
> bottomRow
)
7550 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7552 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxBRUSHSTYLE_SOLID
) );
7553 dc
.SetPen( *wxTRANSPARENT_PEN
);
7555 if ( right
> rightCol
)
7557 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7560 if ( bottom
> bottomRow
)
7562 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7567 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7569 int row
= coords
.GetRow();
7570 int col
= coords
.GetCol();
7572 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7575 // we draw the cell border ourselves
7576 #if !WXGRID_DRAW_LINES
7577 if ( m_gridLinesEnabled
)
7578 DrawCellBorder( dc
, coords
);
7581 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7583 bool isCurrent
= coords
== m_currentCellCoords
;
7585 wxRect rect
= CellToRect( row
, col
);
7587 // if the editor is shown, we should use it and not the renderer
7588 // Note: However, only if it is really _shown_, i.e. not hidden!
7589 if ( isCurrent
&& IsCellEditControlShown() )
7591 // NB: this "#if..." is temporary and fixes a problem where the
7592 // edit control is erased by this code after being rendered.
7593 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7594 // implicitly, causing this out-of order render.
7595 #if !defined(__WXMAC__)
7596 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7597 editor
->PaintBackground(rect
, attr
);
7603 // but all the rest is drawn by the cell renderer and hence may be customized
7604 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7605 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7612 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7614 // don't show highlight when the grid doesn't have focus
7618 int row
= m_currentCellCoords
.GetRow();
7619 int col
= m_currentCellCoords
.GetCol();
7621 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7624 wxRect rect
= CellToRect(row
, col
);
7626 // hmmm... what could we do here to show that the cell is disabled?
7627 // for now, I just draw a thinner border than for the other ones, but
7628 // it doesn't look really good
7630 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7634 // The center of the drawn line is where the position/width/height of
7635 // the rectangle is actually at (on wxMSW at least), so the
7636 // size of the rectangle is reduced to compensate for the thickness of
7637 // the line. If this is too strange on non-wxMSW platforms then
7638 // please #ifdef this appropriately.
7639 rect
.x
+= penWidth
/ 2;
7640 rect
.y
+= penWidth
/ 2;
7641 rect
.width
-= penWidth
- 1;
7642 rect
.height
-= penWidth
- 1;
7644 // Now draw the rectangle
7645 // use the cellHighlightColour if the cell is inside a selection, this
7646 // will ensure the cell is always visible.
7647 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
: m_cellHighlightColour
, penWidth
, wxPENSTYLE_SOLID
));
7648 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7649 dc
.DrawRectangle(rect
);
7653 // VZ: my experiments with 3D borders...
7655 // how to properly set colours for arbitrary bg?
7656 wxCoord x1
= rect
.x
,
7658 x2
= rect
.x
+ rect
.width
- 1,
7659 y2
= rect
.y
+ rect
.height
- 1;
7661 dc
.SetPen(*wxWHITE_PEN
);
7662 dc
.DrawLine(x1
, y1
, x2
, y1
);
7663 dc
.DrawLine(x1
, y1
, x1
, y2
);
7665 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7666 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7668 dc
.SetPen(*wxBLACK_PEN
);
7669 dc
.DrawLine(x1
, y2
, x2
, y2
);
7670 dc
.DrawLine(x2
, y1
, x2
, y2
+ 1);
7674 wxPen
wxGrid::GetDefaultGridLinePen()
7676 return wxPen(GetGridLineColour(), 1, wxPENSTYLE_SOLID
);
7679 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
7681 return GetDefaultGridLinePen();
7684 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
7686 return GetDefaultGridLinePen();
7689 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7691 int row
= coords
.GetRow();
7692 int col
= coords
.GetCol();
7693 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7697 wxRect rect
= CellToRect( row
, col
);
7699 // right hand border
7700 dc
.SetPen( GetColGridLinePen(col
) );
7701 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7702 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7705 dc
.SetPen( GetRowGridLinePen(row
) );
7706 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7707 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7710 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7712 // This if block was previously in wxGrid::OnPaint but that doesn't
7713 // seem to get called under wxGTK - MB
7715 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7716 m_numRows
&& m_numCols
)
7718 m_currentCellCoords
.Set(0, 0);
7721 if ( IsCellEditControlShown() )
7723 // don't show highlight when the edit control is shown
7727 // if the active cell was repainted, repaint its highlight too because it
7728 // might have been damaged by the grid lines
7729 size_t count
= cells
.GetCount();
7730 for ( size_t n
= 0; n
< count
; n
++ )
7732 if ( cells
[n
] == m_currentCellCoords
)
7734 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7735 DrawCellHighlight(dc
, attr
);
7743 // TODO: remove this ???
7744 // This is used to redraw all grid lines e.g. when the grid line colour
7747 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7749 #if !WXGRID_DRAW_LINES
7753 if ( !m_gridLinesEnabled
|| !m_numRows
|| !m_numCols
)
7756 int top
, bottom
, left
, right
;
7758 #if 0 //#ifndef __WXGTK__
7762 m_gridWin
->GetClientSize(&cw
, &ch
);
7764 // virtual coords of visible area
7766 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7767 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7772 reg
.GetBox(x
, y
, w
, h
);
7773 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7774 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7778 m_gridWin
->GetClientSize(&cw
, &ch
);
7779 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7780 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7783 // avoid drawing grid lines past the last row and col
7785 right
= wxMin( right
, GetColRight(GetColAt( m_numCols
- 1 )) );
7786 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7788 // no gridlines inside multicells, clip them out
7789 int leftCol
= GetColPos( internalXToCol(left
) );
7790 int topRow
= internalYToRow(top
);
7791 int rightCol
= GetColPos( internalXToCol(right
) );
7792 int bottomRow
= internalYToRow(bottom
);
7794 wxRegion
clippedcells(0, 0, cw
, ch
);
7796 int i
, j
, cell_rows
, cell_cols
;
7799 for (j
=topRow
; j
<=bottomRow
; j
++)
7802 for (colPos
=leftCol
; colPos
<=rightCol
; colPos
++)
7804 i
= GetColAt( colPos
);
7806 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7807 if ((cell_rows
> 1) || (cell_cols
> 1))
7809 rect
= CellToRect(j
,i
);
7810 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7811 clippedcells
.Subtract(rect
);
7813 else if ((cell_rows
< 0) || (cell_cols
< 0))
7815 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7816 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7817 clippedcells
.Subtract(rect
);
7822 dc
.SetDeviceClippingRegion( clippedcells
);
7825 // horizontal grid lines
7827 // already declared above - int i;
7828 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7830 int bot
= GetRowBottom(i
) - 1;
7839 dc
.SetPen( GetRowGridLinePen(i
) );
7840 dc
.DrawLine( left
, bot
, right
, bot
);
7844 // vertical grid lines
7847 for ( colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
7849 i
= GetColAt( colPos
);
7851 int colRight
= GetColRight(i
);
7853 if (GetLayoutDirection() != wxLayout_RightToLeft
)
7857 if ( colRight
> right
)
7862 if ( colRight
>= left
)
7864 dc
.SetPen( GetColGridLinePen(i
) );
7865 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7869 dc
.DestroyClippingRegion();
7872 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
7878 size_t numLabels
= rows
.GetCount();
7880 for ( i
= 0; i
< numLabels
; i
++ )
7882 DrawRowLabel( dc
, rows
[i
] );
7886 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7888 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7893 int rowTop
= GetRowTop(row
),
7894 rowBottom
= GetRowBottom(row
) - 1;
7896 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxPENSTYLE_SOLID
) );
7897 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
7898 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7899 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7901 dc
.SetPen( *wxWHITE_PEN
);
7902 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7903 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
7905 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
7906 dc
.SetTextForeground( GetLabelTextColour() );
7907 dc
.SetFont( GetLabelFont() );
7910 GetRowLabelAlignment( &hAlign
, &vAlign
);
7913 rect
.SetY( GetRowTop(row
) + 2 );
7914 rect
.SetWidth( m_rowLabelWidth
- 4 );
7915 rect
.SetHeight( GetRowHeight(row
) - 4 );
7916 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7919 void wxGrid::SetUseNativeColLabels( bool native
)
7921 m_nativeColumnLabels
= native
;
7924 int height
= wxRendererNative::Get().GetHeaderButtonHeight( this );
7925 SetColLabelSize( height
);
7928 m_colLabelWin
->Refresh();
7931 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7937 size_t numLabels
= cols
.GetCount();
7939 for ( i
= 0; i
< numLabels
; i
++ )
7941 DrawColLabel( dc
, cols
[i
] );
7945 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7947 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7950 int colLeft
= GetColLeft(col
);
7954 if (m_nativeColumnLabels
)
7956 rect
.SetX( colLeft
);
7958 rect
.SetWidth( GetColWidth(col
));
7959 rect
.SetHeight( m_colLabelHeight
);
7961 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7962 wxRendererNative::Get().DrawHeaderButton( win_dc
->GetWindow(), dc
, rect
, 0 );
7966 int colRight
= GetColRight(col
) - 1;
7968 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxPENSTYLE_SOLID
) );
7969 dc
.DrawLine( colRight
, 0, colRight
, m_colLabelHeight
- 1 );
7970 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7971 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
7972 colRight
+ 1, m_colLabelHeight
- 1 );
7974 dc
.SetPen( *wxWHITE_PEN
);
7975 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
7976 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7979 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
7980 dc
.SetTextForeground( GetLabelTextColour() );
7981 dc
.SetFont( GetLabelFont() );
7983 int hAlign
, vAlign
, orient
;
7984 GetColLabelAlignment( &hAlign
, &vAlign
);
7985 orient
= GetColLabelTextOrientation();
7987 rect
.SetX( colLeft
+ 2 );
7989 rect
.SetWidth( GetColWidth(col
) - 4 );
7990 rect
.SetHeight( m_colLabelHeight
- 4 );
7991 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7994 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7995 const wxString
& value
,
7999 int textOrientation
)
8001 wxArrayString lines
;
8003 StringToLines( value
, lines
);
8005 // Forward to new API.
8006 DrawTextRectangle( dc
,
8014 // VZ: this should be replaced with wxDC::DrawLabel() to which we just have to
8015 // add textOrientation support
8016 void wxGrid::DrawTextRectangle(wxDC
& dc
,
8017 const wxArrayString
& lines
,
8021 int textOrientation
)
8023 if ( lines
.empty() )
8026 wxDCClipper
clip(dc
, rect
);
8031 if ( textOrientation
== wxHORIZONTAL
)
8032 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
8034 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
8038 switch ( vertAlign
)
8040 case wxALIGN_BOTTOM
:
8041 if ( textOrientation
== wxHORIZONTAL
)
8042 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
8044 x
= rect
.x
+ rect
.width
- textWidth
;
8047 case wxALIGN_CENTRE
:
8048 if ( textOrientation
== wxHORIZONTAL
)
8049 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
8051 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
8056 if ( textOrientation
== wxHORIZONTAL
)
8063 // Align each line of a multi-line label
8064 size_t nLines
= lines
.GetCount();
8065 for ( size_t l
= 0; l
< nLines
; l
++ )
8067 const wxString
& line
= lines
[l
];
8071 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
8075 wxCoord lineWidth
= 0,
8077 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
8079 switch ( horizAlign
)
8082 if ( textOrientation
== wxHORIZONTAL
)
8083 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
8085 y
= rect
.y
+ lineWidth
+ 1;
8088 case wxALIGN_CENTRE
:
8089 if ( textOrientation
== wxHORIZONTAL
)
8090 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
8092 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
8097 if ( textOrientation
== wxHORIZONTAL
)
8100 y
= rect
.y
+ rect
.height
- 1;
8104 if ( textOrientation
== wxHORIZONTAL
)
8106 dc
.DrawText( line
, x
, y
);
8111 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
8117 // Split multi-line text up into an array of strings.
8118 // Any existing contents of the string array are preserved.
8120 // TODO: refactor wxTextFile::Read() and reuse the same code from here
8121 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
8125 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
8126 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
8128 while ( startPos
< (int)tVal
.length() )
8130 pos
= tVal
.Mid(startPos
).Find( eol
);
8135 else if ( pos
== 0 )
8137 lines
.Add( wxEmptyString
);
8141 lines
.Add( tVal
.Mid(startPos
, pos
) );
8144 startPos
+= pos
+ 1;
8147 if ( startPos
< (int)tVal
.length() )
8149 lines
.Add( tVal
.Mid( startPos
) );
8153 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
8154 const wxArrayString
& lines
,
8155 long *width
, long *height
) const
8159 wxCoord lineW
= 0, lineH
= 0;
8162 for ( i
= 0; i
< lines
.GetCount(); i
++ )
8164 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
8165 w
= wxMax( w
, lineW
);
8174 // ------ Batch processing.
8176 void wxGrid::EndBatch()
8178 if ( m_batchCount
> 0 )
8181 if ( !m_batchCount
)
8184 m_rowLabelWin
->Refresh();
8185 m_colLabelWin
->Refresh();
8186 m_cornerLabelWin
->Refresh();
8187 m_gridWin
->Refresh();
8192 // Use this, rather than wxWindow::Refresh(), to force an immediate
8193 // repainting of the grid. Has no effect if you are already inside a
8194 // BeginBatch / EndBatch block.
8196 void wxGrid::ForceRefresh()
8202 bool wxGrid::Enable(bool enable
)
8204 if ( !wxScrolledWindow::Enable(enable
) )
8207 // redraw in the new state
8208 m_gridWin
->Refresh();
8214 // ------ Edit control functions
8217 void wxGrid::EnableEditing( bool edit
)
8219 // TODO: improve this ?
8221 if ( edit
!= m_editable
)
8224 EnableCellEditControl(edit
);
8229 void wxGrid::EnableCellEditControl( bool enable
)
8234 if ( enable
!= m_cellEditCtrlEnabled
)
8238 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
8241 // this should be checked by the caller!
8242 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8244 // do it before ShowCellEditControl()
8245 m_cellEditCtrlEnabled
= enable
;
8247 ShowCellEditControl();
8251 //FIXME:add veto support
8252 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
8254 HideCellEditControl();
8255 SaveEditControlValue();
8257 // do it after HideCellEditControl()
8258 m_cellEditCtrlEnabled
= enable
;
8263 bool wxGrid::IsCurrentCellReadOnly() const
8266 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
8267 bool readonly
= attr
->IsReadOnly();
8273 bool wxGrid::CanEnableCellControl() const
8275 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
8276 !IsCurrentCellReadOnly();
8279 bool wxGrid::IsCellEditControlEnabled() const
8281 // the cell edit control might be disable for all cells or just for the
8282 // current one if it's read only
8283 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
8286 bool wxGrid::IsCellEditControlShown() const
8288 bool isShown
= false;
8290 if ( m_cellEditCtrlEnabled
)
8292 int row
= m_currentCellCoords
.GetRow();
8293 int col
= m_currentCellCoords
.GetCol();
8294 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8295 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
8300 if ( editor
->IsCreated() )
8302 isShown
= editor
->GetControl()->IsShown();
8312 void wxGrid::ShowCellEditControl()
8314 if ( IsCellEditControlEnabled() )
8316 if ( !IsVisible( m_currentCellCoords
, false ) )
8318 m_cellEditCtrlEnabled
= false;
8323 wxRect rect
= CellToRect( m_currentCellCoords
);
8324 int row
= m_currentCellCoords
.GetRow();
8325 int col
= m_currentCellCoords
.GetCol();
8327 // if this is part of a multicell, find owner (topleft)
8328 int cell_rows
, cell_cols
;
8329 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8330 if ( cell_rows
<= 0 || cell_cols
<= 0 )
8334 m_currentCellCoords
.SetRow( row
);
8335 m_currentCellCoords
.SetCol( col
);
8338 // erase the highlight and the cell contents because the editor
8339 // might not cover the entire cell
8340 wxClientDC
dc( m_gridWin
);
8342 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8343 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxBRUSHSTYLE_SOLID
));
8344 dc
.SetPen(*wxTRANSPARENT_PEN
);
8345 dc
.DrawRectangle(rect
);
8347 // convert to scrolled coords
8348 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8354 // cell is shifted by one pixel
8355 // However, don't allow x or y to become negative
8356 // since the SetSize() method interprets that as
8363 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8364 if ( !editor
->IsCreated() )
8366 editor
->Create(m_gridWin
, wxID_ANY
,
8367 new wxGridCellEditorEvtHandler(this, editor
));
8369 wxGridEditorCreatedEvent
evt(GetId(),
8370 wxEVT_GRID_EDITOR_CREATED
,
8374 editor
->GetControl());
8375 GetEventHandler()->ProcessEvent(evt
);
8378 // resize editor to overflow into righthand cells if allowed
8379 int maxWidth
= rect
.width
;
8380 wxString value
= GetCellValue(row
, col
);
8381 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
8384 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
8385 if (maxWidth
< rect
.width
)
8386 maxWidth
= rect
.width
;
8389 int client_right
= m_gridWin
->GetClientSize().GetWidth();
8390 if (rect
.x
+ maxWidth
> client_right
)
8391 maxWidth
= client_right
- rect
.x
;
8393 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
8395 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8396 // may have changed earlier
8397 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
8400 GetCellSize( row
, i
, &c_rows
, &c_cols
);
8402 // looks weird going over a multicell
8403 if (m_table
->IsEmptyCell( row
, i
) &&
8404 (rect
.width
< maxWidth
) && (c_rows
== 1))
8406 rect
.width
+= GetColWidth( i
);
8412 if (rect
.GetRight() > client_right
)
8413 rect
.SetRight( client_right
- 1 );
8416 editor
->SetCellAttr( attr
);
8417 editor
->SetSize( rect
);
8419 editor
->GetControl()->Move(
8420 editor
->GetControl()->GetPosition().x
+ nXMove
,
8421 editor
->GetControl()->GetPosition().y
);
8422 editor
->Show( true, attr
);
8424 // recalc dimensions in case we need to
8425 // expand the scrolled window to account for editor
8428 editor
->BeginEdit(row
, col
, this);
8429 editor
->SetCellAttr(NULL
);
8437 void wxGrid::HideCellEditControl()
8439 if ( IsCellEditControlEnabled() )
8441 int row
= m_currentCellCoords
.GetRow();
8442 int col
= m_currentCellCoords
.GetCol();
8444 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8445 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
8446 editor
->Show( false );
8450 m_gridWin
->SetFocus();
8452 // refresh whole row to the right
8453 wxRect
rect( CellToRect(row
, col
) );
8454 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8455 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8458 // ensure that the pixels under the focus ring get refreshed as well
8459 rect
.Inflate(10, 10);
8462 m_gridWin
->Refresh( false, &rect
);
8466 void wxGrid::SaveEditControlValue()
8468 if ( IsCellEditControlEnabled() )
8470 int row
= m_currentCellCoords
.GetRow();
8471 int col
= m_currentCellCoords
.GetCol();
8473 wxString oldval
= GetCellValue(row
, col
);
8475 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8476 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8477 bool changed
= editor
->EndEdit(row
, col
, this);
8484 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
8485 m_currentCellCoords
.GetRow(),
8486 m_currentCellCoords
.GetCol() ) < 0 )
8488 // Event has been vetoed, set the data back.
8489 SetCellValue(row
, col
, oldval
);
8496 // ------ Grid location functions
8497 // Note that all of these functions work with the logical coordinates of
8498 // grid cells and labels so you will need to convert from device
8499 // coordinates for mouse events etc.
8502 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
) const
8504 int row
= YToRow(y
);
8505 int col
= XToCol(x
);
8507 if ( row
== -1 || col
== -1 )
8509 coords
= wxGridNoCellCoords
;
8513 coords
.Set( row
, col
);
8517 // Internal Helper function for computing row or column from some
8518 // (unscrolled) coordinate value, using either
8519 // m_defaultRowHeight/m_defaultColWidth or binary search on array
8520 // of m_rowBottoms/m_ColRights to speed up the search!
8522 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
8523 const wxArrayInt
& BorderArray
, int nMax
,
8527 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
8532 size_t i_max
= coord
/ defaultDist
,
8535 if (BorderArray
.IsEmpty())
8537 if ((int) i_max
< nMax
)
8539 return clipToMinMax
? nMax
- 1 : -1;
8542 if ( i_max
>= BorderArray
.GetCount())
8544 i_max
= BorderArray
.GetCount() - 1;
8548 if ( coord
>= BorderArray
[i_max
])
8552 i_max
= coord
/ minDist
;
8554 i_max
= BorderArray
.GetCount() - 1;
8557 if ( i_max
>= BorderArray
.GetCount())
8558 i_max
= BorderArray
.GetCount() - 1;
8561 if ( coord
>= BorderArray
[i_max
])
8562 return clipToMinMax
? (int)i_max
: -1;
8563 if ( coord
< BorderArray
[0] )
8566 while ( i_max
- i_min
> 0 )
8568 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
8569 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8570 if (coord
>= BorderArray
[ i_max
- 1])
8574 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
8575 if (coord
< BorderArray
[median
])
8584 int wxGrid::YToRow( int y
) const
8586 return CoordToRowOrCol(y
, m_defaultRowHeight
,
8587 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8590 int wxGrid::XToCol( int x
, bool clipToMinMax
) const
8593 return clipToMinMax
&& (m_numCols
> 0) ? GetColAt( 0 ) : -1;
8595 wxASSERT_MSG(m_defaultColWidth
> 0, wxT("Default column width can not be zero"));
8597 int maxPos
= x
/ m_defaultColWidth
;
8600 if (m_colRights
.IsEmpty())
8602 if(maxPos
< m_numCols
)
8603 return GetColAt( maxPos
);
8604 return clipToMinMax
? GetColAt( m_numCols
- 1 ) : -1;
8607 if ( maxPos
>= m_numCols
)
8608 maxPos
= m_numCols
- 1;
8611 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8614 if (m_minAcceptableColWidth
)
8615 maxPos
= x
/ m_minAcceptableColWidth
;
8617 maxPos
= m_numCols
- 1;
8619 if ( maxPos
>= m_numCols
)
8620 maxPos
= m_numCols
- 1;
8623 //X is beyond the last column
8624 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8625 return clipToMinMax
? GetColAt( maxPos
) : -1;
8627 //X is before the first column
8628 if ( x
< m_colRights
[GetColAt( 0 )] )
8629 return GetColAt( 0 );
8631 //Perform a binary search
8632 while ( maxPos
- minPos
> 0 )
8634 wxCHECK_MSG(m_colRights
[GetColAt( minPos
)] <= x
&& x
< m_colRights
[GetColAt( maxPos
)],
8635 0, _T("wxGrid: internal error in XToCol"));
8637 if (x
>= m_colRights
[GetColAt( maxPos
- 1 )])
8638 return GetColAt( maxPos
);
8641 int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
8642 if (x
< m_colRights
[GetColAt( median
)])
8647 return GetColAt( maxPos
);
8650 // return the row number that that the y coord is near
8651 // the edge of, or -1 if not near an edge.
8652 // coords can only possibly be near an edge if
8653 // (a) the row/column is large enough to still allow for an "inner" area
8654 // that is _not_ nead the edge (i.e., if the height/width is smaller
8655 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8658 // (b) resizing rows/columns (the thing for which edge detection is
8659 // relevant at all) is enabled.
8661 int wxGrid::YToEdgeOfRow( int y
) const
8664 i
= internalYToRow(y
);
8666 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragRowSize() )
8668 // We know that we are in row i, test whether we are
8669 // close enough to lower or upper border, respectively.
8670 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8672 else if ( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8679 // return the col number that that the x coord is near the edge of, or
8680 // -1 if not near an edge
8681 // See comment at YToEdgeOfRow for conditions on edge detection.
8683 int wxGrid::XToEdgeOfCol( int x
) const
8686 i
= internalXToCol(x
);
8688 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragColSize() )
8690 // We know that we are in column i; test whether we are
8691 // close enough to right or left border, respectively.
8692 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8694 else if ( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8701 wxRect
wxGrid::CellToRect( int row
, int col
) const
8703 wxRect
rect( -1, -1, -1, -1 );
8705 if ( row
>= 0 && row
< m_numRows
&&
8706 col
>= 0 && col
< m_numCols
)
8708 int i
, cell_rows
, cell_cols
;
8709 rect
.width
= rect
.height
= 0;
8710 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8711 // if negative then find multicell owner
8716 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8718 rect
.x
= GetColLeft(col
);
8719 rect
.y
= GetRowTop(row
);
8720 for (i
=col
; i
< col
+ cell_cols
; i
++)
8721 rect
.width
+= GetColWidth(i
);
8722 for (i
=row
; i
< row
+ cell_rows
; i
++)
8723 rect
.height
+= GetRowHeight(i
);
8726 // if grid lines are enabled, then the area of the cell is a bit smaller
8727 if (m_gridLinesEnabled
)
8736 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
8738 // get the cell rectangle in logical coords
8740 wxRect
r( CellToRect( row
, col
) );
8742 // convert to device coords
8744 int left
, top
, right
, bottom
;
8745 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8746 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8748 // check against the client area of the grid window
8750 m_gridWin
->GetClientSize( &cw
, &ch
);
8752 if ( wholeCellVisible
)
8754 // is the cell wholly visible ?
8755 return ( left
>= 0 && right
<= cw
&&
8756 top
>= 0 && bottom
<= ch
);
8760 // is the cell partly visible ?
8762 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8763 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8767 // make the specified cell location visible by doing a minimal amount
8770 void wxGrid::MakeCellVisible( int row
, int col
)
8773 int xpos
= -1, ypos
= -1;
8775 if ( row
>= 0 && row
< m_numRows
&&
8776 col
>= 0 && col
< m_numCols
)
8778 // get the cell rectangle in logical coords
8779 wxRect
r( CellToRect( row
, col
) );
8781 // convert to device coords
8782 int left
, top
, right
, bottom
;
8783 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8784 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8787 m_gridWin
->GetClientSize( &cw
, &ch
);
8793 else if ( bottom
> ch
)
8795 int h
= r
.GetHeight();
8797 for ( i
= row
- 1; i
>= 0; i
-- )
8799 int rowHeight
= GetRowHeight(i
);
8800 if ( h
+ rowHeight
> ch
)
8807 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8808 // have rounding errors (this is important, because if we do,
8809 // we might not scroll at all and some cells won't be redrawn)
8811 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
8812 // so just add a full scroll unit...
8813 ypos
+= m_scrollLineY
;
8816 // special handling for wide cells - show always left part of the cell!
8817 // Otherwise, e.g. when stepping from row to row, it would jump between
8818 // left and right part of the cell on every step!
8820 if ( left
< 0 || (right
- left
) >= cw
)
8824 else if ( right
> cw
)
8826 // position the view so that the cell is on the right
8828 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8829 xpos
= x0
+ (right
- cw
);
8831 // see comment for ypos above
8832 xpos
+= m_scrollLineX
;
8835 if ( xpos
!= -1 || ypos
!= -1 )
8838 xpos
/= m_scrollLineX
;
8840 ypos
/= m_scrollLineY
;
8841 Scroll( xpos
, ypos
);
8848 // ------ Grid cursor movement functions
8851 bool wxGrid::MoveCursorUp( bool expandSelection
)
8853 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8854 m_currentCellCoords
.GetRow() >= 0 )
8856 if ( expandSelection
)
8858 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8859 m_selectingKeyboard
= m_currentCellCoords
;
8860 if ( m_selectingKeyboard
.GetRow() > 0 )
8862 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8863 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8864 m_selectingKeyboard
.GetCol() );
8865 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8868 else if ( m_currentCellCoords
.GetRow() > 0 )
8870 int row
= m_currentCellCoords
.GetRow() - 1;
8871 int col
= m_currentCellCoords
.GetCol();
8873 MakeCellVisible( row
, col
);
8874 SetCurrentCell( row
, col
);
8885 bool wxGrid::MoveCursorDown( bool expandSelection
)
8887 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8888 m_currentCellCoords
.GetRow() < m_numRows
)
8890 if ( expandSelection
)
8892 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8893 m_selectingKeyboard
= m_currentCellCoords
;
8894 if ( m_selectingKeyboard
.GetRow() < m_numRows
- 1 )
8896 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8897 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8898 m_selectingKeyboard
.GetCol() );
8899 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8902 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8904 int row
= m_currentCellCoords
.GetRow() + 1;
8905 int col
= m_currentCellCoords
.GetCol();
8907 MakeCellVisible( row
, col
);
8908 SetCurrentCell( row
, col
);
8919 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8921 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8922 m_currentCellCoords
.GetCol() >= 0 )
8924 if ( expandSelection
)
8926 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8927 m_selectingKeyboard
= m_currentCellCoords
;
8928 if ( m_selectingKeyboard
.GetCol() > 0 )
8930 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8931 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8932 m_selectingKeyboard
.GetCol() );
8933 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8936 else if ( GetColPos( m_currentCellCoords
.GetCol() ) > 0 )
8938 int row
= m_currentCellCoords
.GetRow();
8939 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) - 1 );
8942 MakeCellVisible( row
, col
);
8943 SetCurrentCell( row
, col
);
8954 bool wxGrid::MoveCursorRight( bool expandSelection
)
8956 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8957 m_currentCellCoords
.GetCol() < m_numCols
)
8959 if ( expandSelection
)
8961 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8962 m_selectingKeyboard
= m_currentCellCoords
;
8963 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8965 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8966 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8967 m_selectingKeyboard
.GetCol() );
8968 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8971 else if ( GetColPos( m_currentCellCoords
.GetCol() ) < m_numCols
- 1 )
8973 int row
= m_currentCellCoords
.GetRow();
8974 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) + 1 );
8977 MakeCellVisible( row
, col
);
8978 SetCurrentCell( row
, col
);
8989 bool wxGrid::MovePageUp()
8991 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8994 int row
= m_currentCellCoords
.GetRow();
8998 m_gridWin
->GetClientSize( &cw
, &ch
);
9000 int y
= GetRowTop(row
);
9001 int newRow
= internalYToRow( y
- ch
+ 1 );
9003 if ( newRow
== row
)
9005 // row > 0, so newRow can never be less than 0 here.
9009 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
9010 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
9018 bool wxGrid::MovePageDown()
9020 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9023 int row
= m_currentCellCoords
.GetRow();
9024 if ( (row
+ 1) < m_numRows
)
9027 m_gridWin
->GetClientSize( &cw
, &ch
);
9029 int y
= GetRowTop(row
);
9030 int newRow
= internalYToRow( y
+ ch
);
9031 if ( newRow
== row
)
9033 // row < m_numRows, so newRow can't overflow here.
9037 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
9038 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
9046 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
9049 m_currentCellCoords
!= wxGridNoCellCoords
&&
9050 m_currentCellCoords
.GetRow() > 0 )
9052 int row
= m_currentCellCoords
.GetRow();
9053 int col
= m_currentCellCoords
.GetCol();
9055 if ( m_table
->IsEmptyCell(row
, col
) )
9057 // starting in an empty cell: find the next block of
9063 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9067 else if ( m_table
->IsEmptyCell(row
- 1, col
) )
9069 // starting at the top of a block: find the next block
9075 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9081 // starting within a block: find the top of the block
9086 if ( m_table
->IsEmptyCell(row
, col
) )
9094 MakeCellVisible( row
, col
);
9095 if ( expandSelection
)
9097 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9098 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9103 SetCurrentCell( row
, col
);
9112 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
9115 m_currentCellCoords
!= wxGridNoCellCoords
&&
9116 m_currentCellCoords
.GetRow() < m_numRows
- 1 )
9118 int row
= m_currentCellCoords
.GetRow();
9119 int col
= m_currentCellCoords
.GetCol();
9121 if ( m_table
->IsEmptyCell(row
, col
) )
9123 // starting in an empty cell: find the next block of
9126 while ( row
< m_numRows
- 1 )
9129 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9133 else if ( m_table
->IsEmptyCell(row
+ 1, col
) )
9135 // starting at the bottom of a block: find the next block
9138 while ( row
< m_numRows
- 1 )
9141 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9147 // starting within a block: find the bottom of the block
9149 while ( row
< m_numRows
- 1 )
9152 if ( m_table
->IsEmptyCell(row
, col
) )
9160 MakeCellVisible( row
, col
);
9161 if ( expandSelection
)
9163 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9164 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9169 SetCurrentCell( row
, col
);
9178 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
9181 m_currentCellCoords
!= wxGridNoCellCoords
&&
9182 m_currentCellCoords
.GetCol() > 0 )
9184 int row
= m_currentCellCoords
.GetRow();
9185 int col
= m_currentCellCoords
.GetCol();
9187 if ( m_table
->IsEmptyCell(row
, col
) )
9189 // starting in an empty cell: find the next block of
9195 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9199 else if ( m_table
->IsEmptyCell(row
, col
- 1) )
9201 // starting at the left of a block: find the next block
9207 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9213 // starting within a block: find the left of the block
9218 if ( m_table
->IsEmptyCell(row
, col
) )
9226 MakeCellVisible( row
, col
);
9227 if ( expandSelection
)
9229 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9230 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9235 SetCurrentCell( row
, col
);
9244 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
9247 m_currentCellCoords
!= wxGridNoCellCoords
&&
9248 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
9250 int row
= m_currentCellCoords
.GetRow();
9251 int col
= m_currentCellCoords
.GetCol();
9253 if ( m_table
->IsEmptyCell(row
, col
) )
9255 // starting in an empty cell: find the next block of
9258 while ( col
< m_numCols
- 1 )
9261 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9265 else if ( m_table
->IsEmptyCell(row
, col
+ 1) )
9267 // starting at the right of a block: find the next block
9270 while ( col
< m_numCols
- 1 )
9273 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9279 // starting within a block: find the right of the block
9281 while ( col
< m_numCols
- 1 )
9284 if ( m_table
->IsEmptyCell(row
, col
) )
9292 MakeCellVisible( row
, col
);
9293 if ( expandSelection
)
9295 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9296 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9301 SetCurrentCell( row
, col
);
9311 // ------ Label values and formatting
9314 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
9317 *horiz
= m_rowLabelHorizAlign
;
9319 *vert
= m_rowLabelVertAlign
;
9322 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
9325 *horiz
= m_colLabelHorizAlign
;
9327 *vert
= m_colLabelVertAlign
;
9330 int wxGrid::GetColLabelTextOrientation() const
9332 return m_colLabelTextOrientation
;
9335 wxString
wxGrid::GetRowLabelValue( int row
) const
9339 return m_table
->GetRowLabelValue( row
);
9349 wxString
wxGrid::GetColLabelValue( int col
) const
9353 return m_table
->GetColLabelValue( col
);
9363 void wxGrid::SetRowLabelSize( int width
)
9365 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
9367 if ( width
== wxGRID_AUTOSIZE
)
9369 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
9372 if ( width
!= m_rowLabelWidth
)
9376 m_rowLabelWin
->Show( false );
9377 m_cornerLabelWin
->Show( false );
9379 else if ( m_rowLabelWidth
== 0 )
9381 m_rowLabelWin
->Show( true );
9382 if ( m_colLabelHeight
> 0 )
9383 m_cornerLabelWin
->Show( true );
9386 m_rowLabelWidth
= width
;
9388 wxScrolledWindow::Refresh( true );
9392 void wxGrid::SetColLabelSize( int height
)
9394 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
9396 if ( height
== wxGRID_AUTOSIZE
)
9398 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
9401 if ( height
!= m_colLabelHeight
)
9405 m_colLabelWin
->Show( false );
9406 m_cornerLabelWin
->Show( false );
9408 else if ( m_colLabelHeight
== 0 )
9410 m_colLabelWin
->Show( true );
9411 if ( m_rowLabelWidth
> 0 )
9412 m_cornerLabelWin
->Show( true );
9415 m_colLabelHeight
= height
;
9417 wxScrolledWindow::Refresh( true );
9421 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
9423 if ( m_labelBackgroundColour
!= colour
)
9425 m_labelBackgroundColour
= colour
;
9426 m_rowLabelWin
->SetBackgroundColour( colour
);
9427 m_colLabelWin
->SetBackgroundColour( colour
);
9428 m_cornerLabelWin
->SetBackgroundColour( colour
);
9430 if ( !GetBatchCount() )
9432 m_rowLabelWin
->Refresh();
9433 m_colLabelWin
->Refresh();
9434 m_cornerLabelWin
->Refresh();
9439 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
9441 if ( m_labelTextColour
!= colour
)
9443 m_labelTextColour
= colour
;
9444 if ( !GetBatchCount() )
9446 m_rowLabelWin
->Refresh();
9447 m_colLabelWin
->Refresh();
9452 void wxGrid::SetLabelFont( const wxFont
& font
)
9455 if ( !GetBatchCount() )
9457 m_rowLabelWin
->Refresh();
9458 m_colLabelWin
->Refresh();
9462 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
9464 // allow old (incorrect) defs to be used
9467 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9468 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9469 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9474 case wxTOP
: vert
= wxALIGN_TOP
; break;
9475 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9476 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9479 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9481 m_rowLabelHorizAlign
= horiz
;
9484 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9486 m_rowLabelVertAlign
= vert
;
9489 if ( !GetBatchCount() )
9491 m_rowLabelWin
->Refresh();
9495 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
9497 // allow old (incorrect) defs to be used
9500 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9501 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9502 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9507 case wxTOP
: vert
= wxALIGN_TOP
; break;
9508 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9509 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9512 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9514 m_colLabelHorizAlign
= horiz
;
9517 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9519 m_colLabelVertAlign
= vert
;
9522 if ( !GetBatchCount() )
9524 m_colLabelWin
->Refresh();
9528 // Note: under MSW, the default column label font must be changed because it
9529 // does not support vertical printing
9531 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9532 // pGrid->SetLabelFont(font);
9533 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9535 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9537 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9538 m_colLabelTextOrientation
= textOrientation
;
9540 if ( !GetBatchCount() )
9541 m_colLabelWin
->Refresh();
9544 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9548 m_table
->SetRowLabelValue( row
, s
);
9549 if ( !GetBatchCount() )
9551 wxRect rect
= CellToRect( row
, 0 );
9552 if ( rect
.height
> 0 )
9554 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9556 rect
.width
= m_rowLabelWidth
;
9557 m_rowLabelWin
->Refresh( true, &rect
);
9563 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9567 m_table
->SetColLabelValue( col
, s
);
9568 if ( !GetBatchCount() )
9570 wxRect rect
= CellToRect( 0, col
);
9571 if ( rect
.width
> 0 )
9573 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9575 rect
.height
= m_colLabelHeight
;
9576 m_colLabelWin
->Refresh( true, &rect
);
9582 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9584 if ( m_gridLineColour
!= colour
)
9586 m_gridLineColour
= colour
;
9588 wxClientDC
dc( m_gridWin
);
9590 DrawAllGridLines( dc
, wxRegion() );
9594 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9596 if ( m_cellHighlightColour
!= colour
)
9598 m_cellHighlightColour
= colour
;
9600 wxClientDC
dc( m_gridWin
);
9602 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9603 DrawCellHighlight(dc
, attr
);
9608 void wxGrid::SetCellHighlightPenWidth(int width
)
9610 if (m_cellHighlightPenWidth
!= width
)
9612 m_cellHighlightPenWidth
= width
;
9614 // Just redrawing the cell highlight is not enough since that won't
9615 // make any visible change if the the thickness is getting smaller.
9616 int row
= m_currentCellCoords
.GetRow();
9617 int col
= m_currentCellCoords
.GetCol();
9618 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9621 wxRect rect
= CellToRect(row
, col
);
9622 m_gridWin
->Refresh(true, &rect
);
9626 void wxGrid::SetCellHighlightROPenWidth(int width
)
9628 if (m_cellHighlightROPenWidth
!= width
)
9630 m_cellHighlightROPenWidth
= width
;
9632 // Just redrawing the cell highlight is not enough since that won't
9633 // make any visible change if the the thickness is getting smaller.
9634 int row
= m_currentCellCoords
.GetRow();
9635 int col
= m_currentCellCoords
.GetCol();
9636 if ( row
== -1 || col
== -1 ||
9637 GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9640 wxRect rect
= CellToRect(row
, col
);
9641 m_gridWin
->Refresh(true, &rect
);
9645 void wxGrid::EnableGridLines( bool enable
)
9647 if ( enable
!= m_gridLinesEnabled
)
9649 m_gridLinesEnabled
= enable
;
9651 if ( !GetBatchCount() )
9655 wxClientDC
dc( m_gridWin
);
9657 DrawAllGridLines( dc
, wxRegion() );
9661 m_gridWin
->Refresh();
9667 int wxGrid::GetDefaultRowSize() const
9669 return m_defaultRowHeight
;
9672 int wxGrid::GetRowSize( int row
) const
9674 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9676 return GetRowHeight(row
);
9679 int wxGrid::GetDefaultColSize() const
9681 return m_defaultColWidth
;
9684 int wxGrid::GetColSize( int col
) const
9686 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9688 return GetColWidth(col
);
9691 // ============================================================================
9692 // access to the grid attributes: each of them has a default value in the grid
9693 // itself and may be overidden on a per-cell basis
9694 // ============================================================================
9696 // ----------------------------------------------------------------------------
9697 // setting default attributes
9698 // ----------------------------------------------------------------------------
9700 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9702 m_defaultCellAttr
->SetBackgroundColour(col
);
9704 m_gridWin
->SetBackgroundColour(col
);
9708 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9710 m_defaultCellAttr
->SetTextColour(col
);
9713 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9715 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9718 void wxGrid::SetDefaultCellOverflow( bool allow
)
9720 m_defaultCellAttr
->SetOverflow(allow
);
9723 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9725 m_defaultCellAttr
->SetFont(font
);
9728 // For editors and renderers the type registry takes precedence over the
9729 // default attr, so we need to register the new editor/renderer for the string
9730 // data type in order to make setting a default editor/renderer appear to
9733 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9735 RegisterDataType(wxGRID_VALUE_STRING
,
9737 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9740 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9742 RegisterDataType(wxGRID_VALUE_STRING
,
9743 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9747 // ----------------------------------------------------------------------------
9748 // access to the default attributes
9749 // ----------------------------------------------------------------------------
9751 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
9753 return m_defaultCellAttr
->GetBackgroundColour();
9756 wxColour
wxGrid::GetDefaultCellTextColour() const
9758 return m_defaultCellAttr
->GetTextColour();
9761 wxFont
wxGrid::GetDefaultCellFont() const
9763 return m_defaultCellAttr
->GetFont();
9766 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
9768 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9771 bool wxGrid::GetDefaultCellOverflow() const
9773 return m_defaultCellAttr
->GetOverflow();
9776 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9778 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9781 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9783 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9786 // ----------------------------------------------------------------------------
9787 // access to cell attributes
9788 // ----------------------------------------------------------------------------
9790 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
9792 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9793 wxColour colour
= attr
->GetBackgroundColour();
9799 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
9801 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9802 wxColour colour
= attr
->GetTextColour();
9808 wxFont
wxGrid::GetCellFont( int row
, int col
) const
9810 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9811 wxFont font
= attr
->GetFont();
9817 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
9819 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9820 attr
->GetAlignment(horiz
, vert
);
9824 bool wxGrid::GetCellOverflow( int row
, int col
) const
9826 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9827 bool allow
= attr
->GetOverflow();
9833 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
9835 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9836 attr
->GetSize( num_rows
, num_cols
);
9840 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
9842 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9843 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9849 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
9851 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9852 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9858 bool wxGrid::IsReadOnly(int row
, int col
) const
9860 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9861 bool isReadOnly
= attr
->IsReadOnly();
9867 // ----------------------------------------------------------------------------
9868 // attribute support: cache, automatic provider creation, ...
9869 // ----------------------------------------------------------------------------
9871 bool wxGrid::CanHaveAttributes() const
9878 return m_table
->CanHaveAttributes();
9881 void wxGrid::ClearAttrCache()
9883 if ( m_attrCache
.row
!= -1 )
9885 wxSafeDecRef(m_attrCache
.attr
);
9886 m_attrCache
.attr
= NULL
;
9887 m_attrCache
.row
= -1;
9891 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9895 wxGrid
*self
= (wxGrid
*)this; // const_cast
9897 self
->ClearAttrCache();
9898 self
->m_attrCache
.row
= row
;
9899 self
->m_attrCache
.col
= col
;
9900 self
->m_attrCache
.attr
= attr
;
9905 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9907 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9909 *attr
= m_attrCache
.attr
;
9910 wxSafeIncRef(m_attrCache
.attr
);
9912 #ifdef DEBUG_ATTR_CACHE
9913 gs_nAttrCacheHits
++;
9920 #ifdef DEBUG_ATTR_CACHE
9921 gs_nAttrCacheMisses
++;
9928 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9930 wxGridCellAttr
*attr
= NULL
;
9931 // Additional test to avoid looking at the cache e.g. for
9932 // wxNoCellCoords, as this will confuse memory management.
9935 if ( !LookupAttr(row
, col
, &attr
) )
9937 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9938 : (wxGridCellAttr
*)NULL
;
9939 CacheAttr(row
, col
, attr
);
9945 attr
->SetDefAttr(m_defaultCellAttr
);
9949 attr
= m_defaultCellAttr
;
9956 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9958 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9959 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9961 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9962 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9964 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9967 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9969 // artificially inc the ref count to match DecRef() in caller
9971 m_table
->SetAttr(attr
, row
, col
);
9977 // ----------------------------------------------------------------------------
9978 // setting column attributes (wrappers around SetColAttr)
9979 // ----------------------------------------------------------------------------
9981 void wxGrid::SetColFormatBool(int col
)
9983 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9986 void wxGrid::SetColFormatNumber(int col
)
9988 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9991 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9993 wxString typeName
= wxGRID_VALUE_FLOAT
;
9994 if ( (width
!= -1) || (precision
!= -1) )
9996 typeName
<< _T(':') << width
<< _T(',') << precision
;
9999 SetColFormatCustom(col
, typeName
);
10002 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
10004 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
10006 attr
= new wxGridCellAttr
;
10007 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
10008 attr
->SetRenderer(renderer
);
10010 SetColAttr(col
, attr
);
10014 // ----------------------------------------------------------------------------
10015 // setting cell attributes: this is forwarded to the table
10016 // ----------------------------------------------------------------------------
10018 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
10020 if ( CanHaveAttributes() )
10022 m_table
->SetAttr(attr
, row
, col
);
10027 wxSafeDecRef(attr
);
10031 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
10033 if ( CanHaveAttributes() )
10035 m_table
->SetRowAttr(attr
, row
);
10040 wxSafeDecRef(attr
);
10044 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
10046 if ( CanHaveAttributes() )
10048 m_table
->SetColAttr(attr
, col
);
10053 wxSafeDecRef(attr
);
10057 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
10059 if ( CanHaveAttributes() )
10061 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10062 attr
->SetBackgroundColour(colour
);
10067 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
10069 if ( CanHaveAttributes() )
10071 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10072 attr
->SetTextColour(colour
);
10077 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
10079 if ( CanHaveAttributes() )
10081 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10082 attr
->SetFont(font
);
10087 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
10089 if ( CanHaveAttributes() )
10091 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10092 attr
->SetAlignment(horiz
, vert
);
10097 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
10099 if ( CanHaveAttributes() )
10101 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10102 attr
->SetOverflow(allow
);
10107 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
10109 if ( CanHaveAttributes() )
10111 int cell_rows
, cell_cols
;
10113 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10114 attr
->GetSize(&cell_rows
, &cell_cols
);
10115 attr
->SetSize(num_rows
, num_cols
);
10118 // Cannot set the size of a cell to 0 or negative values
10119 // While it is perfectly legal to do that, this function cannot
10120 // handle all the possibilies, do it by hand by getting the CellAttr.
10121 // You can only set the size of a cell to 1,1 or greater with this fn
10122 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
10123 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10124 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
10125 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10127 // if this was already a multicell then "turn off" the other cells first
10128 if ((cell_rows
> 1) || (cell_rows
> 1))
10131 for (j
=row
; j
< row
+ cell_rows
; j
++)
10133 for (i
=col
; i
< col
+ cell_cols
; i
++)
10135 if ((i
!= col
) || (j
!= row
))
10137 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10138 attr_stub
->SetSize( 1, 1 );
10139 attr_stub
->DecRef();
10145 // mark the cells that will be covered by this cell to
10146 // negative or zero values to point back at this cell
10147 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
10150 for (j
=row
; j
< row
+ num_rows
; j
++)
10152 for (i
=col
; i
< col
+ num_cols
; i
++)
10154 if ((i
!= col
) || (j
!= row
))
10156 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10157 attr_stub
->SetSize( row
- j
, col
- i
);
10158 attr_stub
->DecRef();
10166 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
10168 if ( CanHaveAttributes() )
10170 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10171 attr
->SetRenderer(renderer
);
10176 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
10178 if ( CanHaveAttributes() )
10180 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10181 attr
->SetEditor(editor
);
10186 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
10188 if ( CanHaveAttributes() )
10190 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10191 attr
->SetReadOnly(isReadOnly
);
10196 // ----------------------------------------------------------------------------
10197 // Data type registration
10198 // ----------------------------------------------------------------------------
10200 void wxGrid::RegisterDataType(const wxString
& typeName
,
10201 wxGridCellRenderer
* renderer
,
10202 wxGridCellEditor
* editor
)
10204 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
10208 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
10210 wxString typeName
= m_table
->GetTypeName(row
, col
);
10211 return GetDefaultEditorForType(typeName
);
10214 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
10216 wxString typeName
= m_table
->GetTypeName(row
, col
);
10217 return GetDefaultRendererForType(typeName
);
10220 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
10222 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10223 if ( index
== wxNOT_FOUND
)
10225 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10230 return m_typeRegistry
->GetEditor(index
);
10233 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
10235 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10236 if ( index
== wxNOT_FOUND
)
10238 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10243 return m_typeRegistry
->GetRenderer(index
);
10246 // ----------------------------------------------------------------------------
10248 // ----------------------------------------------------------------------------
10250 void wxGrid::EnableDragRowSize( bool enable
)
10252 m_canDragRowSize
= enable
;
10255 void wxGrid::EnableDragColSize( bool enable
)
10257 m_canDragColSize
= enable
;
10260 void wxGrid::EnableDragGridSize( bool enable
)
10262 m_canDragGridSize
= enable
;
10265 void wxGrid::EnableDragCell( bool enable
)
10267 m_canDragCell
= enable
;
10270 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
10272 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
10274 if ( resizeExistingRows
)
10276 // since we are resizing all rows to the default row size,
10277 // we can simply clear the row heights and row bottoms
10278 // arrays (which also allows us to take advantage of
10279 // some speed optimisations)
10280 m_rowHeights
.Empty();
10281 m_rowBottoms
.Empty();
10282 if ( !GetBatchCount() )
10287 void wxGrid::SetRowSize( int row
, int height
)
10289 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
10291 // See comment in SetColSize
10292 if ( height
< GetRowMinimalAcceptableHeight())
10295 if ( m_rowHeights
.IsEmpty() )
10297 // need to really create the array
10301 int h
= wxMax( 0, height
);
10302 int diff
= h
- m_rowHeights
[row
];
10304 m_rowHeights
[row
] = h
;
10305 for ( int i
= row
; i
< m_numRows
; i
++ )
10307 m_rowBottoms
[i
] += diff
;
10310 if ( !GetBatchCount() )
10314 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
10316 // we dont allow zero default column width
10317 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
10319 if ( resizeExistingCols
)
10321 // since we are resizing all columns to the default column size,
10322 // we can simply clear the col widths and col rights
10323 // arrays (which also allows us to take advantage of
10324 // some speed optimisations)
10325 m_colWidths
.Empty();
10326 m_colRights
.Empty();
10327 if ( !GetBatchCount() )
10332 void wxGrid::SetColSize( int col
, int width
)
10334 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
10336 // should we check that it's bigger than GetColMinimalWidth(col) here?
10338 // No, because it is reasonable to assume the library user know's
10339 // what he is doing. However we should test against the weaker
10340 // constraint of minimalAcceptableWidth, as this breaks rendering
10342 // This test then fixes sf.net bug #645734
10344 if ( width
< GetColMinimalAcceptableWidth() )
10347 if ( m_colWidths
.IsEmpty() )
10349 // need to really create the array
10353 // if < 0 then calculate new width from label
10357 wxArrayString lines
;
10358 wxClientDC
dc(m_colLabelWin
);
10359 dc
.SetFont(GetLabelFont());
10360 StringToLines(GetColLabelValue(col
), lines
);
10361 GetTextBoxSize(dc
, lines
, &w
, &h
);
10365 int w
= wxMax( 0, width
);
10366 int diff
= w
- m_colWidths
[col
];
10367 m_colWidths
[col
] = w
;
10369 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
10371 m_colRights
[GetColAt(colPos
)] += diff
;
10374 if ( !GetBatchCount() )
10378 void wxGrid::SetColMinimalWidth( int col
, int width
)
10380 if (width
> GetColMinimalAcceptableWidth())
10382 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10383 m_colMinWidths
[key
] = width
;
10387 void wxGrid::SetRowMinimalHeight( int row
, int width
)
10389 if (width
> GetRowMinimalAcceptableHeight())
10391 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10392 m_rowMinHeights
[key
] = width
;
10396 int wxGrid::GetColMinimalWidth(int col
) const
10398 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10399 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
10401 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
10404 int wxGrid::GetRowMinimalHeight(int row
) const
10406 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10407 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
10409 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
10412 void wxGrid::SetColMinimalAcceptableWidth( int width
)
10414 // We do allow a width of 0 since this gives us
10415 // an easy way to temporarily hiding columns.
10417 m_minAcceptableColWidth
= width
;
10420 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
10422 // We do allow a height of 0 since this gives us
10423 // an easy way to temporarily hiding rows.
10425 m_minAcceptableRowHeight
= height
;
10428 int wxGrid::GetColMinimalAcceptableWidth() const
10430 return m_minAcceptableColWidth
;
10433 int wxGrid::GetRowMinimalAcceptableHeight() const
10435 return m_minAcceptableRowHeight
;
10438 // ----------------------------------------------------------------------------
10440 // ----------------------------------------------------------------------------
10443 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
10445 const bool column
= direction
== wxGRID_COLUMN
;
10447 wxClientDC
dc(m_gridWin
);
10449 // cancel editing of cell
10450 HideCellEditControl();
10451 SaveEditControlValue();
10453 // init both of them to avoid compiler warnings, even if we only need one
10461 wxCoord extent
, extentMax
= 0;
10462 int max
= column
? m_numRows
: m_numCols
;
10463 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
10470 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
10471 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
10474 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
10475 extent
= column
? size
.x
: size
.y
;
10476 if ( extent
> extentMax
)
10477 extentMax
= extent
;
10479 renderer
->DecRef();
10485 // now also compare with the column label extent
10487 dc
.SetFont( GetLabelFont() );
10491 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
10492 if ( GetColLabelTextOrientation() == wxVERTICAL
)
10496 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
10498 extent
= column
? w
: h
;
10499 if ( extent
> extentMax
)
10500 extentMax
= extent
;
10504 // empty column - give default extent (notice that if extentMax is less
10505 // than default extent but != 0, it's OK)
10506 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
10511 // leave some space around text
10519 // Ensure automatic width is not less than minimal width. See the
10520 // comment in SetColSize() for explanation of why this isn't done
10521 // in SetColSize().
10523 extentMax
= wxMax(extentMax
, GetColMinimalWidth(col
));
10525 SetColSize( col
, extentMax
);
10526 if ( !GetBatchCount() )
10529 m_gridWin
->GetClientSize( &cw
, &ch
);
10530 wxRect
rect ( CellToRect( 0, col
) );
10532 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10533 rect
.width
= cw
- rect
.x
;
10534 rect
.height
= m_colLabelHeight
;
10535 m_colLabelWin
->Refresh( true, &rect
);
10540 // Ensure automatic width is not less than minimal height. See the
10541 // comment in SetColSize() for explanation of why this isn't done
10542 // in SetRowSize().
10544 extentMax
= wxMax(extentMax
, GetRowMinimalHeight(row
));
10546 SetRowSize(row
, extentMax
);
10547 if ( !GetBatchCount() )
10550 m_gridWin
->GetClientSize( &cw
, &ch
);
10551 wxRect
rect( CellToRect( row
, 0 ) );
10553 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10554 rect
.width
= m_rowLabelWidth
;
10555 rect
.height
= ch
- rect
.y
;
10556 m_rowLabelWin
->Refresh( true, &rect
);
10563 SetColMinimalWidth(col
, extentMax
);
10565 SetRowMinimalHeight(row
, extentMax
);
10569 wxCoord
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
10571 // calculate size for the rows or columns?
10572 const bool calcRows
= direction
== wxGRID_ROW
;
10574 wxClientDC
dc(calcRows
? GetGridRowLabelWindow()
10575 : GetGridColLabelWindow());
10576 dc
.SetFont(GetLabelFont());
10578 // which dimension should we take into account for calculations?
10580 // for columns, the text can be only horizontal so it's easy but for rows
10581 // we also have to take into account the text orientation
10583 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
10585 wxArrayString lines
;
10586 wxCoord extentMax
= 0;
10588 const int numRowsOrCols
= calcRows
? m_numRows
: m_numCols
;
10589 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
10593 wxString label
= calcRows
? GetRowLabelValue(rowOrCol
)
10594 : GetColLabelValue(rowOrCol
);
10595 StringToLines(label
, lines
);
10598 GetTextBoxSize(dc
, lines
, &w
, &h
);
10600 const wxCoord extent
= useWidth
? w
: h
;
10601 if ( extent
> extentMax
)
10602 extentMax
= extent
;
10607 // empty column - give default extent (notice that if extentMax is less
10608 // than default extent but != 0, it's OK)
10609 extentMax
= calcRows
? GetDefaultRowLabelSize()
10610 : GetDefaultColLabelSize();
10613 // leave some space around text (taken from AutoSizeColOrRow)
10622 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10624 int width
= m_rowLabelWidth
;
10626 wxGridUpdateLocker locker
;
10628 locker
.Create(this);
10630 for ( int col
= 0; col
< m_numCols
; col
++ )
10633 AutoSizeColumn(col
, setAsMin
);
10635 width
+= GetColWidth(col
);
10641 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10643 int height
= m_colLabelHeight
;
10645 wxGridUpdateLocker locker
;
10647 locker
.Create(this);
10649 for ( int row
= 0; row
< m_numRows
; row
++ )
10652 AutoSizeRow(row
, setAsMin
);
10654 height
+= GetRowHeight(row
);
10660 void wxGrid::AutoSize()
10662 wxGridUpdateLocker
locker(this);
10664 // we need to round up the size of the scrollable area to a multiple of
10665 // scroll step to ensure that we don't get the scrollbars when we're sized
10666 // exactly to fit our contents
10667 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
10668 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
10669 wxSize
sizeFit(GetScrollX(size
.x
) * GetScrollLineX(),
10670 GetScrollY(size
.y
) * GetScrollLineY());
10672 // distribute the extra space between the columns/rows to avoid having
10673 // extra white space
10674 wxCoord diff
= sizeFit
.x
- size
.x
;
10675 if ( diff
&& m_numCols
)
10677 // try to resize the columns uniformly
10678 wxCoord diffPerCol
= diff
/ m_numCols
;
10681 for ( int col
= 0; col
< m_numCols
; col
++ )
10683 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
10687 // add remaining amount to the last columns
10688 diff
-= diffPerCol
* m_numCols
;
10691 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
10693 SetColSize(col
, GetColWidth(col
) + 1);
10699 diff
= sizeFit
.y
- size
.y
;
10700 if ( diff
&& m_numRows
)
10702 // try to resize the columns uniformly
10703 wxCoord diffPerRow
= diff
/ m_numRows
;
10706 for ( int row
= 0; row
< m_numRows
; row
++ )
10708 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
10712 // add remaining amount to the last rows
10713 diff
-= diffPerRow
* m_numRows
;
10716 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
10718 SetRowSize(row
, GetRowHeight(row
) + 1);
10723 // we know that we're not going to have scrollbars so disable them now to
10724 // avoid trouble in SetClientSize() which can otherwise set the correct
10725 // client size but also leave space for (not needed any more) scrollbars
10726 SetScrollbars(0, 0, 0, 0, 0, 0, true);
10727 SetClientSize(sizeFit
.x
+ m_rowLabelWidth
, sizeFit
.y
+ m_colLabelHeight
);
10730 void wxGrid::AutoSizeRowLabelSize( int row
)
10732 wxArrayString lines
;
10735 // Hide the edit control, so it
10736 // won't interfere with drag-shrinking.
10737 if ( IsCellEditControlShown() )
10739 HideCellEditControl();
10740 SaveEditControlValue();
10743 // autosize row height depending on label text
10744 StringToLines( GetRowLabelValue( row
), lines
);
10745 wxClientDC
dc( m_rowLabelWin
);
10746 GetTextBoxSize( dc
, lines
, &w
, &h
);
10747 if ( h
< m_defaultRowHeight
)
10748 h
= m_defaultRowHeight
;
10749 SetRowSize(row
, h
);
10753 void wxGrid::AutoSizeColLabelSize( int col
)
10755 wxArrayString lines
;
10758 // Hide the edit control, so it
10759 // won't interfere with drag-shrinking.
10760 if ( IsCellEditControlShown() )
10762 HideCellEditControl();
10763 SaveEditControlValue();
10766 // autosize column width depending on label text
10767 StringToLines( GetColLabelValue( col
), lines
);
10768 wxClientDC
dc( m_colLabelWin
);
10769 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10770 GetTextBoxSize( dc
, lines
, &w
, &h
);
10772 GetTextBoxSize( dc
, lines
, &h
, &w
);
10773 if ( w
< m_defaultColWidth
)
10774 w
= m_defaultColWidth
;
10775 SetColSize(col
, w
);
10779 wxSize
wxGrid::DoGetBestSize() const
10781 wxGrid
*self
= (wxGrid
*)this; // const_cast
10783 // we do the same as in AutoSize() here with the exception that we don't
10784 // change the column/row sizes, only calculate them
10785 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
10786 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
10787 wxSize
sizeFit(GetScrollX(size
.x
) * GetScrollLineX(),
10788 GetScrollY(size
.y
) * GetScrollLineY());
10790 // NOTE: This size should be cached, but first we need to add calls to
10791 // InvalidateBestSize everywhere that could change the results of this
10793 // CacheBestSize(size);
10795 return wxSize(sizeFit
.x
+ m_rowLabelWidth
, sizeFit
.y
+ m_colLabelHeight
)
10796 + GetWindowBorderSize();
10804 wxPen
& wxGrid::GetDividerPen() const
10809 // ----------------------------------------------------------------------------
10810 // cell value accessor functions
10811 // ----------------------------------------------------------------------------
10813 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10817 m_table
->SetValue( row
, col
, s
);
10818 if ( !GetBatchCount() )
10821 wxRect
rect( CellToRect( row
, col
) );
10823 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10824 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10825 m_gridWin
->Refresh( false, &rect
);
10828 if ( m_currentCellCoords
.GetRow() == row
&&
10829 m_currentCellCoords
.GetCol() == col
&&
10830 IsCellEditControlShown())
10831 // Note: If we are using IsCellEditControlEnabled,
10832 // this interacts badly with calling SetCellValue from
10833 // an EVT_GRID_CELL_CHANGE handler.
10835 HideCellEditControl();
10836 ShowCellEditControl(); // will reread data from table
10841 // ----------------------------------------------------------------------------
10842 // block, row and column selection
10843 // ----------------------------------------------------------------------------
10845 void wxGrid::SelectRow( int row
, bool addToSelected
)
10847 if ( IsSelection() && !addToSelected
)
10851 m_selection
->SelectRow( row
, false, addToSelected
);
10854 void wxGrid::SelectCol( int col
, bool addToSelected
)
10856 if ( IsSelection() && !addToSelected
)
10860 m_selection
->SelectCol( col
, false, addToSelected
);
10863 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10864 bool addToSelected
)
10866 if ( IsSelection() && !addToSelected
)
10870 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10871 false, addToSelected
);
10874 void wxGrid::SelectAll()
10876 if ( m_numRows
> 0 && m_numCols
> 0 )
10879 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10883 // ----------------------------------------------------------------------------
10884 // cell, row and col deselection
10885 // ----------------------------------------------------------------------------
10887 void wxGrid::DeselectRow( int row
)
10889 if ( !m_selection
)
10892 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10894 if ( m_selection
->IsInSelection(row
, 0 ) )
10895 m_selection
->ToggleCellSelection(row
, 0);
10899 int nCols
= GetNumberCols();
10900 for ( int i
= 0; i
< nCols
; i
++ )
10902 if ( m_selection
->IsInSelection(row
, i
) )
10903 m_selection
->ToggleCellSelection(row
, i
);
10908 void wxGrid::DeselectCol( int col
)
10910 if ( !m_selection
)
10913 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10915 if ( m_selection
->IsInSelection(0, col
) )
10916 m_selection
->ToggleCellSelection(0, col
);
10920 int nRows
= GetNumberRows();
10921 for ( int i
= 0; i
< nRows
; i
++ )
10923 if ( m_selection
->IsInSelection(i
, col
) )
10924 m_selection
->ToggleCellSelection(i
, col
);
10929 void wxGrid::DeselectCell( int row
, int col
)
10931 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10932 m_selection
->ToggleCellSelection(row
, col
);
10935 bool wxGrid::IsSelection() const
10937 return ( m_selection
&& (m_selection
->IsSelection() ||
10938 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10939 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10942 bool wxGrid::IsInSelection( int row
, int col
) const
10944 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10945 ( row
>= m_selectingTopLeft
.GetRow() &&
10946 col
>= m_selectingTopLeft
.GetCol() &&
10947 row
<= m_selectingBottomRight
.GetRow() &&
10948 col
<= m_selectingBottomRight
.GetCol() )) );
10951 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10955 wxGridCellCoordsArray a
;
10959 return m_selection
->m_cellSelection
;
10962 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10966 wxGridCellCoordsArray a
;
10970 return m_selection
->m_blockSelectionTopLeft
;
10973 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10977 wxGridCellCoordsArray a
;
10981 return m_selection
->m_blockSelectionBottomRight
;
10984 wxArrayInt
wxGrid::GetSelectedRows() const
10992 return m_selection
->m_rowSelection
;
10995 wxArrayInt
wxGrid::GetSelectedCols() const
11003 return m_selection
->m_colSelection
;
11006 void wxGrid::ClearSelection()
11008 m_selectingTopLeft
=
11009 m_selectingBottomRight
=
11010 m_selectingKeyboard
= wxGridNoCellCoords
;
11012 m_selection
->ClearSelection();
11015 // This function returns the rectangle that encloses the given block
11016 // in device coords clipped to the client size of the grid window.
11018 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
11019 const wxGridCellCoords
& bottomRight
) const
11022 wxRect tempCellRect
= CellToRect(topLeft
);
11023 if ( tempCellRect
!= wxGridNoCellRect
)
11025 resultRect
= tempCellRect
;
11029 resultRect
= wxRect(0, 0, 0, 0);
11032 tempCellRect
= CellToRect(bottomRight
);
11033 if ( tempCellRect
!= wxGridNoCellRect
)
11035 resultRect
+= tempCellRect
;
11039 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
11040 return wxGridNoCellRect
;
11043 // Ensure that left/right and top/bottom pairs are in order.
11044 int left
= resultRect
.GetLeft();
11045 int top
= resultRect
.GetTop();
11046 int right
= resultRect
.GetRight();
11047 int bottom
= resultRect
.GetBottom();
11049 int leftCol
= topLeft
.GetCol();
11050 int topRow
= topLeft
.GetRow();
11051 int rightCol
= bottomRight
.GetCol();
11052 int bottomRow
= bottomRight
.GetRow();
11061 leftCol
= rightCol
;
11072 topRow
= bottomRow
;
11076 // The following loop is ONLY necessary to detect and handle merged cells.
11078 m_gridWin
->GetClientSize( &cw
, &ch
);
11080 // Get the origin coordinates: notice that they will be negative if the
11081 // grid is scrolled downwards/to the right.
11082 int gridOriginX
= 0;
11083 int gridOriginY
= 0;
11084 CalcScrolledPosition(gridOriginX
, gridOriginY
, &gridOriginX
, &gridOriginY
);
11086 int onScreenLeftmostCol
= internalXToCol(-gridOriginX
);
11087 int onScreenUppermostRow
= internalYToRow(-gridOriginY
);
11089 int onScreenRightmostCol
= internalXToCol(-gridOriginX
+ cw
);
11090 int onScreenBottommostRow
= internalYToRow(-gridOriginY
+ ch
);
11092 // Bound our loop so that we only examine the portion of the selected block
11093 // that is shown on screen. Therefore, we compare the Top-Left block values
11094 // to the Top-Left screen values, and the Bottom-Right block values to the
11095 // Bottom-Right screen values, choosing appropriately.
11096 const int visibleTopRow
= wxMax(topRow
, onScreenUppermostRow
);
11097 const int visibleBottomRow
= wxMin(bottomRow
, onScreenBottommostRow
);
11098 const int visibleLeftCol
= wxMax(leftCol
, onScreenLeftmostCol
);
11099 const int visibleRightCol
= wxMin(rightCol
, onScreenRightmostCol
);
11101 for ( int j
= visibleTopRow
; j
<= visibleBottomRow
; j
++ )
11103 for ( int i
= visibleLeftCol
; i
<= visibleRightCol
; i
++ )
11105 if ( (j
== visibleTopRow
) || (j
== visibleBottomRow
) ||
11106 (i
== visibleLeftCol
) || (i
== visibleRightCol
) )
11108 tempCellRect
= CellToRect( j
, i
);
11110 if (tempCellRect
.x
< left
)
11111 left
= tempCellRect
.x
;
11112 if (tempCellRect
.y
< top
)
11113 top
= tempCellRect
.y
;
11114 if (tempCellRect
.x
+ tempCellRect
.width
> right
)
11115 right
= tempCellRect
.x
+ tempCellRect
.width
;
11116 if (tempCellRect
.y
+ tempCellRect
.height
> bottom
)
11117 bottom
= tempCellRect
.y
+ tempCellRect
.height
;
11121 i
= visibleRightCol
; // jump over inner cells.
11126 // Convert to scrolled coords
11127 CalcScrolledPosition( left
, top
, &left
, &top
);
11128 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
11130 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
11131 return wxRect(0,0,0,0);
11133 resultRect
.SetLeft( wxMax(0, left
) );
11134 resultRect
.SetTop( wxMax(0, top
) );
11135 resultRect
.SetRight( wxMin(cw
, right
) );
11136 resultRect
.SetBottom( wxMin(ch
, bottom
) );
11141 // ----------------------------------------------------------------------------
11143 // ----------------------------------------------------------------------------
11145 #if wxUSE_DRAG_AND_DROP
11147 // this allow setting drop target directly on wxGrid
11148 void wxGrid::SetDropTarget(wxDropTarget
*dropTarget
)
11150 GetGridWindow()->SetDropTarget(dropTarget
);
11153 #endif // wxUSE_DRAG_AND_DROP
11155 // ----------------------------------------------------------------------------
11156 // grid event classes
11157 // ----------------------------------------------------------------------------
11159 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
11161 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
11162 int row
, int col
, int x
, int y
, bool sel
,
11163 bool control
, bool shift
, bool alt
, bool meta
)
11164 : wxNotifyEvent( type
, id
)
11171 m_control
= control
;
11176 SetEventObject(obj
);
11180 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
11182 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
11183 int rowOrCol
, int x
, int y
,
11184 bool control
, bool shift
, bool alt
, bool meta
)
11185 : wxNotifyEvent( type
, id
)
11187 m_rowOrCol
= rowOrCol
;
11190 m_control
= control
;
11195 SetEventObject(obj
);
11199 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
11201 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
11202 const wxGridCellCoords
& topLeft
,
11203 const wxGridCellCoords
& bottomRight
,
11204 bool sel
, bool control
,
11205 bool shift
, bool alt
, bool meta
)
11206 : wxNotifyEvent( type
, id
)
11208 m_topLeft
= topLeft
;
11209 m_bottomRight
= bottomRight
;
11211 m_control
= control
;
11216 SetEventObject(obj
);
11220 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
11222 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
11223 wxObject
* obj
, int row
,
11224 int col
, wxControl
* ctrl
)
11225 : wxCommandEvent(type
, id
)
11227 SetEventObject(obj
);
11233 #endif // wxUSE_GRID