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(), wxSOLID
));
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
= wxBORDER_NONE
;
1520 style
|= wxCB_READONLY
;
1521 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1522 wxDefaultPosition
, wxDefaultSize
,
1525 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1528 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1529 wxGridCellAttr
* attr
)
1531 // as we fill the entire client area, don't do anything here to minimize
1534 // TODO: It doesn't actually fill the client area since the height of a
1535 // combo always defaults to the standard. Until someone has time to
1536 // figure out the right rectangle to paint, just do it the normal way.
1537 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1540 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1542 wxASSERT_MSG(m_control
,
1543 wxT("The wxGridCellEditor must be created first!"));
1545 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1547 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1549 // Don't immediately end if we get a kill focus event within BeginEdit
1551 evtHandler
->SetInSetFocus(true);
1553 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1557 Combo()->SetValue(m_startValue
);
1558 Combo()->SetInsertionPointEnd();
1560 else // the combobox is read-only
1562 // find the right position, or default to the first if not found
1563 int pos
= Combo()->FindString(m_startValue
);
1564 if (pos
== wxNOT_FOUND
)
1566 Combo()->SetSelection(pos
);
1569 Combo()->SetFocus();
1573 // When dropping down the menu, a kill focus event
1574 // happens after this point, so we can't reset the flag yet.
1575 #if !defined(__WXGTK20__)
1576 evtHandler
->SetInSetFocus(false);
1581 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1584 wxString value
= Combo()->GetValue();
1585 if ( value
== m_startValue
)
1588 grid
->GetTable()->SetValue(row
, col
, value
);
1593 void wxGridCellChoiceEditor::Reset()
1595 Combo()->SetValue(m_startValue
);
1596 Combo()->SetInsertionPointEnd();
1599 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1609 wxStringTokenizer
tk(params
, _T(','));
1610 while ( tk
.HasMoreTokens() )
1612 m_choices
.Add(tk
.GetNextToken());
1616 // return the value in the text control
1617 wxString
wxGridCellChoiceEditor::GetValue() const
1619 return Combo()->GetValue();
1622 #endif // wxUSE_COMBOBOX
1624 // ----------------------------------------------------------------------------
1625 // wxGridCellEditorEvtHandler
1626 // ----------------------------------------------------------------------------
1628 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
1630 // Don't disable the cell if we're just starting to edit it
1635 m_grid
->DisableCellEditControl();
1640 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1642 switch ( event
.GetKeyCode() )
1646 m_grid
->DisableCellEditControl();
1650 m_grid
->GetEventHandler()->ProcessEvent( event
);
1654 case WXK_NUMPAD_ENTER
:
1655 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1656 m_editor
->HandleReturn(event
);
1665 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1667 int row
= m_grid
->GetGridCursorRow();
1668 int col
= m_grid
->GetGridCursorCol();
1669 wxRect rect
= m_grid
->CellToRect( row
, col
);
1671 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
1673 // if cell width is smaller than grid client area, cell is wholly visible
1674 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
1676 switch ( event
.GetKeyCode() )
1681 case WXK_NUMPAD_ENTER
:
1686 if ( wholeCellVisible
)
1688 // no special processing needed...
1693 // do special processing for partly visible cell...
1695 // get the widths of all cells previous to this one
1697 for ( int i
= 0; i
< col
; i
++ )
1699 colXPos
+= m_grid
->GetColSize(i
);
1702 int xUnit
= 1, yUnit
= 1;
1703 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1706 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1710 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
1718 if ( wholeCellVisible
)
1720 // no special processing needed...
1725 // do special processing for partly visible cell...
1728 wxString value
= m_grid
->GetCellValue(row
, col
);
1729 if ( wxEmptyString
!= value
)
1731 // get width of cell CONTENTS (text)
1733 wxFont font
= m_grid
->GetCellFont(row
, col
);
1734 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
1736 // try to RIGHT align the text by scrolling
1737 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
1739 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
1740 // otherwise the last part of the cell content might be hidden below the scroll bar
1741 // FIXME: maybe there is a more suitable correction?
1742 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
1743 if ( textWidth
< 0 )
1749 // get the widths of all cells previous to this one
1751 for ( int i
= 0; i
< col
; i
++ )
1753 colXPos
+= m_grid
->GetColSize(i
);
1756 // and add the (modified) text width of the cell contents
1757 // as we'd like to see the last part of the cell contents
1758 colXPos
+= textWidth
;
1760 int xUnit
= 1, yUnit
= 1;
1761 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1762 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1773 // ----------------------------------------------------------------------------
1774 // wxGridCellWorker is an (almost) empty common base class for
1775 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1776 // ----------------------------------------------------------------------------
1778 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1783 wxGridCellWorker::~wxGridCellWorker()
1787 // ============================================================================
1789 // ============================================================================
1791 // ----------------------------------------------------------------------------
1792 // wxGridCellRenderer
1793 // ----------------------------------------------------------------------------
1795 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1796 wxGridCellAttr
& attr
,
1799 int WXUNUSED(row
), int WXUNUSED(col
),
1802 dc
.SetBackgroundMode( wxSOLID
);
1804 // grey out fields if the grid is disabled
1805 if ( grid
.IsEnabled() )
1810 if ( grid
.HasFocus() )
1811 clr
= grid
.GetSelectionBackground();
1813 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
1814 dc
.SetBrush( wxBrush(clr
, wxSOLID
) );
1818 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1823 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1826 dc
.SetPen( *wxTRANSPARENT_PEN
);
1827 dc
.DrawRectangle(rect
);
1830 // ----------------------------------------------------------------------------
1831 // wxGridCellStringRenderer
1832 // ----------------------------------------------------------------------------
1834 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
1835 const wxGridCellAttr
& attr
,
1839 dc
.SetBackgroundMode( wxTRANSPARENT
);
1841 // TODO some special colours for attr.IsReadOnly() case?
1843 // different coloured text when the grid is disabled
1844 if ( grid
.IsEnabled() )
1849 if ( grid
.HasFocus() )
1850 clr
= grid
.GetSelectionBackground();
1852 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
1853 dc
.SetTextBackground( clr
);
1854 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1858 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1859 dc
.SetTextForeground( attr
.GetTextColour() );
1864 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1865 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
1868 dc
.SetFont( attr
.GetFont() );
1871 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
1873 const wxString
& text
)
1875 wxCoord x
= 0, y
= 0, max_x
= 0;
1876 dc
.SetFont(attr
.GetFont());
1877 wxStringTokenizer
tk(text
, _T('\n'));
1878 while ( tk
.HasMoreTokens() )
1880 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1881 max_x
= wxMax(max_x
, x
);
1884 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1886 return wxSize(max_x
, y
);
1889 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1890 wxGridCellAttr
& attr
,
1894 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1897 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1898 wxGridCellAttr
& attr
,
1900 const wxRect
& rectCell
,
1904 wxRect rect
= rectCell
;
1907 // erase only this cells background, overflow cells should have been erased
1908 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1911 attr
.GetAlignment(&hAlign
, &vAlign
);
1913 int overflowCols
= 0;
1915 if (attr
.GetOverflow())
1917 int cols
= grid
.GetNumberCols();
1918 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1919 int cell_rows
, cell_cols
;
1920 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <= 0
1921 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1923 int i
, c_cols
, c_rows
;
1924 for (i
= col
+cell_cols
; i
< cols
; i
++)
1926 bool is_empty
= true;
1927 for (int j
=row
; j
< row
+ cell_rows
; j
++)
1929 // check w/ anchor cell for multicell block
1930 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1933 if (!grid
.GetTable()->IsEmptyCell(j
+ c_rows
, i
))
1942 rect
.width
+= grid
.GetColSize(i
);
1950 if (rect
.width
>= best_width
)
1954 overflowCols
= i
- col
- cell_cols
+ 1;
1955 if (overflowCols
>= cols
)
1956 overflowCols
= cols
- 1;
1959 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1961 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1963 clip
.x
+= rectCell
.width
;
1964 // draw each overflow cell individually
1965 int col_end
= col
+ cell_cols
+ overflowCols
;
1966 if (col_end
>= grid
.GetNumberCols())
1967 col_end
= grid
.GetNumberCols() - 1;
1968 for (int i
= col
+ cell_cols
; i
<= col_end
; i
++)
1970 clip
.width
= grid
.GetColSize(i
) - 1;
1971 dc
.DestroyClippingRegion();
1972 dc
.SetClippingRegion(clip
);
1974 SetTextColoursAndFont(grid
, attr
, dc
,
1975 grid
.IsInSelection(row
,i
));
1977 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1978 rect
, hAlign
, vAlign
);
1979 clip
.x
+= grid
.GetColSize(i
) - 1;
1985 dc
.DestroyClippingRegion();
1989 // now we only have to draw the text
1990 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1992 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1993 rect
, hAlign
, vAlign
);
1996 // ----------------------------------------------------------------------------
1997 // wxGridCellNumberRenderer
1998 // ----------------------------------------------------------------------------
2000 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2002 wxGridTableBase
*table
= grid
.GetTable();
2004 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
2006 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
2010 text
= table
->GetValue(row
, col
);
2016 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
2017 wxGridCellAttr
& attr
,
2019 const wxRect
& rectCell
,
2023 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2025 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2027 // draw the text right aligned by default
2029 attr
.GetAlignment(&hAlign
, &vAlign
);
2030 hAlign
= wxALIGN_RIGHT
;
2032 wxRect rect
= rectCell
;
2035 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2038 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
2039 wxGridCellAttr
& attr
,
2043 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2046 // ----------------------------------------------------------------------------
2047 // wxGridCellFloatRenderer
2048 // ----------------------------------------------------------------------------
2050 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
2053 SetPrecision(precision
);
2056 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
2058 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
2059 renderer
->m_width
= m_width
;
2060 renderer
->m_precision
= m_precision
;
2061 renderer
->m_format
= m_format
;
2066 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2068 wxGridTableBase
*table
= grid
.GetTable();
2073 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
2075 val
= table
->GetValueAsDouble(row
, col
);
2080 text
= table
->GetValue(row
, col
);
2081 hasDouble
= text
.ToDouble(&val
);
2088 if ( m_width
== -1 )
2090 if ( m_precision
== -1 )
2092 // default width/precision
2093 m_format
= _T("%f");
2097 m_format
.Printf(_T("%%.%df"), m_precision
);
2100 else if ( m_precision
== -1 )
2102 // default precision
2103 m_format
.Printf(_T("%%%d.f"), m_width
);
2107 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
2111 text
.Printf(m_format
, val
);
2114 //else: text already contains the string
2119 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
2120 wxGridCellAttr
& attr
,
2122 const wxRect
& rectCell
,
2126 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2128 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2130 // draw the text right aligned by default
2132 attr
.GetAlignment(&hAlign
, &vAlign
);
2133 hAlign
= wxALIGN_RIGHT
;
2135 wxRect rect
= rectCell
;
2138 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2141 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
2142 wxGridCellAttr
& attr
,
2146 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2149 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
2153 // reset to defaults
2159 wxString tmp
= params
.BeforeFirst(_T(','));
2163 if ( tmp
.ToLong(&width
) )
2165 SetWidth((int)width
);
2169 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
2173 tmp
= params
.AfterFirst(_T(','));
2177 if ( tmp
.ToLong(&precision
) )
2179 SetPrecision((int)precision
);
2183 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
2189 // ----------------------------------------------------------------------------
2190 // wxGridCellBoolRenderer
2191 // ----------------------------------------------------------------------------
2193 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
2195 // FIXME these checkbox size calculations are really ugly...
2197 // between checkmark and box
2198 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2200 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2201 wxGridCellAttr
& WXUNUSED(attr
),
2206 // compute it only once (no locks for MT safeness in GUI thread...)
2207 if ( !ms_sizeCheckMark
.x
)
2209 // get checkbox size
2210 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2211 wxSize size
= checkbox
->GetBestSize();
2212 wxCoord checkSize
= size
.y
+ 2 * wxGRID_CHECKMARK_MARGIN
;
2214 #if defined(__WXMOTIF__)
2215 checkSize
-= size
.y
/ 2;
2220 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2223 return ms_sizeCheckMark
;
2226 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2227 wxGridCellAttr
& attr
,
2233 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2235 // draw a check mark in the centre (ignoring alignment - TODO)
2236 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2238 // don't draw outside the cell
2239 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2240 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2242 // and even leave (at least) 1 pixel margin
2243 size
.x
= size
.y
= minSize
;
2246 // draw a border around checkmark
2248 attr
.GetAlignment(&hAlign
, &vAlign
);
2251 if (hAlign
== wxALIGN_CENTRE
)
2253 rectBorder
.x
= rect
.x
+ rect
.width
/ 2 - size
.x
/ 2;
2254 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2255 rectBorder
.width
= size
.x
;
2256 rectBorder
.height
= size
.y
;
2258 else if (hAlign
== wxALIGN_LEFT
)
2260 rectBorder
.x
= rect
.x
+ 2;
2261 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2262 rectBorder
.width
= size
.x
;
2263 rectBorder
.height
= size
.y
;
2265 else if (hAlign
== wxALIGN_RIGHT
)
2267 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2268 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2269 rectBorder
.width
= size
.x
;
2270 rectBorder
.height
= size
.y
;
2274 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2276 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2280 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2281 value
= wxGridCellBoolEditor::IsTrueValue(cellval
);
2286 flags
|= wxCONTROL_CHECKED
;
2288 wxRendererNative::Get().DrawCheckBox( &grid
, dc
, rectBorder
, flags
);
2291 // ----------------------------------------------------------------------------
2293 // ----------------------------------------------------------------------------
2295 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2299 m_isReadOnly
= Unset
;
2304 m_attrkind
= wxGridCellAttr::Cell
;
2306 m_sizeRows
= m_sizeCols
= 1;
2307 m_overflow
= UnsetOverflow
;
2309 SetDefAttr(attrDefault
);
2312 wxGridCellAttr
*wxGridCellAttr::Clone() const
2314 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2316 if ( HasTextColour() )
2317 attr
->SetTextColour(GetTextColour());
2318 if ( HasBackgroundColour() )
2319 attr
->SetBackgroundColour(GetBackgroundColour());
2321 attr
->SetFont(GetFont());
2322 if ( HasAlignment() )
2323 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2325 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2329 attr
->SetRenderer(m_renderer
);
2330 m_renderer
->IncRef();
2334 attr
->SetEditor(m_editor
);
2339 attr
->SetReadOnly();
2341 attr
->SetOverflow( m_overflow
== Overflow
);
2342 attr
->SetKind( m_attrkind
);
2347 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2349 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2350 SetTextColour(mergefrom
->GetTextColour());
2351 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2352 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2353 if ( !HasFont() && mergefrom
->HasFont() )
2354 SetFont(mergefrom
->GetFont());
2355 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2358 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2359 SetAlignment(hAlign
, vAlign
);
2361 if ( !HasSize() && mergefrom
->HasSize() )
2362 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2364 // Directly access member functions as GetRender/Editor don't just return
2365 // m_renderer/m_editor
2367 // Maybe add support for merge of Render and Editor?
2368 if (!HasRenderer() && mergefrom
->HasRenderer() )
2370 m_renderer
= mergefrom
->m_renderer
;
2371 m_renderer
->IncRef();
2373 if ( !HasEditor() && mergefrom
->HasEditor() )
2375 m_editor
= mergefrom
->m_editor
;
2378 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2379 SetReadOnly(mergefrom
->IsReadOnly());
2381 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2382 SetOverflow(mergefrom
->GetOverflow());
2384 SetDefAttr(mergefrom
->m_defGridAttr
);
2387 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2389 // The size of a cell is normally 1,1
2391 // If this cell is larger (2,2) then this is the top left cell
2392 // the other cells that will be covered (lower right cells) must be
2393 // set to negative or zero values such that
2394 // row + num_rows of the covered cell points to the larger cell (this cell)
2395 // same goes for the col + num_cols.
2397 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2399 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
2400 !((num_rows
<= 0) && (num_cols
> 0)) ||
2401 !((num_rows
== 0) && (num_cols
== 0))),
2402 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2404 m_sizeRows
= num_rows
;
2405 m_sizeCols
= num_cols
;
2408 const wxColour
& wxGridCellAttr::GetTextColour() const
2410 if (HasTextColour())
2414 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2416 return m_defGridAttr
->GetTextColour();
2420 wxFAIL_MSG(wxT("Missing default cell attribute"));
2421 return wxNullColour
;
2425 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2427 if (HasBackgroundColour())
2431 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2433 return m_defGridAttr
->GetBackgroundColour();
2437 wxFAIL_MSG(wxT("Missing default cell attribute"));
2438 return wxNullColour
;
2442 const wxFont
& wxGridCellAttr::GetFont() const
2448 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2450 return m_defGridAttr
->GetFont();
2454 wxFAIL_MSG(wxT("Missing default cell attribute"));
2459 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2468 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2470 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2474 wxFAIL_MSG(wxT("Missing default cell attribute"));
2478 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2481 *num_rows
= m_sizeRows
;
2483 *num_cols
= m_sizeCols
;
2486 // GetRenderer and GetEditor use a slightly different decision path about
2487 // which attribute to use. If a non-default attr object has one then it is
2488 // used, otherwise the default editor or renderer is fetched from the grid and
2489 // used. It should be the default for the data type of the cell. If it is
2490 // NULL (because the table has a type that the grid does not have in its
2491 // registry), then the grid's default editor or renderer is used.
2493 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(const wxGrid
* grid
, int row
, int col
) const
2495 wxGridCellRenderer
*renderer
= NULL
;
2497 if ( m_renderer
&& this != m_defGridAttr
)
2499 // use the cells renderer if it has one
2500 renderer
= m_renderer
;
2503 else // no non-default cell renderer
2505 // get default renderer for the data type
2508 // GetDefaultRendererForCell() will do IncRef() for us
2509 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2512 if ( renderer
== NULL
)
2514 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2516 // if we still don't have one then use the grid default
2517 // (no need for IncRef() here neither)
2518 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2520 else // default grid attr
2522 // use m_renderer which we had decided not to use initially
2523 renderer
= m_renderer
;
2530 // we're supposed to always find something
2531 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2536 // same as above, except for s/renderer/editor/g
2537 wxGridCellEditor
* wxGridCellAttr::GetEditor(const wxGrid
* grid
, int row
, int col
) const
2539 wxGridCellEditor
*editor
= NULL
;
2541 if ( m_editor
&& this != m_defGridAttr
)
2543 // use the cells editor if it has one
2547 else // no non default cell editor
2549 // get default editor for the data type
2552 // GetDefaultEditorForCell() will do IncRef() for us
2553 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2556 if ( editor
== NULL
)
2558 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2560 // if we still don't have one then use the grid default
2561 // (no need for IncRef() here neither)
2562 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2564 else // default grid attr
2566 // use m_editor which we had decided not to use initially
2574 // we're supposed to always find something
2575 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2580 // ----------------------------------------------------------------------------
2581 // wxGridCellAttrData
2582 // ----------------------------------------------------------------------------
2584 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2586 int n
= FindIndex(row
, col
);
2587 if ( n
== wxNOT_FOUND
)
2589 // add the attribute
2590 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2594 // free the old attribute
2595 m_attrs
[(size_t)n
].attr
->DecRef();
2599 // change the attribute
2600 m_attrs
[(size_t)n
].attr
= attr
;
2604 // remove this attribute
2605 m_attrs
.RemoveAt((size_t)n
);
2610 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2612 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2614 int n
= FindIndex(row
, col
);
2615 if ( n
!= wxNOT_FOUND
)
2617 attr
= m_attrs
[(size_t)n
].attr
;
2624 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2626 size_t count
= m_attrs
.GetCount();
2627 for ( size_t n
= 0; n
< count
; n
++ )
2629 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2630 wxCoord row
= coords
.GetRow();
2631 if ((size_t)row
>= pos
)
2635 // If rows inserted, include row counter where necessary
2636 coords
.SetRow(row
+ numRows
);
2638 else if (numRows
< 0)
2640 // If rows deleted ...
2641 if ((size_t)row
>= pos
- numRows
)
2643 // ...either decrement row counter (if row still exists)...
2644 coords
.SetRow(row
+ numRows
);
2648 // ...or remove the attribute
2649 // No need to DecRef the attribute itself since this is
2650 // done be wxGridCellWithAttr's destructor!
2651 m_attrs
.RemoveAt(n
);
2660 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2662 size_t count
= m_attrs
.GetCount();
2663 for ( size_t n
= 0; n
< count
; n
++ )
2665 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2666 wxCoord col
= coords
.GetCol();
2667 if ( (size_t)col
>= pos
)
2671 // If rows inserted, include row counter where necessary
2672 coords
.SetCol(col
+ numCols
);
2674 else if (numCols
< 0)
2676 // If rows deleted ...
2677 if ((size_t)col
>= pos
- numCols
)
2679 // ...either decrement row counter (if row still exists)...
2680 coords
.SetCol(col
+ numCols
);
2684 // ...or remove the attribute
2685 // No need to DecRef the attribute itself since this is
2686 // done be wxGridCellWithAttr's destructor!
2687 m_attrs
.RemoveAt(n
);
2696 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2698 size_t count
= m_attrs
.GetCount();
2699 for ( size_t n
= 0; n
< count
; n
++ )
2701 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2702 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2711 // ----------------------------------------------------------------------------
2712 // wxGridRowOrColAttrData
2713 // ----------------------------------------------------------------------------
2715 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2717 size_t count
= m_attrs
.GetCount();
2718 for ( size_t n
= 0; n
< count
; n
++ )
2720 m_attrs
[n
]->DecRef();
2724 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2726 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2728 int n
= m_rowsOrCols
.Index(rowOrCol
);
2729 if ( n
!= wxNOT_FOUND
)
2731 attr
= m_attrs
[(size_t)n
];
2738 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2740 int i
= m_rowsOrCols
.Index(rowOrCol
);
2741 if ( i
== wxNOT_FOUND
)
2745 // add the attribute
2746 m_rowsOrCols
.Add(rowOrCol
);
2749 // nothing to remove
2753 size_t n
= (size_t)i
;
2756 // change the attribute
2757 m_attrs
[n
]->DecRef();
2762 // remove this attribute
2763 m_attrs
[n
]->DecRef();
2764 m_rowsOrCols
.RemoveAt(n
);
2765 m_attrs
.RemoveAt(n
);
2770 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2772 size_t count
= m_attrs
.GetCount();
2773 for ( size_t n
= 0; n
< count
; n
++ )
2775 int & rowOrCol
= m_rowsOrCols
[n
];
2776 if ( (size_t)rowOrCol
>= pos
)
2778 if ( numRowsOrCols
> 0 )
2780 // If rows inserted, include row counter where necessary
2781 rowOrCol
+= numRowsOrCols
;
2783 else if ( numRowsOrCols
< 0)
2785 // If rows deleted, either decrement row counter (if row still exists)
2786 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2787 rowOrCol
+= numRowsOrCols
;
2790 m_rowsOrCols
.RemoveAt(n
);
2791 m_attrs
[n
]->DecRef();
2792 m_attrs
.RemoveAt(n
);
2801 // ----------------------------------------------------------------------------
2802 // wxGridCellAttrProvider
2803 // ----------------------------------------------------------------------------
2805 wxGridCellAttrProvider::wxGridCellAttrProvider()
2807 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2810 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2815 void wxGridCellAttrProvider::InitData()
2817 m_data
= new wxGridCellAttrProviderData
;
2820 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2821 wxGridCellAttr::wxAttrKind kind
) const
2823 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2828 case (wxGridCellAttr::Any
):
2829 // Get cached merge attributes.
2830 // Currently not used as no cache implemented as not mutable
2831 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2834 // Basically implement old version.
2835 // Also check merge cache, so we don't have to re-merge every time..
2836 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2837 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2838 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2840 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
2842 // Two or more are non NULL
2843 attr
= new wxGridCellAttr
;
2844 attr
->SetKind(wxGridCellAttr::Merged
);
2846 // Order is important..
2849 attr
->MergeWith(attrcell
);
2854 attr
->MergeWith(attrcol
);
2859 attr
->MergeWith(attrrow
);
2863 // store merge attr if cache implemented
2865 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2869 // one or none is non null return it or null.
2888 case (wxGridCellAttr::Cell
):
2889 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2892 case (wxGridCellAttr::Col
):
2893 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2896 case (wxGridCellAttr::Row
):
2897 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2902 // (wxGridCellAttr::Default):
2903 // (wxGridCellAttr::Merged):
2911 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2917 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2920 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2925 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2928 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2933 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2936 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2940 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2942 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2946 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2950 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2952 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2956 // ----------------------------------------------------------------------------
2957 // wxGridTypeRegistry
2958 // ----------------------------------------------------------------------------
2960 wxGridTypeRegistry::~wxGridTypeRegistry()
2962 size_t count
= m_typeinfo
.GetCount();
2963 for ( size_t i
= 0; i
< count
; i
++ )
2964 delete m_typeinfo
[i
];
2967 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2968 wxGridCellRenderer
* renderer
,
2969 wxGridCellEditor
* editor
)
2971 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2973 // is it already registered?
2974 int loc
= FindRegisteredDataType(typeName
);
2975 if ( loc
!= wxNOT_FOUND
)
2977 delete m_typeinfo
[loc
];
2978 m_typeinfo
[loc
] = info
;
2982 m_typeinfo
.Add(info
);
2986 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2988 size_t count
= m_typeinfo
.GetCount();
2989 for ( size_t i
= 0; i
< count
; i
++ )
2991 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
3000 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
3002 int index
= FindRegisteredDataType(typeName
);
3003 if ( index
== wxNOT_FOUND
)
3005 // check whether this is one of the standard ones, in which case
3006 // register it "on the fly"
3008 if ( typeName
== wxGRID_VALUE_STRING
)
3010 RegisterDataType(wxGRID_VALUE_STRING
,
3011 new wxGridCellStringRenderer
,
3012 new wxGridCellTextEditor
);
3015 #endif // wxUSE_TEXTCTRL
3017 if ( typeName
== wxGRID_VALUE_BOOL
)
3019 RegisterDataType(wxGRID_VALUE_BOOL
,
3020 new wxGridCellBoolRenderer
,
3021 new wxGridCellBoolEditor
);
3024 #endif // wxUSE_CHECKBOX
3026 if ( typeName
== wxGRID_VALUE_NUMBER
)
3028 RegisterDataType(wxGRID_VALUE_NUMBER
,
3029 new wxGridCellNumberRenderer
,
3030 new wxGridCellNumberEditor
);
3032 else if ( typeName
== wxGRID_VALUE_FLOAT
)
3034 RegisterDataType(wxGRID_VALUE_FLOAT
,
3035 new wxGridCellFloatRenderer
,
3036 new wxGridCellFloatEditor
);
3039 #endif // wxUSE_TEXTCTRL
3041 if ( typeName
== wxGRID_VALUE_CHOICE
)
3043 RegisterDataType(wxGRID_VALUE_CHOICE
,
3044 new wxGridCellStringRenderer
,
3045 new wxGridCellChoiceEditor
);
3048 #endif // wxUSE_COMBOBOX
3053 // we get here only if just added the entry for this type, so return
3055 index
= m_typeinfo
.GetCount() - 1;
3061 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
3063 int index
= FindDataType(typeName
);
3064 if ( index
== wxNOT_FOUND
)
3066 // the first part of the typename is the "real" type, anything after ':'
3067 // are the parameters for the renderer
3068 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
3069 if ( index
== wxNOT_FOUND
)
3074 wxGridCellRenderer
*renderer
= GetRenderer(index
);
3075 wxGridCellRenderer
*rendererOld
= renderer
;
3076 renderer
= renderer
->Clone();
3077 rendererOld
->DecRef();
3079 wxGridCellEditor
*editor
= GetEditor(index
);
3080 wxGridCellEditor
*editorOld
= editor
;
3081 editor
= editor
->Clone();
3082 editorOld
->DecRef();
3084 // do it even if there are no parameters to reset them to defaults
3085 wxString params
= typeName
.AfterFirst(_T(':'));
3086 renderer
->SetParameters(params
);
3087 editor
->SetParameters(params
);
3089 // register the new typename
3090 RegisterDataType(typeName
, renderer
, editor
);
3092 // we just registered it, it's the last one
3093 index
= m_typeinfo
.GetCount() - 1;
3099 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
3101 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
3108 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
3110 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
3117 // ----------------------------------------------------------------------------
3119 // ----------------------------------------------------------------------------
3121 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
3123 wxGridTableBase::wxGridTableBase()
3125 m_view
= (wxGrid
*) NULL
;
3126 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
3129 wxGridTableBase::~wxGridTableBase()
3131 delete m_attrProvider
;
3134 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
3136 delete m_attrProvider
;
3137 m_attrProvider
= attrProvider
;
3140 bool wxGridTableBase::CanHaveAttributes()
3142 if ( ! GetAttrProvider() )
3144 // use the default attr provider by default
3145 SetAttrProvider(new wxGridCellAttrProvider
);
3151 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
3153 if ( m_attrProvider
)
3154 return m_attrProvider
->GetAttr(row
, col
, kind
);
3156 return (wxGridCellAttr
*)NULL
;
3159 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
3161 if ( m_attrProvider
)
3163 attr
->SetKind(wxGridCellAttr::Cell
);
3164 m_attrProvider
->SetAttr(attr
, row
, col
);
3168 // as we take ownership of the pointer and don't store it, we must
3174 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3176 if ( m_attrProvider
)
3178 attr
->SetKind(wxGridCellAttr::Row
);
3179 m_attrProvider
->SetRowAttr(attr
, row
);
3183 // as we take ownership of the pointer and don't store it, we must
3189 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3191 if ( m_attrProvider
)
3193 attr
->SetKind(wxGridCellAttr::Col
);
3194 m_attrProvider
->SetColAttr(attr
, col
);
3198 // as we take ownership of the pointer and don't store it, we must
3204 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3205 size_t WXUNUSED(numRows
) )
3207 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3212 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3214 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3219 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3220 size_t WXUNUSED(numRows
) )
3222 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3227 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3228 size_t WXUNUSED(numCols
) )
3230 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3235 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3237 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3242 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3243 size_t WXUNUSED(numCols
) )
3245 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3250 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3254 // RD: Starting the rows at zero confuses users,
3255 // no matter how much it makes sense to us geeks.
3261 wxString
wxGridTableBase::GetColLabelValue( int col
)
3263 // default col labels are:
3264 // cols 0 to 25 : A-Z
3265 // cols 26 to 675 : AA-ZZ
3270 for ( n
= 1; ; n
++ )
3272 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
3278 // reverse the string...
3280 for ( i
= 0; i
< n
; i
++ )
3288 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3290 return wxGRID_VALUE_STRING
;
3293 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3294 const wxString
& typeName
)
3296 return typeName
== wxGRID_VALUE_STRING
;
3299 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3301 return CanGetValueAs(row
, col
, typeName
);
3304 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3309 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3314 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3319 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3320 long WXUNUSED(value
) )
3324 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3325 double WXUNUSED(value
) )
3329 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3330 bool WXUNUSED(value
) )
3334 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3335 const wxString
& WXUNUSED(typeName
) )
3340 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3341 const wxString
& WXUNUSED(typeName
),
3342 void* WXUNUSED(value
) )
3346 //////////////////////////////////////////////////////////////////////
3348 // Message class for the grid table to send requests and notifications
3352 wxGridTableMessage::wxGridTableMessage()
3354 m_table
= (wxGridTableBase
*) NULL
;
3360 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3361 int commandInt1
, int commandInt2
)
3365 m_comInt1
= commandInt1
;
3366 m_comInt2
= commandInt2
;
3369 //////////////////////////////////////////////////////////////////////
3371 // A basic grid table for string data. An object of this class will
3372 // created by wxGrid if you don't specify an alternative table class.
3375 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3377 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3379 wxGridStringTable::wxGridStringTable()
3384 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3387 m_data
.Alloc( numRows
);
3390 sa
.Alloc( numCols
);
3391 sa
.Add( wxEmptyString
, numCols
);
3393 m_data
.Add( sa
, numRows
);
3396 wxGridStringTable::~wxGridStringTable()
3400 int wxGridStringTable::GetNumberRows()
3402 return m_data
.GetCount();
3405 int wxGridStringTable::GetNumberCols()
3407 if ( m_data
.GetCount() > 0 )
3408 return m_data
[0].GetCount();
3413 wxString
wxGridStringTable::GetValue( int row
, int col
)
3415 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3417 _T("invalid row or column index in wxGridStringTable") );
3419 return m_data
[row
][col
];
3422 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3424 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3425 _T("invalid row or column index in wxGridStringTable") );
3427 m_data
[row
][col
] = value
;
3430 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3432 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3434 _T("invalid row or column index in wxGridStringTable") );
3436 return (m_data
[row
][col
] == wxEmptyString
);
3439 void wxGridStringTable::Clear()
3442 int numRows
, numCols
;
3444 numRows
= m_data
.GetCount();
3447 numCols
= m_data
[0].GetCount();
3449 for ( row
= 0; row
< numRows
; row
++ )
3451 for ( col
= 0; col
< numCols
; col
++ )
3453 m_data
[row
][col
] = wxEmptyString
;
3459 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3461 size_t curNumRows
= m_data
.GetCount();
3462 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3463 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3465 if ( pos
>= curNumRows
)
3467 return AppendRows( numRows
);
3471 sa
.Alloc( curNumCols
);
3472 sa
.Add( wxEmptyString
, curNumCols
);
3473 m_data
.Insert( sa
, pos
, numRows
);
3477 wxGridTableMessage
msg( this,
3478 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3482 GetView()->ProcessTableMessage( msg
);
3488 bool wxGridStringTable::AppendRows( size_t numRows
)
3490 size_t curNumRows
= m_data
.GetCount();
3491 size_t curNumCols
= ( curNumRows
> 0
3492 ? m_data
[0].GetCount()
3493 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3496 if ( curNumCols
> 0 )
3498 sa
.Alloc( curNumCols
);
3499 sa
.Add( wxEmptyString
, curNumCols
);
3502 m_data
.Add( sa
, numRows
);
3506 wxGridTableMessage
msg( this,
3507 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3510 GetView()->ProcessTableMessage( msg
);
3516 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3518 size_t curNumRows
= m_data
.GetCount();
3520 if ( pos
>= curNumRows
)
3522 wxFAIL_MSG( wxString::Format
3524 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3526 (unsigned long)numRows
,
3527 (unsigned long)curNumRows
3533 if ( numRows
> curNumRows
- pos
)
3535 numRows
= curNumRows
- pos
;
3538 if ( numRows
>= curNumRows
)
3544 m_data
.RemoveAt( pos
, numRows
);
3549 wxGridTableMessage
msg( this,
3550 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3554 GetView()->ProcessTableMessage( msg
);
3560 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3564 size_t curNumRows
= m_data
.GetCount();
3565 size_t curNumCols
= ( curNumRows
> 0
3566 ? m_data
[0].GetCount()
3567 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3569 if ( pos
>= curNumCols
)
3571 return AppendCols( numCols
);
3574 if ( !m_colLabels
.IsEmpty() )
3576 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
3579 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
3580 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
3583 for ( row
= 0; row
< curNumRows
; row
++ )
3585 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3587 m_data
[row
].Insert( wxEmptyString
, col
);
3593 wxGridTableMessage
msg( this,
3594 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3598 GetView()->ProcessTableMessage( msg
);
3604 bool wxGridStringTable::AppendCols( size_t numCols
)
3608 size_t curNumRows
= m_data
.GetCount();
3613 // TODO: something better than this ?
3615 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3620 for ( row
= 0; row
< curNumRows
; row
++ )
3622 m_data
[row
].Add( wxEmptyString
, numCols
);
3627 wxGridTableMessage
msg( this,
3628 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3631 GetView()->ProcessTableMessage( msg
);
3637 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3641 size_t curNumRows
= m_data
.GetCount();
3642 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3643 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3645 if ( pos
>= curNumCols
)
3647 wxFAIL_MSG( wxString::Format
3649 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3651 (unsigned long)numCols
,
3652 (unsigned long)curNumCols
3659 colID
= GetView()->GetColAt( pos
);
3663 if ( numCols
> curNumCols
- colID
)
3665 numCols
= curNumCols
- colID
;
3668 if ( !m_colLabels
.IsEmpty() )
3670 // m_colLabels stores just as many elements as it needs, e.g. if only
3671 // the label of the first column had been set it would have only one
3672 // element and not numCols, so account for it
3673 int nToRm
= m_colLabels
.size() - colID
;
3675 m_colLabels
.RemoveAt( colID
, nToRm
);
3678 for ( row
= 0; row
< curNumRows
; row
++ )
3680 if ( numCols
>= curNumCols
)
3682 m_data
[row
].Clear();
3686 m_data
[row
].RemoveAt( colID
, numCols
);
3692 wxGridTableMessage
msg( this,
3693 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3697 GetView()->ProcessTableMessage( msg
);
3703 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3705 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3707 // using default label
3709 return wxGridTableBase::GetRowLabelValue( row
);
3713 return m_rowLabels
[row
];
3717 wxString
wxGridStringTable::GetColLabelValue( int col
)
3719 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3721 // using default label
3723 return wxGridTableBase::GetColLabelValue( col
);
3727 return m_colLabels
[col
];
3731 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3733 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3735 int n
= m_rowLabels
.GetCount();
3738 for ( i
= n
; i
<= row
; i
++ )
3740 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3744 m_rowLabels
[row
] = value
;
3747 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3749 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3751 int n
= m_colLabels
.GetCount();
3754 for ( i
= n
; i
<= col
; i
++ )
3756 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3760 m_colLabels
[col
] = value
;
3764 //////////////////////////////////////////////////////////////////////
3765 //////////////////////////////////////////////////////////////////////
3767 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
)
3768 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost
)
3771 void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
3773 m_owner
->CancelMouseCapture();
3776 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3778 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow
)
3779 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3780 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3781 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3784 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3786 const wxPoint
&pos
, const wxSize
&size
)
3787 : wxGridSubwindow(parent
, id
, pos
, size
)
3792 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3796 // NO - don't do this because it will set both the x and y origin
3797 // coords to match the parent scrolled window and we just want to
3798 // set the y coord - MB
3800 // m_owner->PrepareDC( dc );
3803 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3804 wxPoint pt
= dc
.GetDeviceOrigin();
3805 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
3807 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3808 m_owner
->DrawRowLabels( dc
, rows
);
3811 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3813 m_owner
->ProcessRowLabelMouseEvent( event
);
3816 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3818 m_owner
->GetEventHandler()->ProcessEvent( event
);
3821 //////////////////////////////////////////////////////////////////////
3823 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3825 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow
)
3826 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3827 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3828 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3831 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3833 const wxPoint
&pos
, const wxSize
&size
)
3834 : wxGridSubwindow(parent
, id
, pos
, size
)
3839 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3843 // NO - don't do this because it will set both the x and y origin
3844 // coords to match the parent scrolled window and we just want to
3845 // set the x coord - MB
3847 // m_owner->PrepareDC( dc );
3850 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3851 wxPoint pt
= dc
.GetDeviceOrigin();
3852 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3853 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
3855 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
3857 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3858 m_owner
->DrawColLabels( dc
, cols
);
3861 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3863 m_owner
->ProcessColLabelMouseEvent( event
);
3866 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3868 m_owner
->GetEventHandler()->ProcessEvent( event
);
3871 //////////////////////////////////////////////////////////////////////
3873 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3875 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow
)
3876 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3877 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3878 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3881 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3883 const wxPoint
&pos
, const wxSize
&size
)
3884 : wxGridSubwindow(parent
, id
, pos
, size
)
3889 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3893 int client_height
= 0;
3894 int client_width
= 0;
3895 GetClientSize( &client_width
, &client_height
);
3897 // VZ: any reason for this ifdef? (FIXME)
3903 rect
.SetWidth( client_width
- 2 );
3904 rect
.SetHeight( client_height
- 2 );
3906 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3908 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
3909 dc
.DrawLine( client_width
- 1, client_height
- 1, client_width
- 1, 0 );
3910 dc
.DrawLine( client_width
- 1, client_height
- 1, 0, client_height
- 1 );
3911 dc
.DrawLine( 0, 0, client_width
, 0 );
3912 dc
.DrawLine( 0, 0, 0, client_height
);
3914 dc
.SetPen( *wxWHITE_PEN
);
3915 dc
.DrawLine( 1, 1, client_width
- 1, 1 );
3916 dc
.DrawLine( 1, 1, 1, client_height
- 1 );
3920 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3922 m_owner
->ProcessCornerLabelMouseEvent( event
);
3925 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3927 m_owner
->GetEventHandler()->ProcessEvent(event
);
3930 //////////////////////////////////////////////////////////////////////
3932 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3934 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow
)
3935 EVT_PAINT( wxGridWindow::OnPaint
)
3936 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3937 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3938 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3939 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3940 EVT_CHAR( wxGridWindow::OnChar
)
3941 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3942 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3943 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3946 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3947 wxGridRowLabelWindow
*rowLblWin
,
3948 wxGridColLabelWindow
*colLblWin
,
3951 const wxSize
&size
)
3952 : wxGridSubwindow(parent
, id
, pos
, size
,
3953 wxWANTS_CHARS
| wxCLIP_CHILDREN
,
3954 wxT("grid window") )
3957 m_rowLabelWin
= rowLblWin
;
3958 m_colLabelWin
= colLblWin
;
3961 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3963 wxPaintDC
dc( this );
3964 m_owner
->PrepareDC( dc
);
3965 wxRegion reg
= GetUpdateRegion();
3966 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
3967 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
3969 #if WXGRID_DRAW_LINES
3970 m_owner
->DrawAllGridLines( dc
, reg
);
3973 m_owner
->DrawGridSpace( dc
);
3974 m_owner
->DrawHighlight( dc
, dirtyCells
);
3977 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3979 wxWindow::ScrollWindow( dx
, dy
, rect
);
3980 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3981 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3984 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3986 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
3989 m_owner
->ProcessGridCellMouseEvent( event
);
3992 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3994 m_owner
->GetEventHandler()->ProcessEvent( event
);
3997 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3998 // cursor must be in the cell edit control to get key events
4000 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
4002 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4006 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
4008 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4012 void wxGridWindow::OnChar( wxKeyEvent
& event
)
4014 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4018 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
4022 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
4024 // and if we have any selection, it has to be repainted, because it
4025 // uses different colour when the grid is not focused:
4026 if ( m_owner
->IsSelection() )
4032 // NB: Note that this code is in "else" branch only because the other
4033 // branch refreshes everything and so there's no point in calling
4034 // Refresh() again, *not* because it should only be done if
4035 // !IsSelection(). If the above code is ever optimized to refresh
4036 // only selected area, this needs to be moved out of the "else"
4037 // branch so that it's always executed.
4039 // current cell cursor {dis,re}appears on focus change:
4040 const wxGridCellCoords
cursorCoords(m_owner
->GetGridCursorRow(),
4041 m_owner
->GetGridCursorCol());
4042 const wxRect cursor
=
4043 m_owner
->BlockToDeviceRect(cursorCoords
, cursorCoords
);
4044 Refresh(true, &cursor
);
4047 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4051 //////////////////////////////////////////////////////////////////////
4053 // Internal Helper function for computing row or column from some
4054 // (unscrolled) coordinate value, using either
4055 // m_defaultRowHeight/m_defaultColWidth or binary search on array
4056 // of m_rowBottoms/m_ColRights to speed up the search!
4058 // Internal helper macros for simpler use of that function
4060 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
4061 const wxArrayInt
& BorderArray
, int nMax
,
4064 #define internalXToCol(x) XToCol(x, true)
4065 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
4066 m_minAcceptableRowHeight, \
4067 m_rowBottoms, m_numRows, true)
4069 /////////////////////////////////////////////////////////////////////
4071 #if wxUSE_EXTENDED_RTTI
4072 WX_DEFINE_FLAGS( wxGridStyle
)
4074 wxBEGIN_FLAGS( wxGridStyle
)
4075 // new style border flags, we put them first to
4076 // use them for streaming out
4077 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4078 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4079 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4080 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4081 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4082 wxFLAGS_MEMBER(wxBORDER_NONE
)
4084 // old style border flags
4085 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4086 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4087 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4088 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4089 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4090 wxFLAGS_MEMBER(wxBORDER
)
4092 // standard window styles
4093 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4094 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4095 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4096 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4097 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4098 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4099 wxFLAGS_MEMBER(wxVSCROLL
)
4100 wxFLAGS_MEMBER(wxHSCROLL
)
4102 wxEND_FLAGS( wxGridStyle
)
4104 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4106 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4107 wxHIDE_PROPERTY( Children
)
4108 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4109 wxEND_PROPERTIES_TABLE()
4111 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4112 wxEND_HANDLERS_TABLE()
4114 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4117 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
4120 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4123 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4124 EVT_PAINT( wxGrid::OnPaint
)
4125 EVT_SIZE( wxGrid::OnSize
)
4126 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
4127 EVT_KEY_UP( wxGrid::OnKeyUp
)
4128 EVT_CHAR ( wxGrid::OnChar
)
4129 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
4134 // in order to make sure that a size event is not
4135 // trigerred in a unfinished state
4136 m_cornerLabelWin
= NULL
;
4137 m_rowLabelWin
= NULL
;
4138 m_colLabelWin
= NULL
;
4142 wxGrid::wxGrid( wxWindow
*parent
,
4147 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 // set to true by CreateGrid
4209 // create the type registry
4210 m_typeRegistry
= new wxGridTypeRegistry
;
4213 m_table
= (wxGridTableBase
*) NULL
;
4216 m_cellEditCtrlEnabled
= false;
4218 m_defaultCellAttr
= new wxGridCellAttr();
4220 // Set default cell attributes
4221 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4222 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4223 m_defaultCellAttr
->SetFont(GetFont());
4224 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4225 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4226 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4229 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4230 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4232 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4233 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4236 m_defaultCellAttr
->SetTextColour(
4237 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4238 m_defaultCellAttr
->SetBackgroundColour(
4239 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4244 m_currentCellCoords
= wxGridNoCellCoords
;
4246 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4247 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4249 // subwindow components that make up the wxGrid
4250 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4255 m_colLabelWin
= new wxGridColLabelWindow( this,
4260 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4265 m_gridWin
= new wxGridWindow( this,
4272 SetTargetWindow( m_gridWin
);
4275 wxColour gfg
= gva
.colFg
;
4276 wxColour gbg
= gva
.colBg
;
4277 wxColour lfg
= lva
.colFg
;
4278 wxColour lbg
= lva
.colBg
;
4280 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4281 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4282 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4283 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4286 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4287 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4288 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4289 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4290 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4291 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4293 m_gridWin
->SetOwnForegroundColour(gfg
);
4294 m_gridWin
->SetOwnBackgroundColour(gbg
);
4299 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4300 wxGrid::wxGridSelectionModes selmode
)
4302 wxCHECK_MSG( !m_created
,
4304 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4306 m_numRows
= numRows
;
4307 m_numCols
= numCols
;
4309 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4310 m_table
->SetView( this );
4312 m_selection
= new wxGridSelection( this, selmode
);
4321 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4323 wxCHECK_RET( m_created
,
4324 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4326 m_selection
->SetSelectionMode( selmode
);
4329 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4331 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4332 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4334 return m_selection
->GetSelectionMode();
4337 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4338 wxGrid::wxGridSelectionModes selmode
)
4340 bool checkSelection
= false;
4343 // stop all processing
4348 m_table
->SetView(0);
4360 checkSelection
= true;
4362 // kill row and column size arrays
4363 m_colWidths
.Empty();
4364 m_colRights
.Empty();
4365 m_rowHeights
.Empty();
4366 m_rowBottoms
.Empty();
4371 m_numRows
= table
->GetNumberRows();
4372 m_numCols
= table
->GetNumberCols();
4375 m_table
->SetView( this );
4376 m_ownTable
= takeOwnership
;
4377 m_selection
= new wxGridSelection( this, selmode
);
4380 // If the newly set table is smaller than the
4381 // original one current cell and selection regions
4382 // might be invalid,
4383 m_selectingKeyboard
= wxGridNoCellCoords
;
4384 m_currentCellCoords
=
4385 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
4386 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
4387 if (m_selectingTopLeft
.GetRow() >= m_numRows
||
4388 m_selectingTopLeft
.GetCol() >= m_numCols
)
4390 m_selectingTopLeft
= wxGridNoCellCoords
;
4391 m_selectingBottomRight
= wxGridNoCellCoords
;
4394 m_selectingBottomRight
=
4395 wxGridCellCoords(wxMin(m_numRows
,
4396 m_selectingBottomRight
.GetRow()),
4398 m_selectingBottomRight
.GetCol()));
4410 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4411 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4413 if ( m_rowLabelWin
)
4415 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4419 m_labelBackgroundColour
= *wxWHITE
;
4422 m_labelTextColour
= *wxBLACK
;
4425 m_attrCache
.row
= -1;
4426 m_attrCache
.col
= -1;
4427 m_attrCache
.attr
= NULL
;
4429 // TODO: something better than this ?
4431 m_labelFont
= this->GetFont();
4432 m_labelFont
.SetWeight( wxBOLD
);
4434 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4435 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4437 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4438 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4439 m_colLabelTextOrientation
= wxHORIZONTAL
;
4441 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4442 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4444 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4445 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4447 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4448 m_defaultRowHeight
+= 8;
4450 m_defaultRowHeight
+= 4;
4453 m_gridLineColour
= wxColour( 192,192,192 );
4454 m_gridLinesEnabled
= true;
4455 m_cellHighlightColour
= *wxBLACK
;
4456 m_cellHighlightPenWidth
= 2;
4457 m_cellHighlightROPenWidth
= 1;
4459 m_canDragColMove
= false;
4461 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4462 m_winCapture
= (wxWindow
*)NULL
;
4463 m_canDragRowSize
= true;
4464 m_canDragColSize
= true;
4465 m_canDragGridSize
= true;
4466 m_canDragCell
= false;
4468 m_dragRowOrCol
= -1;
4469 m_isDragging
= false;
4470 m_startDragPos
= wxDefaultPosition
;
4471 m_nativeColumnLabels
= false;
4473 m_waitForSlowClick
= false;
4475 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4476 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4478 m_currentCellCoords
= wxGridNoCellCoords
;
4482 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4483 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4485 m_editable
= true; // default for whole grid
4487 m_inOnKeyDown
= false;
4493 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4494 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4497 // ----------------------------------------------------------------------------
4498 // the idea is to call these functions only when necessary because they create
4499 // quite big arrays which eat memory mostly unnecessary - in particular, if
4500 // default widths/heights are used for all rows/columns, we may not use these
4503 // with some extra code, it should be possible to only store the widths/heights
4504 // different from default ones (resulting in space savings for huge grids) but
4505 // this is not done currently
4506 // ----------------------------------------------------------------------------
4508 void wxGrid::InitRowHeights()
4510 m_rowHeights
.Empty();
4511 m_rowBottoms
.Empty();
4513 m_rowHeights
.Alloc( m_numRows
);
4514 m_rowBottoms
.Alloc( m_numRows
);
4516 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4519 for ( int i
= 0; i
< m_numRows
; i
++ )
4521 rowBottom
+= m_defaultRowHeight
;
4522 m_rowBottoms
.Add( rowBottom
);
4526 void wxGrid::InitColWidths()
4528 m_colWidths
.Empty();
4529 m_colRights
.Empty();
4531 m_colWidths
.Alloc( m_numCols
);
4532 m_colRights
.Alloc( m_numCols
);
4534 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4537 for ( int i
= 0; i
< m_numCols
; i
++ )
4539 colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
4540 m_colRights
.Add( colRight
);
4544 int wxGrid::GetColWidth(int col
) const
4546 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4549 int wxGrid::GetColLeft(int col
) const
4551 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
4552 : m_colRights
[col
] - m_colWidths
[col
];
4555 int wxGrid::GetColRight(int col
) const
4557 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
4561 int wxGrid::GetRowHeight(int row
) const
4563 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4566 int wxGrid::GetRowTop(int row
) const
4568 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4569 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4572 int wxGrid::GetRowBottom(int row
) const
4574 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4575 : m_rowBottoms
[row
];
4578 void wxGrid::CalcDimensions()
4580 // compute the size of the scrollable area
4581 int w
= m_numCols
> 0 ? GetColRight(GetColAt(m_numCols
- 1)) : 0;
4582 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
4587 // take into account editor if shown
4588 if ( IsCellEditControlShown() )
4591 int r
= m_currentCellCoords
.GetRow();
4592 int c
= m_currentCellCoords
.GetCol();
4593 int x
= GetColLeft(c
);
4594 int y
= GetRowTop(r
);
4596 // how big is the editor
4597 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4598 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4599 editor
->GetControl()->GetSize(&w2
, &h2
);
4610 // preserve (more or less) the previous position
4612 GetViewStart( &x
, &y
);
4614 // ensure the position is valid for the new scroll ranges
4616 x
= wxMax( w
- 1, 0 );
4618 y
= wxMax( h
- 1, 0 );
4620 // do set scrollbar parameters
4621 SetScrollbars( m_scrollLineX
, m_scrollLineY
,
4622 GetScrollX(w
), GetScrollY(h
),
4624 GetBatchCount() != 0);
4626 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4627 // still must reposition the children
4631 void wxGrid::CalcWindowSizes()
4633 // escape if the window is has not been fully created yet
4635 if ( m_cornerLabelWin
== NULL
)
4639 GetClientSize( &cw
, &ch
);
4641 // this block of code tries to work around the following problem: the grid
4642 // could have been just resized to have enough space to show the full grid
4643 // window contents without the scrollbars, but its client size could be
4644 // not big enough because the grid has the scrollbars right now and so the
4645 // scrollbars would remain even though we don't need them any more
4647 // to prevent this from happening, check if we have enough space for
4648 // everything without the scrollbars and explicitly disable them then
4649 wxSize size
= GetSize() - GetWindowBorderSize();
4650 if ( size
!= wxSize(cw
, ch
) )
4652 // check if we have enough space for grid window after accounting for
4653 // the fixed size elements
4654 size
.x
-= m_rowLabelWidth
;
4655 size
.y
-= m_colLabelHeight
;
4657 const wxSize vsize
= m_gridWin
->GetVirtualSize();
4659 if ( size
.x
>= vsize
.x
&& size
.y
>= vsize
.y
)
4661 // yes, we do, so remove the scrollbars and use the new client size
4662 // (which should be the same as full window size - borders now)
4663 SetScrollbars(0, 0, 0, 0);
4664 GetClientSize(&cw
, &ch
);
4668 // the grid may be too small to have enough space for the labels yet, don't
4669 // size the windows to negative sizes in this case
4670 int gw
= cw
- m_rowLabelWidth
;
4671 int gh
= ch
- m_colLabelHeight
;
4677 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4678 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4680 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4681 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, gw
, m_colLabelHeight
);
4683 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4684 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, gh
);
4686 if ( m_gridWin
&& m_gridWin
->IsShown() )
4687 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, gw
, gh
);
4690 // this is called when the grid table sends a message
4691 // to indicate that it has been redimensioned
4693 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4696 bool result
= false;
4698 // Clear the attribute cache as the attribute might refer to a different
4699 // cell than stored in the cache after adding/removing rows/columns.
4702 // By the same reasoning, the editor should be dismissed if columns are
4703 // added or removed. And for consistency, it should IMHO always be
4704 // removed, not only if the cell "underneath" it actually changes.
4705 // For now, I intentionally do not save the editor's content as the
4706 // cell it might want to save that stuff to might no longer exist.
4707 HideCellEditControl();
4710 // if we were using the default widths/heights so far, we must change them
4712 if ( m_colWidths
.IsEmpty() )
4717 if ( m_rowHeights
.IsEmpty() )
4723 switch ( msg
.GetId() )
4725 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4727 size_t pos
= msg
.GetCommandInt();
4728 int numRows
= msg
.GetCommandInt2();
4730 m_numRows
+= numRows
;
4732 if ( !m_rowHeights
.IsEmpty() )
4734 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4735 m_rowBottoms
.Insert( 0, pos
, numRows
);
4739 bottom
= m_rowBottoms
[pos
- 1];
4741 for ( i
= pos
; i
< m_numRows
; i
++ )
4743 bottom
+= m_rowHeights
[i
];
4744 m_rowBottoms
[i
] = bottom
;
4748 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4750 // if we have just inserted cols into an empty grid the current
4751 // cell will be undefined...
4753 SetCurrentCell( 0, 0 );
4757 m_selection
->UpdateRows( pos
, numRows
);
4758 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4760 attrProvider
->UpdateAttrRows( pos
, numRows
);
4762 if ( !GetBatchCount() )
4765 m_rowLabelWin
->Refresh();
4771 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4773 int numRows
= msg
.GetCommandInt();
4774 int oldNumRows
= m_numRows
;
4775 m_numRows
+= numRows
;
4777 if ( !m_rowHeights
.IsEmpty() )
4779 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4780 m_rowBottoms
.Add( 0, numRows
);
4783 if ( oldNumRows
> 0 )
4784 bottom
= m_rowBottoms
[oldNumRows
- 1];
4786 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4788 bottom
+= m_rowHeights
[i
];
4789 m_rowBottoms
[i
] = bottom
;
4793 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4795 // if we have just inserted cols into an empty grid the current
4796 // cell will be undefined...
4798 SetCurrentCell( 0, 0 );
4801 if ( !GetBatchCount() )
4804 m_rowLabelWin
->Refresh();
4810 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4812 size_t pos
= msg
.GetCommandInt();
4813 int numRows
= msg
.GetCommandInt2();
4814 m_numRows
-= numRows
;
4816 if ( !m_rowHeights
.IsEmpty() )
4818 m_rowHeights
.RemoveAt( pos
, numRows
);
4819 m_rowBottoms
.RemoveAt( pos
, numRows
);
4822 for ( i
= 0; i
< m_numRows
; i
++ )
4824 h
+= m_rowHeights
[i
];
4825 m_rowBottoms
[i
] = h
;
4831 m_currentCellCoords
= wxGridNoCellCoords
;
4835 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4836 m_currentCellCoords
.Set( 0, 0 );
4840 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4841 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4844 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4846 // ifdef'd out following patch from Paul Gammans
4848 // No need to touch column attributes, unless we
4849 // removed _all_ rows, in this case, we remove
4850 // all column attributes.
4851 // I hate to do this here, but the
4852 // needed data is not available inside UpdateAttrRows.
4853 if ( !GetNumberRows() )
4854 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4858 if ( !GetBatchCount() )
4861 m_rowLabelWin
->Refresh();
4867 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4869 size_t pos
= msg
.GetCommandInt();
4870 int numCols
= msg
.GetCommandInt2();
4871 m_numCols
+= numCols
;
4873 if ( !m_colAt
.IsEmpty() )
4875 //Shift the column IDs
4877 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
4879 if ( m_colAt
[i
] >= (int)pos
)
4880 m_colAt
[i
] += numCols
;
4883 m_colAt
.Insert( pos
, pos
, numCols
);
4885 //Set the new columns' positions
4886 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
4892 if ( !m_colWidths
.IsEmpty() )
4894 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4895 m_colRights
.Insert( 0, pos
, numCols
);
4899 right
= m_colRights
[GetColAt( pos
- 1 )];
4902 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
4904 i
= GetColAt( colPos
);
4906 right
+= m_colWidths
[i
];
4907 m_colRights
[i
] = right
;
4911 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4913 // if we have just inserted cols into an empty grid the current
4914 // cell will be undefined...
4916 SetCurrentCell( 0, 0 );
4920 m_selection
->UpdateCols( pos
, numCols
);
4921 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4923 attrProvider
->UpdateAttrCols( pos
, numCols
);
4924 if ( !GetBatchCount() )
4927 m_colLabelWin
->Refresh();
4933 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4935 int numCols
= msg
.GetCommandInt();
4936 int oldNumCols
= m_numCols
;
4937 m_numCols
+= numCols
;
4939 if ( !m_colAt
.IsEmpty() )
4941 m_colAt
.Add( 0, numCols
);
4943 //Set the new columns' positions
4945 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4951 if ( !m_colWidths
.IsEmpty() )
4953 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4954 m_colRights
.Add( 0, numCols
);
4957 if ( oldNumCols
> 0 )
4958 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
4961 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
4963 i
= GetColAt( colPos
);
4965 right
+= m_colWidths
[i
];
4966 m_colRights
[i
] = right
;
4970 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4972 // if we have just inserted cols into an empty grid the current
4973 // cell will be undefined...
4975 SetCurrentCell( 0, 0 );
4977 if ( !GetBatchCount() )
4980 m_colLabelWin
->Refresh();
4986 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4988 size_t pos
= msg
.GetCommandInt();
4989 int numCols
= msg
.GetCommandInt2();
4990 m_numCols
-= numCols
;
4992 if ( !m_colAt
.IsEmpty() )
4994 int colID
= GetColAt( pos
);
4996 m_colAt
.RemoveAt( pos
, numCols
);
4998 //Shift the column IDs
5000 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5002 if ( m_colAt
[colPos
] > colID
)
5003 m_colAt
[colPos
] -= numCols
;
5007 if ( !m_colWidths
.IsEmpty() )
5009 m_colWidths
.RemoveAt( pos
, numCols
);
5010 m_colRights
.RemoveAt( pos
, numCols
);
5014 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5016 i
= GetColAt( colPos
);
5018 w
+= m_colWidths
[i
];
5025 m_currentCellCoords
= wxGridNoCellCoords
;
5029 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
5030 m_currentCellCoords
.Set( 0, 0 );
5034 m_selection
->UpdateCols( pos
, -((int)numCols
) );
5035 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5038 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
5040 // ifdef'd out following patch from Paul Gammans
5042 // No need to touch row attributes, unless we
5043 // removed _all_ columns, in this case, we remove
5044 // all row attributes.
5045 // I hate to do this here, but the
5046 // needed data is not available inside UpdateAttrCols.
5047 if ( !GetNumberCols() )
5048 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
5052 if ( !GetBatchCount() )
5055 m_colLabelWin
->Refresh();
5062 if (result
&& !GetBatchCount() )
5063 m_gridWin
->Refresh();
5068 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
) const
5070 wxRegionIterator
iter( reg
);
5073 wxArrayInt rowlabels
;
5080 // TODO: remove this when we can...
5081 // There is a bug in wxMotif that gives garbage update
5082 // rectangles if you jump-scroll a long way by clicking the
5083 // scrollbar with middle button. This is a work-around
5085 #if defined(__WXMOTIF__)
5087 m_gridWin
->GetClientSize( &cw
, &ch
);
5088 if ( r
.GetTop() > ch
)
5090 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5093 // logical bounds of update region
5096 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
5097 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
5099 // find the row labels within these bounds
5102 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5104 if ( GetRowBottom(row
) < top
)
5107 if ( GetRowTop(row
) > bottom
)
5110 rowlabels
.Add( row
);
5119 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
) const
5121 wxRegionIterator
iter( reg
);
5124 wxArrayInt colLabels
;
5131 // TODO: remove this when we can...
5132 // There is a bug in wxMotif that gives garbage update
5133 // rectangles if you jump-scroll a long way by clicking the
5134 // scrollbar with middle button. This is a work-around
5136 #if defined(__WXMOTIF__)
5138 m_gridWin
->GetClientSize( &cw
, &ch
);
5139 if ( r
.GetLeft() > cw
)
5141 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5144 // logical bounds of update region
5147 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
5148 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
5150 // find the cells within these bounds
5154 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5156 col
= GetColAt( colPos
);
5158 if ( GetColRight(col
) < left
)
5161 if ( GetColLeft(col
) > right
)
5164 colLabels
.Add( col
);
5173 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
) const
5175 wxRegionIterator
iter( reg
);
5178 wxGridCellCoordsArray cellsExposed
;
5180 int left
, top
, right
, bottom
;
5185 // TODO: remove this when we can...
5186 // There is a bug in wxMotif that gives garbage update
5187 // rectangles if you jump-scroll a long way by clicking the
5188 // scrollbar with middle button. This is a work-around
5190 #if defined(__WXMOTIF__)
5192 m_gridWin
->GetClientSize( &cw
, &ch
);
5193 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5194 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5195 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5196 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5199 // logical bounds of update region
5201 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5202 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5204 // find the cells within these bounds
5207 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5209 if ( GetRowBottom(row
) <= top
)
5212 if ( GetRowTop(row
) > bottom
)
5216 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5218 col
= GetColAt( colPos
);
5220 if ( GetColRight(col
) <= left
)
5223 if ( GetColLeft(col
) > right
)
5226 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
5233 return cellsExposed
;
5237 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5240 wxPoint
pos( event
.GetPosition() );
5241 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5243 if ( event
.Dragging() )
5247 m_isDragging
= true;
5248 m_rowLabelWin
->CaptureMouse();
5251 if ( event
.LeftIsDown() )
5253 switch ( m_cursorMode
)
5255 case WXGRID_CURSOR_RESIZE_ROW
:
5257 int cw
, ch
, left
, dummy
;
5258 m_gridWin
->GetClientSize( &cw
, &ch
);
5259 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5261 wxClientDC
dc( m_gridWin
);
5264 GetRowTop(m_dragRowOrCol
) +
5265 GetRowMinimalHeight(m_dragRowOrCol
) );
5266 dc
.SetLogicalFunction(wxINVERT
);
5267 if ( m_dragLastPos
>= 0 )
5269 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5271 dc
.DrawLine( left
, y
, left
+cw
, y
);
5276 case WXGRID_CURSOR_SELECT_ROW
:
5278 if ( (row
= YToRow( y
)) >= 0 )
5282 m_selection
->SelectRow( row
,
5283 event
.ControlDown(),
5292 // default label to suppress warnings about "enumeration value
5293 // 'xxx' not handled in switch
5301 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5306 if (m_rowLabelWin
->HasCapture())
5307 m_rowLabelWin
->ReleaseMouse();
5308 m_isDragging
= false;
5311 // ------------ Entering or leaving the window
5313 if ( event
.Entering() || event
.Leaving() )
5315 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5318 // ------------ Left button pressed
5320 else if ( event
.LeftDown() )
5322 // don't send a label click event for a hit on the
5323 // edge of the row label - this is probably the user
5324 // wanting to resize the row
5326 if ( YToEdgeOfRow(y
) < 0 )
5330 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5332 if ( !event
.ShiftDown() && !event
.CmdDown() )
5336 if ( event
.ShiftDown() )
5338 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5341 GetNumberCols() - 1,
5342 event
.ControlDown(),
5349 m_selection
->SelectRow( row
,
5350 event
.ControlDown(),
5357 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5362 // starting to drag-resize a row
5363 if ( CanDragRowSize() )
5364 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5368 // ------------ Left double click
5370 else if (event
.LeftDClick() )
5372 row
= YToEdgeOfRow(y
);
5377 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5379 // no default action at the moment
5384 // adjust row height depending on label text
5385 AutoSizeRowLabelSize( row
);
5387 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5392 // ------------ Left button released
5394 else if ( event
.LeftUp() )
5396 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5398 DoEndDragResizeRow();
5400 // Note: we are ending the event *after* doing
5401 // default processing in this case
5403 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5406 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5410 // ------------ Right button down
5412 else if ( event
.RightDown() )
5416 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5418 // no default action at the moment
5422 // ------------ Right double click
5424 else if ( event
.RightDClick() )
5428 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5430 // no default action at the moment
5434 // ------------ No buttons down and mouse moving
5436 else if ( event
.Moving() )
5438 m_dragRowOrCol
= YToEdgeOfRow( y
);
5439 if ( m_dragRowOrCol
>= 0 )
5441 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5443 // don't capture the mouse yet
5444 if ( CanDragRowSize() )
5445 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5448 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5450 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5455 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5458 wxPoint
pos( event
.GetPosition() );
5459 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5461 if ( event
.Dragging() )
5465 m_isDragging
= true;
5466 m_colLabelWin
->CaptureMouse();
5468 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
)
5469 m_dragRowOrCol
= XToCol( x
);
5472 if ( event
.LeftIsDown() )
5474 switch ( m_cursorMode
)
5476 case WXGRID_CURSOR_RESIZE_COL
:
5478 int cw
, ch
, dummy
, top
;
5479 m_gridWin
->GetClientSize( &cw
, &ch
);
5480 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5482 wxClientDC
dc( m_gridWin
);
5485 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5486 GetColMinimalWidth(m_dragRowOrCol
));
5487 dc
.SetLogicalFunction(wxINVERT
);
5488 if ( m_dragLastPos
>= 0 )
5490 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5492 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5497 case WXGRID_CURSOR_SELECT_COL
:
5499 if ( (col
= XToCol( x
)) >= 0 )
5503 m_selection
->SelectCol( col
,
5504 event
.ControlDown(),
5513 case WXGRID_CURSOR_MOVE_COL
:
5516 m_moveToCol
= GetColAt( 0 );
5518 m_moveToCol
= XToCol( x
);
5522 if ( m_moveToCol
< 0 )
5523 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5525 markerX
= GetColLeft( m_moveToCol
);
5527 if ( markerX
!= m_dragLastPos
)
5529 wxClientDC
dc( m_colLabelWin
);
5532 m_colLabelWin
->GetClientSize( &cw
, &ch
);
5536 //Clean up the last indicator
5537 if ( m_dragLastPos
>= 0 )
5539 wxPen
pen( m_colLabelWin
->GetBackgroundColour(), 2 );
5541 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
5542 dc
.SetPen(wxNullPen
);
5544 if ( XToCol( m_dragLastPos
) != -1 )
5545 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
5548 //Moving to the same place? Don't draw a marker
5549 if ( (m_moveToCol
== m_dragRowOrCol
)
5550 || (GetColPos( m_moveToCol
) == GetColPos( m_dragRowOrCol
) + 1)
5551 || (m_moveToCol
< 0 && m_dragRowOrCol
== GetColAt( m_numCols
- 1 )))
5558 wxPen
pen( *wxBLUE
, 2 );
5561 dc
.DrawLine( markerX
, 0, markerX
, ch
);
5563 dc
.SetPen(wxNullPen
);
5565 m_dragLastPos
= markerX
- 1;
5570 // default label to suppress warnings about "enumeration value
5571 // 'xxx' not handled in switch
5579 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5584 if (m_colLabelWin
->HasCapture())
5585 m_colLabelWin
->ReleaseMouse();
5586 m_isDragging
= false;
5589 // ------------ Entering or leaving the window
5591 if ( event
.Entering() || event
.Leaving() )
5593 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5596 // ------------ Left button pressed
5598 else if ( event
.LeftDown() )
5600 // don't send a label click event for a hit on the
5601 // edge of the col label - this is probably the user
5602 // wanting to resize the col
5604 if ( XToEdgeOfCol(x
) < 0 )
5608 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5610 if ( m_canDragColMove
)
5612 //Show button as pressed
5613 wxClientDC
dc( m_colLabelWin
);
5614 int colLeft
= GetColLeft( col
);
5615 int colRight
= GetColRight( col
) - 1;
5616 dc
.SetPen( wxPen( m_colLabelWin
->GetBackgroundColour(), 1 ) );
5617 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
5618 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
5620 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, m_colLabelWin
);
5624 if ( !event
.ShiftDown() && !event
.CmdDown() )
5628 if ( event
.ShiftDown() )
5630 m_selection
->SelectBlock( 0,
5631 m_currentCellCoords
.GetCol(),
5632 GetNumberRows() - 1, col
,
5633 event
.ControlDown(),
5640 m_selection
->SelectCol( col
,
5641 event
.ControlDown(),
5648 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5654 // starting to drag-resize a col
5656 if ( CanDragColSize() )
5657 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5661 // ------------ Left double click
5663 if ( event
.LeftDClick() )
5665 col
= XToEdgeOfCol(x
);
5670 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5672 // no default action at the moment
5677 // adjust column width depending on label text
5678 AutoSizeColLabelSize( col
);
5680 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5685 // ------------ Left button released
5687 else if ( event
.LeftUp() )
5689 switch ( m_cursorMode
)
5691 case WXGRID_CURSOR_RESIZE_COL
:
5692 DoEndDragResizeCol();
5694 // Note: we are ending the event *after* doing
5695 // default processing in this case
5697 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5700 case WXGRID_CURSOR_MOVE_COL
:
5703 SendEvent( wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
, event
);
5706 case WXGRID_CURSOR_SELECT_COL
:
5707 case WXGRID_CURSOR_SELECT_CELL
:
5708 case WXGRID_CURSOR_RESIZE_ROW
:
5709 case WXGRID_CURSOR_SELECT_ROW
:
5710 // nothing to do (?)
5714 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5718 // ------------ Right button down
5720 else if ( event
.RightDown() )
5724 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5726 // no default action at the moment
5730 // ------------ Right double click
5732 else if ( event
.RightDClick() )
5736 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5738 // no default action at the moment
5742 // ------------ No buttons down and mouse moving
5744 else if ( event
.Moving() )
5746 m_dragRowOrCol
= XToEdgeOfCol( x
);
5747 if ( m_dragRowOrCol
>= 0 )
5749 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5751 // don't capture the cursor yet
5752 if ( CanDragColSize() )
5753 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5756 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5758 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5763 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5765 if ( event
.LeftDown() )
5767 // indicate corner label by having both row and
5770 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5775 else if ( event
.LeftDClick() )
5777 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5779 else if ( event
.RightDown() )
5781 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5783 // no default action at the moment
5786 else if ( event
.RightDClick() )
5788 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5790 // no default action at the moment
5795 void wxGrid::CancelMouseCapture()
5797 // cancel operation currently in progress, whatever it is
5800 m_isDragging
= false;
5801 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
5802 m_winCapture
->SetCursor( *wxSTANDARD_CURSOR
);
5803 m_winCapture
= NULL
;
5805 // remove traces of whatever we drew on screen
5810 void wxGrid::ChangeCursorMode(CursorMode mode
,
5815 static const wxChar
*cursorModes
[] =
5825 wxLogTrace(_T("grid"),
5826 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5827 win
== m_colLabelWin
? _T("colLabelWin")
5828 : win
? _T("rowLabelWin")
5830 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5833 if ( mode
== m_cursorMode
&&
5834 win
== m_winCapture
&&
5835 captureMouse
== (m_winCapture
!= NULL
))
5840 // by default use the grid itself
5846 if (m_winCapture
->HasCapture())
5847 m_winCapture
->ReleaseMouse();
5848 m_winCapture
= (wxWindow
*)NULL
;
5851 m_cursorMode
= mode
;
5853 switch ( m_cursorMode
)
5855 case WXGRID_CURSOR_RESIZE_ROW
:
5856 win
->SetCursor( m_rowResizeCursor
);
5859 case WXGRID_CURSOR_RESIZE_COL
:
5860 win
->SetCursor( m_colResizeCursor
);
5863 case WXGRID_CURSOR_MOVE_COL
:
5864 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
5868 win
->SetCursor( *wxSTANDARD_CURSOR
);
5872 // we need to capture mouse when resizing
5873 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5874 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5876 if ( captureMouse
&& resize
)
5878 win
->CaptureMouse();
5883 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5886 wxPoint
pos( event
.GetPosition() );
5887 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5889 wxGridCellCoords coords
;
5890 XYToCell( x
, y
, coords
);
5892 int cell_rows
, cell_cols
;
5893 bool isFirstDrag
= !m_isDragging
;
5894 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5895 if ((cell_rows
< 0) || (cell_cols
< 0))
5897 coords
.SetRow(coords
.GetRow() + cell_rows
);
5898 coords
.SetCol(coords
.GetCol() + cell_cols
);
5901 if ( event
.Dragging() )
5903 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5905 // Don't start doing anything until the mouse has been dragged at
5906 // least 3 pixels in any direction...
5909 if (m_startDragPos
== wxDefaultPosition
)
5911 m_startDragPos
= pos
;
5914 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5918 m_isDragging
= true;
5919 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5921 // Hide the edit control, so it
5922 // won't interfere with drag-shrinking.
5923 if ( IsCellEditControlShown() )
5925 HideCellEditControl();
5926 SaveEditControlValue();
5929 if ( coords
!= wxGridNoCellCoords
)
5931 if ( event
.CmdDown() )
5933 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5934 m_selectingKeyboard
= coords
;
5935 HighlightBlock( m_selectingKeyboard
, coords
);
5937 else if ( CanDragCell() )
5941 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5942 m_selectingKeyboard
= coords
;
5944 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5953 if ( !IsSelection() )
5955 HighlightBlock( coords
, coords
);
5959 HighlightBlock( m_currentCellCoords
, coords
);
5963 if (! IsVisible(coords
))
5965 MakeCellVisible(coords
);
5966 // TODO: need to introduce a delay or something here. The
5967 // scrolling is way to fast, at least on MSW - also on GTK.
5970 // Have we captured the mouse yet?
5973 m_winCapture
= m_gridWin
;
5974 m_winCapture
->CaptureMouse();
5979 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5981 int cw
, ch
, left
, dummy
;
5982 m_gridWin
->GetClientSize( &cw
, &ch
);
5983 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5985 wxClientDC
dc( m_gridWin
);
5987 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5988 GetRowMinimalHeight(m_dragRowOrCol
) );
5989 dc
.SetLogicalFunction(wxINVERT
);
5990 if ( m_dragLastPos
>= 0 )
5992 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5994 dc
.DrawLine( left
, y
, left
+cw
, y
);
5997 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5999 int cw
, ch
, dummy
, top
;
6000 m_gridWin
->GetClientSize( &cw
, &ch
);
6001 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6003 wxClientDC
dc( m_gridWin
);
6005 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
6006 GetColMinimalWidth(m_dragRowOrCol
) );
6007 dc
.SetLogicalFunction(wxINVERT
);
6008 if ( m_dragLastPos
>= 0 )
6010 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6012 dc
.DrawLine( x
, top
, x
, top
+ ch
);
6019 m_isDragging
= false;
6020 m_startDragPos
= wxDefaultPosition
;
6022 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
6023 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
6026 if ( event
.Entering() || event
.Leaving() )
6028 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6029 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
6034 // ------------ Left button pressed
6036 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
6038 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
6043 if ( !event
.CmdDown() )
6045 if ( event
.ShiftDown() )
6049 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
6050 m_currentCellCoords
.GetCol(),
6053 event
.ControlDown(),
6059 else if ( XToEdgeOfCol(x
) < 0 &&
6060 YToEdgeOfRow(y
) < 0 )
6062 DisableCellEditControl();
6063 MakeCellVisible( coords
);
6065 if ( event
.CmdDown() )
6069 m_selection
->ToggleCellSelection( coords
.GetRow(),
6071 event
.ControlDown(),
6076 m_selectingTopLeft
= wxGridNoCellCoords
;
6077 m_selectingBottomRight
= wxGridNoCellCoords
;
6078 m_selectingKeyboard
= coords
;
6082 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
6083 SetCurrentCell( coords
);
6086 if ( m_selection
->GetSelectionMode() !=
6087 wxGrid::wxGridSelectCells
)
6089 HighlightBlock( coords
, coords
);
6097 // ------------ Left double click
6099 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
6101 DisableCellEditControl();
6103 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
6105 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
6110 // we want double click to select a cell and start editing
6111 // (i.e. to behave in same way as sequence of two slow clicks):
6112 m_waitForSlowClick
= true;
6117 // ------------ Left button released
6119 else if ( event
.LeftUp() )
6121 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6125 if (m_winCapture
->HasCapture())
6126 m_winCapture
->ReleaseMouse();
6127 m_winCapture
= NULL
;
6130 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
6133 EnableCellEditControl();
6135 wxGridCellAttr
*attr
= GetCellAttr(coords
);
6136 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
6137 editor
->StartingClick();
6141 m_waitForSlowClick
= false;
6143 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6144 m_selectingBottomRight
!= wxGridNoCellCoords
)
6148 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6149 m_selectingTopLeft
.GetCol(),
6150 m_selectingBottomRight
.GetRow(),
6151 m_selectingBottomRight
.GetCol(),
6152 event
.ControlDown(),
6158 m_selectingTopLeft
= wxGridNoCellCoords
;
6159 m_selectingBottomRight
= wxGridNoCellCoords
;
6161 // Show the edit control, if it has been hidden for
6163 ShowCellEditControl();
6166 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6168 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6169 DoEndDragResizeRow();
6171 // Note: we are ending the event *after* doing
6172 // default processing in this case
6174 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
6176 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6178 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6179 DoEndDragResizeCol();
6181 // Note: we are ending the event *after* doing
6182 // default processing in this case
6184 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
6190 // ------------ Right button down
6192 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
6194 DisableCellEditControl();
6195 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
6200 // no default action at the moment
6204 // ------------ Right double click
6206 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
6208 DisableCellEditControl();
6209 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
6214 // no default action at the moment
6218 // ------------ Moving and no button action
6220 else if ( event
.Moving() && !event
.IsButton() )
6222 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
6224 // out of grid cell area
6225 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6229 int dragRow
= YToEdgeOfRow( y
);
6230 int dragCol
= XToEdgeOfCol( x
);
6232 // Dragging on the corner of a cell to resize in both
6233 // directions is not implemented yet...
6235 if ( dragRow
>= 0 && dragCol
>= 0 )
6237 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6243 m_dragRowOrCol
= dragRow
;
6245 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6247 if ( CanDragRowSize() && CanDragGridSize() )
6248 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false);
6251 else if ( dragCol
>= 0 )
6253 m_dragRowOrCol
= dragCol
;
6255 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6257 if ( CanDragColSize() && CanDragGridSize() )
6258 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false);
6261 else // Neither on a row or col edge
6263 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6265 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6271 void wxGrid::DoEndDragResizeRow()
6273 if ( m_dragLastPos
>= 0 )
6275 // erase the last line and resize the row
6277 int cw
, ch
, left
, dummy
;
6278 m_gridWin
->GetClientSize( &cw
, &ch
);
6279 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6281 wxClientDC
dc( m_gridWin
);
6283 dc
.SetLogicalFunction( wxINVERT
);
6284 dc
.DrawLine( left
, m_dragLastPos
, left
+ cw
, m_dragLastPos
);
6285 HideCellEditControl();
6286 SaveEditControlValue();
6288 int rowTop
= GetRowTop(m_dragRowOrCol
);
6289 SetRowSize( m_dragRowOrCol
,
6290 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
6292 if ( !GetBatchCount() )
6294 // Only needed to get the correct rect.y:
6295 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
6297 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6298 rect
.width
= m_rowLabelWidth
;
6299 rect
.height
= ch
- rect
.y
;
6300 m_rowLabelWin
->Refresh( true, &rect
);
6303 // if there is a multicell block, paint all of it
6306 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
6307 int leftCol
= XToCol(left
);
6308 int rightCol
= internalXToCol(left
+ cw
);
6311 for (i
=leftCol
; i
<rightCol
; i
++)
6313 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
6314 if (cell_rows
< subtract_rows
)
6315 subtract_rows
= cell_rows
;
6317 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
6318 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6319 rect
.height
= ch
- rect
.y
;
6322 m_gridWin
->Refresh( false, &rect
);
6325 ShowCellEditControl();
6330 void wxGrid::DoEndDragResizeCol()
6332 if ( m_dragLastPos
>= 0 )
6334 // erase the last line and resize the col
6336 int cw
, ch
, dummy
, top
;
6337 m_gridWin
->GetClientSize( &cw
, &ch
);
6338 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6340 wxClientDC
dc( m_gridWin
);
6342 dc
.SetLogicalFunction( wxINVERT
);
6343 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6344 HideCellEditControl();
6345 SaveEditControlValue();
6347 int colLeft
= GetColLeft(m_dragRowOrCol
);
6348 SetColSize( m_dragRowOrCol
,
6349 wxMax( m_dragLastPos
- colLeft
,
6350 GetColMinimalWidth(m_dragRowOrCol
) ) );
6352 if ( !GetBatchCount() )
6354 // Only needed to get the correct rect.x:
6355 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
6357 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6358 rect
.width
= cw
- rect
.x
;
6359 rect
.height
= m_colLabelHeight
;
6360 m_colLabelWin
->Refresh( true, &rect
);
6363 // if there is a multicell block, paint all of it
6366 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
6367 int topRow
= YToRow(top
);
6368 int bottomRow
= internalYToRow(top
+ cw
);
6371 for (i
=topRow
; i
<bottomRow
; i
++)
6373 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
6374 if (cell_cols
< subtract_cols
)
6375 subtract_cols
= cell_cols
;
6378 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
6379 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6380 rect
.width
= cw
- rect
.x
;
6384 m_gridWin
->Refresh( false, &rect
);
6387 ShowCellEditControl();
6391 void wxGrid::DoEndDragMoveCol()
6393 //The user clicked on the column but didn't actually drag
6394 if ( m_dragLastPos
< 0 )
6396 m_colLabelWin
->Refresh(); //Do this to "unpress" the column
6401 if ( m_moveToCol
== -1 )
6402 newPos
= m_numCols
- 1;
6405 newPos
= GetColPos( m_moveToCol
);
6406 if ( newPos
> GetColPos( m_dragRowOrCol
) )
6410 SetColPos( m_dragRowOrCol
, newPos
);
6413 void wxGrid::SetColPos( int colID
, int newPos
)
6415 if ( m_colAt
.IsEmpty() )
6417 m_colAt
.Alloc( m_numCols
);
6420 for ( i
= 0; i
< m_numCols
; i
++ )
6426 int oldPos
= GetColPos( colID
);
6428 //Reshuffle the m_colAt array
6429 if ( newPos
> oldPos
)
6432 for ( i
= oldPos
; i
< newPos
; i
++ )
6434 m_colAt
[i
] = m_colAt
[i
+1];
6440 for ( i
= oldPos
; i
> newPos
; i
-- )
6442 m_colAt
[i
] = m_colAt
[i
-1];
6446 m_colAt
[newPos
] = colID
;
6448 //Recalculate the column rights
6449 if ( !m_colWidths
.IsEmpty() )
6453 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6455 int colID
= GetColAt( colPos
);
6457 colRight
+= m_colWidths
[colID
];
6458 m_colRights
[colID
] = colRight
;
6462 m_colLabelWin
->Refresh();
6463 m_gridWin
->Refresh();
6468 void wxGrid::EnableDragColMove( bool enable
)
6470 if ( m_canDragColMove
== enable
)
6473 m_canDragColMove
= enable
;
6475 if ( !m_canDragColMove
)
6479 //Recalculate the column rights
6480 if ( !m_colWidths
.IsEmpty() )
6484 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6486 colRight
+= m_colWidths
[colPos
];
6487 m_colRights
[colPos
] = colRight
;
6491 m_colLabelWin
->Refresh();
6492 m_gridWin
->Refresh();
6498 // ------ interaction with data model
6500 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6502 switch ( msg
.GetId() )
6504 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6505 return GetModelValues();
6507 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6508 return SetModelValues();
6510 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6511 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6512 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6513 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6514 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6515 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6516 return Redimension( msg
);
6523 // The behaviour of this function depends on the grid table class
6524 // Clear() function. For the default wxGridStringTable class the
6525 // behavious is to replace all cell contents with wxEmptyString but
6526 // not to change the number of rows or cols.
6528 void wxGrid::ClearGrid()
6532 if (IsCellEditControlEnabled())
6533 DisableCellEditControl();
6536 if (!GetBatchCount())
6537 m_gridWin
->Refresh();
6541 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6543 // TODO: something with updateLabels flag
6547 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6553 if (IsCellEditControlEnabled())
6554 DisableCellEditControl();
6556 bool done
= m_table
->InsertRows( pos
, numRows
);
6559 // the table will have sent the results of the insert row
6560 // operation to this view object as a grid table message
6566 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6568 // TODO: something with updateLabels flag
6572 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6578 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6581 // the table will have sent the results of the append row
6582 // operation to this view object as a grid table message
6588 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6590 // TODO: something with updateLabels flag
6594 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6600 if (IsCellEditControlEnabled())
6601 DisableCellEditControl();
6603 bool done
= m_table
->DeleteRows( pos
, numRows
);
6605 // the table will have sent the results of the delete row
6606 // operation to this view object as a grid table message
6612 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6614 // TODO: something with updateLabels flag
6618 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6624 if (IsCellEditControlEnabled())
6625 DisableCellEditControl();
6627 bool done
= m_table
->InsertCols( pos
, numCols
);
6629 // the table will have sent the results of the insert col
6630 // operation to this view object as a grid table message
6636 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6638 // TODO: something with updateLabels flag
6642 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6648 bool done
= m_table
->AppendCols( numCols
);
6650 // the table will have sent the results of the append col
6651 // operation to this view object as a grid table message
6657 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6659 // TODO: something with updateLabels flag
6663 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6669 if (IsCellEditControlEnabled())
6670 DisableCellEditControl();
6672 bool done
= m_table
->DeleteCols( pos
, numCols
);
6674 // the table will have sent the results of the delete col
6675 // operation to this view object as a grid table message
6682 // ----- event handlers
6685 // Generate a grid event based on a mouse event and
6686 // return the result of ProcessEvent()
6688 int wxGrid::SendEvent( const wxEventType type
,
6690 wxMouseEvent
& mouseEv
)
6692 bool claimed
, vetoed
;
6694 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6696 int rowOrCol
= (row
== -1 ? col
: row
);
6698 wxGridSizeEvent
gridEvt( GetId(),
6702 mouseEv
.GetX() + GetRowLabelSize(),
6703 mouseEv
.GetY() + GetColLabelSize(),
6704 mouseEv
.ControlDown(),
6705 mouseEv
.ShiftDown(),
6707 mouseEv
.MetaDown() );
6709 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6710 vetoed
= !gridEvt
.IsAllowed();
6712 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6714 // Right now, it should _never_ end up here!
6715 wxGridRangeSelectEvent
gridEvt( GetId(),
6719 m_selectingBottomRight
,
6721 mouseEv
.ControlDown(),
6722 mouseEv
.ShiftDown(),
6724 mouseEv
.MetaDown() );
6726 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6727 vetoed
= !gridEvt
.IsAllowed();
6729 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
6730 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
6731 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
6732 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
6734 wxPoint pos
= mouseEv
.GetPosition();
6736 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
6737 pos
.y
+= GetColLabelSize();
6738 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
6739 pos
.x
+= GetRowLabelSize();
6741 wxGridEvent
gridEvt( GetId(),
6748 mouseEv
.ControlDown(),
6749 mouseEv
.ShiftDown(),
6751 mouseEv
.MetaDown() );
6752 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6753 vetoed
= !gridEvt
.IsAllowed();
6757 wxGridEvent
gridEvt( GetId(),
6761 mouseEv
.GetX() + GetRowLabelSize(),
6762 mouseEv
.GetY() + GetColLabelSize(),
6764 mouseEv
.ControlDown(),
6765 mouseEv
.ShiftDown(),
6767 mouseEv
.MetaDown() );
6768 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6769 vetoed
= !gridEvt
.IsAllowed();
6772 // A Veto'd event may not be `claimed' so test this first
6776 return claimed
? 1 : 0;
6779 // Generate a grid event of specified type and return the result
6780 // of ProcessEvent().
6782 int wxGrid::SendEvent( const wxEventType type
,
6785 bool claimed
, vetoed
;
6787 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6789 int rowOrCol
= (row
== -1 ? col
: row
);
6791 wxGridSizeEvent
gridEvt( GetId(), type
, this, rowOrCol
);
6793 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6794 vetoed
= !gridEvt
.IsAllowed();
6798 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
6800 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6801 vetoed
= !gridEvt
.IsAllowed();
6804 // A Veto'd event may not be `claimed' so test this first
6808 return claimed
? 1 : 0;
6811 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6813 // needed to prevent zillions of paint events on MSW
6817 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6819 // Don't do anything if between Begin/EndBatch...
6820 // EndBatch() will do all this on the last nested one anyway.
6821 if (! GetBatchCount())
6823 // Refresh to get correct scrolled position:
6824 wxScrolledWindow::Refresh(eraseb
, rect
);
6828 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6829 int width_label
, width_cell
, height_label
, height_cell
;
6832 // Copy rectangle can get scroll offsets..
6833 rect_x
= rect
->GetX();
6834 rect_y
= rect
->GetY();
6835 rectWidth
= rect
->GetWidth();
6836 rectHeight
= rect
->GetHeight();
6838 width_label
= m_rowLabelWidth
- rect_x
;
6839 if (width_label
> rectWidth
)
6840 width_label
= rectWidth
;
6842 height_label
= m_colLabelHeight
- rect_y
;
6843 if (height_label
> rectHeight
)
6844 height_label
= rectHeight
;
6846 if (rect_x
> m_rowLabelWidth
)
6848 x
= rect_x
- m_rowLabelWidth
;
6849 width_cell
= rectWidth
;
6854 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6857 if (rect_y
> m_colLabelHeight
)
6859 y
= rect_y
- m_colLabelHeight
;
6860 height_cell
= rectHeight
;
6865 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6868 // Paint corner label part intersecting rect.
6869 if ( width_label
> 0 && height_label
> 0 )
6871 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6872 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6875 // Paint col labels part intersecting rect.
6876 if ( width_cell
> 0 && height_label
> 0 )
6878 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6879 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6882 // Paint row labels part intersecting rect.
6883 if ( width_label
> 0 && height_cell
> 0 )
6885 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6886 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6889 // Paint cell area part intersecting rect.
6890 if ( width_cell
> 0 && height_cell
> 0 )
6892 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6893 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6898 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6899 m_colLabelWin
->Refresh(eraseb
, NULL
);
6900 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6901 m_gridWin
->Refresh(eraseb
, NULL
);
6906 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
))
6908 if (m_targetWindow
!= this) // check whether initialisation has been done
6910 // update our children window positions and scrollbars
6915 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6917 if ( m_inOnKeyDown
)
6919 // shouldn't be here - we are going round in circles...
6921 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6924 m_inOnKeyDown
= true;
6926 // propagate the event up and see if it gets processed
6927 wxWindow
*parent
= GetParent();
6928 wxKeyEvent
keyEvt( event
);
6929 keyEvt
.SetEventObject( parent
);
6931 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6933 if (GetLayoutDirection() == wxLayout_RightToLeft
)
6935 if (event
.GetKeyCode() == WXK_RIGHT
)
6936 event
.m_keyCode
= WXK_LEFT
;
6937 else if (event
.GetKeyCode() == WXK_LEFT
)
6938 event
.m_keyCode
= WXK_RIGHT
;
6941 // try local handlers
6942 switch ( event
.GetKeyCode() )
6945 if ( event
.ControlDown() )
6946 MoveCursorUpBlock( event
.ShiftDown() );
6948 MoveCursorUp( event
.ShiftDown() );
6952 if ( event
.ControlDown() )
6953 MoveCursorDownBlock( event
.ShiftDown() );
6955 MoveCursorDown( event
.ShiftDown() );
6959 if ( event
.ControlDown() )
6960 MoveCursorLeftBlock( event
.ShiftDown() );
6962 MoveCursorLeft( event
.ShiftDown() );
6966 if ( event
.ControlDown() )
6967 MoveCursorRightBlock( event
.ShiftDown() );
6969 MoveCursorRight( event
.ShiftDown() );
6973 case WXK_NUMPAD_ENTER
:
6974 if ( event
.ControlDown() )
6976 event
.Skip(); // to let the edit control have the return
6980 if ( GetGridCursorRow() < GetNumberRows()-1 )
6982 MoveCursorDown( event
.ShiftDown() );
6986 // at the bottom of a column
6987 DisableCellEditControl();
6997 if (event
.ShiftDown())
6999 if ( GetGridCursorCol() > 0 )
7001 MoveCursorLeft( false );
7006 DisableCellEditControl();
7011 if ( GetGridCursorCol() < GetNumberCols() - 1 )
7013 MoveCursorRight( false );
7018 DisableCellEditControl();
7024 if ( event
.ControlDown() )
7026 MakeCellVisible( 0, 0 );
7027 SetCurrentCell( 0, 0 );
7036 if ( event
.ControlDown() )
7038 MakeCellVisible( m_numRows
- 1, m_numCols
- 1 );
7039 SetCurrentCell( m_numRows
- 1, m_numCols
- 1 );
7056 if ( event
.ControlDown() )
7060 m_selection
->ToggleCellSelection(
7061 m_currentCellCoords
.GetRow(),
7062 m_currentCellCoords
.GetCol(),
7063 event
.ControlDown(),
7071 if ( !IsEditable() )
7072 MoveCursorRight( false );
7083 m_inOnKeyDown
= false;
7086 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
7088 // try local handlers
7090 if ( event
.GetKeyCode() == WXK_SHIFT
)
7092 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
7093 m_selectingBottomRight
!= wxGridNoCellCoords
)
7097 m_selection
->SelectBlock(
7098 m_selectingTopLeft
.GetRow(),
7099 m_selectingTopLeft
.GetCol(),
7100 m_selectingBottomRight
.GetRow(),
7101 m_selectingBottomRight
.GetCol(),
7102 event
.ControlDown(),
7109 m_selectingTopLeft
= wxGridNoCellCoords
;
7110 m_selectingBottomRight
= wxGridNoCellCoords
;
7111 m_selectingKeyboard
= wxGridNoCellCoords
;
7115 void wxGrid::OnChar( wxKeyEvent
& event
)
7117 // is it possible to edit the current cell at all?
7118 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7120 // yes, now check whether the cells editor accepts the key
7121 int row
= m_currentCellCoords
.GetRow();
7122 int col
= m_currentCellCoords
.GetCol();
7123 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7124 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7126 // <F2> is special and will always start editing, for
7127 // other keys - ask the editor itself
7128 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
7129 || editor
->IsAcceptedKey(event
) )
7131 // ensure cell is visble
7132 MakeCellVisible(row
, col
);
7133 EnableCellEditControl();
7135 // a problem can arise if the cell is not completely
7136 // visible (even after calling MakeCellVisible the
7137 // control is not created and calling StartingKey will
7139 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
7140 editor
->StartingKey(event
);
7156 void wxGrid::OnEraseBackground(wxEraseEvent
&)
7160 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
7162 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
7164 // the event has been intercepted - do nothing
7168 #if !defined(__WXMAC__)
7169 wxClientDC
dc( m_gridWin
);
7173 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
7175 DisableCellEditControl();
7177 if ( IsVisible( m_currentCellCoords
, false ) )
7180 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
7181 if ( !m_gridLinesEnabled
)
7189 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
7191 // Otherwise refresh redraws the highlight!
7192 m_currentCellCoords
= coords
;
7194 #if defined(__WXMAC__)
7195 m_gridWin
->Refresh(true /*, & r */);
7197 DrawGridCellArea( dc
, cells
);
7198 DrawAllGridLines( dc
, r
);
7203 m_currentCellCoords
= coords
;
7205 wxGridCellAttr
*attr
= GetCellAttr( coords
);
7206 #if !defined(__WXMAC__)
7207 DrawCellHighlight( dc
, attr
);
7212 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7215 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7219 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
7222 rightCol
= GetNumberCols() - 1;
7224 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
7227 bottomRow
= GetNumberRows() - 1;
7231 if ( topRow
> bottomRow
)
7238 if ( leftCol
> rightCol
)
7245 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7246 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7248 // First the case that we selected a completely new area
7249 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
7250 m_selectingBottomRight
== wxGridNoCellCoords
)
7253 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
7254 wxGridCellCoords ( bottomRow
, rightCol
) );
7255 m_gridWin
->Refresh( false, &rect
);
7258 // Now handle changing an existing selection area.
7259 else if ( m_selectingTopLeft
!= updateTopLeft
||
7260 m_selectingBottomRight
!= updateBottomRight
)
7262 // Compute two optimal update rectangles:
7263 // Either one rectangle is a real subset of the
7264 // other, or they are (almost) disjoint!
7266 bool need_refresh
[4];
7270 need_refresh
[3] = false;
7273 // Store intermediate values
7274 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
7275 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
7276 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
7277 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
7279 // Determine the outer/inner coordinates.
7280 if (oldLeft
> leftCol
)
7286 if (oldTop
> topRow
)
7292 if (oldRight
< rightCol
)
7295 oldRight
= rightCol
;
7298 if (oldBottom
< bottomRow
)
7301 oldBottom
= bottomRow
;
7305 // Now, either the stuff marked old is the outer
7306 // rectangle or we don't have a situation where one
7307 // is contained in the other.
7309 if ( oldLeft
< leftCol
)
7311 // Refresh the newly selected or deselected
7312 // area to the left of the old or new selection.
7313 need_refresh
[0] = true;
7314 rect
[0] = BlockToDeviceRect(
7315 wxGridCellCoords( oldTop
, oldLeft
),
7316 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
7319 if ( oldTop
< topRow
)
7321 // Refresh the newly selected or deselected
7322 // area above the old or new selection.
7323 need_refresh
[1] = true;
7324 rect
[1] = BlockToDeviceRect(
7325 wxGridCellCoords( oldTop
, leftCol
),
7326 wxGridCellCoords( topRow
- 1, rightCol
) );
7329 if ( oldRight
> rightCol
)
7331 // Refresh the newly selected or deselected
7332 // area to the right of the old or new selection.
7333 need_refresh
[2] = true;
7334 rect
[2] = BlockToDeviceRect(
7335 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
7336 wxGridCellCoords( oldBottom
, oldRight
) );
7339 if ( oldBottom
> bottomRow
)
7341 // Refresh the newly selected or deselected
7342 // area below the old or new selection.
7343 need_refresh
[3] = true;
7344 rect
[3] = BlockToDeviceRect(
7345 wxGridCellCoords( bottomRow
+ 1, leftCol
),
7346 wxGridCellCoords( oldBottom
, rightCol
) );
7349 // various Refresh() calls
7350 for (i
= 0; i
< 4; i
++ )
7351 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7352 m_gridWin
->Refresh( false, &(rect
[i
]) );
7356 m_selectingTopLeft
= updateTopLeft
;
7357 m_selectingBottomRight
= updateBottomRight
;
7361 // ------ functions to get/send data (see also public functions)
7364 bool wxGrid::GetModelValues()
7366 // Hide the editor, so it won't hide a changed value.
7367 HideCellEditControl();
7371 // all we need to do is repaint the grid
7373 m_gridWin
->Refresh();
7380 bool wxGrid::SetModelValues()
7384 // Disable the editor, so it won't hide a changed value.
7385 // Do we also want to save the current value of the editor first?
7387 DisableCellEditControl();
7391 for ( row
= 0; row
< m_numRows
; row
++ )
7393 for ( col
= 0; col
< m_numCols
; col
++ )
7395 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
7405 // Note - this function only draws cells that are in the list of
7406 // exposed cells (usually set from the update region by
7407 // CalcExposedCells)
7409 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7411 if ( !m_numRows
|| !m_numCols
)
7414 int i
, numCells
= cells
.GetCount();
7415 int row
, col
, cell_rows
, cell_cols
;
7416 wxGridCellCoordsArray redrawCells
;
7418 for ( i
= numCells
- 1; i
>= 0; i
-- )
7420 row
= cells
[i
].GetRow();
7421 col
= cells
[i
].GetCol();
7422 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7424 // If this cell is part of a multicell block, find owner for repaint
7425 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7427 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
7428 bool marked
= false;
7429 for ( int j
= 0; j
< numCells
; j
++ )
7431 if ( cell
== cells
[j
] )
7440 int count
= redrawCells
.GetCount();
7441 for (int j
= 0; j
< count
; j
++)
7443 if ( cell
== redrawCells
[j
] )
7451 redrawCells
.Add( cell
);
7454 // don't bother drawing this cell
7458 // If this cell is empty, find cell to left that might want to overflow
7459 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7461 for ( int l
= 0; l
< cell_rows
; l
++ )
7463 // find a cell in this row to leave already marked for repaint
7465 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7466 if ((redrawCells
[k
].GetCol() < left
) &&
7467 (redrawCells
[k
].GetRow() == row
))
7469 left
= redrawCells
[k
].GetCol();
7473 left
= 0; // oh well
7475 for (int j
= col
- 1; j
>= left
; j
--)
7477 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7479 if (GetCellOverflow(row
+ l
, j
))
7481 wxGridCellCoords
cell(row
+ l
, j
);
7482 bool marked
= false;
7484 for (int k
= 0; k
< numCells
; k
++)
7486 if ( cell
== cells
[k
] )
7495 int count
= redrawCells
.GetCount();
7496 for (int k
= 0; k
< count
; k
++)
7498 if ( cell
== redrawCells
[k
] )
7505 redrawCells
.Add( cell
);
7514 DrawCell( dc
, cells
[i
] );
7517 numCells
= redrawCells
.GetCount();
7519 for ( i
= numCells
- 1; i
>= 0; i
-- )
7521 DrawCell( dc
, redrawCells
[i
] );
7525 void wxGrid::DrawGridSpace( wxDC
& dc
)
7528 m_gridWin
->GetClientSize( &cw
, &ch
);
7531 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7533 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
7534 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7536 if ( right
> rightCol
|| bottom
> bottomRow
)
7539 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7541 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
7542 dc
.SetPen( *wxTRANSPARENT_PEN
);
7544 if ( right
> rightCol
)
7546 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7549 if ( bottom
> bottomRow
)
7551 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7556 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7558 int row
= coords
.GetRow();
7559 int col
= coords
.GetCol();
7561 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7564 // we draw the cell border ourselves
7565 #if !WXGRID_DRAW_LINES
7566 if ( m_gridLinesEnabled
)
7567 DrawCellBorder( dc
, coords
);
7570 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7572 bool isCurrent
= coords
== m_currentCellCoords
;
7574 wxRect rect
= CellToRect( row
, col
);
7576 // if the editor is shown, we should use it and not the renderer
7577 // Note: However, only if it is really _shown_, i.e. not hidden!
7578 if ( isCurrent
&& IsCellEditControlShown() )
7580 // NB: this "#if..." is temporary and fixes a problem where the
7581 // edit control is erased by this code after being rendered.
7582 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7583 // implicitly, causing this out-of order render.
7584 #if !defined(__WXMAC__)
7585 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7586 editor
->PaintBackground(rect
, attr
);
7592 // but all the rest is drawn by the cell renderer and hence may be customized
7593 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7594 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7601 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7603 // don't show highlight when the grid doesn't have focus
7607 int row
= m_currentCellCoords
.GetRow();
7608 int col
= m_currentCellCoords
.GetCol();
7610 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7613 wxRect rect
= CellToRect(row
, col
);
7615 // hmmm... what could we do here to show that the cell is disabled?
7616 // for now, I just draw a thinner border than for the other ones, but
7617 // it doesn't look really good
7619 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7623 // The center of the drawn line is where the position/width/height of
7624 // the rectangle is actually at (on wxMSW at least), so the
7625 // size of the rectangle is reduced to compensate for the thickness of
7626 // the line. If this is too strange on non-wxMSW platforms then
7627 // please #ifdef this appropriately.
7628 rect
.x
+= penWidth
/ 2;
7629 rect
.y
+= penWidth
/ 2;
7630 rect
.width
-= penWidth
- 1;
7631 rect
.height
-= penWidth
- 1;
7633 // Now draw the rectangle
7634 // use the cellHighlightColour if the cell is inside a selection, this
7635 // will ensure the cell is always visible.
7636 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
: m_cellHighlightColour
, penWidth
, wxSOLID
));
7637 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7638 dc
.DrawRectangle(rect
);
7642 // VZ: my experiments with 3D borders...
7644 // how to properly set colours for arbitrary bg?
7645 wxCoord x1
= rect
.x
,
7647 x2
= rect
.x
+ rect
.width
- 1,
7648 y2
= rect
.y
+ rect
.height
- 1;
7650 dc
.SetPen(*wxWHITE_PEN
);
7651 dc
.DrawLine(x1
, y1
, x2
, y1
);
7652 dc
.DrawLine(x1
, y1
, x1
, y2
);
7654 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7655 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7657 dc
.SetPen(*wxBLACK_PEN
);
7658 dc
.DrawLine(x1
, y2
, x2
, y2
);
7659 dc
.DrawLine(x2
, y1
, x2
, y2
+ 1);
7663 wxPen
wxGrid::GetDefaultGridLinePen()
7665 return wxPen(GetGridLineColour(), 1, wxSOLID
);
7668 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
7670 return GetDefaultGridLinePen();
7673 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
7675 return GetDefaultGridLinePen();
7678 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7680 int row
= coords
.GetRow();
7681 int col
= coords
.GetCol();
7682 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7686 wxRect rect
= CellToRect( row
, col
);
7688 // right hand border
7689 dc
.SetPen( GetColGridLinePen(col
) );
7690 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7691 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7694 dc
.SetPen( GetRowGridLinePen(row
) );
7695 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7696 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7699 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7701 // This if block was previously in wxGrid::OnPaint but that doesn't
7702 // seem to get called under wxGTK - MB
7704 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7705 m_numRows
&& m_numCols
)
7707 m_currentCellCoords
.Set(0, 0);
7710 if ( IsCellEditControlShown() )
7712 // don't show highlight when the edit control is shown
7716 // if the active cell was repainted, repaint its highlight too because it
7717 // might have been damaged by the grid lines
7718 size_t count
= cells
.GetCount();
7719 for ( size_t n
= 0; n
< count
; n
++ )
7721 if ( cells
[n
] == m_currentCellCoords
)
7723 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7724 DrawCellHighlight(dc
, attr
);
7732 // TODO: remove this ???
7733 // This is used to redraw all grid lines e.g. when the grid line colour
7736 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7738 #if !WXGRID_DRAW_LINES
7742 if ( !m_gridLinesEnabled
|| !m_numRows
|| !m_numCols
)
7745 int top
, bottom
, left
, right
;
7747 #if 0 //#ifndef __WXGTK__
7751 m_gridWin
->GetClientSize(&cw
, &ch
);
7753 // virtual coords of visible area
7755 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7756 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7761 reg
.GetBox(x
, y
, w
, h
);
7762 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7763 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7767 m_gridWin
->GetClientSize(&cw
, &ch
);
7768 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7769 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7772 // avoid drawing grid lines past the last row and col
7774 right
= wxMin( right
, GetColRight(GetColAt( m_numCols
- 1 )) );
7775 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7777 // no gridlines inside multicells, clip them out
7778 int leftCol
= GetColPos( internalXToCol(left
) );
7779 int topRow
= internalYToRow(top
);
7780 int rightCol
= GetColPos( internalXToCol(right
) );
7781 int bottomRow
= internalYToRow(bottom
);
7783 wxRegion
clippedcells(0, 0, cw
, ch
);
7785 int i
, j
, cell_rows
, cell_cols
;
7788 for (j
=topRow
; j
<=bottomRow
; j
++)
7791 for (colPos
=leftCol
; colPos
<=rightCol
; colPos
++)
7793 i
= GetColAt( colPos
);
7795 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7796 if ((cell_rows
> 1) || (cell_cols
> 1))
7798 rect
= CellToRect(j
,i
);
7799 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7800 clippedcells
.Subtract(rect
);
7802 else if ((cell_rows
< 0) || (cell_cols
< 0))
7804 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7805 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7806 clippedcells
.Subtract(rect
);
7811 dc
.SetClippingRegion( clippedcells
);
7814 // horizontal grid lines
7816 // already declared above - int i;
7817 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7819 int bot
= GetRowBottom(i
) - 1;
7828 dc
.SetPen( GetRowGridLinePen(i
) );
7829 dc
.DrawLine( left
, bot
, right
, bot
);
7833 // vertical grid lines
7836 for ( colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
7838 i
= GetColAt( colPos
);
7840 int colRight
= GetColRight(i
);
7842 if (GetLayoutDirection() != wxLayout_RightToLeft
)
7846 if ( colRight
> right
)
7851 if ( colRight
>= left
)
7853 dc
.SetPen( GetColGridLinePen(i
) );
7854 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7858 dc
.DestroyClippingRegion();
7861 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
7867 size_t numLabels
= rows
.GetCount();
7869 for ( i
= 0; i
< numLabels
; i
++ )
7871 DrawRowLabel( dc
, rows
[i
] );
7875 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7877 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7882 int rowTop
= GetRowTop(row
),
7883 rowBottom
= GetRowBottom(row
) - 1;
7885 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
7886 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
7887 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7888 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7890 dc
.SetPen( *wxWHITE_PEN
);
7891 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7892 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
7894 dc
.SetBackgroundMode( wxTRANSPARENT
);
7895 dc
.SetTextForeground( GetLabelTextColour() );
7896 dc
.SetFont( GetLabelFont() );
7899 GetRowLabelAlignment( &hAlign
, &vAlign
);
7902 rect
.SetY( GetRowTop(row
) + 2 );
7903 rect
.SetWidth( m_rowLabelWidth
- 4 );
7904 rect
.SetHeight( GetRowHeight(row
) - 4 );
7905 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7908 void wxGrid::SetUseNativeColLabels( bool native
)
7910 m_nativeColumnLabels
= native
;
7913 int height
= wxRendererNative::Get().GetHeaderButtonHeight( this );
7914 SetColLabelSize( height
);
7917 m_colLabelWin
->Refresh();
7920 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7926 size_t numLabels
= cols
.GetCount();
7928 for ( i
= 0; i
< numLabels
; i
++ )
7930 DrawColLabel( dc
, cols
[i
] );
7934 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7936 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7939 int colLeft
= GetColLeft(col
);
7943 if (m_nativeColumnLabels
)
7945 rect
.SetX( colLeft
);
7947 rect
.SetWidth( GetColWidth(col
));
7948 rect
.SetHeight( m_colLabelHeight
);
7950 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7951 wxRendererNative::Get().DrawHeaderButton( win_dc
->GetWindow(), dc
, rect
, 0 );
7955 int colRight
= GetColRight(col
) - 1;
7957 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
7958 dc
.DrawLine( colRight
, 0, colRight
, m_colLabelHeight
- 1 );
7959 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7960 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
7961 colRight
+ 1, m_colLabelHeight
- 1 );
7963 dc
.SetPen( *wxWHITE_PEN
);
7964 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
7965 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7968 dc
.SetBackgroundMode( wxTRANSPARENT
);
7969 dc
.SetTextForeground( GetLabelTextColour() );
7970 dc
.SetFont( GetLabelFont() );
7972 int hAlign
, vAlign
, orient
;
7973 GetColLabelAlignment( &hAlign
, &vAlign
);
7974 orient
= GetColLabelTextOrientation();
7976 rect
.SetX( colLeft
+ 2 );
7978 rect
.SetWidth( GetColWidth(col
) - 4 );
7979 rect
.SetHeight( m_colLabelHeight
- 4 );
7980 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7983 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7984 const wxString
& value
,
7988 int textOrientation
)
7990 wxArrayString lines
;
7992 StringToLines( value
, lines
);
7994 // Forward to new API.
7995 DrawTextRectangle( dc
,
8003 // VZ: this should be replaced with wxDC::DrawLabel() to which we just have to
8004 // add textOrientation support
8005 void wxGrid::DrawTextRectangle(wxDC
& dc
,
8006 const wxArrayString
& lines
,
8010 int textOrientation
)
8012 if ( lines
.empty() )
8015 wxDCClipper
clip(dc
, rect
);
8020 if ( textOrientation
== wxHORIZONTAL
)
8021 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
8023 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
8027 switch ( vertAlign
)
8029 case wxALIGN_BOTTOM
:
8030 if ( textOrientation
== wxHORIZONTAL
)
8031 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
8033 x
= rect
.x
+ rect
.width
- textWidth
;
8036 case wxALIGN_CENTRE
:
8037 if ( textOrientation
== wxHORIZONTAL
)
8038 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
8040 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
8045 if ( textOrientation
== wxHORIZONTAL
)
8052 // Align each line of a multi-line label
8053 size_t nLines
= lines
.GetCount();
8054 for ( size_t l
= 0; l
< nLines
; l
++ )
8056 const wxString
& line
= lines
[l
];
8060 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
8064 wxCoord lineWidth
= 0,
8066 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
8068 switch ( horizAlign
)
8071 if ( textOrientation
== wxHORIZONTAL
)
8072 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
8074 y
= rect
.y
+ lineWidth
+ 1;
8077 case wxALIGN_CENTRE
:
8078 if ( textOrientation
== wxHORIZONTAL
)
8079 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
8081 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
8086 if ( textOrientation
== wxHORIZONTAL
)
8089 y
= rect
.y
+ rect
.height
- 1;
8093 if ( textOrientation
== wxHORIZONTAL
)
8095 dc
.DrawText( line
, x
, y
);
8100 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
8106 // Split multi-line text up into an array of strings.
8107 // Any existing contents of the string array are preserved.
8109 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
8113 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
8114 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
8116 while ( startPos
< (int)tVal
.length() )
8118 pos
= tVal
.Mid(startPos
).Find( eol
);
8123 else if ( pos
== 0 )
8125 lines
.Add( wxEmptyString
);
8129 lines
.Add( value
.Mid(startPos
, pos
) );
8132 startPos
+= pos
+ 1;
8135 if ( startPos
< (int)value
.length() )
8137 lines
.Add( value
.Mid( startPos
) );
8141 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
8142 const wxArrayString
& lines
,
8143 long *width
, long *height
) const
8147 wxCoord lineW
= 0, lineH
= 0;
8150 for ( i
= 0; i
< lines
.GetCount(); i
++ )
8152 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
8153 w
= wxMax( w
, lineW
);
8162 // ------ Batch processing.
8164 void wxGrid::EndBatch()
8166 if ( m_batchCount
> 0 )
8169 if ( !m_batchCount
)
8172 m_rowLabelWin
->Refresh();
8173 m_colLabelWin
->Refresh();
8174 m_cornerLabelWin
->Refresh();
8175 m_gridWin
->Refresh();
8180 // Use this, rather than wxWindow::Refresh(), to force an immediate
8181 // repainting of the grid. Has no effect if you are already inside a
8182 // BeginBatch / EndBatch block.
8184 void wxGrid::ForceRefresh()
8190 bool wxGrid::Enable(bool enable
)
8192 if ( !wxScrolledWindow::Enable(enable
) )
8195 // redraw in the new state
8196 m_gridWin
->Refresh();
8202 // ------ Edit control functions
8205 void wxGrid::EnableEditing( bool edit
)
8207 // TODO: improve this ?
8209 if ( edit
!= m_editable
)
8212 EnableCellEditControl(edit
);
8217 void wxGrid::EnableCellEditControl( bool enable
)
8222 if ( enable
!= m_cellEditCtrlEnabled
)
8226 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
8229 // this should be checked by the caller!
8230 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8232 // do it before ShowCellEditControl()
8233 m_cellEditCtrlEnabled
= enable
;
8235 ShowCellEditControl();
8239 //FIXME:add veto support
8240 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
8242 HideCellEditControl();
8243 SaveEditControlValue();
8245 // do it after HideCellEditControl()
8246 m_cellEditCtrlEnabled
= enable
;
8251 bool wxGrid::IsCurrentCellReadOnly() const
8254 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
8255 bool readonly
= attr
->IsReadOnly();
8261 bool wxGrid::CanEnableCellControl() const
8263 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
8264 !IsCurrentCellReadOnly();
8267 bool wxGrid::IsCellEditControlEnabled() const
8269 // the cell edit control might be disable for all cells or just for the
8270 // current one if it's read only
8271 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
8274 bool wxGrid::IsCellEditControlShown() const
8276 bool isShown
= false;
8278 if ( m_cellEditCtrlEnabled
)
8280 int row
= m_currentCellCoords
.GetRow();
8281 int col
= m_currentCellCoords
.GetCol();
8282 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8283 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
8288 if ( editor
->IsCreated() )
8290 isShown
= editor
->GetControl()->IsShown();
8300 void wxGrid::ShowCellEditControl()
8302 if ( IsCellEditControlEnabled() )
8304 if ( !IsVisible( m_currentCellCoords
, false ) )
8306 m_cellEditCtrlEnabled
= false;
8311 wxRect rect
= CellToRect( m_currentCellCoords
);
8312 int row
= m_currentCellCoords
.GetRow();
8313 int col
= m_currentCellCoords
.GetCol();
8315 // if this is part of a multicell, find owner (topleft)
8316 int cell_rows
, cell_cols
;
8317 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8318 if ( cell_rows
<= 0 || cell_cols
<= 0 )
8322 m_currentCellCoords
.SetRow( row
);
8323 m_currentCellCoords
.SetCol( col
);
8326 // erase the highlight and the cell contents because the editor
8327 // might not cover the entire cell
8328 wxClientDC
dc( m_gridWin
);
8330 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8331 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
8332 dc
.SetPen(*wxTRANSPARENT_PEN
);
8333 dc
.DrawRectangle(rect
);
8335 // convert to scrolled coords
8336 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8342 // cell is shifted by one pixel
8343 // However, don't allow x or y to become negative
8344 // since the SetSize() method interprets that as
8351 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8352 if ( !editor
->IsCreated() )
8354 editor
->Create(m_gridWin
, wxID_ANY
,
8355 new wxGridCellEditorEvtHandler(this, editor
));
8357 wxGridEditorCreatedEvent
evt(GetId(),
8358 wxEVT_GRID_EDITOR_CREATED
,
8362 editor
->GetControl());
8363 GetEventHandler()->ProcessEvent(evt
);
8366 // resize editor to overflow into righthand cells if allowed
8367 int maxWidth
= rect
.width
;
8368 wxString value
= GetCellValue(row
, col
);
8369 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
8372 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
8373 if (maxWidth
< rect
.width
)
8374 maxWidth
= rect
.width
;
8377 int client_right
= m_gridWin
->GetClientSize().GetWidth();
8378 if (rect
.x
+ maxWidth
> client_right
)
8379 maxWidth
= client_right
- rect
.x
;
8381 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
8383 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8384 // may have changed earlier
8385 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
8388 GetCellSize( row
, i
, &c_rows
, &c_cols
);
8390 // looks weird going over a multicell
8391 if (m_table
->IsEmptyCell( row
, i
) &&
8392 (rect
.width
< maxWidth
) && (c_rows
== 1))
8394 rect
.width
+= GetColWidth( i
);
8400 if (rect
.GetRight() > client_right
)
8401 rect
.SetRight( client_right
- 1 );
8404 editor
->SetCellAttr( attr
);
8405 editor
->SetSize( rect
);
8407 editor
->GetControl()->Move(
8408 editor
->GetControl()->GetPosition().x
+ nXMove
,
8409 editor
->GetControl()->GetPosition().y
);
8410 editor
->Show( true, attr
);
8412 // recalc dimensions in case we need to
8413 // expand the scrolled window to account for editor
8416 editor
->BeginEdit(row
, col
, this);
8417 editor
->SetCellAttr(NULL
);
8425 void wxGrid::HideCellEditControl()
8427 if ( IsCellEditControlEnabled() )
8429 int row
= m_currentCellCoords
.GetRow();
8430 int col
= m_currentCellCoords
.GetCol();
8432 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8433 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
8434 editor
->Show( false );
8438 m_gridWin
->SetFocus();
8440 // refresh whole row to the right
8441 wxRect
rect( CellToRect(row
, col
) );
8442 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8443 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8446 // ensure that the pixels under the focus ring get refreshed as well
8447 rect
.Inflate(10, 10);
8450 m_gridWin
->Refresh( false, &rect
);
8454 void wxGrid::SaveEditControlValue()
8456 if ( IsCellEditControlEnabled() )
8458 int row
= m_currentCellCoords
.GetRow();
8459 int col
= m_currentCellCoords
.GetCol();
8461 wxString oldval
= GetCellValue(row
, col
);
8463 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8464 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8465 bool changed
= editor
->EndEdit(row
, col
, this);
8472 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
8473 m_currentCellCoords
.GetRow(),
8474 m_currentCellCoords
.GetCol() ) < 0 )
8476 // Event has been vetoed, set the data back.
8477 SetCellValue(row
, col
, oldval
);
8484 // ------ Grid location functions
8485 // Note that all of these functions work with the logical coordinates of
8486 // grid cells and labels so you will need to convert from device
8487 // coordinates for mouse events etc.
8490 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
) const
8492 int row
= YToRow(y
);
8493 int col
= XToCol(x
);
8495 if ( row
== -1 || col
== -1 )
8497 coords
= wxGridNoCellCoords
;
8501 coords
.Set( row
, col
);
8505 // Internal Helper function for computing row or column from some
8506 // (unscrolled) coordinate value, using either
8507 // m_defaultRowHeight/m_defaultColWidth or binary search on array
8508 // of m_rowBottoms/m_ColRights to speed up the search!
8510 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
8511 const wxArrayInt
& BorderArray
, int nMax
,
8515 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
8520 size_t i_max
= coord
/ defaultDist
,
8523 if (BorderArray
.IsEmpty())
8525 if ((int) i_max
< nMax
)
8527 return clipToMinMax
? nMax
- 1 : -1;
8530 if ( i_max
>= BorderArray
.GetCount())
8532 i_max
= BorderArray
.GetCount() - 1;
8536 if ( coord
>= BorderArray
[i_max
])
8540 i_max
= coord
/ minDist
;
8542 i_max
= BorderArray
.GetCount() - 1;
8545 if ( i_max
>= BorderArray
.GetCount())
8546 i_max
= BorderArray
.GetCount() - 1;
8549 if ( coord
>= BorderArray
[i_max
])
8550 return clipToMinMax
? (int)i_max
: -1;
8551 if ( coord
< BorderArray
[0] )
8554 while ( i_max
- i_min
> 0 )
8556 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
8557 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8558 if (coord
>= BorderArray
[ i_max
- 1])
8562 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
8563 if (coord
< BorderArray
[median
])
8572 int wxGrid::YToRow( int y
) const
8574 return CoordToRowOrCol(y
, m_defaultRowHeight
,
8575 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8578 int wxGrid::XToCol( int x
, bool clipToMinMax
) const
8581 return clipToMinMax
&& (m_numCols
> 0) ? GetColAt( 0 ) : -1;
8583 wxASSERT_MSG(m_defaultColWidth
> 0, wxT("Default column width can not be zero"));
8585 int maxPos
= x
/ m_defaultColWidth
;
8588 if (m_colRights
.IsEmpty())
8590 if(maxPos
< m_numCols
)
8591 return GetColAt( maxPos
);
8592 return clipToMinMax
? GetColAt( m_numCols
- 1 ) : -1;
8595 if ( maxPos
>= m_numCols
)
8596 maxPos
= m_numCols
- 1;
8599 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8602 if (m_minAcceptableColWidth
)
8603 maxPos
= x
/ m_minAcceptableColWidth
;
8605 maxPos
= m_numCols
- 1;
8607 if ( maxPos
>= m_numCols
)
8608 maxPos
= m_numCols
- 1;
8611 //X is beyond the last column
8612 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8613 return clipToMinMax
? GetColAt( maxPos
) : -1;
8615 //X is before the first column
8616 if ( x
< m_colRights
[GetColAt( 0 )] )
8617 return GetColAt( 0 );
8619 //Perform a binary search
8620 while ( maxPos
- minPos
> 0 )
8622 wxCHECK_MSG(m_colRights
[GetColAt( minPos
)] <= x
&& x
< m_colRights
[GetColAt( maxPos
)],
8623 0, _T("wxGrid: internal error in XToCol"));
8625 if (x
>= m_colRights
[GetColAt( maxPos
- 1 )])
8626 return GetColAt( maxPos
);
8629 int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
8630 if (x
< m_colRights
[GetColAt( median
)])
8635 return GetColAt( maxPos
);
8638 // return the row number that that the y coord is near
8639 // the edge of, or -1 if not near an edge.
8640 // coords can only possibly be near an edge if
8641 // (a) the row/column is large enough to still allow for an "inner" area
8642 // that is _not_ nead the edge (i.e., if the height/width is smaller
8643 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8646 // (b) resizing rows/columns (the thing for which edge detection is
8647 // relevant at all) is enabled.
8649 int wxGrid::YToEdgeOfRow( int y
) const
8652 i
= internalYToRow(y
);
8654 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragRowSize() )
8656 // We know that we are in row i, test whether we are
8657 // close enough to lower or upper border, respectively.
8658 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8660 else if ( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8667 // return the col number that that the x coord is near the edge of, or
8668 // -1 if not near an edge
8669 // See comment at YToEdgeOfRow for conditions on edge detection.
8671 int wxGrid::XToEdgeOfCol( int x
) const
8674 i
= internalXToCol(x
);
8676 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragColSize() )
8678 // We know that we are in column i; test whether we are
8679 // close enough to right or left border, respectively.
8680 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8682 else if ( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8689 wxRect
wxGrid::CellToRect( int row
, int col
) const
8691 wxRect
rect( -1, -1, -1, -1 );
8693 if ( row
>= 0 && row
< m_numRows
&&
8694 col
>= 0 && col
< m_numCols
)
8696 int i
, cell_rows
, cell_cols
;
8697 rect
.width
= rect
.height
= 0;
8698 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8699 // if negative then find multicell owner
8704 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8706 rect
.x
= GetColLeft(col
);
8707 rect
.y
= GetRowTop(row
);
8708 for (i
=col
; i
< col
+ cell_cols
; i
++)
8709 rect
.width
+= GetColWidth(i
);
8710 for (i
=row
; i
< row
+ cell_rows
; i
++)
8711 rect
.height
+= GetRowHeight(i
);
8714 // if grid lines are enabled, then the area of the cell is a bit smaller
8715 if (m_gridLinesEnabled
)
8724 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
8726 // get the cell rectangle in logical coords
8728 wxRect
r( CellToRect( row
, col
) );
8730 // convert to device coords
8732 int left
, top
, right
, bottom
;
8733 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8734 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8736 // check against the client area of the grid window
8738 m_gridWin
->GetClientSize( &cw
, &ch
);
8740 if ( wholeCellVisible
)
8742 // is the cell wholly visible ?
8743 return ( left
>= 0 && right
<= cw
&&
8744 top
>= 0 && bottom
<= ch
);
8748 // is the cell partly visible ?
8750 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8751 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8755 // make the specified cell location visible by doing a minimal amount
8758 void wxGrid::MakeCellVisible( int row
, int col
)
8761 int xpos
= -1, ypos
= -1;
8763 if ( row
>= 0 && row
< m_numRows
&&
8764 col
>= 0 && col
< m_numCols
)
8766 // get the cell rectangle in logical coords
8767 wxRect
r( CellToRect( row
, col
) );
8769 // convert to device coords
8770 int left
, top
, right
, bottom
;
8771 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8772 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8775 m_gridWin
->GetClientSize( &cw
, &ch
);
8781 else if ( bottom
> ch
)
8783 int h
= r
.GetHeight();
8785 for ( i
= row
- 1; i
>= 0; i
-- )
8787 int rowHeight
= GetRowHeight(i
);
8788 if ( h
+ rowHeight
> ch
)
8795 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8796 // have rounding errors (this is important, because if we do,
8797 // we might not scroll at all and some cells won't be redrawn)
8799 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
8800 // so just add a full scroll unit...
8801 ypos
+= m_scrollLineY
;
8804 // special handling for wide cells - show always left part of the cell!
8805 // Otherwise, e.g. when stepping from row to row, it would jump between
8806 // left and right part of the cell on every step!
8808 if ( left
< 0 || (right
- left
) >= cw
)
8812 else if ( right
> cw
)
8814 // position the view so that the cell is on the right
8816 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8817 xpos
= x0
+ (right
- cw
);
8819 // see comment for ypos above
8820 xpos
+= m_scrollLineX
;
8823 if ( xpos
!= -1 || ypos
!= -1 )
8826 xpos
/= m_scrollLineX
;
8828 ypos
/= m_scrollLineY
;
8829 Scroll( xpos
, ypos
);
8836 // ------ Grid cursor movement functions
8839 bool wxGrid::MoveCursorUp( bool expandSelection
)
8841 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8842 m_currentCellCoords
.GetRow() >= 0 )
8844 if ( expandSelection
)
8846 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8847 m_selectingKeyboard
= m_currentCellCoords
;
8848 if ( m_selectingKeyboard
.GetRow() > 0 )
8850 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8851 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8852 m_selectingKeyboard
.GetCol() );
8853 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8856 else if ( m_currentCellCoords
.GetRow() > 0 )
8858 int row
= m_currentCellCoords
.GetRow() - 1;
8859 int col
= m_currentCellCoords
.GetCol();
8861 MakeCellVisible( row
, col
);
8862 SetCurrentCell( row
, col
);
8873 bool wxGrid::MoveCursorDown( bool expandSelection
)
8875 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8876 m_currentCellCoords
.GetRow() < m_numRows
)
8878 if ( expandSelection
)
8880 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8881 m_selectingKeyboard
= m_currentCellCoords
;
8882 if ( m_selectingKeyboard
.GetRow() < m_numRows
- 1 )
8884 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8885 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8886 m_selectingKeyboard
.GetCol() );
8887 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8890 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8892 int row
= m_currentCellCoords
.GetRow() + 1;
8893 int col
= m_currentCellCoords
.GetCol();
8895 MakeCellVisible( row
, col
);
8896 SetCurrentCell( row
, col
);
8907 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8909 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8910 m_currentCellCoords
.GetCol() >= 0 )
8912 if ( expandSelection
)
8914 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8915 m_selectingKeyboard
= m_currentCellCoords
;
8916 if ( m_selectingKeyboard
.GetCol() > 0 )
8918 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8919 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8920 m_selectingKeyboard
.GetCol() );
8921 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8924 else if ( GetColPos( m_currentCellCoords
.GetCol() ) > 0 )
8926 int row
= m_currentCellCoords
.GetRow();
8927 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) - 1 );
8930 MakeCellVisible( row
, col
);
8931 SetCurrentCell( row
, col
);
8942 bool wxGrid::MoveCursorRight( bool expandSelection
)
8944 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8945 m_currentCellCoords
.GetCol() < m_numCols
)
8947 if ( expandSelection
)
8949 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8950 m_selectingKeyboard
= m_currentCellCoords
;
8951 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8953 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8954 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8955 m_selectingKeyboard
.GetCol() );
8956 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8959 else if ( GetColPos( m_currentCellCoords
.GetCol() ) < m_numCols
- 1 )
8961 int row
= m_currentCellCoords
.GetRow();
8962 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) + 1 );
8965 MakeCellVisible( row
, col
);
8966 SetCurrentCell( row
, col
);
8977 bool wxGrid::MovePageUp()
8979 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8982 int row
= m_currentCellCoords
.GetRow();
8986 m_gridWin
->GetClientSize( &cw
, &ch
);
8988 int y
= GetRowTop(row
);
8989 int newRow
= internalYToRow( y
- ch
+ 1 );
8991 if ( newRow
== row
)
8993 // row > 0, so newRow can never be less than 0 here.
8997 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8998 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
9006 bool wxGrid::MovePageDown()
9008 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9011 int row
= m_currentCellCoords
.GetRow();
9012 if ( (row
+ 1) < m_numRows
)
9015 m_gridWin
->GetClientSize( &cw
, &ch
);
9017 int y
= GetRowTop(row
);
9018 int newRow
= internalYToRow( y
+ ch
);
9019 if ( newRow
== row
)
9021 // row < m_numRows, so newRow can't overflow here.
9025 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
9026 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
9034 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
9037 m_currentCellCoords
!= wxGridNoCellCoords
&&
9038 m_currentCellCoords
.GetRow() > 0 )
9040 int row
= m_currentCellCoords
.GetRow();
9041 int col
= m_currentCellCoords
.GetCol();
9043 if ( m_table
->IsEmptyCell(row
, col
) )
9045 // starting in an empty cell: find the next block of
9051 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9055 else if ( m_table
->IsEmptyCell(row
- 1, col
) )
9057 // starting at the top of a block: find the next block
9063 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9069 // starting within a block: find the top of the block
9074 if ( m_table
->IsEmptyCell(row
, col
) )
9082 MakeCellVisible( row
, col
);
9083 if ( expandSelection
)
9085 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9086 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9091 SetCurrentCell( row
, col
);
9100 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
9103 m_currentCellCoords
!= wxGridNoCellCoords
&&
9104 m_currentCellCoords
.GetRow() < m_numRows
- 1 )
9106 int row
= m_currentCellCoords
.GetRow();
9107 int col
= m_currentCellCoords
.GetCol();
9109 if ( m_table
->IsEmptyCell(row
, col
) )
9111 // starting in an empty cell: find the next block of
9114 while ( row
< m_numRows
- 1 )
9117 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9121 else if ( m_table
->IsEmptyCell(row
+ 1, col
) )
9123 // starting at the bottom of a block: find the next block
9126 while ( row
< m_numRows
- 1 )
9129 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9135 // starting within a block: find the bottom of the block
9137 while ( row
< m_numRows
- 1 )
9140 if ( m_table
->IsEmptyCell(row
, col
) )
9148 MakeCellVisible( row
, col
);
9149 if ( expandSelection
)
9151 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9152 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9157 SetCurrentCell( row
, col
);
9166 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
9169 m_currentCellCoords
!= wxGridNoCellCoords
&&
9170 m_currentCellCoords
.GetCol() > 0 )
9172 int row
= m_currentCellCoords
.GetRow();
9173 int col
= m_currentCellCoords
.GetCol();
9175 if ( m_table
->IsEmptyCell(row
, col
) )
9177 // starting in an empty cell: find the next block of
9183 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9187 else if ( m_table
->IsEmptyCell(row
, col
- 1) )
9189 // starting at the left of a block: find the next block
9195 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9201 // starting within a block: find the left of the block
9206 if ( m_table
->IsEmptyCell(row
, col
) )
9214 MakeCellVisible( row
, col
);
9215 if ( expandSelection
)
9217 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9218 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9223 SetCurrentCell( row
, col
);
9232 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
9235 m_currentCellCoords
!= wxGridNoCellCoords
&&
9236 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
9238 int row
= m_currentCellCoords
.GetRow();
9239 int col
= m_currentCellCoords
.GetCol();
9241 if ( m_table
->IsEmptyCell(row
, col
) )
9243 // starting in an empty cell: find the next block of
9246 while ( col
< m_numCols
- 1 )
9249 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9253 else if ( m_table
->IsEmptyCell(row
, col
+ 1) )
9255 // starting at the right of a block: find the next block
9258 while ( col
< m_numCols
- 1 )
9261 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9267 // starting within a block: find the right of the block
9269 while ( col
< m_numCols
- 1 )
9272 if ( m_table
->IsEmptyCell(row
, col
) )
9280 MakeCellVisible( row
, col
);
9281 if ( expandSelection
)
9283 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9284 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9289 SetCurrentCell( row
, col
);
9299 // ------ Label values and formatting
9302 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
9305 *horiz
= m_rowLabelHorizAlign
;
9307 *vert
= m_rowLabelVertAlign
;
9310 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
9313 *horiz
= m_colLabelHorizAlign
;
9315 *vert
= m_colLabelVertAlign
;
9318 int wxGrid::GetColLabelTextOrientation() const
9320 return m_colLabelTextOrientation
;
9323 wxString
wxGrid::GetRowLabelValue( int row
) const
9327 return m_table
->GetRowLabelValue( row
);
9337 wxString
wxGrid::GetColLabelValue( int col
) const
9341 return m_table
->GetColLabelValue( col
);
9351 void wxGrid::SetRowLabelSize( int width
)
9353 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
9355 if ( width
== wxGRID_AUTOSIZE
)
9357 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
9360 if ( width
!= m_rowLabelWidth
)
9364 m_rowLabelWin
->Show( false );
9365 m_cornerLabelWin
->Show( false );
9367 else if ( m_rowLabelWidth
== 0 )
9369 m_rowLabelWin
->Show( true );
9370 if ( m_colLabelHeight
> 0 )
9371 m_cornerLabelWin
->Show( true );
9374 m_rowLabelWidth
= width
;
9376 wxScrolledWindow::Refresh( true );
9380 void wxGrid::SetColLabelSize( int height
)
9382 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
9384 if ( height
== wxGRID_AUTOSIZE
)
9386 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
9389 if ( height
!= m_colLabelHeight
)
9393 m_colLabelWin
->Show( false );
9394 m_cornerLabelWin
->Show( false );
9396 else if ( m_colLabelHeight
== 0 )
9398 m_colLabelWin
->Show( true );
9399 if ( m_rowLabelWidth
> 0 )
9400 m_cornerLabelWin
->Show( true );
9403 m_colLabelHeight
= height
;
9405 wxScrolledWindow::Refresh( true );
9409 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
9411 if ( m_labelBackgroundColour
!= colour
)
9413 m_labelBackgroundColour
= colour
;
9414 m_rowLabelWin
->SetBackgroundColour( colour
);
9415 m_colLabelWin
->SetBackgroundColour( colour
);
9416 m_cornerLabelWin
->SetBackgroundColour( colour
);
9418 if ( !GetBatchCount() )
9420 m_rowLabelWin
->Refresh();
9421 m_colLabelWin
->Refresh();
9422 m_cornerLabelWin
->Refresh();
9427 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
9429 if ( m_labelTextColour
!= colour
)
9431 m_labelTextColour
= colour
;
9432 if ( !GetBatchCount() )
9434 m_rowLabelWin
->Refresh();
9435 m_colLabelWin
->Refresh();
9440 void wxGrid::SetLabelFont( const wxFont
& font
)
9443 if ( !GetBatchCount() )
9445 m_rowLabelWin
->Refresh();
9446 m_colLabelWin
->Refresh();
9450 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
9452 // allow old (incorrect) defs to be used
9455 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9456 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9457 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9462 case wxTOP
: vert
= wxALIGN_TOP
; break;
9463 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9464 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9467 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9469 m_rowLabelHorizAlign
= horiz
;
9472 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9474 m_rowLabelVertAlign
= vert
;
9477 if ( !GetBatchCount() )
9479 m_rowLabelWin
->Refresh();
9483 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
9485 // allow old (incorrect) defs to be used
9488 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9489 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9490 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9495 case wxTOP
: vert
= wxALIGN_TOP
; break;
9496 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9497 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9500 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9502 m_colLabelHorizAlign
= horiz
;
9505 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9507 m_colLabelVertAlign
= vert
;
9510 if ( !GetBatchCount() )
9512 m_colLabelWin
->Refresh();
9516 // Note: under MSW, the default column label font must be changed because it
9517 // does not support vertical printing
9519 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9520 // pGrid->SetLabelFont(font);
9521 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9523 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9525 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9526 m_colLabelTextOrientation
= textOrientation
;
9528 if ( !GetBatchCount() )
9529 m_colLabelWin
->Refresh();
9532 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9536 m_table
->SetRowLabelValue( row
, s
);
9537 if ( !GetBatchCount() )
9539 wxRect rect
= CellToRect( row
, 0 );
9540 if ( rect
.height
> 0 )
9542 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9544 rect
.width
= m_rowLabelWidth
;
9545 m_rowLabelWin
->Refresh( true, &rect
);
9551 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9555 m_table
->SetColLabelValue( col
, s
);
9556 if ( !GetBatchCount() )
9558 wxRect rect
= CellToRect( 0, col
);
9559 if ( rect
.width
> 0 )
9561 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9563 rect
.height
= m_colLabelHeight
;
9564 m_colLabelWin
->Refresh( true, &rect
);
9570 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9572 if ( m_gridLineColour
!= colour
)
9574 m_gridLineColour
= colour
;
9576 wxClientDC
dc( m_gridWin
);
9578 DrawAllGridLines( dc
, wxRegion() );
9582 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9584 if ( m_cellHighlightColour
!= colour
)
9586 m_cellHighlightColour
= colour
;
9588 wxClientDC
dc( m_gridWin
);
9590 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9591 DrawCellHighlight(dc
, attr
);
9596 void wxGrid::SetCellHighlightPenWidth(int width
)
9598 if (m_cellHighlightPenWidth
!= width
)
9600 m_cellHighlightPenWidth
= width
;
9602 // Just redrawing the cell highlight is not enough since that won't
9603 // make any visible change if the the thickness is getting smaller.
9604 int row
= m_currentCellCoords
.GetRow();
9605 int col
= m_currentCellCoords
.GetCol();
9606 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9609 wxRect rect
= CellToRect(row
, col
);
9610 m_gridWin
->Refresh(true, &rect
);
9614 void wxGrid::SetCellHighlightROPenWidth(int width
)
9616 if (m_cellHighlightROPenWidth
!= width
)
9618 m_cellHighlightROPenWidth
= width
;
9620 // Just redrawing the cell highlight is not enough since that won't
9621 // make any visible change if the the thickness is getting smaller.
9622 int row
= m_currentCellCoords
.GetRow();
9623 int col
= m_currentCellCoords
.GetCol();
9624 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9627 wxRect rect
= CellToRect(row
, col
);
9628 m_gridWin
->Refresh(true, &rect
);
9632 void wxGrid::EnableGridLines( bool enable
)
9634 if ( enable
!= m_gridLinesEnabled
)
9636 m_gridLinesEnabled
= enable
;
9638 if ( !GetBatchCount() )
9642 wxClientDC
dc( m_gridWin
);
9644 DrawAllGridLines( dc
, wxRegion() );
9648 m_gridWin
->Refresh();
9654 int wxGrid::GetDefaultRowSize() const
9656 return m_defaultRowHeight
;
9659 int wxGrid::GetRowSize( int row
) const
9661 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9663 return GetRowHeight(row
);
9666 int wxGrid::GetDefaultColSize() const
9668 return m_defaultColWidth
;
9671 int wxGrid::GetColSize( int col
) const
9673 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9675 return GetColWidth(col
);
9678 // ============================================================================
9679 // access to the grid attributes: each of them has a default value in the grid
9680 // itself and may be overidden on a per-cell basis
9681 // ============================================================================
9683 // ----------------------------------------------------------------------------
9684 // setting default attributes
9685 // ----------------------------------------------------------------------------
9687 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9689 m_defaultCellAttr
->SetBackgroundColour(col
);
9691 m_gridWin
->SetBackgroundColour(col
);
9695 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9697 m_defaultCellAttr
->SetTextColour(col
);
9700 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9702 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9705 void wxGrid::SetDefaultCellOverflow( bool allow
)
9707 m_defaultCellAttr
->SetOverflow(allow
);
9710 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9712 m_defaultCellAttr
->SetFont(font
);
9715 // For editors and renderers the type registry takes precedence over the
9716 // default attr, so we need to register the new editor/renderer for the string
9717 // data type in order to make setting a default editor/renderer appear to
9720 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9722 RegisterDataType(wxGRID_VALUE_STRING
,
9724 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9727 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9729 RegisterDataType(wxGRID_VALUE_STRING
,
9730 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9734 // ----------------------------------------------------------------------------
9735 // access to the default attributes
9736 // ----------------------------------------------------------------------------
9738 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
9740 return m_defaultCellAttr
->GetBackgroundColour();
9743 wxColour
wxGrid::GetDefaultCellTextColour() const
9745 return m_defaultCellAttr
->GetTextColour();
9748 wxFont
wxGrid::GetDefaultCellFont() const
9750 return m_defaultCellAttr
->GetFont();
9753 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
9755 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9758 bool wxGrid::GetDefaultCellOverflow() const
9760 return m_defaultCellAttr
->GetOverflow();
9763 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9765 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9768 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9770 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9773 // ----------------------------------------------------------------------------
9774 // access to cell attributes
9775 // ----------------------------------------------------------------------------
9777 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
9779 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9780 wxColour colour
= attr
->GetBackgroundColour();
9786 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
9788 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9789 wxColour colour
= attr
->GetTextColour();
9795 wxFont
wxGrid::GetCellFont( int row
, int col
) const
9797 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9798 wxFont font
= attr
->GetFont();
9804 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
9806 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9807 attr
->GetAlignment(horiz
, vert
);
9811 bool wxGrid::GetCellOverflow( int row
, int col
) const
9813 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9814 bool allow
= attr
->GetOverflow();
9820 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
9822 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9823 attr
->GetSize( num_rows
, num_cols
);
9827 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
9829 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9830 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9836 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
9838 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9839 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9845 bool wxGrid::IsReadOnly(int row
, int col
) const
9847 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9848 bool isReadOnly
= attr
->IsReadOnly();
9854 // ----------------------------------------------------------------------------
9855 // attribute support: cache, automatic provider creation, ...
9856 // ----------------------------------------------------------------------------
9858 bool wxGrid::CanHaveAttributes() const
9865 return m_table
->CanHaveAttributes();
9868 void wxGrid::ClearAttrCache()
9870 if ( m_attrCache
.row
!= -1 )
9872 wxSafeDecRef(m_attrCache
.attr
);
9873 m_attrCache
.attr
= NULL
;
9874 m_attrCache
.row
= -1;
9878 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9882 wxGrid
*self
= (wxGrid
*)this; // const_cast
9884 self
->ClearAttrCache();
9885 self
->m_attrCache
.row
= row
;
9886 self
->m_attrCache
.col
= col
;
9887 self
->m_attrCache
.attr
= attr
;
9892 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9894 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9896 *attr
= m_attrCache
.attr
;
9897 wxSafeIncRef(m_attrCache
.attr
);
9899 #ifdef DEBUG_ATTR_CACHE
9900 gs_nAttrCacheHits
++;
9907 #ifdef DEBUG_ATTR_CACHE
9908 gs_nAttrCacheMisses
++;
9915 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9917 wxGridCellAttr
*attr
= NULL
;
9918 // Additional test to avoid looking at the cache e.g. for
9919 // wxNoCellCoords, as this will confuse memory management.
9922 if ( !LookupAttr(row
, col
, &attr
) )
9924 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9925 : (wxGridCellAttr
*)NULL
;
9926 CacheAttr(row
, col
, attr
);
9932 attr
->SetDefAttr(m_defaultCellAttr
);
9936 attr
= m_defaultCellAttr
;
9943 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9945 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9946 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9948 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9949 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9951 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9954 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9956 // artificially inc the ref count to match DecRef() in caller
9958 m_table
->SetAttr(attr
, row
, col
);
9964 // ----------------------------------------------------------------------------
9965 // setting column attributes (wrappers around SetColAttr)
9966 // ----------------------------------------------------------------------------
9968 void wxGrid::SetColFormatBool(int col
)
9970 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9973 void wxGrid::SetColFormatNumber(int col
)
9975 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9978 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9980 wxString typeName
= wxGRID_VALUE_FLOAT
;
9981 if ( (width
!= -1) || (precision
!= -1) )
9983 typeName
<< _T(':') << width
<< _T(',') << precision
;
9986 SetColFormatCustom(col
, typeName
);
9989 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9991 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9993 attr
= new wxGridCellAttr
;
9994 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9995 attr
->SetRenderer(renderer
);
9997 SetColAttr(col
, attr
);
10001 // ----------------------------------------------------------------------------
10002 // setting cell attributes: this is forwarded to the table
10003 // ----------------------------------------------------------------------------
10005 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
10007 if ( CanHaveAttributes() )
10009 m_table
->SetAttr(attr
, row
, col
);
10014 wxSafeDecRef(attr
);
10018 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
10020 if ( CanHaveAttributes() )
10022 m_table
->SetRowAttr(attr
, row
);
10027 wxSafeDecRef(attr
);
10031 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
10033 if ( CanHaveAttributes() )
10035 m_table
->SetColAttr(attr
, col
);
10040 wxSafeDecRef(attr
);
10044 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
10046 if ( CanHaveAttributes() )
10048 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10049 attr
->SetBackgroundColour(colour
);
10054 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
10056 if ( CanHaveAttributes() )
10058 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10059 attr
->SetTextColour(colour
);
10064 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
10066 if ( CanHaveAttributes() )
10068 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10069 attr
->SetFont(font
);
10074 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
10076 if ( CanHaveAttributes() )
10078 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10079 attr
->SetAlignment(horiz
, vert
);
10084 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
10086 if ( CanHaveAttributes() )
10088 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10089 attr
->SetOverflow(allow
);
10094 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
10096 if ( CanHaveAttributes() )
10098 int cell_rows
, cell_cols
;
10100 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10101 attr
->GetSize(&cell_rows
, &cell_cols
);
10102 attr
->SetSize(num_rows
, num_cols
);
10105 // Cannot set the size of a cell to 0 or negative values
10106 // While it is perfectly legal to do that, this function cannot
10107 // handle all the possibilies, do it by hand by getting the CellAttr.
10108 // You can only set the size of a cell to 1,1 or greater with this fn
10109 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
10110 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10111 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
10112 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10114 // if this was already a multicell then "turn off" the other cells first
10115 if ((cell_rows
> 1) || (cell_rows
> 1))
10118 for (j
=row
; j
< row
+ cell_rows
; j
++)
10120 for (i
=col
; i
< col
+ cell_cols
; i
++)
10122 if ((i
!= col
) || (j
!= row
))
10124 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10125 attr_stub
->SetSize( 1, 1 );
10126 attr_stub
->DecRef();
10132 // mark the cells that will be covered by this cell to
10133 // negative or zero values to point back at this cell
10134 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
10137 for (j
=row
; j
< row
+ num_rows
; j
++)
10139 for (i
=col
; i
< col
+ num_cols
; i
++)
10141 if ((i
!= col
) || (j
!= row
))
10143 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10144 attr_stub
->SetSize( row
- j
, col
- i
);
10145 attr_stub
->DecRef();
10153 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
10155 if ( CanHaveAttributes() )
10157 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10158 attr
->SetRenderer(renderer
);
10163 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
10165 if ( CanHaveAttributes() )
10167 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10168 attr
->SetEditor(editor
);
10173 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
10175 if ( CanHaveAttributes() )
10177 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10178 attr
->SetReadOnly(isReadOnly
);
10183 // ----------------------------------------------------------------------------
10184 // Data type registration
10185 // ----------------------------------------------------------------------------
10187 void wxGrid::RegisterDataType(const wxString
& typeName
,
10188 wxGridCellRenderer
* renderer
,
10189 wxGridCellEditor
* editor
)
10191 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
10195 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
10197 wxString typeName
= m_table
->GetTypeName(row
, col
);
10198 return GetDefaultEditorForType(typeName
);
10201 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
10203 wxString typeName
= m_table
->GetTypeName(row
, col
);
10204 return GetDefaultRendererForType(typeName
);
10207 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
10209 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10210 if ( index
== wxNOT_FOUND
)
10212 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10217 return m_typeRegistry
->GetEditor(index
);
10220 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(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
->GetRenderer(index
);
10233 // ----------------------------------------------------------------------------
10235 // ----------------------------------------------------------------------------
10237 void wxGrid::EnableDragRowSize( bool enable
)
10239 m_canDragRowSize
= enable
;
10242 void wxGrid::EnableDragColSize( bool enable
)
10244 m_canDragColSize
= enable
;
10247 void wxGrid::EnableDragGridSize( bool enable
)
10249 m_canDragGridSize
= enable
;
10252 void wxGrid::EnableDragCell( bool enable
)
10254 m_canDragCell
= enable
;
10257 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
10259 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
10261 if ( resizeExistingRows
)
10263 // since we are resizing all rows to the default row size,
10264 // we can simply clear the row heights and row bottoms
10265 // arrays (which also allows us to take advantage of
10266 // some speed optimisations)
10267 m_rowHeights
.Empty();
10268 m_rowBottoms
.Empty();
10269 if ( !GetBatchCount() )
10274 void wxGrid::SetRowSize( int row
, int height
)
10276 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
10278 // See comment in SetColSize
10279 if ( height
< GetRowMinimalAcceptableHeight())
10282 if ( m_rowHeights
.IsEmpty() )
10284 // need to really create the array
10288 int h
= wxMax( 0, height
);
10289 int diff
= h
- m_rowHeights
[row
];
10291 m_rowHeights
[row
] = h
;
10292 for ( int i
= row
; i
< m_numRows
; i
++ )
10294 m_rowBottoms
[i
] += diff
;
10297 if ( !GetBatchCount() )
10301 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
10303 // we dont allow zero default column width
10304 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
10306 if ( resizeExistingCols
)
10308 // since we are resizing all columns to the default column size,
10309 // we can simply clear the col widths and col rights
10310 // arrays (which also allows us to take advantage of
10311 // some speed optimisations)
10312 m_colWidths
.Empty();
10313 m_colRights
.Empty();
10314 if ( !GetBatchCount() )
10319 void wxGrid::SetColSize( int col
, int width
)
10321 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
10323 // should we check that it's bigger than GetColMinimalWidth(col) here?
10325 // No, because it is reasonable to assume the library user know's
10326 // what he is doing. However we should test against the weaker
10327 // constraint of minimalAcceptableWidth, as this breaks rendering
10329 // This test then fixes sf.net bug #645734
10331 if ( width
< GetColMinimalAcceptableWidth() )
10334 if ( m_colWidths
.IsEmpty() )
10336 // need to really create the array
10340 // if < 0 then calculate new width from label
10344 wxArrayString lines
;
10345 wxClientDC
dc(m_colLabelWin
);
10346 dc
.SetFont(GetLabelFont());
10347 StringToLines(GetColLabelValue(col
), lines
);
10348 GetTextBoxSize(dc
, lines
, &w
, &h
);
10352 int w
= wxMax( 0, width
);
10353 int diff
= w
- m_colWidths
[col
];
10354 m_colWidths
[col
] = w
;
10356 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
10358 m_colRights
[GetColAt(colPos
)] += diff
;
10361 if ( !GetBatchCount() )
10365 void wxGrid::SetColMinimalWidth( int col
, int width
)
10367 if (width
> GetColMinimalAcceptableWidth())
10369 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10370 m_colMinWidths
[key
] = width
;
10374 void wxGrid::SetRowMinimalHeight( int row
, int width
)
10376 if (width
> GetRowMinimalAcceptableHeight())
10378 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10379 m_rowMinHeights
[key
] = width
;
10383 int wxGrid::GetColMinimalWidth(int col
) const
10385 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10386 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
10388 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
10391 int wxGrid::GetRowMinimalHeight(int row
) const
10393 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10394 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
10396 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
10399 void wxGrid::SetColMinimalAcceptableWidth( int width
)
10401 // We do allow a width of 0 since this gives us
10402 // an easy way to temporarily hiding columns.
10404 m_minAcceptableColWidth
= width
;
10407 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
10409 // We do allow a height of 0 since this gives us
10410 // an easy way to temporarily hiding rows.
10412 m_minAcceptableRowHeight
= height
;
10415 int wxGrid::GetColMinimalAcceptableWidth() const
10417 return m_minAcceptableColWidth
;
10420 int wxGrid::GetRowMinimalAcceptableHeight() const
10422 return m_minAcceptableRowHeight
;
10425 // ----------------------------------------------------------------------------
10427 // ----------------------------------------------------------------------------
10430 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
10432 const bool column
= direction
== wxGRID_COLUMN
;
10434 wxClientDC
dc(m_gridWin
);
10436 // cancel editing of cell
10437 HideCellEditControl();
10438 SaveEditControlValue();
10440 // init both of them to avoid compiler warnings, even if we only need one
10448 wxCoord extent
, extentMax
= 0;
10449 int max
= column
? m_numRows
: m_numCols
;
10450 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
10457 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
10458 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
10461 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
10462 extent
= column
? size
.x
: size
.y
;
10463 if ( extent
> extentMax
)
10464 extentMax
= extent
;
10466 renderer
->DecRef();
10472 // now also compare with the column label extent
10474 dc
.SetFont( GetLabelFont() );
10478 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
10479 if ( GetColLabelTextOrientation() == wxVERTICAL
)
10483 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
10485 extent
= column
? w
: h
;
10486 if ( extent
> extentMax
)
10487 extentMax
= extent
;
10491 // empty column - give default extent (notice that if extentMax is less
10492 // than default extent but != 0, it's OK)
10493 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
10498 // leave some space around text
10506 // Ensure automatic width is not less than minimal width. See the
10507 // comment in SetColSize() for explanation of why this isn't done
10508 // in SetColSize().
10510 extentMax
= wxMax(extentMax
, GetColMinimalWidth(col
));
10512 SetColSize( col
, extentMax
);
10513 if ( !GetBatchCount() )
10516 m_gridWin
->GetClientSize( &cw
, &ch
);
10517 wxRect
rect ( CellToRect( 0, col
) );
10519 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10520 rect
.width
= cw
- rect
.x
;
10521 rect
.height
= m_colLabelHeight
;
10522 m_colLabelWin
->Refresh( true, &rect
);
10527 // Ensure automatic width is not less than minimal height. See the
10528 // comment in SetColSize() for explanation of why this isn't done
10529 // in SetRowSize().
10531 extentMax
= wxMax(extentMax
, GetRowMinimalHeight(row
));
10533 SetRowSize(row
, extentMax
);
10534 if ( !GetBatchCount() )
10537 m_gridWin
->GetClientSize( &cw
, &ch
);
10538 wxRect
rect( CellToRect( row
, 0 ) );
10540 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10541 rect
.width
= m_rowLabelWidth
;
10542 rect
.height
= ch
- rect
.y
;
10543 m_rowLabelWin
->Refresh( true, &rect
);
10550 SetColMinimalWidth(col
, extentMax
);
10552 SetRowMinimalHeight(row
, extentMax
);
10556 wxCoord
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
10558 // calculate size for the rows or columns?
10559 const bool calcRows
= direction
== wxGRID_ROW
;
10561 wxClientDC
dc(calcRows
? GetGridRowLabelWindow()
10562 : GetGridColLabelWindow());
10563 dc
.SetFont(GetLabelFont());
10565 // which dimension should we take into account for calculations?
10567 // for columns, the text can be only horizontal so it's easy but for rows
10568 // we also have to take into account the text orientation
10570 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
10572 wxArrayString lines
;
10573 wxCoord extentMax
= 0;
10575 const int numRowsOrCols
= calcRows
? m_numRows
: m_numCols
;
10576 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
10580 wxString label
= calcRows
? GetRowLabelValue(rowOrCol
)
10581 : GetColLabelValue(rowOrCol
);
10582 StringToLines(label
, lines
);
10585 GetTextBoxSize(dc
, lines
, &w
, &h
);
10587 const wxCoord extent
= useWidth
? w
: h
;
10588 if ( extent
> extentMax
)
10589 extentMax
= extent
;
10594 // empty column - give default extent (notice that if extentMax is less
10595 // than default extent but != 0, it's OK)
10596 extentMax
= calcRows
? GetDefaultRowLabelSize()
10597 : GetDefaultColLabelSize();
10600 // leave some space around text (taken from AutoSizeColOrRow)
10609 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10611 int width
= m_rowLabelWidth
;
10613 wxGridUpdateLocker locker
;
10615 locker
.Create(this);
10617 for ( int col
= 0; col
< m_numCols
; col
++ )
10620 AutoSizeColumn(col
, setAsMin
);
10622 width
+= GetColWidth(col
);
10628 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10630 int height
= m_colLabelHeight
;
10632 wxGridUpdateLocker locker
;
10634 locker
.Create(this);
10636 for ( int row
= 0; row
< m_numRows
; row
++ )
10639 AutoSizeRow(row
, setAsMin
);
10641 height
+= GetRowHeight(row
);
10647 void wxGrid::AutoSize()
10649 wxGridUpdateLocker
locker(this);
10651 // we need to round up the size of the scrollable area to a multiple of
10652 // scroll step to ensure that we don't get the scrollbars when we're sized
10653 // exactly to fit our contents
10654 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
10655 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
10656 wxSize
sizeFit(GetScrollX(size
.x
) * GetScrollLineX(),
10657 GetScrollY(size
.y
) * GetScrollLineY());
10659 // distribute the extra space between the columns/rows to avoid having
10660 // extra white space
10661 wxCoord diff
= sizeFit
.x
- size
.x
;
10662 if ( diff
&& m_numCols
)
10664 // try to resize the columns uniformly
10665 wxCoord diffPerCol
= diff
/ m_numCols
;
10668 for ( int col
= 0; col
< m_numCols
; col
++ )
10670 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
10674 // add remaining amount to the last columns
10675 diff
-= diffPerCol
* m_numCols
;
10678 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
10680 SetColSize(col
, GetColWidth(col
) + 1);
10686 diff
= sizeFit
.y
- size
.y
;
10687 if ( diff
&& m_numRows
)
10689 // try to resize the columns uniformly
10690 wxCoord diffPerRow
= diff
/ m_numRows
;
10693 for ( int row
= 0; row
< m_numRows
; row
++ )
10695 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
10699 // add remaining amount to the last rows
10700 diff
-= diffPerRow
* m_numRows
;
10703 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
10705 SetRowSize(row
, GetRowHeight(row
) + 1);
10710 // we know that we're not going to have scrollbars so disable them now to
10711 // avoid trouble in SetClientSize() which can otherwise set the correct
10712 // client size but also leave space for (not needed any more) scrollbars
10713 SetScrollbars(0, 0, 0, 0, 0, 0, true);
10714 SetClientSize(sizeFit
.x
+ m_rowLabelWidth
, sizeFit
.y
+ m_colLabelHeight
);
10717 void wxGrid::AutoSizeRowLabelSize( int row
)
10719 wxArrayString lines
;
10722 // Hide the edit control, so it
10723 // won't interfere with drag-shrinking.
10724 if ( IsCellEditControlShown() )
10726 HideCellEditControl();
10727 SaveEditControlValue();
10730 // autosize row height depending on label text
10731 StringToLines( GetRowLabelValue( row
), lines
);
10732 wxClientDC
dc( m_rowLabelWin
);
10733 GetTextBoxSize( dc
, lines
, &w
, &h
);
10734 if ( h
< m_defaultRowHeight
)
10735 h
= m_defaultRowHeight
;
10736 SetRowSize(row
, h
);
10740 void wxGrid::AutoSizeColLabelSize( int col
)
10742 wxArrayString lines
;
10745 // Hide the edit control, so it
10746 // won't interfere with drag-shrinking.
10747 if ( IsCellEditControlShown() )
10749 HideCellEditControl();
10750 SaveEditControlValue();
10753 // autosize column width depending on label text
10754 StringToLines( GetColLabelValue( col
), lines
);
10755 wxClientDC
dc( m_colLabelWin
);
10756 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10757 GetTextBoxSize( dc
, lines
, &w
, &h
);
10759 GetTextBoxSize( dc
, lines
, &h
, &w
);
10760 if ( w
< m_defaultColWidth
)
10761 w
= m_defaultColWidth
;
10762 SetColSize(col
, w
);
10766 wxSize
wxGrid::DoGetBestSize() const
10768 wxGrid
*self
= (wxGrid
*)this; // const_cast
10770 // we do the same as in AutoSize() here with the exception that we don't
10771 // change the column/row sizes, only calculate them
10772 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
10773 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
10774 wxSize
sizeFit(GetScrollX(size
.x
) * GetScrollLineX(),
10775 GetScrollY(size
.y
) * GetScrollLineY());
10777 // NOTE: This size should be cached, but first we need to add calls to
10778 // InvalidateBestSize everywhere that could change the results of this
10780 // CacheBestSize(size);
10782 return wxSize(sizeFit
.x
+ m_rowLabelWidth
, sizeFit
.y
+ m_colLabelHeight
)
10783 + GetWindowBorderSize();
10791 wxPen
& wxGrid::GetDividerPen() const
10796 // ----------------------------------------------------------------------------
10797 // cell value accessor functions
10798 // ----------------------------------------------------------------------------
10800 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10804 m_table
->SetValue( row
, col
, s
);
10805 if ( !GetBatchCount() )
10808 wxRect
rect( CellToRect( row
, col
) );
10810 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10811 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10812 m_gridWin
->Refresh( false, &rect
);
10815 if ( m_currentCellCoords
.GetRow() == row
&&
10816 m_currentCellCoords
.GetCol() == col
&&
10817 IsCellEditControlShown())
10818 // Note: If we are using IsCellEditControlEnabled,
10819 // this interacts badly with calling SetCellValue from
10820 // an EVT_GRID_CELL_CHANGE handler.
10822 HideCellEditControl();
10823 ShowCellEditControl(); // will reread data from table
10828 // ----------------------------------------------------------------------------
10829 // block, row and column selection
10830 // ----------------------------------------------------------------------------
10832 void wxGrid::SelectRow( int row
, bool addToSelected
)
10834 if ( IsSelection() && !addToSelected
)
10838 m_selection
->SelectRow( row
, false, addToSelected
);
10841 void wxGrid::SelectCol( int col
, bool addToSelected
)
10843 if ( IsSelection() && !addToSelected
)
10847 m_selection
->SelectCol( col
, false, addToSelected
);
10850 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10851 bool addToSelected
)
10853 if ( IsSelection() && !addToSelected
)
10857 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10858 false, addToSelected
);
10861 void wxGrid::SelectAll()
10863 if ( m_numRows
> 0 && m_numCols
> 0 )
10866 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10870 // ----------------------------------------------------------------------------
10871 // cell, row and col deselection
10872 // ----------------------------------------------------------------------------
10874 void wxGrid::DeselectRow( int row
)
10876 if ( !m_selection
)
10879 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10881 if ( m_selection
->IsInSelection(row
, 0 ) )
10882 m_selection
->ToggleCellSelection(row
, 0);
10886 int nCols
= GetNumberCols();
10887 for ( int i
= 0; i
< nCols
; i
++ )
10889 if ( m_selection
->IsInSelection(row
, i
) )
10890 m_selection
->ToggleCellSelection(row
, i
);
10895 void wxGrid::DeselectCol( int col
)
10897 if ( !m_selection
)
10900 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10902 if ( m_selection
->IsInSelection(0, col
) )
10903 m_selection
->ToggleCellSelection(0, col
);
10907 int nRows
= GetNumberRows();
10908 for ( int i
= 0; i
< nRows
; i
++ )
10910 if ( m_selection
->IsInSelection(i
, col
) )
10911 m_selection
->ToggleCellSelection(i
, col
);
10916 void wxGrid::DeselectCell( int row
, int col
)
10918 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10919 m_selection
->ToggleCellSelection(row
, col
);
10922 bool wxGrid::IsSelection() const
10924 return ( m_selection
&& (m_selection
->IsSelection() ||
10925 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10926 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10929 bool wxGrid::IsInSelection( int row
, int col
) const
10931 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10932 ( row
>= m_selectingTopLeft
.GetRow() &&
10933 col
>= m_selectingTopLeft
.GetCol() &&
10934 row
<= m_selectingBottomRight
.GetRow() &&
10935 col
<= m_selectingBottomRight
.GetCol() )) );
10938 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10942 wxGridCellCoordsArray a
;
10946 return m_selection
->m_cellSelection
;
10949 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10953 wxGridCellCoordsArray a
;
10957 return m_selection
->m_blockSelectionTopLeft
;
10960 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10964 wxGridCellCoordsArray a
;
10968 return m_selection
->m_blockSelectionBottomRight
;
10971 wxArrayInt
wxGrid::GetSelectedRows() const
10979 return m_selection
->m_rowSelection
;
10982 wxArrayInt
wxGrid::GetSelectedCols() const
10990 return m_selection
->m_colSelection
;
10993 void wxGrid::ClearSelection()
10995 m_selectingTopLeft
=
10996 m_selectingBottomRight
=
10997 m_selectingKeyboard
= wxGridNoCellCoords
;
10999 m_selection
->ClearSelection();
11002 // This function returns the rectangle that encloses the given block
11003 // in device coords clipped to the client size of the grid window.
11005 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
11006 const wxGridCellCoords
&bottomRight
) const
11008 wxRect
rect( wxGridNoCellRect
);
11011 cellRect
= CellToRect( topLeft
);
11012 if ( cellRect
!= wxGridNoCellRect
)
11018 rect
= wxRect(0, 0, 0, 0);
11021 cellRect
= CellToRect( bottomRight
);
11022 if ( cellRect
!= wxGridNoCellRect
)
11028 return wxGridNoCellRect
;
11032 int left
= rect
.GetLeft();
11033 int top
= rect
.GetTop();
11034 int right
= rect
.GetRight();
11035 int bottom
= rect
.GetBottom();
11037 int leftCol
= topLeft
.GetCol();
11038 int topRow
= topLeft
.GetRow();
11039 int rightCol
= bottomRight
.GetCol();
11040 int bottomRow
= bottomRight
.GetRow();
11048 leftCol
= rightCol
;
11058 topRow
= bottomRow
;
11062 for ( j
= topRow
; j
<= bottomRow
; j
++ )
11064 for ( i
= leftCol
; i
<= rightCol
; i
++ )
11066 if ((j
== topRow
) || (j
== bottomRow
) || (i
== leftCol
) || (i
== rightCol
))
11068 cellRect
= CellToRect( j
, i
);
11070 if (cellRect
.x
< left
)
11072 if (cellRect
.y
< top
)
11074 if (cellRect
.x
+ cellRect
.width
> right
)
11075 right
= cellRect
.x
+ cellRect
.width
;
11076 if (cellRect
.y
+ cellRect
.height
> bottom
)
11077 bottom
= cellRect
.y
+ cellRect
.height
;
11081 i
= rightCol
; // jump over inner cells.
11086 // convert to scrolled coords
11088 CalcScrolledPosition( left
, top
, &left
, &top
);
11089 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
11092 m_gridWin
->GetClientSize( &cw
, &ch
);
11094 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
11095 return wxRect(0,0,0,0);
11097 rect
.SetLeft( wxMax(0, left
) );
11098 rect
.SetTop( wxMax(0, top
) );
11099 rect
.SetRight( wxMin(cw
, right
) );
11100 rect
.SetBottom( wxMin(ch
, bottom
) );
11105 // ----------------------------------------------------------------------------
11106 // grid event classes
11107 // ----------------------------------------------------------------------------
11109 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
11111 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
11112 int row
, int col
, int x
, int y
, bool sel
,
11113 bool control
, bool shift
, bool alt
, bool meta
)
11114 : wxNotifyEvent( type
, id
)
11121 m_control
= control
;
11126 SetEventObject(obj
);
11130 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
11132 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
11133 int rowOrCol
, int x
, int y
,
11134 bool control
, bool shift
, bool alt
, bool meta
)
11135 : wxNotifyEvent( type
, id
)
11137 m_rowOrCol
= rowOrCol
;
11140 m_control
= control
;
11145 SetEventObject(obj
);
11149 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
11151 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
11152 const wxGridCellCoords
& topLeft
,
11153 const wxGridCellCoords
& bottomRight
,
11154 bool sel
, bool control
,
11155 bool shift
, bool alt
, bool meta
)
11156 : wxNotifyEvent( type
, id
)
11158 m_topLeft
= topLeft
;
11159 m_bottomRight
= bottomRight
;
11161 m_control
= control
;
11166 SetEventObject(obj
);
11170 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
11172 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
11173 wxObject
* obj
, int row
,
11174 int col
, wxControl
* ctrl
)
11175 : wxCommandEvent(type
, id
)
11177 SetEventObject(obj
);
11183 #endif // wxUSE_GRID