1 ///////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
5 // Modified by: Robin Dunn, Vadim Zeitlin, Santiago Palacios
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
25 #include "wx/dcclient.h"
26 #include "wx/settings.h"
28 #include "wx/textctrl.h"
29 #include "wx/checkbox.h"
30 #include "wx/combobox.h"
31 #include "wx/valtext.h"
34 #include "wx/listbox.h"
37 #include "wx/textfile.h"
38 #include "wx/spinctrl.h"
39 #include "wx/tokenzr.h"
40 #include "wx/renderer.h"
42 #include "wx/generic/gridsel.h"
44 const wxChar wxGridNameStr
[] = wxT("grid");
46 #if defined(__WXMOTIF__)
47 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
49 #define WXUNUSED_MOTIF(identifier) identifier
52 #if defined(__WXGTK__)
53 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
55 #define WXUNUSED_GTK(identifier) identifier
58 // Required for wxIs... functions
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr
*, wxArrayAttrs
,
66 class WXDLLIMPEXP_ADV
);
68 struct wxGridCellWithAttr
70 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
71 : coords(row
, col
), attr(attr_
)
76 wxGridCellWithAttr(const wxGridCellWithAttr
& other
)
77 : coords(other
.coords
),
83 wxGridCellWithAttr
& operator=(const wxGridCellWithAttr
& other
)
85 coords
= other
.coords
;
86 if (attr
!= other
.attr
)
95 void ChangeAttr(wxGridCellAttr
* new_attr
)
99 // "Delete" (i.e. DecRef) the old attribute.
102 // Take ownership of the new attribute, i.e. no IncRef.
106 ~wxGridCellWithAttr()
111 wxGridCellCoords coords
;
112 wxGridCellAttr
*attr
;
115 WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr
, wxGridCellWithAttrArray
,
116 class WXDLLIMPEXP_ADV
);
118 #include "wx/arrimpl.cpp"
120 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
121 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
123 // ----------------------------------------------------------------------------
125 // ----------------------------------------------------------------------------
127 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
128 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
129 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
130 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
131 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_BEGIN_DRAG
)
132 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
133 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
134 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
135 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
136 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
137 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
138 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_MOVE
)
139 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
140 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
141 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
142 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
143 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
144 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
146 // ----------------------------------------------------------------------------
148 // ----------------------------------------------------------------------------
150 // common base class for various grid subwindows
151 class WXDLLIMPEXP_ADV wxGridSubwindow
: public wxWindow
154 wxGridSubwindow() { m_owner
= NULL
; }
155 wxGridSubwindow(wxGrid
*owner
,
159 int additionalStyle
= 0,
160 const wxString
& name
= wxPanelNameStr
)
161 : wxWindow(owner
, id
, pos
, size
,
162 wxBORDER_NONE
| additionalStyle
,
168 virtual bool AcceptsFocus() const { return false; }
170 wxGrid
*GetOwner() { return m_owner
; }
173 void OnMouseCaptureLost(wxMouseCaptureLostEvent
& event
);
177 DECLARE_EVENT_TABLE()
178 DECLARE_NO_COPY_CLASS(wxGridSubwindow
)
181 class WXDLLIMPEXP_ADV wxGridRowLabelWindow
: public wxGridSubwindow
184 wxGridRowLabelWindow() { }
185 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
186 const wxPoint
&pos
, const wxSize
&size
);
189 void OnPaint( wxPaintEvent
& event
);
190 void OnMouseEvent( wxMouseEvent
& event
);
191 void OnMouseWheel( wxMouseEvent
& event
);
193 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
194 DECLARE_EVENT_TABLE()
195 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow
)
199 class WXDLLIMPEXP_ADV wxGridColLabelWindow
: public wxGridSubwindow
202 wxGridColLabelWindow() { }
203 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
204 const wxPoint
&pos
, const wxSize
&size
);
207 void OnPaint( wxPaintEvent
& event
);
208 void OnMouseEvent( wxMouseEvent
& event
);
209 void OnMouseWheel( wxMouseEvent
& event
);
211 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
212 DECLARE_EVENT_TABLE()
213 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow
)
217 class WXDLLIMPEXP_ADV wxGridCornerLabelWindow
: public wxGridSubwindow
220 wxGridCornerLabelWindow() { }
221 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
222 const wxPoint
&pos
, const wxSize
&size
);
225 void OnMouseEvent( wxMouseEvent
& event
);
226 void OnMouseWheel( wxMouseEvent
& event
);
227 void OnPaint( wxPaintEvent
& event
);
229 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
230 DECLARE_EVENT_TABLE()
231 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow
)
234 class WXDLLIMPEXP_ADV wxGridWindow
: public wxGridSubwindow
239 m_rowLabelWin
= NULL
;
240 m_colLabelWin
= NULL
;
243 wxGridWindow( wxGrid
*parent
,
244 wxGridRowLabelWindow
*rowLblWin
,
245 wxGridColLabelWindow
*colLblWin
,
246 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
248 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
250 virtual bool AcceptsFocus() const { return true; }
253 wxGridRowLabelWindow
*m_rowLabelWin
;
254 wxGridColLabelWindow
*m_colLabelWin
;
256 void OnPaint( wxPaintEvent
&event
);
257 void OnMouseWheel( wxMouseEvent
& event
);
258 void OnMouseEvent( wxMouseEvent
& event
);
259 void OnKeyDown( wxKeyEvent
& );
260 void OnKeyUp( wxKeyEvent
& );
261 void OnChar( wxKeyEvent
& );
262 void OnEraseBackground( wxEraseEvent
& );
263 void OnFocus( wxFocusEvent
& );
265 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
266 DECLARE_EVENT_TABLE()
267 DECLARE_NO_COPY_CLASS(wxGridWindow
)
271 class wxGridCellEditorEvtHandler
: public wxEvtHandler
274 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
281 void OnKillFocus(wxFocusEvent
& event
);
282 void OnKeyDown(wxKeyEvent
& event
);
283 void OnChar(wxKeyEvent
& event
);
285 void SetInSetFocus(bool inSetFocus
) { m_inSetFocus
= inSetFocus
; }
289 wxGridCellEditor
*m_editor
;
291 // Work around the fact that a focus kill event can be sent to
292 // a combobox within a set focus event.
295 DECLARE_EVENT_TABLE()
296 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
297 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler
)
301 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler
, wxEvtHandler
)
303 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
304 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus
)
305 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
306 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
310 // ----------------------------------------------------------------------------
311 // the internal data representation used by wxGridCellAttrProvider
312 // ----------------------------------------------------------------------------
314 // this class stores attributes set for cells
315 class WXDLLIMPEXP_ADV wxGridCellAttrData
318 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
319 wxGridCellAttr
*GetAttr(int row
, int col
) const;
320 void UpdateAttrRows( size_t pos
, int numRows
);
321 void UpdateAttrCols( size_t pos
, int numCols
);
324 // searches for the attr for given cell, returns wxNOT_FOUND if not found
325 int FindIndex(int row
, int col
) const;
327 wxGridCellWithAttrArray m_attrs
;
330 // this class stores attributes set for rows or columns
331 class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
334 // empty ctor to suppress warnings
335 wxGridRowOrColAttrData() {}
336 ~wxGridRowOrColAttrData();
338 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
339 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
340 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
343 wxArrayInt m_rowsOrCols
;
344 wxArrayAttrs m_attrs
;
347 // NB: this is just a wrapper around 3 objects: one which stores cell
348 // attributes, and 2 others for row/col ones
349 class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
352 wxGridCellAttrData m_cellAttrs
;
353 wxGridRowOrColAttrData m_rowAttrs
,
358 // ----------------------------------------------------------------------------
359 // data structures used for the data type registry
360 // ----------------------------------------------------------------------------
362 struct wxGridDataTypeInfo
364 wxGridDataTypeInfo(const wxString
& typeName
,
365 wxGridCellRenderer
* renderer
,
366 wxGridCellEditor
* editor
)
367 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
370 ~wxGridDataTypeInfo()
372 wxSafeDecRef(m_renderer
);
373 wxSafeDecRef(m_editor
);
377 wxGridCellRenderer
* m_renderer
;
378 wxGridCellEditor
* m_editor
;
380 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo
)
384 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
,
385 class WXDLLIMPEXP_ADV
);
388 class WXDLLIMPEXP_ADV wxGridTypeRegistry
391 wxGridTypeRegistry() {}
392 ~wxGridTypeRegistry();
394 void RegisterDataType(const wxString
& typeName
,
395 wxGridCellRenderer
* renderer
,
396 wxGridCellEditor
* editor
);
398 // find one of already registered data types
399 int FindRegisteredDataType(const wxString
& typeName
);
401 // try to FindRegisteredDataType(), if this fails and typeName is one of
402 // standard typenames, register it and return its index
403 int FindDataType(const wxString
& typeName
);
405 // try to FindDataType(), if it fails see if it is not one of already
406 // registered data types with some params in which case clone the
407 // registered data type and set params for it
408 int FindOrCloneDataType(const wxString
& typeName
);
410 wxGridCellRenderer
* GetRenderer(int index
);
411 wxGridCellEditor
* GetEditor(int index
);
414 wxGridDataTypeInfoArray m_typeinfo
;
418 // ----------------------------------------------------------------------------
419 // conditional compilation
420 // ----------------------------------------------------------------------------
422 #ifndef WXGRID_DRAW_LINES
423 #define WXGRID_DRAW_LINES 1
426 // ----------------------------------------------------------------------------
428 // ----------------------------------------------------------------------------
430 //#define DEBUG_ATTR_CACHE
431 #ifdef DEBUG_ATTR_CACHE
432 static size_t gs_nAttrCacheHits
= 0;
433 static size_t gs_nAttrCacheMisses
= 0;
436 // ----------------------------------------------------------------------------
438 // ----------------------------------------------------------------------------
440 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
441 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
444 // TODO: this doesn't work at all, grid cells have different sizes and approx
445 // calculations don't work as because of the size mismatch scrollbars
446 // sometimes fail to be shown when they should be or vice versa
448 // The scroll bars may be a little flakey once in a while, but that is
449 // surely much less horrible than having scroll lines of only 1!!!
452 // Well, it's still seriously broken so it might be better but needs
455 static const size_t GRID_SCROLL_LINE_X
= 15; // 1;
456 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
458 // the size of hash tables used a bit everywhere (the max number of elements
459 // in these hash tables is the number of rows/columns)
460 static const int GRID_HASH_SIZE
= 100;
463 // ----------------------------------------------------------------------------
465 // ----------------------------------------------------------------------------
467 static inline int GetScrollX(int x
)
469 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
472 static inline int GetScrollY(int y
)
474 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
478 // ============================================================================
480 // ============================================================================
482 // ----------------------------------------------------------------------------
484 // ----------------------------------------------------------------------------
486 wxGridCellEditor::wxGridCellEditor()
492 wxGridCellEditor::~wxGridCellEditor()
497 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
498 wxWindowID
WXUNUSED(id
),
499 wxEvtHandler
* evtHandler
)
502 m_control
->PushEventHandler(evtHandler
);
505 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
506 wxGridCellAttr
*attr
)
508 // erase the background because we might not fill the cell
509 wxClientDC
dc(m_control
->GetParent());
510 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
512 gridWindow
->GetOwner()->PrepareDC(dc
);
514 dc
.SetPen(*wxTRANSPARENT_PEN
);
515 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxBRUSHSTYLE_SOLID
));
516 dc
.DrawRectangle(rectCell
);
518 // redraw the control we just painted over
519 m_control
->Refresh();
522 void wxGridCellEditor::Destroy()
526 m_control
->PopEventHandler( true /* delete it*/ );
528 m_control
->Destroy();
533 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
535 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
537 m_control
->Show(show
);
541 // set the colours/fonts if we have any
544 m_colFgOld
= m_control
->GetForegroundColour();
545 m_control
->SetForegroundColour(attr
->GetTextColour());
547 m_colBgOld
= m_control
->GetBackgroundColour();
548 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
550 // Workaround for GTK+1 font setting problem on some platforms
551 #if !defined(__WXGTK__) || defined(__WXGTK20__)
552 m_fontOld
= m_control
->GetFont();
553 m_control
->SetFont(attr
->GetFont());
556 // can't do anything more in the base class version, the other
557 // attributes may only be used by the derived classes
562 // restore the standard colours fonts
563 if ( m_colFgOld
.Ok() )
565 m_control
->SetForegroundColour(m_colFgOld
);
566 m_colFgOld
= wxNullColour
;
569 if ( m_colBgOld
.Ok() )
571 m_control
->SetBackgroundColour(m_colBgOld
);
572 m_colBgOld
= wxNullColour
;
575 // Workaround for GTK+1 font setting problem on some platforms
576 #if !defined(__WXGTK__) || defined(__WXGTK20__)
577 if ( m_fontOld
.Ok() )
579 m_control
->SetFont(m_fontOld
);
580 m_fontOld
= wxNullFont
;
586 void wxGridCellEditor::SetSize(const wxRect
& rect
)
588 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
590 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
593 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
598 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
600 bool ctrl
= event
.ControlDown();
601 bool alt
= event
.AltDown();
604 // On the Mac the Alt key is more like shift and is used for entry of
605 // valid characters, so check for Ctrl and Meta instead.
606 alt
= event
.MetaDown();
609 // Assume it's not a valid char if ctrl or alt is down, but if both are
610 // down then it may be because of an AltGr key combination, so let them
611 // through in that case.
612 if ((ctrl
|| alt
) && !(ctrl
&& alt
))
619 // If it's a F-Key or other special key then it shouldn't start the
621 if (event
.GetKeyCode() >= WXK_START
)
625 // if the unicode key code is not really a unicode character (it may
626 // be a function key or etc., the platforms appear to always give us a
627 // small value in this case) then fallback to the ASCII key code but
628 // don't do anything for function keys or etc.
629 key
= event
.GetUnicodeKey();
632 key
= event
.GetKeyCode();
633 keyOk
= (key
<= 127);
636 key
= event
.GetKeyCode();
637 keyOk
= (key
<= 255);
643 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
648 void wxGridCellEditor::StartingClick()
654 // ----------------------------------------------------------------------------
655 // wxGridCellTextEditor
656 // ----------------------------------------------------------------------------
658 wxGridCellTextEditor::wxGridCellTextEditor()
663 void wxGridCellTextEditor::Create(wxWindow
* parent
,
665 wxEvtHandler
* evtHandler
)
667 DoCreate(parent
, id
, evtHandler
);
670 void wxGridCellTextEditor::DoCreate(wxWindow
* parent
,
672 wxEvtHandler
* evtHandler
,
675 style
|= wxTE_PROCESS_ENTER
| wxTE_PROCESS_TAB
| wxNO_BORDER
;
677 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
678 wxDefaultPosition
, wxDefaultSize
,
681 // set max length allowed in the textctrl, if the parameter was set
682 if ( m_maxChars
!= 0 )
684 Text()->SetMaxLength(m_maxChars
);
687 wxGridCellEditor::Create(parent
, id
, evtHandler
);
690 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
691 wxGridCellAttr
* WXUNUSED(attr
))
693 // as we fill the entire client area,
694 // don't do anything here to minimize flicker
697 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
699 wxRect
rect(rectOrig
);
701 // Make the edit control large enough to allow for internal margins
703 // TODO: remove this if the text ctrl sizing is improved esp. for unix
705 #if defined(__WXGTK__)
713 #elif defined(__WXMSW__)
727 int extra_x
= ( rect
.x
> 2 ) ? 2 : 1;
728 int extra_y
= ( rect
.y
> 2 ) ? 2 : 1;
730 #if defined(__WXMOTIF__)
735 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
736 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
737 rect
.SetRight( rect
.GetRight() + 2 * extra_x
);
738 rect
.SetBottom( rect
.GetBottom() + 2 * extra_y
);
741 wxGridCellEditor::SetSize(rect
);
744 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
746 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
748 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
750 DoBeginEdit(m_startValue
);
753 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
755 Text()->SetValue(startValue
);
756 Text()->SetInsertionPointEnd();
757 Text()->SetSelection(-1, -1);
761 bool wxGridCellTextEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
763 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
765 bool changed
= false;
766 wxString value
= Text()->GetValue();
767 if (value
!= m_startValue
)
771 grid
->GetTable()->SetValue(row
, col
, value
);
773 m_startValue
= wxEmptyString
;
775 // No point in setting the text of the hidden control
776 //Text()->SetValue(m_startValue);
781 void wxGridCellTextEditor::Reset()
783 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
785 DoReset(m_startValue
);
788 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
790 Text()->SetValue(startValue
);
791 Text()->SetInsertionPointEnd();
794 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
796 return wxGridCellEditor::IsAcceptedKey(event
);
799 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
801 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
802 // longer an appropriate way to get the character into the text control.
803 // Do it ourselves instead. We know that if we get this far that we have
804 // a valid character, so not a whole lot of testing needs to be done.
806 wxTextCtrl
* tc
= Text();
811 ch
= event
.GetUnicodeKey();
813 ch
= (wxChar
)event
.GetKeyCode();
815 ch
= (wxChar
)event
.GetKeyCode();
821 // delete the character at the cursor
822 pos
= tc
->GetInsertionPoint();
823 if (pos
< tc
->GetLastPosition())
824 tc
->Remove(pos
, pos
+ 1);
828 // delete the character before the cursor
829 pos
= tc
->GetInsertionPoint();
831 tc
->Remove(pos
- 1, pos
);
840 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
841 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
843 #if defined(__WXMOTIF__) || defined(__WXGTK__)
844 // wxMotif needs a little extra help...
845 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
846 wxString
s( Text()->GetValue() );
847 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
849 Text()->SetInsertionPoint( pos
);
851 // the other ports can handle a Return key press
857 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
867 if ( params
.ToLong(&tmp
) )
869 m_maxChars
= (size_t)tmp
;
873 wxLogDebug( _T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str() );
878 // return the value in the text control
879 wxString
wxGridCellTextEditor::GetValue() const
881 return Text()->GetValue();
884 // ----------------------------------------------------------------------------
885 // wxGridCellNumberEditor
886 // ----------------------------------------------------------------------------
888 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
894 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
896 wxEvtHandler
* evtHandler
)
901 // create a spin ctrl
902 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
903 wxDefaultPosition
, wxDefaultSize
,
907 wxGridCellEditor::Create(parent
, id
, evtHandler
);
912 // just a text control
913 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
916 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
921 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
923 // first get the value
924 wxGridTableBase
*table
= grid
->GetTable();
925 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
927 m_valueOld
= table
->GetValueAsLong(row
, col
);
932 wxString sValue
= table
->GetValue(row
, col
);
933 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty())
935 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
943 Spin()->SetValue((int)m_valueOld
);
949 DoBeginEdit(GetString());
953 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
963 value
= Spin()->GetValue();
964 changed
= value
!= m_valueOld
;
966 text
= wxString::Format(wxT("%ld"), value
);
971 text
= Text()->GetValue();
972 changed
= (text
.empty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
977 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
978 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
980 grid
->GetTable()->SetValue(row
, col
, text
);
986 void wxGridCellNumberEditor::Reset()
991 Spin()->SetValue((int)m_valueOld
);
996 DoReset(GetString());
1000 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
1002 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1004 int keycode
= event
.GetKeyCode();
1005 if ( (keycode
< 128) &&
1006 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
1015 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
1017 int keycode
= event
.GetKeyCode();
1020 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
1022 wxGridCellTextEditor::StartingKey(event
);
1024 // skip Skip() below
1031 if ( wxIsdigit(keycode
) )
1033 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
1034 spin
->SetValue(keycode
- '0');
1035 spin
->SetSelection(1,1);
1044 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
1055 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1059 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1063 // skip the error message below
1068 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1072 // return the value in the spin control if it is there (the text control otherwise)
1073 wxString
wxGridCellNumberEditor::GetValue() const
1080 long value
= Spin()->GetValue();
1081 s
.Printf(wxT("%ld"), value
);
1086 s
= Text()->GetValue();
1092 // ----------------------------------------------------------------------------
1093 // wxGridCellFloatEditor
1094 // ----------------------------------------------------------------------------
1096 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1099 m_precision
= precision
;
1102 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1104 wxEvtHandler
* evtHandler
)
1106 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1108 #if wxUSE_VALIDATORS
1109 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1113 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1115 // first get the value
1116 wxGridTableBase
* const table
= grid
->GetTable();
1117 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1119 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1125 const wxString value
= table
->GetValue(row
, col
);
1126 if ( !value
.empty() )
1128 if ( !value
.ToDouble(&m_valueOld
) )
1130 wxFAIL_MSG( _T("this cell doesn't have float value") );
1136 DoBeginEdit(GetString());
1139 bool wxGridCellFloatEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
1141 const wxString
text(Text()->GetValue()),
1142 textOld(grid
->GetCellValue(row
, col
));
1145 if ( !text
.empty() )
1147 if ( !text
.ToDouble(&value
) )
1150 else // new value is empty string
1152 if ( textOld
.empty() )
1153 return false; // nothing changed
1158 // the test for empty strings ensures that we don't skip the value setting
1159 // when "" is replaced by "0" or vice versa as "" numeric value is also 0.
1160 if ( wxIsSameDouble(value
, m_valueOld
) && !text
.empty() && !textOld
.empty() )
1161 return false; // nothing changed
1163 wxGridTableBase
* const table
= grid
->GetTable();
1165 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1166 table
->SetValueAsDouble(row
, col
, value
);
1168 table
->SetValue(row
, col
, text
);
1173 void wxGridCellFloatEditor::Reset()
1175 DoReset(GetString());
1178 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1180 int keycode
= event
.GetKeyCode();
1182 tmpbuf
[0] = (char) keycode
;
1184 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1187 bool is_decimal_point
= ( strbuf
==
1188 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1190 bool is_decimal_point
= ( strbuf
== _T(".") );
1193 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1194 || is_decimal_point
)
1196 wxGridCellTextEditor::StartingKey(event
);
1198 // skip Skip() below
1205 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1216 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1220 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1222 m_precision
= (int)tmp
;
1224 // skip the error message below
1229 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1233 wxString
wxGridCellFloatEditor::GetString() const
1236 if ( m_precision
== -1 && m_width
!= -1)
1238 // default precision
1239 fmt
.Printf(_T("%%%d.f"), m_width
);
1241 else if ( m_precision
!= -1 && m_width
== -1)
1244 fmt
.Printf(_T("%%.%df"), m_precision
);
1246 else if ( m_precision
!= -1 && m_width
!= -1 )
1248 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1252 // default width/precision
1256 return wxString::Format(fmt
, m_valueOld
);
1259 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1261 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1263 const int keycode
= event
.GetKeyCode();
1264 if ( isascii(keycode
) )
1267 tmpbuf
[0] = (char) keycode
;
1269 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1272 const wxString decimalPoint
=
1273 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
1275 const wxString
decimalPoint(_T('.'));
1278 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1279 if ( wxIsdigit(keycode
) ||
1280 tolower(keycode
) == 'e' ||
1281 keycode
== decimalPoint
||
1293 #endif // wxUSE_TEXTCTRL
1297 // ----------------------------------------------------------------------------
1298 // wxGridCellBoolEditor
1299 // ----------------------------------------------------------------------------
1301 // the default values for GetValue()
1302 wxString
wxGridCellBoolEditor::ms_stringValues
[2] = { _T(""), _T("1") };
1304 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1306 wxEvtHandler
* evtHandler
)
1308 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1309 wxDefaultPosition
, wxDefaultSize
,
1312 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1315 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1317 bool resize
= false;
1318 wxSize size
= m_control
->GetSize();
1319 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1321 // check if the checkbox is not too big/small for this cell
1322 wxSize sizeBest
= m_control
->GetBestSize();
1323 if ( !(size
== sizeBest
) )
1325 // reset to default size if it had been made smaller
1331 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1333 // leave 1 pixel margin
1334 size
.x
= size
.y
= minSize
- 2;
1341 m_control
->SetSize(size
);
1344 // position it in the centre of the rectangle (TODO: support alignment?)
1346 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1347 // the checkbox without label still has some space to the right in wxGTK,
1348 // so shift it to the right
1350 #elif defined(__WXMSW__)
1351 // here too, but in other way
1356 int hAlign
= wxALIGN_CENTRE
;
1357 int vAlign
= wxALIGN_CENTRE
;
1359 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1362 if (hAlign
== wxALIGN_LEFT
)
1370 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1372 else if (hAlign
== wxALIGN_RIGHT
)
1374 x
= r
.x
+ r
.width
- size
.x
- 2;
1375 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1377 else if (hAlign
== wxALIGN_CENTRE
)
1379 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1380 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1383 m_control
->Move(x
, y
);
1386 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1388 m_control
->Show(show
);
1392 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1393 CBox()->SetBackgroundColour(colBg
);
1397 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1399 wxASSERT_MSG(m_control
,
1400 wxT("The wxGridCellEditor must be created first!"));
1402 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1404 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1408 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1410 if ( cellval
== ms_stringValues
[false] )
1411 m_startValue
= false;
1412 else if ( cellval
== ms_stringValues
[true] )
1413 m_startValue
= true;
1416 // do not try to be smart here and convert it to true or false
1417 // because we'll still overwrite it with something different and
1418 // this risks to be very surprising for the user code, let them
1420 wxFAIL_MSG( _T("invalid value for a cell with bool editor!") );
1424 CBox()->SetValue(m_startValue
);
1428 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1431 wxASSERT_MSG(m_control
,
1432 wxT("The wxGridCellEditor must be created first!"));
1434 bool changed
= false;
1435 bool value
= CBox()->GetValue();
1436 if ( value
!= m_startValue
)
1441 wxGridTableBase
* const table
= grid
->GetTable();
1442 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1443 table
->SetValueAsBool(row
, col
, value
);
1445 table
->SetValue(row
, col
, GetValue());
1451 void wxGridCellBoolEditor::Reset()
1453 wxASSERT_MSG(m_control
,
1454 wxT("The wxGridCellEditor must be created first!"));
1456 CBox()->SetValue(m_startValue
);
1459 void wxGridCellBoolEditor::StartingClick()
1461 CBox()->SetValue(!CBox()->GetValue());
1464 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1466 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1468 int keycode
= event
.GetKeyCode();
1481 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1483 int keycode
= event
.GetKeyCode();
1487 CBox()->SetValue(!CBox()->GetValue());
1491 CBox()->SetValue(true);
1495 CBox()->SetValue(false);
1500 wxString
wxGridCellBoolEditor::GetValue() const
1502 return ms_stringValues
[CBox()->GetValue()];
1506 wxGridCellBoolEditor::UseStringValues(const wxString
& valueTrue
,
1507 const wxString
& valueFalse
)
1509 ms_stringValues
[false] = valueFalse
;
1510 ms_stringValues
[true] = valueTrue
;
1514 wxGridCellBoolEditor::IsTrueValue(const wxString
& value
)
1516 return value
== ms_stringValues
[true];
1519 #endif // wxUSE_CHECKBOX
1523 // ----------------------------------------------------------------------------
1524 // wxGridCellChoiceEditor
1525 // ----------------------------------------------------------------------------
1527 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1529 : m_choices(choices
),
1530 m_allowOthers(allowOthers
) { }
1532 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1533 const wxString choices
[],
1535 : m_allowOthers(allowOthers
)
1539 m_choices
.Alloc(count
);
1540 for ( size_t n
= 0; n
< count
; n
++ )
1542 m_choices
.Add(choices
[n
]);
1547 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1549 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1550 editor
->m_allowOthers
= m_allowOthers
;
1551 editor
->m_choices
= m_choices
;
1556 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1558 wxEvtHandler
* evtHandler
)
1560 int style
= wxTE_PROCESS_ENTER
|
1564 if ( !m_allowOthers
)
1565 style
|= wxCB_READONLY
;
1566 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1567 wxDefaultPosition
, wxDefaultSize
,
1571 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1574 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1575 wxGridCellAttr
* attr
)
1577 // as we fill the entire client area, don't do anything here to minimize
1580 // TODO: It doesn't actually fill the client area since the height of a
1581 // combo always defaults to the standard. Until someone has time to
1582 // figure out the right rectangle to paint, just do it the normal way.
1583 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1586 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1588 wxASSERT_MSG(m_control
,
1589 wxT("The wxGridCellEditor must be created first!"));
1591 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1593 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1595 // Don't immediately end if we get a kill focus event within BeginEdit
1597 evtHandler
->SetInSetFocus(true);
1599 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1603 Combo()->SetValue(m_startValue
);
1604 Combo()->SetInsertionPointEnd();
1606 else // the combobox is read-only
1608 // find the right position, or default to the first if not found
1609 int pos
= Combo()->FindString(m_startValue
);
1610 if (pos
== wxNOT_FOUND
)
1612 Combo()->SetSelection(pos
);
1615 Combo()->SetFocus();
1619 // When dropping down the menu, a kill focus event
1620 // happens after this point, so we can't reset the flag yet.
1621 #if !defined(__WXGTK20__)
1622 evtHandler
->SetInSetFocus(false);
1627 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1630 wxString value
= Combo()->GetValue();
1631 if ( value
== m_startValue
)
1634 grid
->GetTable()->SetValue(row
, col
, value
);
1639 void wxGridCellChoiceEditor::Reset()
1641 Combo()->SetValue(m_startValue
);
1642 Combo()->SetInsertionPointEnd();
1645 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1655 wxStringTokenizer
tk(params
, _T(','));
1656 while ( tk
.HasMoreTokens() )
1658 m_choices
.Add(tk
.GetNextToken());
1662 // return the value in the text control
1663 wxString
wxGridCellChoiceEditor::GetValue() const
1665 return Combo()->GetValue();
1668 #endif // wxUSE_COMBOBOX
1670 // ----------------------------------------------------------------------------
1671 // wxGridCellEditorEvtHandler
1672 // ----------------------------------------------------------------------------
1674 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
1676 // Don't disable the cell if we're just starting to edit it
1681 m_grid
->DisableCellEditControl();
1686 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1688 switch ( event
.GetKeyCode() )
1692 m_grid
->DisableCellEditControl();
1696 m_grid
->GetEventHandler()->ProcessEvent( event
);
1700 case WXK_NUMPAD_ENTER
:
1701 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1702 m_editor
->HandleReturn(event
);
1711 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1713 int row
= m_grid
->GetGridCursorRow();
1714 int col
= m_grid
->GetGridCursorCol();
1715 wxRect rect
= m_grid
->CellToRect( row
, col
);
1717 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
1719 // if cell width is smaller than grid client area, cell is wholly visible
1720 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
1722 switch ( event
.GetKeyCode() )
1727 case WXK_NUMPAD_ENTER
:
1732 if ( wholeCellVisible
)
1734 // no special processing needed...
1739 // do special processing for partly visible cell...
1741 // get the widths of all cells previous to this one
1743 for ( int i
= 0; i
< col
; i
++ )
1745 colXPos
+= m_grid
->GetColSize(i
);
1748 int xUnit
= 1, yUnit
= 1;
1749 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1752 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1756 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
1764 if ( wholeCellVisible
)
1766 // no special processing needed...
1771 // do special processing for partly visible cell...
1774 wxString value
= m_grid
->GetCellValue(row
, col
);
1775 if ( wxEmptyString
!= value
)
1777 // get width of cell CONTENTS (text)
1779 wxFont font
= m_grid
->GetCellFont(row
, col
);
1780 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
1782 // try to RIGHT align the text by scrolling
1783 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
1785 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
1786 // otherwise the last part of the cell content might be hidden below the scroll bar
1787 // FIXME: maybe there is a more suitable correction?
1788 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
1789 if ( textWidth
< 0 )
1795 // get the widths of all cells previous to this one
1797 for ( int i
= 0; i
< col
; i
++ )
1799 colXPos
+= m_grid
->GetColSize(i
);
1802 // and add the (modified) text width of the cell contents
1803 // as we'd like to see the last part of the cell contents
1804 colXPos
+= textWidth
;
1806 int xUnit
= 1, yUnit
= 1;
1807 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1808 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1819 // ----------------------------------------------------------------------------
1820 // wxGridCellWorker is an (almost) empty common base class for
1821 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1822 // ----------------------------------------------------------------------------
1824 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1829 wxGridCellWorker::~wxGridCellWorker()
1833 // ============================================================================
1835 // ============================================================================
1837 // ----------------------------------------------------------------------------
1838 // wxGridCellRenderer
1839 // ----------------------------------------------------------------------------
1841 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1842 wxGridCellAttr
& attr
,
1845 int WXUNUSED(row
), int WXUNUSED(col
),
1848 dc
.SetBackgroundMode( wxBRUSHSTYLE_SOLID
);
1850 // grey out fields if the grid is disabled
1851 if ( grid
.IsEnabled() )
1856 if ( grid
.HasFocus() )
1857 clr
= grid
.GetSelectionBackground();
1859 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
1860 dc
.SetBrush( wxBrush(clr
, wxBRUSHSTYLE_SOLID
) );
1864 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxBRUSHSTYLE_SOLID
) );
1869 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
), wxBRUSHSTYLE_SOLID
));
1872 dc
.SetPen( *wxTRANSPARENT_PEN
);
1873 dc
.DrawRectangle(rect
);
1876 // ----------------------------------------------------------------------------
1877 // wxGridCellStringRenderer
1878 // ----------------------------------------------------------------------------
1880 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
1881 const wxGridCellAttr
& attr
,
1885 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
1887 // TODO some special colours for attr.IsReadOnly() case?
1889 // different coloured text when the grid is disabled
1890 if ( grid
.IsEnabled() )
1895 if ( grid
.HasFocus() )
1896 clr
= grid
.GetSelectionBackground();
1898 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
1899 dc
.SetTextBackground( clr
);
1900 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1904 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1905 dc
.SetTextForeground( attr
.GetTextColour() );
1910 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1911 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
1914 dc
.SetFont( attr
.GetFont() );
1917 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
1919 const wxString
& text
)
1921 wxCoord x
= 0, y
= 0, max_x
= 0;
1922 dc
.SetFont(attr
.GetFont());
1923 wxStringTokenizer
tk(text
, _T('\n'));
1924 while ( tk
.HasMoreTokens() )
1926 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1927 max_x
= wxMax(max_x
, x
);
1930 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1932 return wxSize(max_x
, y
);
1935 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1936 wxGridCellAttr
& attr
,
1940 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1943 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1944 wxGridCellAttr
& attr
,
1946 const wxRect
& rectCell
,
1950 wxRect rect
= rectCell
;
1953 // erase only this cells background, overflow cells should have been erased
1954 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1957 attr
.GetAlignment(&hAlign
, &vAlign
);
1959 int overflowCols
= 0;
1961 if (attr
.GetOverflow())
1963 int cols
= grid
.GetNumberCols();
1964 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1965 int cell_rows
, cell_cols
;
1966 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <= 0
1967 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1969 int i
, c_cols
, c_rows
;
1970 for (i
= col
+cell_cols
; i
< cols
; i
++)
1972 bool is_empty
= true;
1973 for (int j
=row
; j
< row
+ cell_rows
; j
++)
1975 // check w/ anchor cell for multicell block
1976 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1979 if (!grid
.GetTable()->IsEmptyCell(j
+ c_rows
, i
))
1988 rect
.width
+= grid
.GetColSize(i
);
1996 if (rect
.width
>= best_width
)
2000 overflowCols
= i
- col
- cell_cols
+ 1;
2001 if (overflowCols
>= cols
)
2002 overflowCols
= cols
- 1;
2005 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
2007 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
2009 clip
.x
+= rectCell
.width
;
2010 // draw each overflow cell individually
2011 int col_end
= col
+ cell_cols
+ overflowCols
;
2012 if (col_end
>= grid
.GetNumberCols())
2013 col_end
= grid
.GetNumberCols() - 1;
2014 for (int i
= col
+ cell_cols
; i
<= col_end
; i
++)
2016 clip
.width
= grid
.GetColSize(i
) - 1;
2017 dc
.DestroyClippingRegion();
2018 dc
.SetClippingRegion(clip
);
2020 SetTextColoursAndFont(grid
, attr
, dc
,
2021 grid
.IsInSelection(row
,i
));
2023 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
2024 rect
, hAlign
, vAlign
);
2025 clip
.x
+= grid
.GetColSize(i
) - 1;
2031 dc
.DestroyClippingRegion();
2035 // now we only have to draw the text
2036 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2038 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
2039 rect
, hAlign
, vAlign
);
2042 // ----------------------------------------------------------------------------
2043 // wxGridCellNumberRenderer
2044 // ----------------------------------------------------------------------------
2046 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2048 wxGridTableBase
*table
= grid
.GetTable();
2050 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
2052 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
2056 text
= table
->GetValue(row
, col
);
2062 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
2063 wxGridCellAttr
& attr
,
2065 const wxRect
& rectCell
,
2069 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2071 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2073 // draw the text right aligned by default
2075 attr
.GetAlignment(&hAlign
, &vAlign
);
2076 hAlign
= wxALIGN_RIGHT
;
2078 wxRect rect
= rectCell
;
2081 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2084 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
2085 wxGridCellAttr
& attr
,
2089 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2092 // ----------------------------------------------------------------------------
2093 // wxGridCellFloatRenderer
2094 // ----------------------------------------------------------------------------
2096 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
2099 SetPrecision(precision
);
2102 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
2104 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
2105 renderer
->m_width
= m_width
;
2106 renderer
->m_precision
= m_precision
;
2107 renderer
->m_format
= m_format
;
2112 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2114 wxGridTableBase
*table
= grid
.GetTable();
2119 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
2121 val
= table
->GetValueAsDouble(row
, col
);
2126 text
= table
->GetValue(row
, col
);
2127 hasDouble
= text
.ToDouble(&val
);
2134 if ( m_width
== -1 )
2136 if ( m_precision
== -1 )
2138 // default width/precision
2139 m_format
= _T("%f");
2143 m_format
.Printf(_T("%%.%df"), m_precision
);
2146 else if ( m_precision
== -1 )
2148 // default precision
2149 m_format
.Printf(_T("%%%d.f"), m_width
);
2153 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
2157 text
.Printf(m_format
, val
);
2160 //else: text already contains the string
2165 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
2166 wxGridCellAttr
& attr
,
2168 const wxRect
& rectCell
,
2172 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2174 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2176 // draw the text right aligned by default
2178 attr
.GetAlignment(&hAlign
, &vAlign
);
2179 hAlign
= wxALIGN_RIGHT
;
2181 wxRect rect
= rectCell
;
2184 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2187 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
2188 wxGridCellAttr
& attr
,
2192 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2195 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
2199 // reset to defaults
2205 wxString tmp
= params
.BeforeFirst(_T(','));
2209 if ( tmp
.ToLong(&width
) )
2211 SetWidth((int)width
);
2215 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
2219 tmp
= params
.AfterFirst(_T(','));
2223 if ( tmp
.ToLong(&precision
) )
2225 SetPrecision((int)precision
);
2229 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
2235 // ----------------------------------------------------------------------------
2236 // wxGridCellBoolRenderer
2237 // ----------------------------------------------------------------------------
2239 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
2241 // FIXME these checkbox size calculations are really ugly...
2243 // between checkmark and box
2244 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2246 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2247 wxGridCellAttr
& WXUNUSED(attr
),
2252 // compute it only once (no locks for MT safeness in GUI thread...)
2253 if ( !ms_sizeCheckMark
.x
)
2255 // get checkbox size
2256 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2257 wxSize size
= checkbox
->GetBestSize();
2258 wxCoord checkSize
= size
.y
+ 2 * wxGRID_CHECKMARK_MARGIN
;
2260 #if defined(__WXMOTIF__)
2261 checkSize
-= size
.y
/ 2;
2266 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2269 return ms_sizeCheckMark
;
2272 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2273 wxGridCellAttr
& attr
,
2279 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2281 // draw a check mark in the centre (ignoring alignment - TODO)
2282 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2284 // don't draw outside the cell
2285 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2286 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2288 // and even leave (at least) 1 pixel margin
2289 size
.x
= size
.y
= minSize
;
2292 // draw a border around checkmark
2294 attr
.GetAlignment(&hAlign
, &vAlign
);
2297 if (hAlign
== wxALIGN_CENTRE
)
2299 rectBorder
.x
= rect
.x
+ rect
.width
/ 2 - size
.x
/ 2;
2300 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2301 rectBorder
.width
= size
.x
;
2302 rectBorder
.height
= size
.y
;
2304 else if (hAlign
== wxALIGN_LEFT
)
2306 rectBorder
.x
= rect
.x
+ 2;
2307 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2308 rectBorder
.width
= size
.x
;
2309 rectBorder
.height
= size
.y
;
2311 else if (hAlign
== wxALIGN_RIGHT
)
2313 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2314 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2315 rectBorder
.width
= size
.x
;
2316 rectBorder
.height
= size
.y
;
2320 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2322 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2326 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2327 value
= wxGridCellBoolEditor::IsTrueValue(cellval
);
2332 flags
|= wxCONTROL_CHECKED
;
2334 wxRendererNative::Get().DrawCheckBox( &grid
, dc
, rectBorder
, flags
);
2337 // ----------------------------------------------------------------------------
2339 // ----------------------------------------------------------------------------
2341 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2345 m_isReadOnly
= Unset
;
2350 m_attrkind
= wxGridCellAttr::Cell
;
2352 m_sizeRows
= m_sizeCols
= 1;
2353 m_overflow
= UnsetOverflow
;
2355 SetDefAttr(attrDefault
);
2358 wxGridCellAttr
*wxGridCellAttr::Clone() const
2360 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2362 if ( HasTextColour() )
2363 attr
->SetTextColour(GetTextColour());
2364 if ( HasBackgroundColour() )
2365 attr
->SetBackgroundColour(GetBackgroundColour());
2367 attr
->SetFont(GetFont());
2368 if ( HasAlignment() )
2369 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2371 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2375 attr
->SetRenderer(m_renderer
);
2376 m_renderer
->IncRef();
2380 attr
->SetEditor(m_editor
);
2385 attr
->SetReadOnly();
2387 attr
->SetOverflow( m_overflow
== Overflow
);
2388 attr
->SetKind( m_attrkind
);
2393 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2395 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2396 SetTextColour(mergefrom
->GetTextColour());
2397 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2398 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2399 if ( !HasFont() && mergefrom
->HasFont() )
2400 SetFont(mergefrom
->GetFont());
2401 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2404 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2405 SetAlignment(hAlign
, vAlign
);
2407 if ( !HasSize() && mergefrom
->HasSize() )
2408 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2410 // Directly access member functions as GetRender/Editor don't just return
2411 // m_renderer/m_editor
2413 // Maybe add support for merge of Render and Editor?
2414 if (!HasRenderer() && mergefrom
->HasRenderer() )
2416 m_renderer
= mergefrom
->m_renderer
;
2417 m_renderer
->IncRef();
2419 if ( !HasEditor() && mergefrom
->HasEditor() )
2421 m_editor
= mergefrom
->m_editor
;
2424 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2425 SetReadOnly(mergefrom
->IsReadOnly());
2427 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2428 SetOverflow(mergefrom
->GetOverflow());
2430 SetDefAttr(mergefrom
->m_defGridAttr
);
2433 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2435 // The size of a cell is normally 1,1
2437 // If this cell is larger (2,2) then this is the top left cell
2438 // the other cells that will be covered (lower right cells) must be
2439 // set to negative or zero values such that
2440 // row + num_rows of the covered cell points to the larger cell (this cell)
2441 // same goes for the col + num_cols.
2443 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2445 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
2446 !((num_rows
<= 0) && (num_cols
> 0)) ||
2447 !((num_rows
== 0) && (num_cols
== 0))),
2448 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2450 m_sizeRows
= num_rows
;
2451 m_sizeCols
= num_cols
;
2454 const wxColour
& wxGridCellAttr::GetTextColour() const
2456 if (HasTextColour())
2460 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2462 return m_defGridAttr
->GetTextColour();
2466 wxFAIL_MSG(wxT("Missing default cell attribute"));
2467 return wxNullColour
;
2471 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2473 if (HasBackgroundColour())
2477 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2479 return m_defGridAttr
->GetBackgroundColour();
2483 wxFAIL_MSG(wxT("Missing default cell attribute"));
2484 return wxNullColour
;
2488 const wxFont
& wxGridCellAttr::GetFont() const
2494 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2496 return m_defGridAttr
->GetFont();
2500 wxFAIL_MSG(wxT("Missing default cell attribute"));
2505 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2514 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2516 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2520 wxFAIL_MSG(wxT("Missing default cell attribute"));
2524 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2527 *num_rows
= m_sizeRows
;
2529 *num_cols
= m_sizeCols
;
2532 // GetRenderer and GetEditor use a slightly different decision path about
2533 // which attribute to use. If a non-default attr object has one then it is
2534 // used, otherwise the default editor or renderer is fetched from the grid and
2535 // used. It should be the default for the data type of the cell. If it is
2536 // NULL (because the table has a type that the grid does not have in its
2537 // registry), then the grid's default editor or renderer is used.
2539 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(const wxGrid
* grid
, int row
, int col
) const
2541 wxGridCellRenderer
*renderer
= NULL
;
2543 if ( m_renderer
&& this != m_defGridAttr
)
2545 // use the cells renderer if it has one
2546 renderer
= m_renderer
;
2549 else // no non-default cell renderer
2551 // get default renderer for the data type
2554 // GetDefaultRendererForCell() will do IncRef() for us
2555 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2558 if ( renderer
== NULL
)
2560 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2562 // if we still don't have one then use the grid default
2563 // (no need for IncRef() here neither)
2564 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2566 else // default grid attr
2568 // use m_renderer which we had decided not to use initially
2569 renderer
= m_renderer
;
2576 // we're supposed to always find something
2577 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2582 // same as above, except for s/renderer/editor/g
2583 wxGridCellEditor
* wxGridCellAttr::GetEditor(const wxGrid
* grid
, int row
, int col
) const
2585 wxGridCellEditor
*editor
= NULL
;
2587 if ( m_editor
&& this != m_defGridAttr
)
2589 // use the cells editor if it has one
2593 else // no non default cell editor
2595 // get default editor for the data type
2598 // GetDefaultEditorForCell() will do IncRef() for us
2599 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2602 if ( editor
== NULL
)
2604 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2606 // if we still don't have one then use the grid default
2607 // (no need for IncRef() here neither)
2608 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2610 else // default grid attr
2612 // use m_editor which we had decided not to use initially
2620 // we're supposed to always find something
2621 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2626 // ----------------------------------------------------------------------------
2627 // wxGridCellAttrData
2628 // ----------------------------------------------------------------------------
2630 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2632 // Note: contrary to wxGridRowOrColAttrData::SetAttr, we must not
2633 // touch attribute's reference counting explicitly, since this
2634 // is managed by class wxGridCellWithAttr
2635 int n
= FindIndex(row
, col
);
2636 if ( n
== wxNOT_FOUND
)
2640 // add the attribute
2641 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2643 //else: nothing to do
2645 else // we already have an attribute for this cell
2649 // change the attribute
2650 m_attrs
[(size_t)n
].ChangeAttr(attr
);
2654 // remove this attribute
2655 m_attrs
.RemoveAt((size_t)n
);
2660 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2662 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2664 int n
= FindIndex(row
, col
);
2665 if ( n
!= wxNOT_FOUND
)
2667 attr
= m_attrs
[(size_t)n
].attr
;
2674 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2676 size_t count
= m_attrs
.GetCount();
2677 for ( size_t n
= 0; n
< count
; n
++ )
2679 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2680 wxCoord row
= coords
.GetRow();
2681 if ((size_t)row
>= pos
)
2685 // If rows inserted, include row counter where necessary
2686 coords
.SetRow(row
+ numRows
);
2688 else if (numRows
< 0)
2690 // If rows deleted ...
2691 if ((size_t)row
>= pos
- numRows
)
2693 // ...either decrement row counter (if row still exists)...
2694 coords
.SetRow(row
+ numRows
);
2698 // ...or remove the attribute
2699 m_attrs
.RemoveAt(n
);
2708 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2710 size_t count
= m_attrs
.GetCount();
2711 for ( size_t n
= 0; n
< count
; n
++ )
2713 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2714 wxCoord col
= coords
.GetCol();
2715 if ( (size_t)col
>= pos
)
2719 // If rows inserted, include row counter where necessary
2720 coords
.SetCol(col
+ numCols
);
2722 else if (numCols
< 0)
2724 // If rows deleted ...
2725 if ((size_t)col
>= pos
- numCols
)
2727 // ...either decrement row counter (if row still exists)...
2728 coords
.SetCol(col
+ numCols
);
2732 // ...or remove the attribute
2733 m_attrs
.RemoveAt(n
);
2742 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2744 size_t count
= m_attrs
.GetCount();
2745 for ( size_t n
= 0; n
< count
; n
++ )
2747 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2748 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2757 // ----------------------------------------------------------------------------
2758 // wxGridRowOrColAttrData
2759 // ----------------------------------------------------------------------------
2761 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2763 size_t count
= m_attrs
.GetCount();
2764 for ( size_t n
= 0; n
< count
; n
++ )
2766 m_attrs
[n
]->DecRef();
2770 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2772 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2774 int n
= m_rowsOrCols
.Index(rowOrCol
);
2775 if ( n
!= wxNOT_FOUND
)
2777 attr
= m_attrs
[(size_t)n
];
2784 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2786 int i
= m_rowsOrCols
.Index(rowOrCol
);
2787 if ( i
== wxNOT_FOUND
)
2791 // add the attribute - no need to do anything to reference count
2792 // since we take ownership of the attribute.
2793 m_rowsOrCols
.Add(rowOrCol
);
2796 // nothing to remove
2800 size_t n
= (size_t)i
;
2801 if ( m_attrs
[n
] == attr
)
2806 // change the attribute, handling reference count manually,
2807 // taking ownership of the new attribute.
2808 m_attrs
[n
]->DecRef();
2813 // remove this attribute, handling reference count manually
2814 m_attrs
[n
]->DecRef();
2815 m_rowsOrCols
.RemoveAt(n
);
2816 m_attrs
.RemoveAt(n
);
2821 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2823 size_t count
= m_attrs
.GetCount();
2824 for ( size_t n
= 0; n
< count
; n
++ )
2826 int & rowOrCol
= m_rowsOrCols
[n
];
2827 if ( (size_t)rowOrCol
>= pos
)
2829 if ( numRowsOrCols
> 0 )
2831 // If rows inserted, include row counter where necessary
2832 rowOrCol
+= numRowsOrCols
;
2834 else if ( numRowsOrCols
< 0)
2836 // If rows deleted, either decrement row counter (if row still exists)
2837 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2838 rowOrCol
+= numRowsOrCols
;
2841 m_rowsOrCols
.RemoveAt(n
);
2842 m_attrs
[n
]->DecRef();
2843 m_attrs
.RemoveAt(n
);
2852 // ----------------------------------------------------------------------------
2853 // wxGridCellAttrProvider
2854 // ----------------------------------------------------------------------------
2856 wxGridCellAttrProvider::wxGridCellAttrProvider()
2858 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2861 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2866 void wxGridCellAttrProvider::InitData()
2868 m_data
= new wxGridCellAttrProviderData
;
2871 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2872 wxGridCellAttr::wxAttrKind kind
) const
2874 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2879 case (wxGridCellAttr::Any
):
2880 // Get cached merge attributes.
2881 // Currently not used as no cache implemented as not mutable
2882 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2885 // Basically implement old version.
2886 // Also check merge cache, so we don't have to re-merge every time..
2887 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2888 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2889 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2891 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
2893 // Two or more are non NULL
2894 attr
= new wxGridCellAttr
;
2895 attr
->SetKind(wxGridCellAttr::Merged
);
2897 // Order is important..
2900 attr
->MergeWith(attrcell
);
2905 attr
->MergeWith(attrcol
);
2910 attr
->MergeWith(attrrow
);
2914 // store merge attr if cache implemented
2916 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2920 // one or none is non null return it or null.
2939 case (wxGridCellAttr::Cell
):
2940 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2943 case (wxGridCellAttr::Col
):
2944 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2947 case (wxGridCellAttr::Row
):
2948 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2953 // (wxGridCellAttr::Default):
2954 // (wxGridCellAttr::Merged):
2962 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2968 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2971 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2976 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2979 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2984 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2987 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2991 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2993 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2997 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
3001 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
3003 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
3007 // ----------------------------------------------------------------------------
3008 // wxGridTypeRegistry
3009 // ----------------------------------------------------------------------------
3011 wxGridTypeRegistry::~wxGridTypeRegistry()
3013 size_t count
= m_typeinfo
.GetCount();
3014 for ( size_t i
= 0; i
< count
; i
++ )
3015 delete m_typeinfo
[i
];
3018 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
3019 wxGridCellRenderer
* renderer
,
3020 wxGridCellEditor
* editor
)
3022 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
3024 // is it already registered?
3025 int loc
= FindRegisteredDataType(typeName
);
3026 if ( loc
!= wxNOT_FOUND
)
3028 delete m_typeinfo
[loc
];
3029 m_typeinfo
[loc
] = info
;
3033 m_typeinfo
.Add(info
);
3037 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
3039 size_t count
= m_typeinfo
.GetCount();
3040 for ( size_t i
= 0; i
< count
; i
++ )
3042 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
3051 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
3053 int index
= FindRegisteredDataType(typeName
);
3054 if ( index
== wxNOT_FOUND
)
3056 // check whether this is one of the standard ones, in which case
3057 // register it "on the fly"
3059 if ( typeName
== wxGRID_VALUE_STRING
)
3061 RegisterDataType(wxGRID_VALUE_STRING
,
3062 new wxGridCellStringRenderer
,
3063 new wxGridCellTextEditor
);
3066 #endif // wxUSE_TEXTCTRL
3068 if ( typeName
== wxGRID_VALUE_BOOL
)
3070 RegisterDataType(wxGRID_VALUE_BOOL
,
3071 new wxGridCellBoolRenderer
,
3072 new wxGridCellBoolEditor
);
3075 #endif // wxUSE_CHECKBOX
3077 if ( typeName
== wxGRID_VALUE_NUMBER
)
3079 RegisterDataType(wxGRID_VALUE_NUMBER
,
3080 new wxGridCellNumberRenderer
,
3081 new wxGridCellNumberEditor
);
3083 else if ( typeName
== wxGRID_VALUE_FLOAT
)
3085 RegisterDataType(wxGRID_VALUE_FLOAT
,
3086 new wxGridCellFloatRenderer
,
3087 new wxGridCellFloatEditor
);
3090 #endif // wxUSE_TEXTCTRL
3092 if ( typeName
== wxGRID_VALUE_CHOICE
)
3094 RegisterDataType(wxGRID_VALUE_CHOICE
,
3095 new wxGridCellStringRenderer
,
3096 new wxGridCellChoiceEditor
);
3099 #endif // wxUSE_COMBOBOX
3104 // we get here only if just added the entry for this type, so return
3106 index
= m_typeinfo
.GetCount() - 1;
3112 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
3114 int index
= FindDataType(typeName
);
3115 if ( index
== wxNOT_FOUND
)
3117 // the first part of the typename is the "real" type, anything after ':'
3118 // are the parameters for the renderer
3119 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
3120 if ( index
== wxNOT_FOUND
)
3125 wxGridCellRenderer
*renderer
= GetRenderer(index
);
3126 wxGridCellRenderer
*rendererOld
= renderer
;
3127 renderer
= renderer
->Clone();
3128 rendererOld
->DecRef();
3130 wxGridCellEditor
*editor
= GetEditor(index
);
3131 wxGridCellEditor
*editorOld
= editor
;
3132 editor
= editor
->Clone();
3133 editorOld
->DecRef();
3135 // do it even if there are no parameters to reset them to defaults
3136 wxString params
= typeName
.AfterFirst(_T(':'));
3137 renderer
->SetParameters(params
);
3138 editor
->SetParameters(params
);
3140 // register the new typename
3141 RegisterDataType(typeName
, renderer
, editor
);
3143 // we just registered it, it's the last one
3144 index
= m_typeinfo
.GetCount() - 1;
3150 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
3152 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
3159 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
3161 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
3168 // ----------------------------------------------------------------------------
3170 // ----------------------------------------------------------------------------
3172 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
3174 wxGridTableBase::wxGridTableBase()
3176 m_view
= (wxGrid
*) NULL
;
3177 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
3180 wxGridTableBase::~wxGridTableBase()
3182 delete m_attrProvider
;
3185 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
3187 delete m_attrProvider
;
3188 m_attrProvider
= attrProvider
;
3191 bool wxGridTableBase::CanHaveAttributes()
3193 if ( ! GetAttrProvider() )
3195 // use the default attr provider by default
3196 SetAttrProvider(new wxGridCellAttrProvider
);
3202 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
3204 if ( m_attrProvider
)
3205 return m_attrProvider
->GetAttr(row
, col
, kind
);
3207 return (wxGridCellAttr
*)NULL
;
3210 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
3212 if ( m_attrProvider
)
3215 attr
->SetKind(wxGridCellAttr::Cell
);
3216 m_attrProvider
->SetAttr(attr
, row
, col
);
3220 // as we take ownership of the pointer and don't store it, we must
3226 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3228 if ( m_attrProvider
)
3230 attr
->SetKind(wxGridCellAttr::Row
);
3231 m_attrProvider
->SetRowAttr(attr
, row
);
3235 // as we take ownership of the pointer and don't store it, we must
3241 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3243 if ( m_attrProvider
)
3245 attr
->SetKind(wxGridCellAttr::Col
);
3246 m_attrProvider
->SetColAttr(attr
, col
);
3250 // as we take ownership of the pointer and don't store it, we must
3256 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3257 size_t WXUNUSED(numRows
) )
3259 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3264 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3266 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3271 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3272 size_t WXUNUSED(numRows
) )
3274 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3279 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3280 size_t WXUNUSED(numCols
) )
3282 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3287 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3289 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3294 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3295 size_t WXUNUSED(numCols
) )
3297 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3302 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3306 // RD: Starting the rows at zero confuses users,
3307 // no matter how much it makes sense to us geeks.
3313 wxString
wxGridTableBase::GetColLabelValue( int col
)
3315 // default col labels are:
3316 // cols 0 to 25 : A-Z
3317 // cols 26 to 675 : AA-ZZ
3322 for ( n
= 1; ; n
++ )
3324 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
3330 // reverse the string...
3332 for ( i
= 0; i
< n
; i
++ )
3340 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3342 return wxGRID_VALUE_STRING
;
3345 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3346 const wxString
& typeName
)
3348 return typeName
== wxGRID_VALUE_STRING
;
3351 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3353 return CanGetValueAs(row
, col
, typeName
);
3356 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3361 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3366 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3371 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3372 long WXUNUSED(value
) )
3376 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3377 double WXUNUSED(value
) )
3381 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3382 bool WXUNUSED(value
) )
3386 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3387 const wxString
& WXUNUSED(typeName
) )
3392 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3393 const wxString
& WXUNUSED(typeName
),
3394 void* WXUNUSED(value
) )
3398 //////////////////////////////////////////////////////////////////////
3400 // Message class for the grid table to send requests and notifications
3404 wxGridTableMessage::wxGridTableMessage()
3406 m_table
= (wxGridTableBase
*) NULL
;
3412 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3413 int commandInt1
, int commandInt2
)
3417 m_comInt1
= commandInt1
;
3418 m_comInt2
= commandInt2
;
3421 //////////////////////////////////////////////////////////////////////
3423 // A basic grid table for string data. An object of this class will
3424 // created by wxGrid if you don't specify an alternative table class.
3427 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3429 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3431 wxGridStringTable::wxGridStringTable()
3436 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3439 m_data
.Alloc( numRows
);
3442 sa
.Alloc( numCols
);
3443 sa
.Add( wxEmptyString
, numCols
);
3445 m_data
.Add( sa
, numRows
);
3448 wxGridStringTable::~wxGridStringTable()
3452 int wxGridStringTable::GetNumberRows()
3454 return m_data
.GetCount();
3457 int wxGridStringTable::GetNumberCols()
3459 if ( m_data
.GetCount() > 0 )
3460 return m_data
[0].GetCount();
3465 wxString
wxGridStringTable::GetValue( int row
, int col
)
3467 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3469 _T("invalid row or column index in wxGridStringTable") );
3471 return m_data
[row
][col
];
3474 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3476 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3477 _T("invalid row or column index in wxGridStringTable") );
3479 m_data
[row
][col
] = value
;
3482 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3484 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3486 _T("invalid row or column index in wxGridStringTable") );
3488 return (m_data
[row
][col
] == wxEmptyString
);
3491 void wxGridStringTable::Clear()
3494 int numRows
, numCols
;
3496 numRows
= m_data
.GetCount();
3499 numCols
= m_data
[0].GetCount();
3501 for ( row
= 0; row
< numRows
; row
++ )
3503 for ( col
= 0; col
< numCols
; col
++ )
3505 m_data
[row
][col
] = wxEmptyString
;
3511 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3513 size_t curNumRows
= m_data
.GetCount();
3514 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3515 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3517 if ( pos
>= curNumRows
)
3519 return AppendRows( numRows
);
3523 sa
.Alloc( curNumCols
);
3524 sa
.Add( wxEmptyString
, curNumCols
);
3525 m_data
.Insert( sa
, pos
, numRows
);
3529 wxGridTableMessage
msg( this,
3530 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3534 GetView()->ProcessTableMessage( msg
);
3540 bool wxGridStringTable::AppendRows( size_t numRows
)
3542 size_t curNumRows
= m_data
.GetCount();
3543 size_t curNumCols
= ( curNumRows
> 0
3544 ? m_data
[0].GetCount()
3545 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3548 if ( curNumCols
> 0 )
3550 sa
.Alloc( curNumCols
);
3551 sa
.Add( wxEmptyString
, curNumCols
);
3554 m_data
.Add( sa
, numRows
);
3558 wxGridTableMessage
msg( this,
3559 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3562 GetView()->ProcessTableMessage( msg
);
3568 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3570 size_t curNumRows
= m_data
.GetCount();
3572 if ( pos
>= curNumRows
)
3574 wxFAIL_MSG( wxString::Format
3576 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3578 (unsigned long)numRows
,
3579 (unsigned long)curNumRows
3585 if ( numRows
> curNumRows
- pos
)
3587 numRows
= curNumRows
- pos
;
3590 if ( numRows
>= curNumRows
)
3596 m_data
.RemoveAt( pos
, numRows
);
3601 wxGridTableMessage
msg( this,
3602 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3606 GetView()->ProcessTableMessage( msg
);
3612 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3616 size_t curNumRows
= m_data
.GetCount();
3617 size_t curNumCols
= ( curNumRows
> 0
3618 ? m_data
[0].GetCount()
3619 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3621 if ( pos
>= curNumCols
)
3623 return AppendCols( numCols
);
3626 if ( !m_colLabels
.IsEmpty() )
3628 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
3631 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
3632 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
3635 for ( row
= 0; row
< curNumRows
; row
++ )
3637 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3639 m_data
[row
].Insert( wxEmptyString
, col
);
3645 wxGridTableMessage
msg( this,
3646 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3650 GetView()->ProcessTableMessage( msg
);
3656 bool wxGridStringTable::AppendCols( size_t numCols
)
3660 size_t curNumRows
= m_data
.GetCount();
3665 // TODO: something better than this ?
3667 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3672 for ( row
= 0; row
< curNumRows
; row
++ )
3674 m_data
[row
].Add( wxEmptyString
, numCols
);
3679 wxGridTableMessage
msg( this,
3680 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3683 GetView()->ProcessTableMessage( msg
);
3689 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3693 size_t curNumRows
= m_data
.GetCount();
3694 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3695 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3697 if ( pos
>= curNumCols
)
3699 wxFAIL_MSG( wxString::Format
3701 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3703 (unsigned long)numCols
,
3704 (unsigned long)curNumCols
3711 colID
= GetView()->GetColAt( pos
);
3715 if ( numCols
> curNumCols
- colID
)
3717 numCols
= curNumCols
- colID
;
3720 if ( !m_colLabels
.IsEmpty() )
3722 // m_colLabels stores just as many elements as it needs, e.g. if only
3723 // the label of the first column had been set it would have only one
3724 // element and not numCols, so account for it
3725 int nToRm
= m_colLabels
.size() - colID
;
3727 m_colLabels
.RemoveAt( colID
, nToRm
);
3730 for ( row
= 0; row
< curNumRows
; row
++ )
3732 if ( numCols
>= curNumCols
)
3734 m_data
[row
].Clear();
3738 m_data
[row
].RemoveAt( colID
, numCols
);
3744 wxGridTableMessage
msg( this,
3745 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3749 GetView()->ProcessTableMessage( msg
);
3755 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3757 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3759 // using default label
3761 return wxGridTableBase::GetRowLabelValue( row
);
3765 return m_rowLabels
[row
];
3769 wxString
wxGridStringTable::GetColLabelValue( int col
)
3771 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3773 // using default label
3775 return wxGridTableBase::GetColLabelValue( col
);
3779 return m_colLabels
[col
];
3783 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3785 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3787 int n
= m_rowLabels
.GetCount();
3790 for ( i
= n
; i
<= row
; i
++ )
3792 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3796 m_rowLabels
[row
] = value
;
3799 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3801 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3803 int n
= m_colLabels
.GetCount();
3806 for ( i
= n
; i
<= col
; i
++ )
3808 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3812 m_colLabels
[col
] = value
;
3816 //////////////////////////////////////////////////////////////////////
3817 //////////////////////////////////////////////////////////////////////
3819 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
)
3820 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost
)
3823 void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
3825 m_owner
->CancelMouseCapture();
3828 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3830 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow
)
3831 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3832 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3833 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3836 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3838 const wxPoint
&pos
, const wxSize
&size
)
3839 : wxGridSubwindow(parent
, id
, pos
, size
)
3844 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3848 // NO - don't do this because it will set both the x and y origin
3849 // coords to match the parent scrolled window and we just want to
3850 // set the y coord - MB
3852 // m_owner->PrepareDC( dc );
3855 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3856 wxPoint pt
= dc
.GetDeviceOrigin();
3857 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
3859 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3860 m_owner
->DrawRowLabels( dc
, rows
);
3863 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3865 m_owner
->ProcessRowLabelMouseEvent( event
);
3868 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3870 m_owner
->GetEventHandler()->ProcessEvent( event
);
3873 //////////////////////////////////////////////////////////////////////
3875 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3877 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow
)
3878 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3879 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3880 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3883 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3885 const wxPoint
&pos
, const wxSize
&size
)
3886 : wxGridSubwindow(parent
, id
, pos
, size
)
3891 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3895 // NO - don't do this because it will set both the x and y origin
3896 // coords to match the parent scrolled window and we just want to
3897 // set the x coord - MB
3899 // m_owner->PrepareDC( dc );
3902 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3903 wxPoint pt
= dc
.GetDeviceOrigin();
3904 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3905 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
3907 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
3909 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3910 m_owner
->DrawColLabels( dc
, cols
);
3913 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3915 m_owner
->ProcessColLabelMouseEvent( event
);
3918 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3920 m_owner
->GetEventHandler()->ProcessEvent( event
);
3923 //////////////////////////////////////////////////////////////////////
3925 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3927 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow
)
3928 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3929 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3930 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3933 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3935 const wxPoint
&pos
, const wxSize
&size
)
3936 : wxGridSubwindow(parent
, id
, pos
, size
)
3941 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3945 int client_height
= 0;
3946 int client_width
= 0;
3947 GetClientSize( &client_width
, &client_height
);
3949 // VZ: any reason for this ifdef? (FIXME)
3955 rect
.SetWidth( client_width
- 2 );
3956 rect
.SetHeight( client_height
- 2 );
3958 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3960 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxPENSTYLE_SOLID
) );
3961 dc
.DrawLine( client_width
- 1, client_height
- 1, client_width
- 1, 0 );
3962 dc
.DrawLine( client_width
- 1, client_height
- 1, 0, client_height
- 1 );
3963 dc
.DrawLine( 0, 0, client_width
, 0 );
3964 dc
.DrawLine( 0, 0, 0, client_height
);
3966 dc
.SetPen( *wxWHITE_PEN
);
3967 dc
.DrawLine( 1, 1, client_width
- 1, 1 );
3968 dc
.DrawLine( 1, 1, 1, client_height
- 1 );
3972 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3974 m_owner
->ProcessCornerLabelMouseEvent( event
);
3977 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3979 m_owner
->GetEventHandler()->ProcessEvent(event
);
3982 //////////////////////////////////////////////////////////////////////
3984 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3986 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow
)
3987 EVT_PAINT( wxGridWindow::OnPaint
)
3988 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3989 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3990 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3991 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3992 EVT_CHAR( wxGridWindow::OnChar
)
3993 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3994 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3995 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3998 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3999 wxGridRowLabelWindow
*rowLblWin
,
4000 wxGridColLabelWindow
*colLblWin
,
4003 const wxSize
&size
)
4004 : wxGridSubwindow(parent
, id
, pos
, size
,
4005 wxWANTS_CHARS
| wxCLIP_CHILDREN
,
4006 wxT("grid window") )
4009 m_rowLabelWin
= rowLblWin
;
4010 m_colLabelWin
= colLblWin
;
4013 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
4015 wxPaintDC
dc( this );
4016 m_owner
->PrepareDC( dc
);
4017 wxRegion reg
= GetUpdateRegion();
4018 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
4019 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
4021 #if WXGRID_DRAW_LINES
4022 m_owner
->DrawAllGridLines( dc
, reg
);
4025 m_owner
->DrawGridSpace( dc
);
4026 m_owner
->DrawHighlight( dc
, dirtyCells
);
4029 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
4031 wxWindow::ScrollWindow( dx
, dy
, rect
);
4032 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
4033 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
4036 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
4038 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
4041 m_owner
->ProcessGridCellMouseEvent( event
);
4044 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
4046 m_owner
->GetEventHandler()->ProcessEvent( event
);
4049 // This seems to be required for wxMotif/wxGTK otherwise the mouse
4050 // cursor must be in the cell edit control to get key events
4052 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
4054 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4058 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
4060 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4064 void wxGridWindow::OnChar( wxKeyEvent
& event
)
4066 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4070 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
4074 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
4076 // and if we have any selection, it has to be repainted, because it
4077 // uses different colour when the grid is not focused:
4078 if ( m_owner
->IsSelection() )
4084 // NB: Note that this code is in "else" branch only because the other
4085 // branch refreshes everything and so there's no point in calling
4086 // Refresh() again, *not* because it should only be done if
4087 // !IsSelection(). If the above code is ever optimized to refresh
4088 // only selected area, this needs to be moved out of the "else"
4089 // branch so that it's always executed.
4091 // current cell cursor {dis,re}appears on focus change:
4092 const wxGridCellCoords
cursorCoords(m_owner
->GetGridCursorRow(),
4093 m_owner
->GetGridCursorCol());
4094 const wxRect cursor
=
4095 m_owner
->BlockToDeviceRect(cursorCoords
, cursorCoords
);
4096 Refresh(true, &cursor
);
4099 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4103 //////////////////////////////////////////////////////////////////////
4105 // Internal Helper function for computing row or column from some
4106 // (unscrolled) coordinate value, using either
4107 // m_defaultRowHeight/m_defaultColWidth or binary search on array
4108 // of m_rowBottoms/m_ColRights to speed up the search!
4110 // Internal helper macros for simpler use of that function
4112 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
4113 const wxArrayInt
& BorderArray
, int nMax
,
4116 #define internalXToCol(x) XToCol(x, true)
4117 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
4118 m_minAcceptableRowHeight, \
4119 m_rowBottoms, m_numRows, true)
4121 /////////////////////////////////////////////////////////////////////
4123 #if wxUSE_EXTENDED_RTTI
4124 WX_DEFINE_FLAGS( wxGridStyle
)
4126 wxBEGIN_FLAGS( wxGridStyle
)
4127 // new style border flags, we put them first to
4128 // use them for streaming out
4129 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4130 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4131 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4132 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4133 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4134 wxFLAGS_MEMBER(wxBORDER_NONE
)
4136 // old style border flags
4137 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4138 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4139 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4140 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4141 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4142 wxFLAGS_MEMBER(wxBORDER
)
4144 // standard window styles
4145 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4146 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4147 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4148 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4149 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4150 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4151 wxFLAGS_MEMBER(wxVSCROLL
)
4152 wxFLAGS_MEMBER(wxHSCROLL
)
4154 wxEND_FLAGS( wxGridStyle
)
4156 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4158 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4159 wxHIDE_PROPERTY( Children
)
4160 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4161 wxEND_PROPERTIES_TABLE()
4163 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4164 wxEND_HANDLERS_TABLE()
4166 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4169 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
4172 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4175 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4176 EVT_PAINT( wxGrid::OnPaint
)
4177 EVT_SIZE( wxGrid::OnSize
)
4178 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
4179 EVT_KEY_UP( wxGrid::OnKeyUp
)
4180 EVT_CHAR ( wxGrid::OnChar
)
4181 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
4189 wxGrid::wxGrid( wxWindow
*parent
,
4194 const wxString
& name
)
4197 Create(parent
, id
, pos
, size
, style
, name
);
4200 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
4201 const wxPoint
& pos
, const wxSize
& size
,
4202 long style
, const wxString
& name
)
4204 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
4205 style
| wxWANTS_CHARS
, name
))
4208 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4209 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4212 SetInitialSize(size
);
4220 // Must do this or ~wxScrollHelper will pop the wrong event handler
4221 SetTargetWindow(this);
4223 wxSafeDecRef(m_defaultCellAttr
);
4225 #ifdef DEBUG_ATTR_CACHE
4226 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4227 wxPrintf(_T("wxGrid attribute cache statistics: "
4228 "total: %u, hits: %u (%u%%)\n"),
4229 total
, gs_nAttrCacheHits
,
4230 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4233 // if we own the table, just delete it, otherwise at least don't leave it
4234 // with dangling view pointer
4237 else if ( m_table
&& m_table
->GetView() == this )
4238 m_table
->SetView(NULL
);
4240 delete m_typeRegistry
;
4245 // ----- internal init and update functions
4248 // NOTE: If using the default visual attributes works everywhere then this can
4249 // be removed as well as the #else cases below.
4250 #define _USE_VISATTR 0
4252 void wxGrid::Create()
4254 // create the type registry
4255 m_typeRegistry
= new wxGridTypeRegistry
;
4257 m_cellEditCtrlEnabled
= false;
4259 m_defaultCellAttr
= new wxGridCellAttr();
4261 // Set default cell attributes
4262 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4263 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4264 m_defaultCellAttr
->SetFont(GetFont());
4265 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4266 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4267 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4270 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4271 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4273 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4274 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4277 m_defaultCellAttr
->SetTextColour(
4278 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4279 m_defaultCellAttr
->SetBackgroundColour(
4280 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4285 m_currentCellCoords
= wxGridNoCellCoords
;
4287 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4288 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4290 // subwindow components that make up the wxGrid
4291 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4296 m_colLabelWin
= new wxGridColLabelWindow( this,
4301 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4306 m_gridWin
= new wxGridWindow( this,
4313 SetTargetWindow( m_gridWin
);
4316 wxColour gfg
= gva
.colFg
;
4317 wxColour gbg
= gva
.colBg
;
4318 wxColour lfg
= lva
.colFg
;
4319 wxColour lbg
= lva
.colBg
;
4321 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4322 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4323 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4324 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4327 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4328 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4329 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4330 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4331 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4332 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4334 m_gridWin
->SetOwnForegroundColour(gfg
);
4335 m_gridWin
->SetOwnBackgroundColour(gbg
);
4340 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4341 wxGrid::wxGridSelectionModes selmode
)
4343 wxCHECK_MSG( !m_created
,
4345 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4347 m_numRows
= numRows
;
4348 m_numCols
= numCols
;
4350 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4351 m_table
->SetView( this );
4353 m_selection
= new wxGridSelection( this, selmode
);
4362 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4364 wxCHECK_RET( m_created
,
4365 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4367 m_selection
->SetSelectionMode( selmode
);
4370 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4372 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4373 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4375 return m_selection
->GetSelectionMode();
4378 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4379 wxGrid::wxGridSelectionModes selmode
)
4381 bool checkSelection
= false;
4384 // stop all processing
4389 m_table
->SetView(0);
4401 checkSelection
= true;
4403 // kill row and column size arrays
4404 m_colWidths
.Empty();
4405 m_colRights
.Empty();
4406 m_rowHeights
.Empty();
4407 m_rowBottoms
.Empty();
4412 m_numRows
= table
->GetNumberRows();
4413 m_numCols
= table
->GetNumberCols();
4416 m_table
->SetView( this );
4417 m_ownTable
= takeOwnership
;
4418 m_selection
= new wxGridSelection( this, selmode
);
4421 // If the newly set table is smaller than the
4422 // original one current cell and selection regions
4423 // might be invalid,
4424 m_selectingKeyboard
= wxGridNoCellCoords
;
4425 m_currentCellCoords
=
4426 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
4427 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
4428 if (m_selectingTopLeft
.GetRow() >= m_numRows
||
4429 m_selectingTopLeft
.GetCol() >= m_numCols
)
4431 m_selectingTopLeft
= wxGridNoCellCoords
;
4432 m_selectingBottomRight
= wxGridNoCellCoords
;
4435 m_selectingBottomRight
=
4436 wxGridCellCoords(wxMin(m_numRows
,
4437 m_selectingBottomRight
.GetRow()),
4439 m_selectingBottomRight
.GetCol()));
4449 void wxGrid::InitVars()
4453 m_cornerLabelWin
= NULL
;
4454 m_rowLabelWin
= NULL
;
4455 m_colLabelWin
= NULL
;
4462 m_defaultCellAttr
= NULL
;
4463 m_typeRegistry
= NULL
;
4464 m_winCapture
= NULL
;
4469 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4470 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4472 if ( m_rowLabelWin
)
4474 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4478 m_labelBackgroundColour
= *wxWHITE
;
4481 m_labelTextColour
= *wxBLACK
;
4484 m_attrCache
.row
= -1;
4485 m_attrCache
.col
= -1;
4486 m_attrCache
.attr
= NULL
;
4488 // TODO: something better than this ?
4490 m_labelFont
= this->GetFont();
4491 m_labelFont
.SetWeight( wxBOLD
);
4493 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4494 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4496 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4497 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4498 m_colLabelTextOrientation
= wxHORIZONTAL
;
4500 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4501 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4503 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4504 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4506 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4507 m_defaultRowHeight
+= 8;
4509 m_defaultRowHeight
+= 4;
4512 m_gridLineColour
= wxColour( 192,192,192 );
4513 m_gridLinesEnabled
= true;
4514 m_cellHighlightColour
= *wxBLACK
;
4515 m_cellHighlightPenWidth
= 2;
4516 m_cellHighlightROPenWidth
= 1;
4518 m_canDragColMove
= false;
4520 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4521 m_winCapture
= (wxWindow
*)NULL
;
4522 m_canDragRowSize
= true;
4523 m_canDragColSize
= true;
4524 m_canDragGridSize
= true;
4525 m_canDragCell
= false;
4527 m_dragRowOrCol
= -1;
4528 m_isDragging
= false;
4529 m_startDragPos
= wxDefaultPosition
;
4530 m_nativeColumnLabels
= false;
4532 m_waitForSlowClick
= false;
4534 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4535 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4537 m_currentCellCoords
= wxGridNoCellCoords
;
4541 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4542 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4544 m_editable
= true; // default for whole grid
4546 m_inOnKeyDown
= false;
4552 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4553 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4556 // ----------------------------------------------------------------------------
4557 // the idea is to call these functions only when necessary because they create
4558 // quite big arrays which eat memory mostly unnecessary - in particular, if
4559 // default widths/heights are used for all rows/columns, we may not use these
4562 // with some extra code, it should be possible to only store the widths/heights
4563 // different from default ones (resulting in space savings for huge grids) but
4564 // this is not done currently
4565 // ----------------------------------------------------------------------------
4567 void wxGrid::InitRowHeights()
4569 m_rowHeights
.Empty();
4570 m_rowBottoms
.Empty();
4572 m_rowHeights
.Alloc( m_numRows
);
4573 m_rowBottoms
.Alloc( m_numRows
);
4575 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4578 for ( int i
= 0; i
< m_numRows
; i
++ )
4580 rowBottom
+= m_defaultRowHeight
;
4581 m_rowBottoms
.Add( rowBottom
);
4585 void wxGrid::InitColWidths()
4587 m_colWidths
.Empty();
4588 m_colRights
.Empty();
4590 m_colWidths
.Alloc( m_numCols
);
4591 m_colRights
.Alloc( m_numCols
);
4593 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4596 for ( int i
= 0; i
< m_numCols
; i
++ )
4598 colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
4599 m_colRights
.Add( colRight
);
4603 int wxGrid::GetColWidth(int col
) const
4605 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4608 int wxGrid::GetColLeft(int col
) const
4610 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
4611 : m_colRights
[col
] - m_colWidths
[col
];
4614 int wxGrid::GetColRight(int col
) const
4616 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
4620 int wxGrid::GetRowHeight(int row
) const
4622 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4625 int wxGrid::GetRowTop(int row
) const
4627 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4628 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4631 int wxGrid::GetRowBottom(int row
) const
4633 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4634 : m_rowBottoms
[row
];
4637 void wxGrid::CalcDimensions()
4639 // compute the size of the scrollable area
4640 int w
= m_numCols
> 0 ? GetColRight(GetColAt(m_numCols
- 1)) : 0;
4641 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
4646 // take into account editor if shown
4647 if ( IsCellEditControlShown() )
4650 int r
= m_currentCellCoords
.GetRow();
4651 int c
= m_currentCellCoords
.GetCol();
4652 int x
= GetColLeft(c
);
4653 int y
= GetRowTop(r
);
4655 // how big is the editor
4656 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4657 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4658 editor
->GetControl()->GetSize(&w2
, &h2
);
4669 // preserve (more or less) the previous position
4671 GetViewStart( &x
, &y
);
4673 // ensure the position is valid for the new scroll ranges
4675 x
= wxMax( w
- 1, 0 );
4677 y
= wxMax( h
- 1, 0 );
4679 // do set scrollbar parameters
4680 SetScrollbars( m_scrollLineX
, m_scrollLineY
,
4681 GetScrollX(w
), GetScrollY(h
),
4683 GetBatchCount() != 0);
4685 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4686 // still must reposition the children
4690 void wxGrid::CalcWindowSizes()
4692 // escape if the window is has not been fully created yet
4694 if ( m_cornerLabelWin
== NULL
)
4698 GetClientSize( &cw
, &ch
);
4700 // this block of code tries to work around the following problem: the grid
4701 // could have been just resized to have enough space to show the full grid
4702 // window contents without the scrollbars, but its client size could be
4703 // not big enough because the grid has the scrollbars right now and so the
4704 // scrollbars would remain even though we don't need them any more
4706 // to prevent this from happening, check if we have enough space for
4707 // everything without the scrollbars and explicitly disable them then
4708 wxSize size
= GetSize() - GetWindowBorderSize();
4709 if ( size
!= wxSize(cw
, ch
) )
4711 // check if we have enough space for grid window after accounting for
4712 // the fixed size elements
4713 size
.x
-= m_rowLabelWidth
;
4714 size
.y
-= m_colLabelHeight
;
4716 const wxSize vsize
= m_gridWin
->GetVirtualSize();
4718 if ( size
.x
>= vsize
.x
&& size
.y
>= vsize
.y
)
4720 // yes, we do, so remove the scrollbars and use the new client size
4721 // (which should be the same as full window size - borders now)
4722 SetScrollbars(0, 0, 0, 0);
4723 GetClientSize(&cw
, &ch
);
4727 // the grid may be too small to have enough space for the labels yet, don't
4728 // size the windows to negative sizes in this case
4729 int gw
= cw
- m_rowLabelWidth
;
4730 int gh
= ch
- m_colLabelHeight
;
4736 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4737 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4739 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4740 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, gw
, m_colLabelHeight
);
4742 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4743 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, gh
);
4745 if ( m_gridWin
&& m_gridWin
->IsShown() )
4746 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, gw
, gh
);
4749 // this is called when the grid table sends a message
4750 // to indicate that it has been redimensioned
4752 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4755 bool result
= false;
4757 // Clear the attribute cache as the attribute might refer to a different
4758 // cell than stored in the cache after adding/removing rows/columns.
4761 // By the same reasoning, the editor should be dismissed if columns are
4762 // added or removed. And for consistency, it should IMHO always be
4763 // removed, not only if the cell "underneath" it actually changes.
4764 // For now, I intentionally do not save the editor's content as the
4765 // cell it might want to save that stuff to might no longer exist.
4766 HideCellEditControl();
4769 // if we were using the default widths/heights so far, we must change them
4771 if ( m_colWidths
.IsEmpty() )
4776 if ( m_rowHeights
.IsEmpty() )
4782 switch ( msg
.GetId() )
4784 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4786 size_t pos
= msg
.GetCommandInt();
4787 int numRows
= msg
.GetCommandInt2();
4789 m_numRows
+= numRows
;
4791 if ( !m_rowHeights
.IsEmpty() )
4793 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4794 m_rowBottoms
.Insert( 0, pos
, numRows
);
4798 bottom
= m_rowBottoms
[pos
- 1];
4800 for ( i
= pos
; i
< m_numRows
; i
++ )
4802 bottom
+= m_rowHeights
[i
];
4803 m_rowBottoms
[i
] = bottom
;
4807 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4809 // if we have just inserted cols into an empty grid the current
4810 // cell will be undefined...
4812 SetCurrentCell( 0, 0 );
4816 m_selection
->UpdateRows( pos
, numRows
);
4817 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4819 attrProvider
->UpdateAttrRows( pos
, numRows
);
4821 if ( !GetBatchCount() )
4824 m_rowLabelWin
->Refresh();
4830 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4832 int numRows
= msg
.GetCommandInt();
4833 int oldNumRows
= m_numRows
;
4834 m_numRows
+= numRows
;
4836 if ( !m_rowHeights
.IsEmpty() )
4838 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4839 m_rowBottoms
.Add( 0, numRows
);
4842 if ( oldNumRows
> 0 )
4843 bottom
= m_rowBottoms
[oldNumRows
- 1];
4845 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4847 bottom
+= m_rowHeights
[i
];
4848 m_rowBottoms
[i
] = bottom
;
4852 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4854 // if we have just inserted cols into an empty grid the current
4855 // cell will be undefined...
4857 SetCurrentCell( 0, 0 );
4860 if ( !GetBatchCount() )
4863 m_rowLabelWin
->Refresh();
4869 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4871 size_t pos
= msg
.GetCommandInt();
4872 int numRows
= msg
.GetCommandInt2();
4873 m_numRows
-= numRows
;
4875 if ( !m_rowHeights
.IsEmpty() )
4877 m_rowHeights
.RemoveAt( pos
, numRows
);
4878 m_rowBottoms
.RemoveAt( pos
, numRows
);
4881 for ( i
= 0; i
< m_numRows
; i
++ )
4883 h
+= m_rowHeights
[i
];
4884 m_rowBottoms
[i
] = h
;
4890 m_currentCellCoords
= wxGridNoCellCoords
;
4894 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4895 m_currentCellCoords
.Set( 0, 0 );
4899 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4900 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4903 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4905 // ifdef'd out following patch from Paul Gammans
4907 // No need to touch column attributes, unless we
4908 // removed _all_ rows, in this case, we remove
4909 // all column attributes.
4910 // I hate to do this here, but the
4911 // needed data is not available inside UpdateAttrRows.
4912 if ( !GetNumberRows() )
4913 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4917 if ( !GetBatchCount() )
4920 m_rowLabelWin
->Refresh();
4926 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4928 size_t pos
= msg
.GetCommandInt();
4929 int numCols
= msg
.GetCommandInt2();
4930 m_numCols
+= numCols
;
4932 if ( !m_colAt
.IsEmpty() )
4934 //Shift the column IDs
4936 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
4938 if ( m_colAt
[i
] >= (int)pos
)
4939 m_colAt
[i
] += numCols
;
4942 m_colAt
.Insert( pos
, pos
, numCols
);
4944 //Set the new columns' positions
4945 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
4951 if ( !m_colWidths
.IsEmpty() )
4953 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4954 m_colRights
.Insert( 0, pos
, numCols
);
4958 right
= m_colRights
[GetColAt( pos
- 1 )];
4961 for ( colPos
= pos
; 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 );
4979 m_selection
->UpdateCols( pos
, numCols
);
4980 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4982 attrProvider
->UpdateAttrCols( pos
, numCols
);
4983 if ( !GetBatchCount() )
4986 m_colLabelWin
->Refresh();
4992 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4994 int numCols
= msg
.GetCommandInt();
4995 int oldNumCols
= m_numCols
;
4996 m_numCols
+= numCols
;
4998 if ( !m_colAt
.IsEmpty() )
5000 m_colAt
.Add( 0, numCols
);
5002 //Set the new columns' positions
5004 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
5010 if ( !m_colWidths
.IsEmpty() )
5012 m_colWidths
.Add( m_defaultColWidth
, numCols
);
5013 m_colRights
.Add( 0, numCols
);
5016 if ( oldNumCols
> 0 )
5017 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
5020 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
5022 i
= GetColAt( colPos
);
5024 right
+= m_colWidths
[i
];
5025 m_colRights
[i
] = right
;
5029 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5031 // if we have just inserted cols into an empty grid the current
5032 // cell will be undefined...
5034 SetCurrentCell( 0, 0 );
5036 if ( !GetBatchCount() )
5039 m_colLabelWin
->Refresh();
5045 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5047 size_t pos
= msg
.GetCommandInt();
5048 int numCols
= msg
.GetCommandInt2();
5049 m_numCols
-= numCols
;
5051 if ( !m_colAt
.IsEmpty() )
5053 int colID
= GetColAt( pos
);
5055 m_colAt
.RemoveAt( pos
, numCols
);
5057 //Shift the column IDs
5059 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5061 if ( m_colAt
[colPos
] > colID
)
5062 m_colAt
[colPos
] -= numCols
;
5066 if ( !m_colWidths
.IsEmpty() )
5068 m_colWidths
.RemoveAt( pos
, numCols
);
5069 m_colRights
.RemoveAt( pos
, numCols
);
5073 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5075 i
= GetColAt( colPos
);
5077 w
+= m_colWidths
[i
];
5084 m_currentCellCoords
= wxGridNoCellCoords
;
5088 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
5089 m_currentCellCoords
.Set( 0, 0 );
5093 m_selection
->UpdateCols( pos
, -((int)numCols
) );
5094 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5097 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
5099 // ifdef'd out following patch from Paul Gammans
5101 // No need to touch row attributes, unless we
5102 // removed _all_ columns, in this case, we remove
5103 // all row attributes.
5104 // I hate to do this here, but the
5105 // needed data is not available inside UpdateAttrCols.
5106 if ( !GetNumberCols() )
5107 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
5111 if ( !GetBatchCount() )
5114 m_colLabelWin
->Refresh();
5121 if (result
&& !GetBatchCount() )
5122 m_gridWin
->Refresh();
5127 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
) const
5129 wxRegionIterator
iter( reg
);
5132 wxArrayInt rowlabels
;
5139 // TODO: remove this when we can...
5140 // There is a bug in wxMotif that gives garbage update
5141 // rectangles if you jump-scroll a long way by clicking the
5142 // scrollbar with middle button. This is a work-around
5144 #if defined(__WXMOTIF__)
5146 m_gridWin
->GetClientSize( &cw
, &ch
);
5147 if ( r
.GetTop() > ch
)
5149 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5152 // logical bounds of update region
5155 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
5156 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
5158 // find the row labels within these bounds
5161 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5163 if ( GetRowBottom(row
) < top
)
5166 if ( GetRowTop(row
) > bottom
)
5169 rowlabels
.Add( row
);
5178 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
) const
5180 wxRegionIterator
iter( reg
);
5183 wxArrayInt colLabels
;
5190 // TODO: remove this when we can...
5191 // There is a bug in wxMotif that gives garbage update
5192 // rectangles if you jump-scroll a long way by clicking the
5193 // scrollbar with middle button. This is a work-around
5195 #if defined(__WXMOTIF__)
5197 m_gridWin
->GetClientSize( &cw
, &ch
);
5198 if ( r
.GetLeft() > cw
)
5200 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5203 // logical bounds of update region
5206 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
5207 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
5209 // find the cells within these bounds
5213 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5215 col
= GetColAt( colPos
);
5217 if ( GetColRight(col
) < left
)
5220 if ( GetColLeft(col
) > right
)
5223 colLabels
.Add( col
);
5232 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
) const
5234 wxRegionIterator
iter( reg
);
5237 wxGridCellCoordsArray cellsExposed
;
5239 int left
, top
, right
, bottom
;
5244 // TODO: remove this when we can...
5245 // There is a bug in wxMotif that gives garbage update
5246 // rectangles if you jump-scroll a long way by clicking the
5247 // scrollbar with middle button. This is a work-around
5249 #if defined(__WXMOTIF__)
5251 m_gridWin
->GetClientSize( &cw
, &ch
);
5252 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5253 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5254 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5255 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5258 // logical bounds of update region
5260 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5261 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5263 // find the cells within these bounds
5266 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5268 if ( GetRowBottom(row
) <= top
)
5271 if ( GetRowTop(row
) > bottom
)
5275 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5277 col
= GetColAt( colPos
);
5279 if ( GetColRight(col
) <= left
)
5282 if ( GetColLeft(col
) > right
)
5285 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
5292 return cellsExposed
;
5296 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5299 wxPoint
pos( event
.GetPosition() );
5300 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5302 if ( event
.Dragging() )
5306 m_isDragging
= true;
5307 m_rowLabelWin
->CaptureMouse();
5310 if ( event
.LeftIsDown() )
5312 switch ( m_cursorMode
)
5314 case WXGRID_CURSOR_RESIZE_ROW
:
5316 int cw
, ch
, left
, dummy
;
5317 m_gridWin
->GetClientSize( &cw
, &ch
);
5318 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5320 wxClientDC
dc( m_gridWin
);
5323 GetRowTop(m_dragRowOrCol
) +
5324 GetRowMinimalHeight(m_dragRowOrCol
) );
5325 dc
.SetLogicalFunction(wxINVERT
);
5326 if ( m_dragLastPos
>= 0 )
5328 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5330 dc
.DrawLine( left
, y
, left
+cw
, y
);
5335 case WXGRID_CURSOR_SELECT_ROW
:
5337 if ( (row
= YToRow( y
)) >= 0 )
5341 m_selection
->SelectRow( row
,
5342 event
.ControlDown(),
5351 // default label to suppress warnings about "enumeration value
5352 // 'xxx' not handled in switch
5360 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5365 if (m_rowLabelWin
->HasCapture())
5366 m_rowLabelWin
->ReleaseMouse();
5367 m_isDragging
= false;
5370 // ------------ Entering or leaving the window
5372 if ( event
.Entering() || event
.Leaving() )
5374 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5377 // ------------ Left button pressed
5379 else if ( event
.LeftDown() )
5381 // don't send a label click event for a hit on the
5382 // edge of the row label - this is probably the user
5383 // wanting to resize the row
5385 if ( YToEdgeOfRow(y
) < 0 )
5389 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5391 if ( !event
.ShiftDown() && !event
.CmdDown() )
5395 if ( event
.ShiftDown() )
5397 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5400 GetNumberCols() - 1,
5401 event
.ControlDown(),
5408 m_selection
->SelectRow( row
,
5409 event
.ControlDown(),
5416 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5421 // starting to drag-resize a row
5422 if ( CanDragRowSize() )
5423 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5427 // ------------ Left double click
5429 else if (event
.LeftDClick() )
5431 row
= YToEdgeOfRow(y
);
5436 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5438 // no default action at the moment
5443 // adjust row height depending on label text
5444 AutoSizeRowLabelSize( row
);
5446 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5451 // ------------ Left button released
5453 else if ( event
.LeftUp() )
5455 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5457 DoEndDragResizeRow();
5459 // Note: we are ending the event *after* doing
5460 // default processing in this case
5462 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5465 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5469 // ------------ Right button down
5471 else if ( event
.RightDown() )
5475 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5477 // no default action at the moment
5481 // ------------ Right double click
5483 else if ( event
.RightDClick() )
5487 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5489 // no default action at the moment
5493 // ------------ No buttons down and mouse moving
5495 else if ( event
.Moving() )
5497 m_dragRowOrCol
= YToEdgeOfRow( y
);
5498 if ( m_dragRowOrCol
>= 0 )
5500 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5502 // don't capture the mouse yet
5503 if ( CanDragRowSize() )
5504 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5507 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5509 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5514 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5517 wxPoint
pos( event
.GetPosition() );
5518 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5520 if ( event
.Dragging() )
5524 m_isDragging
= true;
5525 m_colLabelWin
->CaptureMouse();
5527 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
)
5528 m_dragRowOrCol
= XToCol( x
);
5531 if ( event
.LeftIsDown() )
5533 switch ( m_cursorMode
)
5535 case WXGRID_CURSOR_RESIZE_COL
:
5537 int cw
, ch
, dummy
, top
;
5538 m_gridWin
->GetClientSize( &cw
, &ch
);
5539 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5541 wxClientDC
dc( m_gridWin
);
5544 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5545 GetColMinimalWidth(m_dragRowOrCol
));
5546 dc
.SetLogicalFunction(wxINVERT
);
5547 if ( m_dragLastPos
>= 0 )
5549 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5551 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5556 case WXGRID_CURSOR_SELECT_COL
:
5558 if ( (col
= XToCol( x
)) >= 0 )
5562 m_selection
->SelectCol( col
,
5563 event
.ControlDown(),
5572 case WXGRID_CURSOR_MOVE_COL
:
5575 m_moveToCol
= GetColAt( 0 );
5577 m_moveToCol
= XToCol( x
);
5581 if ( m_moveToCol
< 0 )
5582 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5584 markerX
= GetColLeft( m_moveToCol
);
5586 if ( markerX
!= m_dragLastPos
)
5588 wxClientDC
dc( m_colLabelWin
);
5591 m_colLabelWin
->GetClientSize( &cw
, &ch
);
5595 //Clean up the last indicator
5596 if ( m_dragLastPos
>= 0 )
5598 wxPen
pen( m_colLabelWin
->GetBackgroundColour(), 2 );
5600 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
5601 dc
.SetPen(wxNullPen
);
5603 if ( XToCol( m_dragLastPos
) != -1 )
5604 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
5607 //Moving to the same place? Don't draw a marker
5608 if ( (m_moveToCol
== m_dragRowOrCol
)
5609 || (GetColPos( m_moveToCol
) == GetColPos( m_dragRowOrCol
) + 1)
5610 || (m_moveToCol
< 0 && m_dragRowOrCol
== GetColAt( m_numCols
- 1 )))
5617 wxPen
pen( *wxBLUE
, 2 );
5620 dc
.DrawLine( markerX
, 0, markerX
, ch
);
5622 dc
.SetPen(wxNullPen
);
5624 m_dragLastPos
= markerX
- 1;
5629 // default label to suppress warnings about "enumeration value
5630 // 'xxx' not handled in switch
5638 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5643 if (m_colLabelWin
->HasCapture())
5644 m_colLabelWin
->ReleaseMouse();
5645 m_isDragging
= false;
5648 // ------------ Entering or leaving the window
5650 if ( event
.Entering() || event
.Leaving() )
5652 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5655 // ------------ Left button pressed
5657 else if ( event
.LeftDown() )
5659 // don't send a label click event for a hit on the
5660 // edge of the col label - this is probably the user
5661 // wanting to resize the col
5663 if ( XToEdgeOfCol(x
) < 0 )
5667 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5669 if ( m_canDragColMove
)
5671 //Show button as pressed
5672 wxClientDC
dc( m_colLabelWin
);
5673 int colLeft
= GetColLeft( col
);
5674 int colRight
= GetColRight( col
) - 1;
5675 dc
.SetPen( wxPen( m_colLabelWin
->GetBackgroundColour(), 1 ) );
5676 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
5677 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
5679 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, m_colLabelWin
);
5683 if ( !event
.ShiftDown() && !event
.CmdDown() )
5687 if ( event
.ShiftDown() )
5689 m_selection
->SelectBlock( 0,
5690 m_currentCellCoords
.GetCol(),
5691 GetNumberRows() - 1, col
,
5692 event
.ControlDown(),
5699 m_selection
->SelectCol( col
,
5700 event
.ControlDown(),
5707 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5713 // starting to drag-resize a col
5715 if ( CanDragColSize() )
5716 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5720 // ------------ Left double click
5722 if ( event
.LeftDClick() )
5724 col
= XToEdgeOfCol(x
);
5729 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5731 // no default action at the moment
5736 // adjust column width depending on label text
5737 AutoSizeColLabelSize( col
);
5739 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5744 // ------------ Left button released
5746 else if ( event
.LeftUp() )
5748 switch ( m_cursorMode
)
5750 case WXGRID_CURSOR_RESIZE_COL
:
5751 DoEndDragResizeCol();
5753 // Note: we are ending the event *after* doing
5754 // default processing in this case
5756 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5759 case WXGRID_CURSOR_MOVE_COL
:
5762 SendEvent( wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
, event
);
5765 case WXGRID_CURSOR_SELECT_COL
:
5766 case WXGRID_CURSOR_SELECT_CELL
:
5767 case WXGRID_CURSOR_RESIZE_ROW
:
5768 case WXGRID_CURSOR_SELECT_ROW
:
5769 // nothing to do (?)
5773 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5777 // ------------ Right button down
5779 else if ( event
.RightDown() )
5783 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5785 // no default action at the moment
5789 // ------------ Right double click
5791 else if ( event
.RightDClick() )
5795 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5797 // no default action at the moment
5801 // ------------ No buttons down and mouse moving
5803 else if ( event
.Moving() )
5805 m_dragRowOrCol
= XToEdgeOfCol( x
);
5806 if ( m_dragRowOrCol
>= 0 )
5808 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5810 // don't capture the cursor yet
5811 if ( CanDragColSize() )
5812 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5815 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5817 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5822 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5824 if ( event
.LeftDown() )
5826 // indicate corner label by having both row and
5829 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5834 else if ( event
.LeftDClick() )
5836 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5838 else if ( event
.RightDown() )
5840 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5842 // no default action at the moment
5845 else if ( event
.RightDClick() )
5847 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5849 // no default action at the moment
5854 void wxGrid::CancelMouseCapture()
5856 // cancel operation currently in progress, whatever it is
5859 m_isDragging
= false;
5860 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
5861 m_winCapture
->SetCursor( *wxSTANDARD_CURSOR
);
5862 m_winCapture
= NULL
;
5864 // remove traces of whatever we drew on screen
5869 void wxGrid::ChangeCursorMode(CursorMode mode
,
5874 static const wxChar
*cursorModes
[] =
5884 wxLogTrace(_T("grid"),
5885 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5886 win
== m_colLabelWin
? _T("colLabelWin")
5887 : win
? _T("rowLabelWin")
5889 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5892 if ( mode
== m_cursorMode
&&
5893 win
== m_winCapture
&&
5894 captureMouse
== (m_winCapture
!= NULL
))
5899 // by default use the grid itself
5905 if (m_winCapture
->HasCapture())
5906 m_winCapture
->ReleaseMouse();
5907 m_winCapture
= (wxWindow
*)NULL
;
5910 m_cursorMode
= mode
;
5912 switch ( m_cursorMode
)
5914 case WXGRID_CURSOR_RESIZE_ROW
:
5915 win
->SetCursor( m_rowResizeCursor
);
5918 case WXGRID_CURSOR_RESIZE_COL
:
5919 win
->SetCursor( m_colResizeCursor
);
5922 case WXGRID_CURSOR_MOVE_COL
:
5923 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
5927 win
->SetCursor( *wxSTANDARD_CURSOR
);
5931 // we need to capture mouse when resizing
5932 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5933 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5935 if ( captureMouse
&& resize
)
5937 win
->CaptureMouse();
5942 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5945 wxPoint
pos( event
.GetPosition() );
5946 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5948 wxGridCellCoords coords
;
5949 XYToCell( x
, y
, coords
);
5951 int cell_rows
, cell_cols
;
5952 bool isFirstDrag
= !m_isDragging
;
5953 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5954 if ((cell_rows
< 0) || (cell_cols
< 0))
5956 coords
.SetRow(coords
.GetRow() + cell_rows
);
5957 coords
.SetCol(coords
.GetCol() + cell_cols
);
5960 if ( event
.Dragging() )
5962 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5964 // Don't start doing anything until the mouse has been dragged at
5965 // least 3 pixels in any direction...
5968 if (m_startDragPos
== wxDefaultPosition
)
5970 m_startDragPos
= pos
;
5973 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5977 m_isDragging
= true;
5978 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5980 // Hide the edit control, so it
5981 // won't interfere with drag-shrinking.
5982 if ( IsCellEditControlShown() )
5984 HideCellEditControl();
5985 SaveEditControlValue();
5988 if ( coords
!= wxGridNoCellCoords
)
5990 if ( event
.CmdDown() )
5992 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5993 m_selectingKeyboard
= coords
;
5994 HighlightBlock( m_selectingKeyboard
, coords
);
5996 else if ( CanDragCell() )
6000 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6001 m_selectingKeyboard
= coords
;
6003 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
6012 if ( !IsSelection() )
6014 HighlightBlock( coords
, coords
);
6018 HighlightBlock( m_currentCellCoords
, coords
);
6022 if (! IsVisible(coords
))
6024 MakeCellVisible(coords
);
6025 // TODO: need to introduce a delay or something here. The
6026 // scrolling is way to fast, at least on MSW - also on GTK.
6029 // Have we captured the mouse yet?
6032 m_winCapture
= m_gridWin
;
6033 m_winCapture
->CaptureMouse();
6038 else if ( event
.LeftIsDown() &&
6039 m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6041 int cw
, ch
, left
, dummy
;
6042 m_gridWin
->GetClientSize( &cw
, &ch
);
6043 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6045 wxClientDC
dc( m_gridWin
);
6047 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
6048 GetRowMinimalHeight(m_dragRowOrCol
) );
6049 dc
.SetLogicalFunction(wxINVERT
);
6050 if ( m_dragLastPos
>= 0 )
6052 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
6054 dc
.DrawLine( left
, y
, left
+cw
, y
);
6057 else if ( event
.LeftIsDown() &&
6058 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6060 int cw
, ch
, dummy
, top
;
6061 m_gridWin
->GetClientSize( &cw
, &ch
);
6062 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6064 wxClientDC
dc( m_gridWin
);
6066 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
6067 GetColMinimalWidth(m_dragRowOrCol
) );
6068 dc
.SetLogicalFunction(wxINVERT
);
6069 if ( m_dragLastPos
>= 0 )
6071 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6073 dc
.DrawLine( x
, top
, x
, top
+ ch
);
6080 m_isDragging
= false;
6081 m_startDragPos
= wxDefaultPosition
;
6083 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
6084 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
6087 if ( event
.Entering() || event
.Leaving() )
6089 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6090 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
6095 // ------------ Left button pressed
6097 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
6099 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
6104 if ( !event
.CmdDown() )
6106 if ( event
.ShiftDown() )
6110 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
6111 m_currentCellCoords
.GetCol(),
6114 event
.ControlDown(),
6120 else if ( XToEdgeOfCol(x
) < 0 &&
6121 YToEdgeOfRow(y
) < 0 )
6123 DisableCellEditControl();
6124 MakeCellVisible( coords
);
6126 if ( event
.CmdDown() )
6130 m_selection
->ToggleCellSelection( coords
.GetRow(),
6132 event
.ControlDown(),
6137 m_selectingTopLeft
= wxGridNoCellCoords
;
6138 m_selectingBottomRight
= wxGridNoCellCoords
;
6139 m_selectingKeyboard
= coords
;
6143 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
6144 SetCurrentCell( coords
);
6147 if ( m_selection
->GetSelectionMode() !=
6148 wxGrid::wxGridSelectCells
)
6150 HighlightBlock( coords
, coords
);
6158 // ------------ Left double click
6160 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
6162 DisableCellEditControl();
6164 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
6166 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
6171 // we want double click to select a cell and start editing
6172 // (i.e. to behave in same way as sequence of two slow clicks):
6173 m_waitForSlowClick
= true;
6178 // ------------ Left button released
6180 else if ( event
.LeftUp() )
6182 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6186 if (m_winCapture
->HasCapture())
6187 m_winCapture
->ReleaseMouse();
6188 m_winCapture
= NULL
;
6191 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
6194 EnableCellEditControl();
6196 wxGridCellAttr
*attr
= GetCellAttr(coords
);
6197 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
6198 editor
->StartingClick();
6202 m_waitForSlowClick
= false;
6204 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6205 m_selectingBottomRight
!= wxGridNoCellCoords
)
6209 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6210 m_selectingTopLeft
.GetCol(),
6211 m_selectingBottomRight
.GetRow(),
6212 m_selectingBottomRight
.GetCol(),
6213 event
.ControlDown(),
6219 m_selectingTopLeft
= wxGridNoCellCoords
;
6220 m_selectingBottomRight
= wxGridNoCellCoords
;
6222 // Show the edit control, if it has been hidden for
6224 ShowCellEditControl();
6227 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6229 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6230 DoEndDragResizeRow();
6232 // Note: we are ending the event *after* doing
6233 // default processing in this case
6235 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
6237 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6239 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6240 DoEndDragResizeCol();
6242 // Note: we are ending the event *after* doing
6243 // default processing in this case
6245 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
6251 // ------------ Right button down
6253 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
6255 DisableCellEditControl();
6256 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
6261 // no default action at the moment
6265 // ------------ Right double click
6267 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
6269 DisableCellEditControl();
6270 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
6275 // no default action at the moment
6279 // ------------ Moving and no button action
6281 else if ( event
.Moving() && !event
.IsButton() )
6283 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
6285 // out of grid cell area
6286 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6290 int dragRow
= YToEdgeOfRow( y
);
6291 int dragCol
= XToEdgeOfCol( x
);
6293 // Dragging on the corner of a cell to resize in both
6294 // directions is not implemented yet...
6296 if ( dragRow
>= 0 && dragCol
>= 0 )
6298 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6304 m_dragRowOrCol
= dragRow
;
6306 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6308 if ( CanDragRowSize() && CanDragGridSize() )
6309 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false);
6312 else if ( dragCol
>= 0 )
6314 m_dragRowOrCol
= dragCol
;
6316 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6318 if ( CanDragColSize() && CanDragGridSize() )
6319 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false);
6322 else // Neither on a row or col edge
6324 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6326 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6332 void wxGrid::DoEndDragResizeRow()
6334 if ( m_dragLastPos
>= 0 )
6336 // erase the last line and resize the row
6338 int cw
, ch
, left
, dummy
;
6339 m_gridWin
->GetClientSize( &cw
, &ch
);
6340 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6342 wxClientDC
dc( m_gridWin
);
6344 dc
.SetLogicalFunction( wxINVERT
);
6345 dc
.DrawLine( left
, m_dragLastPos
, left
+ cw
, m_dragLastPos
);
6346 HideCellEditControl();
6347 SaveEditControlValue();
6349 int rowTop
= GetRowTop(m_dragRowOrCol
);
6350 SetRowSize( m_dragRowOrCol
,
6351 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
6353 if ( !GetBatchCount() )
6355 // Only needed to get the correct rect.y:
6356 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
6358 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6359 rect
.width
= m_rowLabelWidth
;
6360 rect
.height
= ch
- rect
.y
;
6361 m_rowLabelWin
->Refresh( true, &rect
);
6364 // if there is a multicell block, paint all of it
6367 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
6368 int leftCol
= XToCol(left
);
6369 int rightCol
= internalXToCol(left
+ cw
);
6372 for (i
=leftCol
; i
<rightCol
; i
++)
6374 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
6375 if (cell_rows
< subtract_rows
)
6376 subtract_rows
= cell_rows
;
6378 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
6379 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6380 rect
.height
= ch
- rect
.y
;
6383 m_gridWin
->Refresh( false, &rect
);
6386 ShowCellEditControl();
6391 void wxGrid::DoEndDragResizeCol()
6393 if ( m_dragLastPos
>= 0 )
6395 // erase the last line and resize the col
6397 int cw
, ch
, dummy
, top
;
6398 m_gridWin
->GetClientSize( &cw
, &ch
);
6399 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6401 wxClientDC
dc( m_gridWin
);
6403 dc
.SetLogicalFunction( wxINVERT
);
6404 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6405 HideCellEditControl();
6406 SaveEditControlValue();
6408 int colLeft
= GetColLeft(m_dragRowOrCol
);
6409 SetColSize( m_dragRowOrCol
,
6410 wxMax( m_dragLastPos
- colLeft
,
6411 GetColMinimalWidth(m_dragRowOrCol
) ) );
6413 if ( !GetBatchCount() )
6415 // Only needed to get the correct rect.x:
6416 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
6418 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6419 rect
.width
= cw
- rect
.x
;
6420 rect
.height
= m_colLabelHeight
;
6421 m_colLabelWin
->Refresh( true, &rect
);
6424 // if there is a multicell block, paint all of it
6427 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
6428 int topRow
= YToRow(top
);
6429 int bottomRow
= internalYToRow(top
+ cw
);
6432 for (i
=topRow
; i
<bottomRow
; i
++)
6434 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
6435 if (cell_cols
< subtract_cols
)
6436 subtract_cols
= cell_cols
;
6439 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
6440 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6441 rect
.width
= cw
- rect
.x
;
6445 m_gridWin
->Refresh( false, &rect
);
6448 ShowCellEditControl();
6452 void wxGrid::DoEndDragMoveCol()
6454 //The user clicked on the column but didn't actually drag
6455 if ( m_dragLastPos
< 0 )
6457 m_colLabelWin
->Refresh(); //Do this to "unpress" the column
6462 if ( m_moveToCol
== -1 )
6463 newPos
= m_numCols
- 1;
6466 newPos
= GetColPos( m_moveToCol
);
6467 if ( newPos
> GetColPos( m_dragRowOrCol
) )
6471 SetColPos( m_dragRowOrCol
, newPos
);
6474 void wxGrid::SetColPos( int colID
, int newPos
)
6476 if ( m_colAt
.IsEmpty() )
6478 m_colAt
.Alloc( m_numCols
);
6481 for ( i
= 0; i
< m_numCols
; i
++ )
6487 int oldPos
= GetColPos( colID
);
6489 //Reshuffle the m_colAt array
6490 if ( newPos
> oldPos
)
6493 for ( i
= oldPos
; i
< newPos
; i
++ )
6495 m_colAt
[i
] = m_colAt
[i
+1];
6501 for ( i
= oldPos
; i
> newPos
; i
-- )
6503 m_colAt
[i
] = m_colAt
[i
-1];
6507 m_colAt
[newPos
] = colID
;
6509 //Recalculate the column rights
6510 if ( !m_colWidths
.IsEmpty() )
6514 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6516 int colID
= GetColAt( colPos
);
6518 colRight
+= m_colWidths
[colID
];
6519 m_colRights
[colID
] = colRight
;
6523 m_colLabelWin
->Refresh();
6524 m_gridWin
->Refresh();
6529 void wxGrid::EnableDragColMove( bool enable
)
6531 if ( m_canDragColMove
== enable
)
6534 m_canDragColMove
= enable
;
6536 if ( !m_canDragColMove
)
6540 //Recalculate the column rights
6541 if ( !m_colWidths
.IsEmpty() )
6545 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6547 colRight
+= m_colWidths
[colPos
];
6548 m_colRights
[colPos
] = colRight
;
6552 m_colLabelWin
->Refresh();
6553 m_gridWin
->Refresh();
6559 // ------ interaction with data model
6561 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6563 switch ( msg
.GetId() )
6565 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6566 return GetModelValues();
6568 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6569 return SetModelValues();
6571 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6572 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6573 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6574 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6575 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6576 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6577 return Redimension( msg
);
6584 // The behaviour of this function depends on the grid table class
6585 // Clear() function. For the default wxGridStringTable class the
6586 // behavious is to replace all cell contents with wxEmptyString but
6587 // not to change the number of rows or cols.
6589 void wxGrid::ClearGrid()
6593 if (IsCellEditControlEnabled())
6594 DisableCellEditControl();
6597 if (!GetBatchCount())
6598 m_gridWin
->Refresh();
6602 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6604 // TODO: something with updateLabels flag
6608 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6614 if (IsCellEditControlEnabled())
6615 DisableCellEditControl();
6617 bool done
= m_table
->InsertRows( pos
, numRows
);
6620 // the table will have sent the results of the insert row
6621 // operation to this view object as a grid table message
6627 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6629 // TODO: something with updateLabels flag
6633 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6639 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6642 // the table will have sent the results of the append row
6643 // operation to this view object as a grid table message
6649 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6651 // TODO: something with updateLabels flag
6655 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6661 if (IsCellEditControlEnabled())
6662 DisableCellEditControl();
6664 bool done
= m_table
->DeleteRows( pos
, numRows
);
6666 // the table will have sent the results of the delete row
6667 // operation to this view object as a grid table message
6673 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6675 // TODO: something with updateLabels flag
6679 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6685 if (IsCellEditControlEnabled())
6686 DisableCellEditControl();
6688 bool done
= m_table
->InsertCols( pos
, numCols
);
6690 // the table will have sent the results of the insert col
6691 // operation to this view object as a grid table message
6697 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6699 // TODO: something with updateLabels flag
6703 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6709 bool done
= m_table
->AppendCols( numCols
);
6711 // the table will have sent the results of the append col
6712 // operation to this view object as a grid table message
6718 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6720 // TODO: something with updateLabels flag
6724 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6730 if (IsCellEditControlEnabled())
6731 DisableCellEditControl();
6733 bool done
= m_table
->DeleteCols( pos
, numCols
);
6735 // the table will have sent the results of the delete col
6736 // operation to this view object as a grid table message
6743 // ----- event handlers
6746 // Generate a grid event based on a mouse event and
6747 // return the result of ProcessEvent()
6749 int wxGrid::SendEvent( const wxEventType type
,
6751 wxMouseEvent
& mouseEv
)
6753 bool claimed
, vetoed
;
6755 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6757 int rowOrCol
= (row
== -1 ? col
: row
);
6759 wxGridSizeEvent
gridEvt( GetId(),
6763 mouseEv
.GetX() + GetRowLabelSize(),
6764 mouseEv
.GetY() + GetColLabelSize(),
6765 mouseEv
.ControlDown(),
6766 mouseEv
.ShiftDown(),
6768 mouseEv
.MetaDown() );
6770 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6771 vetoed
= !gridEvt
.IsAllowed();
6773 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6775 // Right now, it should _never_ end up here!
6776 wxGridRangeSelectEvent
gridEvt( GetId(),
6780 m_selectingBottomRight
,
6782 mouseEv
.ControlDown(),
6783 mouseEv
.ShiftDown(),
6785 mouseEv
.MetaDown() );
6787 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6788 vetoed
= !gridEvt
.IsAllowed();
6790 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
6791 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
6792 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
6793 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
6795 wxPoint pos
= mouseEv
.GetPosition();
6797 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
6798 pos
.y
+= GetColLabelSize();
6799 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
6800 pos
.x
+= GetRowLabelSize();
6802 wxGridEvent
gridEvt( GetId(),
6809 mouseEv
.ControlDown(),
6810 mouseEv
.ShiftDown(),
6812 mouseEv
.MetaDown() );
6813 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6814 vetoed
= !gridEvt
.IsAllowed();
6818 wxGridEvent
gridEvt( GetId(),
6822 mouseEv
.GetX() + GetRowLabelSize(),
6823 mouseEv
.GetY() + GetColLabelSize(),
6825 mouseEv
.ControlDown(),
6826 mouseEv
.ShiftDown(),
6828 mouseEv
.MetaDown() );
6829 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6830 vetoed
= !gridEvt
.IsAllowed();
6833 // A Veto'd event may not be `claimed' so test this first
6837 return claimed
? 1 : 0;
6840 // Generate a grid event of specified type and return the result
6841 // of ProcessEvent().
6843 int wxGrid::SendEvent( const wxEventType type
,
6846 bool claimed
, vetoed
;
6848 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6850 int rowOrCol
= (row
== -1 ? col
: row
);
6852 wxGridSizeEvent
gridEvt( GetId(), type
, this, rowOrCol
);
6854 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6855 vetoed
= !gridEvt
.IsAllowed();
6859 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
6861 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6862 vetoed
= !gridEvt
.IsAllowed();
6865 // A Veto'd event may not be `claimed' so test this first
6869 return claimed
? 1 : 0;
6872 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6874 // needed to prevent zillions of paint events on MSW
6878 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6880 // Don't do anything if between Begin/EndBatch...
6881 // EndBatch() will do all this on the last nested one anyway.
6882 if ( m_created
&& !GetBatchCount() )
6884 // Refresh to get correct scrolled position:
6885 wxScrolledWindow::Refresh(eraseb
, rect
);
6889 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6890 int width_label
, width_cell
, height_label
, height_cell
;
6893 // Copy rectangle can get scroll offsets..
6894 rect_x
= rect
->GetX();
6895 rect_y
= rect
->GetY();
6896 rectWidth
= rect
->GetWidth();
6897 rectHeight
= rect
->GetHeight();
6899 width_label
= m_rowLabelWidth
- rect_x
;
6900 if (width_label
> rectWidth
)
6901 width_label
= rectWidth
;
6903 height_label
= m_colLabelHeight
- rect_y
;
6904 if (height_label
> rectHeight
)
6905 height_label
= rectHeight
;
6907 if (rect_x
> m_rowLabelWidth
)
6909 x
= rect_x
- m_rowLabelWidth
;
6910 width_cell
= rectWidth
;
6915 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6918 if (rect_y
> m_colLabelHeight
)
6920 y
= rect_y
- m_colLabelHeight
;
6921 height_cell
= rectHeight
;
6926 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6929 // Paint corner label part intersecting rect.
6930 if ( width_label
> 0 && height_label
> 0 )
6932 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6933 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6936 // Paint col labels part intersecting rect.
6937 if ( width_cell
> 0 && height_label
> 0 )
6939 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6940 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6943 // Paint row labels part intersecting rect.
6944 if ( width_label
> 0 && height_cell
> 0 )
6946 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6947 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6950 // Paint cell area part intersecting rect.
6951 if ( width_cell
> 0 && height_cell
> 0 )
6953 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6954 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6959 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6960 m_colLabelWin
->Refresh(eraseb
, NULL
);
6961 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6962 m_gridWin
->Refresh(eraseb
, NULL
);
6967 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
))
6969 if (m_targetWindow
!= this) // check whether initialisation has been done
6971 // update our children window positions and scrollbars
6976 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6978 if ( m_inOnKeyDown
)
6980 // shouldn't be here - we are going round in circles...
6982 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6985 m_inOnKeyDown
= true;
6987 // propagate the event up and see if it gets processed
6988 wxWindow
*parent
= GetParent();
6989 wxKeyEvent
keyEvt( event
);
6990 keyEvt
.SetEventObject( parent
);
6992 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6994 if (GetLayoutDirection() == wxLayout_RightToLeft
)
6996 if (event
.GetKeyCode() == WXK_RIGHT
)
6997 event
.m_keyCode
= WXK_LEFT
;
6998 else if (event
.GetKeyCode() == WXK_LEFT
)
6999 event
.m_keyCode
= WXK_RIGHT
;
7002 // try local handlers
7003 switch ( event
.GetKeyCode() )
7006 if ( event
.ControlDown() )
7007 MoveCursorUpBlock( event
.ShiftDown() );
7009 MoveCursorUp( event
.ShiftDown() );
7013 if ( event
.ControlDown() )
7014 MoveCursorDownBlock( event
.ShiftDown() );
7016 MoveCursorDown( event
.ShiftDown() );
7020 if ( event
.ControlDown() )
7021 MoveCursorLeftBlock( event
.ShiftDown() );
7023 MoveCursorLeft( event
.ShiftDown() );
7027 if ( event
.ControlDown() )
7028 MoveCursorRightBlock( event
.ShiftDown() );
7030 MoveCursorRight( event
.ShiftDown() );
7034 case WXK_NUMPAD_ENTER
:
7035 if ( event
.ControlDown() )
7037 event
.Skip(); // to let the edit control have the return
7041 if ( GetGridCursorRow() < GetNumberRows()-1 )
7043 MoveCursorDown( event
.ShiftDown() );
7047 // at the bottom of a column
7048 DisableCellEditControl();
7058 if (event
.ShiftDown())
7060 if ( GetGridCursorCol() > 0 )
7062 MoveCursorLeft( false );
7067 DisableCellEditControl();
7072 if ( GetGridCursorCol() < GetNumberCols() - 1 )
7074 MoveCursorRight( false );
7079 DisableCellEditControl();
7085 if ( event
.ControlDown() )
7087 MakeCellVisible( 0, 0 );
7088 SetCurrentCell( 0, 0 );
7097 if ( event
.ControlDown() )
7099 MakeCellVisible( m_numRows
- 1, m_numCols
- 1 );
7100 SetCurrentCell( m_numRows
- 1, m_numCols
- 1 );
7117 if ( event
.ControlDown() )
7121 m_selection
->ToggleCellSelection(
7122 m_currentCellCoords
.GetRow(),
7123 m_currentCellCoords
.GetCol(),
7124 event
.ControlDown(),
7132 if ( !IsEditable() )
7133 MoveCursorRight( false );
7144 m_inOnKeyDown
= false;
7147 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
7149 // try local handlers
7151 if ( event
.GetKeyCode() == WXK_SHIFT
)
7153 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
7154 m_selectingBottomRight
!= wxGridNoCellCoords
)
7158 m_selection
->SelectBlock(
7159 m_selectingTopLeft
.GetRow(),
7160 m_selectingTopLeft
.GetCol(),
7161 m_selectingBottomRight
.GetRow(),
7162 m_selectingBottomRight
.GetCol(),
7163 event
.ControlDown(),
7170 m_selectingTopLeft
= wxGridNoCellCoords
;
7171 m_selectingBottomRight
= wxGridNoCellCoords
;
7172 m_selectingKeyboard
= wxGridNoCellCoords
;
7176 void wxGrid::OnChar( wxKeyEvent
& event
)
7178 // is it possible to edit the current cell at all?
7179 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7181 // yes, now check whether the cells editor accepts the key
7182 int row
= m_currentCellCoords
.GetRow();
7183 int col
= m_currentCellCoords
.GetCol();
7184 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7185 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7187 // <F2> is special and will always start editing, for
7188 // other keys - ask the editor itself
7189 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
7190 || editor
->IsAcceptedKey(event
) )
7192 // ensure cell is visble
7193 MakeCellVisible(row
, col
);
7194 EnableCellEditControl();
7196 // a problem can arise if the cell is not completely
7197 // visible (even after calling MakeCellVisible the
7198 // control is not created and calling StartingKey will
7200 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
7201 editor
->StartingKey(event
);
7217 void wxGrid::OnEraseBackground(wxEraseEvent
&)
7221 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
7223 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
7225 // the event has been intercepted - do nothing
7229 #if !defined(__WXMAC__)
7230 wxClientDC
dc( m_gridWin
);
7234 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
7236 DisableCellEditControl();
7238 if ( IsVisible( m_currentCellCoords
, false ) )
7241 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
7242 if ( !m_gridLinesEnabled
)
7250 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
7252 // Otherwise refresh redraws the highlight!
7253 m_currentCellCoords
= coords
;
7255 #if defined(__WXMAC__)
7256 m_gridWin
->Refresh(true /*, & r */);
7258 DrawGridCellArea( dc
, cells
);
7259 DrawAllGridLines( dc
, r
);
7264 m_currentCellCoords
= coords
;
7266 wxGridCellAttr
*attr
= GetCellAttr( coords
);
7267 #if !defined(__WXMAC__)
7268 DrawCellHighlight( dc
, attr
);
7273 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7276 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7280 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
7283 rightCol
= GetNumberCols() - 1;
7285 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
7288 bottomRow
= GetNumberRows() - 1;
7292 if ( topRow
> bottomRow
)
7299 if ( leftCol
> rightCol
)
7306 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7307 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7309 // First the case that we selected a completely new area
7310 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
7311 m_selectingBottomRight
== wxGridNoCellCoords
)
7314 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
7315 wxGridCellCoords ( bottomRow
, rightCol
) );
7316 m_gridWin
->Refresh( false, &rect
);
7319 // Now handle changing an existing selection area.
7320 else if ( m_selectingTopLeft
!= updateTopLeft
||
7321 m_selectingBottomRight
!= updateBottomRight
)
7323 // Compute two optimal update rectangles:
7324 // Either one rectangle is a real subset of the
7325 // other, or they are (almost) disjoint!
7327 bool need_refresh
[4];
7331 need_refresh
[3] = false;
7334 // Store intermediate values
7335 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
7336 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
7337 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
7338 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
7340 // Determine the outer/inner coordinates.
7341 if (oldLeft
> leftCol
)
7347 if (oldTop
> topRow
)
7353 if (oldRight
< rightCol
)
7356 oldRight
= rightCol
;
7359 if (oldBottom
< bottomRow
)
7362 oldBottom
= bottomRow
;
7366 // Now, either the stuff marked old is the outer
7367 // rectangle or we don't have a situation where one
7368 // is contained in the other.
7370 if ( oldLeft
< leftCol
)
7372 // Refresh the newly selected or deselected
7373 // area to the left of the old or new selection.
7374 need_refresh
[0] = true;
7375 rect
[0] = BlockToDeviceRect(
7376 wxGridCellCoords( oldTop
, oldLeft
),
7377 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
7380 if ( oldTop
< topRow
)
7382 // Refresh the newly selected or deselected
7383 // area above the old or new selection.
7384 need_refresh
[1] = true;
7385 rect
[1] = BlockToDeviceRect(
7386 wxGridCellCoords( oldTop
, leftCol
),
7387 wxGridCellCoords( topRow
- 1, rightCol
) );
7390 if ( oldRight
> rightCol
)
7392 // Refresh the newly selected or deselected
7393 // area to the right of the old or new selection.
7394 need_refresh
[2] = true;
7395 rect
[2] = BlockToDeviceRect(
7396 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
7397 wxGridCellCoords( oldBottom
, oldRight
) );
7400 if ( oldBottom
> bottomRow
)
7402 // Refresh the newly selected or deselected
7403 // area below the old or new selection.
7404 need_refresh
[3] = true;
7405 rect
[3] = BlockToDeviceRect(
7406 wxGridCellCoords( bottomRow
+ 1, leftCol
),
7407 wxGridCellCoords( oldBottom
, rightCol
) );
7410 // various Refresh() calls
7411 for (i
= 0; i
< 4; i
++ )
7412 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7413 m_gridWin
->Refresh( false, &(rect
[i
]) );
7417 m_selectingTopLeft
= updateTopLeft
;
7418 m_selectingBottomRight
= updateBottomRight
;
7422 // ------ functions to get/send data (see also public functions)
7425 bool wxGrid::GetModelValues()
7427 // Hide the editor, so it won't hide a changed value.
7428 HideCellEditControl();
7432 // all we need to do is repaint the grid
7434 m_gridWin
->Refresh();
7441 bool wxGrid::SetModelValues()
7445 // Disable the editor, so it won't hide a changed value.
7446 // Do we also want to save the current value of the editor first?
7448 DisableCellEditControl();
7452 for ( row
= 0; row
< m_numRows
; row
++ )
7454 for ( col
= 0; col
< m_numCols
; col
++ )
7456 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
7466 // Note - this function only draws cells that are in the list of
7467 // exposed cells (usually set from the update region by
7468 // CalcExposedCells)
7470 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7472 if ( !m_numRows
|| !m_numCols
)
7475 int i
, numCells
= cells
.GetCount();
7476 int row
, col
, cell_rows
, cell_cols
;
7477 wxGridCellCoordsArray redrawCells
;
7479 for ( i
= numCells
- 1; i
>= 0; i
-- )
7481 row
= cells
[i
].GetRow();
7482 col
= cells
[i
].GetCol();
7483 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7485 // If this cell is part of a multicell block, find owner for repaint
7486 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7488 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
7489 bool marked
= false;
7490 for ( int j
= 0; j
< numCells
; j
++ )
7492 if ( cell
== cells
[j
] )
7501 int count
= redrawCells
.GetCount();
7502 for (int j
= 0; j
< count
; j
++)
7504 if ( cell
== redrawCells
[j
] )
7512 redrawCells
.Add( cell
);
7515 // don't bother drawing this cell
7519 // If this cell is empty, find cell to left that might want to overflow
7520 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7522 for ( int l
= 0; l
< cell_rows
; l
++ )
7524 // find a cell in this row to leave already marked for repaint
7526 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7527 if ((redrawCells
[k
].GetCol() < left
) &&
7528 (redrawCells
[k
].GetRow() == row
))
7530 left
= redrawCells
[k
].GetCol();
7534 left
= 0; // oh well
7536 for (int j
= col
- 1; j
>= left
; j
--)
7538 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7540 if (GetCellOverflow(row
+ l
, j
))
7542 wxGridCellCoords
cell(row
+ l
, j
);
7543 bool marked
= false;
7545 for (int k
= 0; k
< numCells
; k
++)
7547 if ( cell
== cells
[k
] )
7556 int count
= redrawCells
.GetCount();
7557 for (int k
= 0; k
< count
; k
++)
7559 if ( cell
== redrawCells
[k
] )
7566 redrawCells
.Add( cell
);
7575 DrawCell( dc
, cells
[i
] );
7578 numCells
= redrawCells
.GetCount();
7580 for ( i
= numCells
- 1; i
>= 0; i
-- )
7582 DrawCell( dc
, redrawCells
[i
] );
7586 void wxGrid::DrawGridSpace( wxDC
& dc
)
7589 m_gridWin
->GetClientSize( &cw
, &ch
);
7592 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7594 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
7595 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7597 if ( right
> rightCol
|| bottom
> bottomRow
)
7600 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7602 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxBRUSHSTYLE_SOLID
) );
7603 dc
.SetPen( *wxTRANSPARENT_PEN
);
7605 if ( right
> rightCol
)
7607 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7610 if ( bottom
> bottomRow
)
7612 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7617 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7619 int row
= coords
.GetRow();
7620 int col
= coords
.GetCol();
7622 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7625 // we draw the cell border ourselves
7626 #if !WXGRID_DRAW_LINES
7627 if ( m_gridLinesEnabled
)
7628 DrawCellBorder( dc
, coords
);
7631 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7633 bool isCurrent
= coords
== m_currentCellCoords
;
7635 wxRect rect
= CellToRect( row
, col
);
7637 // if the editor is shown, we should use it and not the renderer
7638 // Note: However, only if it is really _shown_, i.e. not hidden!
7639 if ( isCurrent
&& IsCellEditControlShown() )
7641 // NB: this "#if..." is temporary and fixes a problem where the
7642 // edit control is erased by this code after being rendered.
7643 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7644 // implicitly, causing this out-of order render.
7645 #if !defined(__WXMAC__)
7646 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7647 editor
->PaintBackground(rect
, attr
);
7653 // but all the rest is drawn by the cell renderer and hence may be customized
7654 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7655 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7662 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7664 // don't show highlight when the grid doesn't have focus
7668 int row
= m_currentCellCoords
.GetRow();
7669 int col
= m_currentCellCoords
.GetCol();
7671 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7674 wxRect rect
= CellToRect(row
, col
);
7676 // hmmm... what could we do here to show that the cell is disabled?
7677 // for now, I just draw a thinner border than for the other ones, but
7678 // it doesn't look really good
7680 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7684 // The center of the drawn line is where the position/width/height of
7685 // the rectangle is actually at (on wxMSW at least), so the
7686 // size of the rectangle is reduced to compensate for the thickness of
7687 // the line. If this is too strange on non-wxMSW platforms then
7688 // please #ifdef this appropriately.
7689 rect
.x
+= penWidth
/ 2;
7690 rect
.y
+= penWidth
/ 2;
7691 rect
.width
-= penWidth
- 1;
7692 rect
.height
-= penWidth
- 1;
7694 // Now draw the rectangle
7695 // use the cellHighlightColour if the cell is inside a selection, this
7696 // will ensure the cell is always visible.
7697 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
: m_cellHighlightColour
, penWidth
, wxPENSTYLE_SOLID
));
7698 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7699 dc
.DrawRectangle(rect
);
7703 // VZ: my experiments with 3D borders...
7705 // how to properly set colours for arbitrary bg?
7706 wxCoord x1
= rect
.x
,
7708 x2
= rect
.x
+ rect
.width
- 1,
7709 y2
= rect
.y
+ rect
.height
- 1;
7711 dc
.SetPen(*wxWHITE_PEN
);
7712 dc
.DrawLine(x1
, y1
, x2
, y1
);
7713 dc
.DrawLine(x1
, y1
, x1
, y2
);
7715 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7716 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7718 dc
.SetPen(*wxBLACK_PEN
);
7719 dc
.DrawLine(x1
, y2
, x2
, y2
);
7720 dc
.DrawLine(x2
, y1
, x2
, y2
+ 1);
7724 wxPen
wxGrid::GetDefaultGridLinePen()
7726 return wxPen(GetGridLineColour(), 1, wxPENSTYLE_SOLID
);
7729 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
7731 return GetDefaultGridLinePen();
7734 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
7736 return GetDefaultGridLinePen();
7739 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7741 int row
= coords
.GetRow();
7742 int col
= coords
.GetCol();
7743 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7747 wxRect rect
= CellToRect( row
, col
);
7749 // right hand border
7750 dc
.SetPen( GetColGridLinePen(col
) );
7751 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7752 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7755 dc
.SetPen( GetRowGridLinePen(row
) );
7756 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7757 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7760 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7762 // This if block was previously in wxGrid::OnPaint but that doesn't
7763 // seem to get called under wxGTK - MB
7765 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7766 m_numRows
&& m_numCols
)
7768 m_currentCellCoords
.Set(0, 0);
7771 if ( IsCellEditControlShown() )
7773 // don't show highlight when the edit control is shown
7777 // if the active cell was repainted, repaint its highlight too because it
7778 // might have been damaged by the grid lines
7779 size_t count
= cells
.GetCount();
7780 for ( size_t n
= 0; n
< count
; n
++ )
7782 if ( cells
[n
] == m_currentCellCoords
)
7784 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7785 DrawCellHighlight(dc
, attr
);
7793 // TODO: remove this ???
7794 // This is used to redraw all grid lines e.g. when the grid line colour
7797 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7799 #if !WXGRID_DRAW_LINES
7803 if ( !m_gridLinesEnabled
|| !m_numRows
|| !m_numCols
)
7806 int top
, bottom
, left
, right
;
7808 #if 0 //#ifndef __WXGTK__
7812 m_gridWin
->GetClientSize(&cw
, &ch
);
7814 // virtual coords of visible area
7816 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7817 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7822 reg
.GetBox(x
, y
, w
, h
);
7823 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7824 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7828 m_gridWin
->GetClientSize(&cw
, &ch
);
7829 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7830 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7833 // avoid drawing grid lines past the last row and col
7835 right
= wxMin( right
, GetColRight(GetColAt( m_numCols
- 1 )) );
7836 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7838 // no gridlines inside multicells, clip them out
7839 int leftCol
= GetColPos( internalXToCol(left
) );
7840 int topRow
= internalYToRow(top
);
7841 int rightCol
= GetColPos( internalXToCol(right
) );
7842 int bottomRow
= internalYToRow(bottom
);
7844 wxRegion
clippedcells(0, 0, cw
, ch
);
7846 int i
, j
, cell_rows
, cell_cols
;
7849 for (j
=topRow
; j
<=bottomRow
; j
++)
7852 for (colPos
=leftCol
; colPos
<=rightCol
; colPos
++)
7854 i
= GetColAt( colPos
);
7856 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7857 if ((cell_rows
> 1) || (cell_cols
> 1))
7859 rect
= CellToRect(j
,i
);
7860 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7861 clippedcells
.Subtract(rect
);
7863 else if ((cell_rows
< 0) || (cell_cols
< 0))
7865 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7866 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7867 clippedcells
.Subtract(rect
);
7872 dc
.SetDeviceClippingRegion( clippedcells
);
7875 // horizontal grid lines
7877 // already declared above - int i;
7878 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7880 int bot
= GetRowBottom(i
) - 1;
7889 dc
.SetPen( GetRowGridLinePen(i
) );
7890 dc
.DrawLine( left
, bot
, right
, bot
);
7894 // vertical grid lines
7897 for ( colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
7899 i
= GetColAt( colPos
);
7901 int colRight
= GetColRight(i
);
7903 if (GetLayoutDirection() != wxLayout_RightToLeft
)
7907 if ( colRight
> right
)
7912 if ( colRight
>= left
)
7914 dc
.SetPen( GetColGridLinePen(i
) );
7915 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7919 dc
.DestroyClippingRegion();
7922 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
7928 size_t numLabels
= rows
.GetCount();
7930 for ( i
= 0; i
< numLabels
; i
++ )
7932 DrawRowLabel( dc
, rows
[i
] );
7936 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7938 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7943 int rowTop
= GetRowTop(row
),
7944 rowBottom
= GetRowBottom(row
) - 1;
7946 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxPENSTYLE_SOLID
) );
7947 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
7948 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7949 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7951 dc
.SetPen( *wxWHITE_PEN
);
7952 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7953 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
7955 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
7956 dc
.SetTextForeground( GetLabelTextColour() );
7957 dc
.SetFont( GetLabelFont() );
7960 GetRowLabelAlignment( &hAlign
, &vAlign
);
7963 rect
.SetY( GetRowTop(row
) + 2 );
7964 rect
.SetWidth( m_rowLabelWidth
- 4 );
7965 rect
.SetHeight( GetRowHeight(row
) - 4 );
7966 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7969 void wxGrid::SetUseNativeColLabels( bool native
)
7971 m_nativeColumnLabels
= native
;
7974 int height
= wxRendererNative::Get().GetHeaderButtonHeight( this );
7975 SetColLabelSize( height
);
7978 m_colLabelWin
->Refresh();
7981 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7987 size_t numLabels
= cols
.GetCount();
7989 for ( i
= 0; i
< numLabels
; i
++ )
7991 DrawColLabel( dc
, cols
[i
] );
7995 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7997 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
8000 int colLeft
= GetColLeft(col
);
8004 if (m_nativeColumnLabels
)
8006 rect
.SetX( colLeft
);
8008 rect
.SetWidth( GetColWidth(col
));
8009 rect
.SetHeight( m_colLabelHeight
);
8011 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
8012 wxRendererNative::Get().DrawHeaderButton( win_dc
->GetWindow(), dc
, rect
, 0 );
8016 int colRight
= GetColRight(col
) - 1;
8018 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxPENSTYLE_SOLID
) );
8019 dc
.DrawLine( colRight
, 0, colRight
, m_colLabelHeight
- 1 );
8020 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
8021 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
8022 colRight
+ 1, m_colLabelHeight
- 1 );
8024 dc
.SetPen( *wxWHITE_PEN
);
8025 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
8026 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
8029 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
8030 dc
.SetTextForeground( GetLabelTextColour() );
8031 dc
.SetFont( GetLabelFont() );
8033 int hAlign
, vAlign
, orient
;
8034 GetColLabelAlignment( &hAlign
, &vAlign
);
8035 orient
= GetColLabelTextOrientation();
8037 rect
.SetX( colLeft
+ 2 );
8039 rect
.SetWidth( GetColWidth(col
) - 4 );
8040 rect
.SetHeight( m_colLabelHeight
- 4 );
8041 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
8044 void wxGrid::DrawTextRectangle( wxDC
& dc
,
8045 const wxString
& value
,
8049 int textOrientation
)
8051 wxArrayString lines
;
8053 StringToLines( value
, lines
);
8055 // Forward to new API.
8056 DrawTextRectangle( dc
,
8064 // VZ: this should be replaced with wxDC::DrawLabel() to which we just have to
8065 // add textOrientation support
8066 void wxGrid::DrawTextRectangle(wxDC
& dc
,
8067 const wxArrayString
& lines
,
8071 int textOrientation
)
8073 if ( lines
.empty() )
8076 wxDCClipper
clip(dc
, rect
);
8081 if ( textOrientation
== wxHORIZONTAL
)
8082 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
8084 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
8088 switch ( vertAlign
)
8090 case wxALIGN_BOTTOM
:
8091 if ( textOrientation
== wxHORIZONTAL
)
8092 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
8094 x
= rect
.x
+ rect
.width
- textWidth
;
8097 case wxALIGN_CENTRE
:
8098 if ( textOrientation
== wxHORIZONTAL
)
8099 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
8101 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
8106 if ( textOrientation
== wxHORIZONTAL
)
8113 // Align each line of a multi-line label
8114 size_t nLines
= lines
.GetCount();
8115 for ( size_t l
= 0; l
< nLines
; l
++ )
8117 const wxString
& line
= lines
[l
];
8121 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
8125 wxCoord lineWidth
= 0,
8127 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
8129 switch ( horizAlign
)
8132 if ( textOrientation
== wxHORIZONTAL
)
8133 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
8135 y
= rect
.y
+ lineWidth
+ 1;
8138 case wxALIGN_CENTRE
:
8139 if ( textOrientation
== wxHORIZONTAL
)
8140 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
8142 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
8147 if ( textOrientation
== wxHORIZONTAL
)
8150 y
= rect
.y
+ rect
.height
- 1;
8154 if ( textOrientation
== wxHORIZONTAL
)
8156 dc
.DrawText( line
, x
, y
);
8161 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
8167 // Split multi-line text up into an array of strings.
8168 // Any existing contents of the string array are preserved.
8170 // TODO: refactor wxTextFile::Read() and reuse the same code from here
8171 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
8175 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
8176 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
8178 while ( startPos
< (int)tVal
.length() )
8180 pos
= tVal
.Mid(startPos
).Find( eol
);
8185 else if ( pos
== 0 )
8187 lines
.Add( wxEmptyString
);
8191 lines
.Add( tVal
.Mid(startPos
, pos
) );
8194 startPos
+= pos
+ 1;
8197 if ( startPos
< (int)tVal
.length() )
8199 lines
.Add( tVal
.Mid( startPos
) );
8203 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
8204 const wxArrayString
& lines
,
8205 long *width
, long *height
) const
8209 wxCoord lineW
= 0, lineH
= 0;
8212 for ( i
= 0; i
< lines
.GetCount(); i
++ )
8214 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
8215 w
= wxMax( w
, lineW
);
8224 // ------ Batch processing.
8226 void wxGrid::EndBatch()
8228 if ( m_batchCount
> 0 )
8231 if ( !m_batchCount
)
8234 m_rowLabelWin
->Refresh();
8235 m_colLabelWin
->Refresh();
8236 m_cornerLabelWin
->Refresh();
8237 m_gridWin
->Refresh();
8242 // Use this, rather than wxWindow::Refresh(), to force an immediate
8243 // repainting of the grid. Has no effect if you are already inside a
8244 // BeginBatch / EndBatch block.
8246 void wxGrid::ForceRefresh()
8252 bool wxGrid::Enable(bool enable
)
8254 if ( !wxScrolledWindow::Enable(enable
) )
8257 // redraw in the new state
8258 m_gridWin
->Refresh();
8264 // ------ Edit control functions
8267 void wxGrid::EnableEditing( bool edit
)
8269 // TODO: improve this ?
8271 if ( edit
!= m_editable
)
8274 EnableCellEditControl(edit
);
8279 void wxGrid::EnableCellEditControl( bool enable
)
8284 if ( enable
!= m_cellEditCtrlEnabled
)
8288 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
8291 // this should be checked by the caller!
8292 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8294 // do it before ShowCellEditControl()
8295 m_cellEditCtrlEnabled
= enable
;
8297 ShowCellEditControl();
8301 //FIXME:add veto support
8302 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
8304 HideCellEditControl();
8305 SaveEditControlValue();
8307 // do it after HideCellEditControl()
8308 m_cellEditCtrlEnabled
= enable
;
8313 bool wxGrid::IsCurrentCellReadOnly() const
8316 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
8317 bool readonly
= attr
->IsReadOnly();
8323 bool wxGrid::CanEnableCellControl() const
8325 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
8326 !IsCurrentCellReadOnly();
8329 bool wxGrid::IsCellEditControlEnabled() const
8331 // the cell edit control might be disable for all cells or just for the
8332 // current one if it's read only
8333 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
8336 bool wxGrid::IsCellEditControlShown() const
8338 bool isShown
= false;
8340 if ( m_cellEditCtrlEnabled
)
8342 int row
= m_currentCellCoords
.GetRow();
8343 int col
= m_currentCellCoords
.GetCol();
8344 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8345 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
8350 if ( editor
->IsCreated() )
8352 isShown
= editor
->GetControl()->IsShown();
8362 void wxGrid::ShowCellEditControl()
8364 if ( IsCellEditControlEnabled() )
8366 if ( !IsVisible( m_currentCellCoords
, false ) )
8368 m_cellEditCtrlEnabled
= false;
8373 wxRect rect
= CellToRect( m_currentCellCoords
);
8374 int row
= m_currentCellCoords
.GetRow();
8375 int col
= m_currentCellCoords
.GetCol();
8377 // if this is part of a multicell, find owner (topleft)
8378 int cell_rows
, cell_cols
;
8379 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8380 if ( cell_rows
<= 0 || cell_cols
<= 0 )
8384 m_currentCellCoords
.SetRow( row
);
8385 m_currentCellCoords
.SetCol( col
);
8388 // erase the highlight and the cell contents because the editor
8389 // might not cover the entire cell
8390 wxClientDC
dc( m_gridWin
);
8392 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8393 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxBRUSHSTYLE_SOLID
));
8394 dc
.SetPen(*wxTRANSPARENT_PEN
);
8395 dc
.DrawRectangle(rect
);
8397 // convert to scrolled coords
8398 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8404 // cell is shifted by one pixel
8405 // However, don't allow x or y to become negative
8406 // since the SetSize() method interprets that as
8413 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8414 if ( !editor
->IsCreated() )
8416 editor
->Create(m_gridWin
, wxID_ANY
,
8417 new wxGridCellEditorEvtHandler(this, editor
));
8419 wxGridEditorCreatedEvent
evt(GetId(),
8420 wxEVT_GRID_EDITOR_CREATED
,
8424 editor
->GetControl());
8425 GetEventHandler()->ProcessEvent(evt
);
8428 // resize editor to overflow into righthand cells if allowed
8429 int maxWidth
= rect
.width
;
8430 wxString value
= GetCellValue(row
, col
);
8431 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
8434 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
8435 if (maxWidth
< rect
.width
)
8436 maxWidth
= rect
.width
;
8439 int client_right
= m_gridWin
->GetClientSize().GetWidth();
8440 if (rect
.x
+ maxWidth
> client_right
)
8441 maxWidth
= client_right
- rect
.x
;
8443 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
8445 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8446 // may have changed earlier
8447 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
8450 GetCellSize( row
, i
, &c_rows
, &c_cols
);
8452 // looks weird going over a multicell
8453 if (m_table
->IsEmptyCell( row
, i
) &&
8454 (rect
.width
< maxWidth
) && (c_rows
== 1))
8456 rect
.width
+= GetColWidth( i
);
8462 if (rect
.GetRight() > client_right
)
8463 rect
.SetRight( client_right
- 1 );
8466 editor
->SetCellAttr( attr
);
8467 editor
->SetSize( rect
);
8469 editor
->GetControl()->Move(
8470 editor
->GetControl()->GetPosition().x
+ nXMove
,
8471 editor
->GetControl()->GetPosition().y
);
8472 editor
->Show( true, attr
);
8474 // recalc dimensions in case we need to
8475 // expand the scrolled window to account for editor
8478 editor
->BeginEdit(row
, col
, this);
8479 editor
->SetCellAttr(NULL
);
8487 void wxGrid::HideCellEditControl()
8489 if ( IsCellEditControlEnabled() )
8491 int row
= m_currentCellCoords
.GetRow();
8492 int col
= m_currentCellCoords
.GetCol();
8494 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8495 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
8496 editor
->Show( false );
8500 m_gridWin
->SetFocus();
8502 // refresh whole row to the right
8503 wxRect
rect( CellToRect(row
, col
) );
8504 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8505 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8508 // ensure that the pixels under the focus ring get refreshed as well
8509 rect
.Inflate(10, 10);
8512 m_gridWin
->Refresh( false, &rect
);
8516 void wxGrid::SaveEditControlValue()
8518 if ( IsCellEditControlEnabled() )
8520 int row
= m_currentCellCoords
.GetRow();
8521 int col
= m_currentCellCoords
.GetCol();
8523 wxString oldval
= GetCellValue(row
, col
);
8525 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8526 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8527 bool changed
= editor
->EndEdit(row
, col
, this);
8534 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
8535 m_currentCellCoords
.GetRow(),
8536 m_currentCellCoords
.GetCol() ) < 0 )
8538 // Event has been vetoed, set the data back.
8539 SetCellValue(row
, col
, oldval
);
8546 // ------ Grid location functions
8547 // Note that all of these functions work with the logical coordinates of
8548 // grid cells and labels so you will need to convert from device
8549 // coordinates for mouse events etc.
8552 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
) const
8554 int row
= YToRow(y
);
8555 int col
= XToCol(x
);
8557 if ( row
== -1 || col
== -1 )
8559 coords
= wxGridNoCellCoords
;
8563 coords
.Set( row
, col
);
8567 // Internal Helper function for computing row or column from some
8568 // (unscrolled) coordinate value, using either
8569 // m_defaultRowHeight/m_defaultColWidth or binary search on array
8570 // of m_rowBottoms/m_ColRights to speed up the search!
8572 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
8573 const wxArrayInt
& BorderArray
, int nMax
,
8577 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
8582 size_t i_max
= coord
/ defaultDist
,
8585 if (BorderArray
.IsEmpty())
8587 if ((int) i_max
< nMax
)
8589 return clipToMinMax
? nMax
- 1 : -1;
8592 if ( i_max
>= BorderArray
.GetCount())
8594 i_max
= BorderArray
.GetCount() - 1;
8598 if ( coord
>= BorderArray
[i_max
])
8602 i_max
= coord
/ minDist
;
8604 i_max
= BorderArray
.GetCount() - 1;
8607 if ( i_max
>= BorderArray
.GetCount())
8608 i_max
= BorderArray
.GetCount() - 1;
8611 if ( coord
>= BorderArray
[i_max
])
8612 return clipToMinMax
? (int)i_max
: -1;
8613 if ( coord
< BorderArray
[0] )
8616 while ( i_max
- i_min
> 0 )
8618 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
8619 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8620 if (coord
>= BorderArray
[ i_max
- 1])
8624 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
8625 if (coord
< BorderArray
[median
])
8634 int wxGrid::YToRow( int y
) const
8636 return CoordToRowOrCol(y
, m_defaultRowHeight
,
8637 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8640 int wxGrid::XToCol( int x
, bool clipToMinMax
) const
8643 return clipToMinMax
&& (m_numCols
> 0) ? GetColAt( 0 ) : -1;
8645 wxASSERT_MSG(m_defaultColWidth
> 0, wxT("Default column width can not be zero"));
8647 int maxPos
= x
/ m_defaultColWidth
;
8650 if (m_colRights
.IsEmpty())
8652 if(maxPos
< m_numCols
)
8653 return GetColAt( maxPos
);
8654 return clipToMinMax
? GetColAt( m_numCols
- 1 ) : -1;
8657 if ( maxPos
>= m_numCols
)
8658 maxPos
= m_numCols
- 1;
8661 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8664 if (m_minAcceptableColWidth
)
8665 maxPos
= x
/ m_minAcceptableColWidth
;
8667 maxPos
= m_numCols
- 1;
8669 if ( maxPos
>= m_numCols
)
8670 maxPos
= m_numCols
- 1;
8673 //X is beyond the last column
8674 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8675 return clipToMinMax
? GetColAt( maxPos
) : -1;
8677 //X is before the first column
8678 if ( x
< m_colRights
[GetColAt( 0 )] )
8679 return GetColAt( 0 );
8681 //Perform a binary search
8682 while ( maxPos
- minPos
> 0 )
8684 wxCHECK_MSG(m_colRights
[GetColAt( minPos
)] <= x
&& x
< m_colRights
[GetColAt( maxPos
)],
8685 0, _T("wxGrid: internal error in XToCol"));
8687 if (x
>= m_colRights
[GetColAt( maxPos
- 1 )])
8688 return GetColAt( maxPos
);
8691 int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
8692 if (x
< m_colRights
[GetColAt( median
)])
8697 return GetColAt( maxPos
);
8700 // return the row number that that the y coord is near
8701 // the edge of, or -1 if not near an edge.
8702 // coords can only possibly be near an edge if
8703 // (a) the row/column is large enough to still allow for an "inner" area
8704 // that is _not_ nead the edge (i.e., if the height/width is smaller
8705 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8708 // (b) resizing rows/columns (the thing for which edge detection is
8709 // relevant at all) is enabled.
8711 int wxGrid::YToEdgeOfRow( int y
) const
8714 i
= internalYToRow(y
);
8716 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragRowSize() )
8718 // We know that we are in row i, test whether we are
8719 // close enough to lower or upper border, respectively.
8720 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8722 else if ( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8729 // return the col number that that the x coord is near the edge of, or
8730 // -1 if not near an edge
8731 // See comment at YToEdgeOfRow for conditions on edge detection.
8733 int wxGrid::XToEdgeOfCol( int x
) const
8736 i
= internalXToCol(x
);
8738 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragColSize() )
8740 // We know that we are in column i; test whether we are
8741 // close enough to right or left border, respectively.
8742 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8744 else if ( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8751 wxRect
wxGrid::CellToRect( int row
, int col
) const
8753 wxRect
rect( -1, -1, -1, -1 );
8755 if ( row
>= 0 && row
< m_numRows
&&
8756 col
>= 0 && col
< m_numCols
)
8758 int i
, cell_rows
, cell_cols
;
8759 rect
.width
= rect
.height
= 0;
8760 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8761 // if negative then find multicell owner
8766 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8768 rect
.x
= GetColLeft(col
);
8769 rect
.y
= GetRowTop(row
);
8770 for (i
=col
; i
< col
+ cell_cols
; i
++)
8771 rect
.width
+= GetColWidth(i
);
8772 for (i
=row
; i
< row
+ cell_rows
; i
++)
8773 rect
.height
+= GetRowHeight(i
);
8776 // if grid lines are enabled, then the area of the cell is a bit smaller
8777 if (m_gridLinesEnabled
)
8786 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
8788 // get the cell rectangle in logical coords
8790 wxRect
r( CellToRect( row
, col
) );
8792 // convert to device coords
8794 int left
, top
, right
, bottom
;
8795 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8796 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8798 // check against the client area of the grid window
8800 m_gridWin
->GetClientSize( &cw
, &ch
);
8802 if ( wholeCellVisible
)
8804 // is the cell wholly visible ?
8805 return ( left
>= 0 && right
<= cw
&&
8806 top
>= 0 && bottom
<= ch
);
8810 // is the cell partly visible ?
8812 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8813 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8817 // make the specified cell location visible by doing a minimal amount
8820 void wxGrid::MakeCellVisible( int row
, int col
)
8823 int xpos
= -1, ypos
= -1;
8825 if ( row
>= 0 && row
< m_numRows
&&
8826 col
>= 0 && col
< m_numCols
)
8828 // get the cell rectangle in logical coords
8829 wxRect
r( CellToRect( row
, col
) );
8831 // convert to device coords
8832 int left
, top
, right
, bottom
;
8833 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8834 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8837 m_gridWin
->GetClientSize( &cw
, &ch
);
8843 else if ( bottom
> ch
)
8845 int h
= r
.GetHeight();
8847 for ( i
= row
- 1; i
>= 0; i
-- )
8849 int rowHeight
= GetRowHeight(i
);
8850 if ( h
+ rowHeight
> ch
)
8857 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8858 // have rounding errors (this is important, because if we do,
8859 // we might not scroll at all and some cells won't be redrawn)
8861 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
8862 // so just add a full scroll unit...
8863 ypos
+= m_scrollLineY
;
8866 // special handling for wide cells - show always left part of the cell!
8867 // Otherwise, e.g. when stepping from row to row, it would jump between
8868 // left and right part of the cell on every step!
8870 if ( left
< 0 || (right
- left
) >= cw
)
8874 else if ( right
> cw
)
8876 // position the view so that the cell is on the right
8878 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8879 xpos
= x0
+ (right
- cw
);
8881 // see comment for ypos above
8882 xpos
+= m_scrollLineX
;
8885 if ( xpos
!= -1 || ypos
!= -1 )
8888 xpos
/= m_scrollLineX
;
8890 ypos
/= m_scrollLineY
;
8891 Scroll( xpos
, ypos
);
8898 // ------ Grid cursor movement functions
8901 bool wxGrid::MoveCursorUp( bool expandSelection
)
8903 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8904 m_currentCellCoords
.GetRow() >= 0 )
8906 if ( expandSelection
)
8908 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8909 m_selectingKeyboard
= m_currentCellCoords
;
8910 if ( m_selectingKeyboard
.GetRow() > 0 )
8912 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8913 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8914 m_selectingKeyboard
.GetCol() );
8915 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8918 else if ( m_currentCellCoords
.GetRow() > 0 )
8920 int row
= m_currentCellCoords
.GetRow() - 1;
8921 int col
= m_currentCellCoords
.GetCol();
8923 MakeCellVisible( row
, col
);
8924 SetCurrentCell( row
, col
);
8935 bool wxGrid::MoveCursorDown( bool expandSelection
)
8937 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8938 m_currentCellCoords
.GetRow() < m_numRows
)
8940 if ( expandSelection
)
8942 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8943 m_selectingKeyboard
= m_currentCellCoords
;
8944 if ( m_selectingKeyboard
.GetRow() < m_numRows
- 1 )
8946 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8947 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8948 m_selectingKeyboard
.GetCol() );
8949 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8952 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8954 int row
= m_currentCellCoords
.GetRow() + 1;
8955 int col
= m_currentCellCoords
.GetCol();
8957 MakeCellVisible( row
, col
);
8958 SetCurrentCell( row
, col
);
8969 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8971 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8972 m_currentCellCoords
.GetCol() >= 0 )
8974 if ( expandSelection
)
8976 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8977 m_selectingKeyboard
= m_currentCellCoords
;
8978 if ( m_selectingKeyboard
.GetCol() > 0 )
8980 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8981 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8982 m_selectingKeyboard
.GetCol() );
8983 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8986 else if ( GetColPos( m_currentCellCoords
.GetCol() ) > 0 )
8988 int row
= m_currentCellCoords
.GetRow();
8989 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) - 1 );
8992 MakeCellVisible( row
, col
);
8993 SetCurrentCell( row
, col
);
9004 bool wxGrid::MoveCursorRight( bool expandSelection
)
9006 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
9007 m_currentCellCoords
.GetCol() < m_numCols
)
9009 if ( expandSelection
)
9011 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
9012 m_selectingKeyboard
= m_currentCellCoords
;
9013 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
9015 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
9016 MakeCellVisible( m_selectingKeyboard
.GetRow(),
9017 m_selectingKeyboard
.GetCol() );
9018 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9021 else if ( GetColPos( m_currentCellCoords
.GetCol() ) < m_numCols
- 1 )
9023 int row
= m_currentCellCoords
.GetRow();
9024 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) + 1 );
9027 MakeCellVisible( row
, col
);
9028 SetCurrentCell( row
, col
);
9039 bool wxGrid::MovePageUp()
9041 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9044 int row
= m_currentCellCoords
.GetRow();
9048 m_gridWin
->GetClientSize( &cw
, &ch
);
9050 int y
= GetRowTop(row
);
9051 int newRow
= internalYToRow( y
- ch
+ 1 );
9053 if ( newRow
== row
)
9055 // row > 0, so newRow can never be less than 0 here.
9059 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
9060 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
9068 bool wxGrid::MovePageDown()
9070 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9073 int row
= m_currentCellCoords
.GetRow();
9074 if ( (row
+ 1) < m_numRows
)
9077 m_gridWin
->GetClientSize( &cw
, &ch
);
9079 int y
= GetRowTop(row
);
9080 int newRow
= internalYToRow( y
+ ch
);
9081 if ( newRow
== row
)
9083 // row < m_numRows, so newRow can't overflow here.
9087 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
9088 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
9096 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
9099 m_currentCellCoords
!= wxGridNoCellCoords
&&
9100 m_currentCellCoords
.GetRow() > 0 )
9102 int row
= m_currentCellCoords
.GetRow();
9103 int col
= m_currentCellCoords
.GetCol();
9105 if ( m_table
->IsEmptyCell(row
, col
) )
9107 // starting in an empty cell: find the next block of
9113 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9117 else if ( m_table
->IsEmptyCell(row
- 1, col
) )
9119 // starting at the top of a block: find the next block
9125 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9131 // starting within a block: find the top of the block
9136 if ( m_table
->IsEmptyCell(row
, col
) )
9144 MakeCellVisible( row
, col
);
9145 if ( expandSelection
)
9147 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9148 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9153 SetCurrentCell( row
, col
);
9162 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
9165 m_currentCellCoords
!= wxGridNoCellCoords
&&
9166 m_currentCellCoords
.GetRow() < m_numRows
- 1 )
9168 int row
= m_currentCellCoords
.GetRow();
9169 int col
= m_currentCellCoords
.GetCol();
9171 if ( m_table
->IsEmptyCell(row
, col
) )
9173 // starting in an empty cell: find the next block of
9176 while ( row
< m_numRows
- 1 )
9179 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9183 else if ( m_table
->IsEmptyCell(row
+ 1, col
) )
9185 // starting at the bottom of a block: find the next block
9188 while ( row
< m_numRows
- 1 )
9191 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9197 // starting within a block: find the bottom of the block
9199 while ( row
< m_numRows
- 1 )
9202 if ( m_table
->IsEmptyCell(row
, col
) )
9210 MakeCellVisible( row
, col
);
9211 if ( expandSelection
)
9213 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9214 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9219 SetCurrentCell( row
, col
);
9228 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
9231 m_currentCellCoords
!= wxGridNoCellCoords
&&
9232 m_currentCellCoords
.GetCol() > 0 )
9234 int row
= m_currentCellCoords
.GetRow();
9235 int col
= m_currentCellCoords
.GetCol();
9237 if ( m_table
->IsEmptyCell(row
, col
) )
9239 // starting in an empty cell: find the next block of
9245 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9249 else if ( m_table
->IsEmptyCell(row
, col
- 1) )
9251 // starting at the left of a block: find the next block
9257 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9263 // starting within a block: find the left of the block
9268 if ( m_table
->IsEmptyCell(row
, col
) )
9276 MakeCellVisible( row
, col
);
9277 if ( expandSelection
)
9279 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9280 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9285 SetCurrentCell( row
, col
);
9294 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
9297 m_currentCellCoords
!= wxGridNoCellCoords
&&
9298 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
9300 int row
= m_currentCellCoords
.GetRow();
9301 int col
= m_currentCellCoords
.GetCol();
9303 if ( m_table
->IsEmptyCell(row
, col
) )
9305 // starting in an empty cell: find the next block of
9308 while ( col
< m_numCols
- 1 )
9311 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9315 else if ( m_table
->IsEmptyCell(row
, col
+ 1) )
9317 // starting at the right of a block: find the next block
9320 while ( col
< m_numCols
- 1 )
9323 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9329 // starting within a block: find the right of the block
9331 while ( col
< m_numCols
- 1 )
9334 if ( m_table
->IsEmptyCell(row
, col
) )
9342 MakeCellVisible( row
, col
);
9343 if ( expandSelection
)
9345 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9346 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9351 SetCurrentCell( row
, col
);
9361 // ------ Label values and formatting
9364 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
9367 *horiz
= m_rowLabelHorizAlign
;
9369 *vert
= m_rowLabelVertAlign
;
9372 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
9375 *horiz
= m_colLabelHorizAlign
;
9377 *vert
= m_colLabelVertAlign
;
9380 int wxGrid::GetColLabelTextOrientation() const
9382 return m_colLabelTextOrientation
;
9385 wxString
wxGrid::GetRowLabelValue( int row
) const
9389 return m_table
->GetRowLabelValue( row
);
9399 wxString
wxGrid::GetColLabelValue( int col
) const
9403 return m_table
->GetColLabelValue( col
);
9413 void wxGrid::SetRowLabelSize( int width
)
9415 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
9417 if ( width
== wxGRID_AUTOSIZE
)
9419 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
9422 if ( width
!= m_rowLabelWidth
)
9426 m_rowLabelWin
->Show( false );
9427 m_cornerLabelWin
->Show( false );
9429 else if ( m_rowLabelWidth
== 0 )
9431 m_rowLabelWin
->Show( true );
9432 if ( m_colLabelHeight
> 0 )
9433 m_cornerLabelWin
->Show( true );
9436 m_rowLabelWidth
= width
;
9438 wxScrolledWindow::Refresh( true );
9442 void wxGrid::SetColLabelSize( int height
)
9444 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
9446 if ( height
== wxGRID_AUTOSIZE
)
9448 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
9451 if ( height
!= m_colLabelHeight
)
9455 m_colLabelWin
->Show( false );
9456 m_cornerLabelWin
->Show( false );
9458 else if ( m_colLabelHeight
== 0 )
9460 m_colLabelWin
->Show( true );
9461 if ( m_rowLabelWidth
> 0 )
9462 m_cornerLabelWin
->Show( true );
9465 m_colLabelHeight
= height
;
9467 wxScrolledWindow::Refresh( true );
9471 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
9473 if ( m_labelBackgroundColour
!= colour
)
9475 m_labelBackgroundColour
= colour
;
9476 m_rowLabelWin
->SetBackgroundColour( colour
);
9477 m_colLabelWin
->SetBackgroundColour( colour
);
9478 m_cornerLabelWin
->SetBackgroundColour( colour
);
9480 if ( !GetBatchCount() )
9482 m_rowLabelWin
->Refresh();
9483 m_colLabelWin
->Refresh();
9484 m_cornerLabelWin
->Refresh();
9489 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
9491 if ( m_labelTextColour
!= colour
)
9493 m_labelTextColour
= colour
;
9494 if ( !GetBatchCount() )
9496 m_rowLabelWin
->Refresh();
9497 m_colLabelWin
->Refresh();
9502 void wxGrid::SetLabelFont( const wxFont
& font
)
9505 if ( !GetBatchCount() )
9507 m_rowLabelWin
->Refresh();
9508 m_colLabelWin
->Refresh();
9512 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
9514 // allow old (incorrect) defs to be used
9517 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9518 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9519 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9524 case wxTOP
: vert
= wxALIGN_TOP
; break;
9525 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9526 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9529 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9531 m_rowLabelHorizAlign
= horiz
;
9534 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9536 m_rowLabelVertAlign
= vert
;
9539 if ( !GetBatchCount() )
9541 m_rowLabelWin
->Refresh();
9545 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
9547 // allow old (incorrect) defs to be used
9550 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9551 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9552 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9557 case wxTOP
: vert
= wxALIGN_TOP
; break;
9558 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9559 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9562 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9564 m_colLabelHorizAlign
= horiz
;
9567 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9569 m_colLabelVertAlign
= vert
;
9572 if ( !GetBatchCount() )
9574 m_colLabelWin
->Refresh();
9578 // Note: under MSW, the default column label font must be changed because it
9579 // does not support vertical printing
9581 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9582 // pGrid->SetLabelFont(font);
9583 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9585 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9587 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9588 m_colLabelTextOrientation
= textOrientation
;
9590 if ( !GetBatchCount() )
9591 m_colLabelWin
->Refresh();
9594 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9598 m_table
->SetRowLabelValue( row
, s
);
9599 if ( !GetBatchCount() )
9601 wxRect rect
= CellToRect( row
, 0 );
9602 if ( rect
.height
> 0 )
9604 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9606 rect
.width
= m_rowLabelWidth
;
9607 m_rowLabelWin
->Refresh( true, &rect
);
9613 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9617 m_table
->SetColLabelValue( col
, s
);
9618 if ( !GetBatchCount() )
9620 wxRect rect
= CellToRect( 0, col
);
9621 if ( rect
.width
> 0 )
9623 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9625 rect
.height
= m_colLabelHeight
;
9626 m_colLabelWin
->Refresh( true, &rect
);
9632 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9634 if ( m_gridLineColour
!= colour
)
9636 m_gridLineColour
= colour
;
9638 wxClientDC
dc( m_gridWin
);
9640 DrawAllGridLines( dc
, wxRegion() );
9644 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9646 if ( m_cellHighlightColour
!= colour
)
9648 m_cellHighlightColour
= colour
;
9650 wxClientDC
dc( m_gridWin
);
9652 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9653 DrawCellHighlight(dc
, attr
);
9658 void wxGrid::SetCellHighlightPenWidth(int width
)
9660 if (m_cellHighlightPenWidth
!= width
)
9662 m_cellHighlightPenWidth
= width
;
9664 // Just redrawing the cell highlight is not enough since that won't
9665 // make any visible change if the the thickness is getting smaller.
9666 int row
= m_currentCellCoords
.GetRow();
9667 int col
= m_currentCellCoords
.GetCol();
9668 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9671 wxRect rect
= CellToRect(row
, col
);
9672 m_gridWin
->Refresh(true, &rect
);
9676 void wxGrid::SetCellHighlightROPenWidth(int width
)
9678 if (m_cellHighlightROPenWidth
!= width
)
9680 m_cellHighlightROPenWidth
= width
;
9682 // Just redrawing the cell highlight is not enough since that won't
9683 // make any visible change if the the thickness is getting smaller.
9684 int row
= m_currentCellCoords
.GetRow();
9685 int col
= m_currentCellCoords
.GetCol();
9686 if ( row
== -1 || col
== -1 ||
9687 GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9690 wxRect rect
= CellToRect(row
, col
);
9691 m_gridWin
->Refresh(true, &rect
);
9695 void wxGrid::EnableGridLines( bool enable
)
9697 if ( enable
!= m_gridLinesEnabled
)
9699 m_gridLinesEnabled
= enable
;
9701 if ( !GetBatchCount() )
9705 wxClientDC
dc( m_gridWin
);
9707 DrawAllGridLines( dc
, wxRegion() );
9711 m_gridWin
->Refresh();
9717 int wxGrid::GetDefaultRowSize() const
9719 return m_defaultRowHeight
;
9722 int wxGrid::GetRowSize( int row
) const
9724 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9726 return GetRowHeight(row
);
9729 int wxGrid::GetDefaultColSize() const
9731 return m_defaultColWidth
;
9734 int wxGrid::GetColSize( int col
) const
9736 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9738 return GetColWidth(col
);
9741 // ============================================================================
9742 // access to the grid attributes: each of them has a default value in the grid
9743 // itself and may be overidden on a per-cell basis
9744 // ============================================================================
9746 // ----------------------------------------------------------------------------
9747 // setting default attributes
9748 // ----------------------------------------------------------------------------
9750 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9752 m_defaultCellAttr
->SetBackgroundColour(col
);
9754 m_gridWin
->SetBackgroundColour(col
);
9758 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9760 m_defaultCellAttr
->SetTextColour(col
);
9763 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9765 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9768 void wxGrid::SetDefaultCellOverflow( bool allow
)
9770 m_defaultCellAttr
->SetOverflow(allow
);
9773 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9775 m_defaultCellAttr
->SetFont(font
);
9778 // For editors and renderers the type registry takes precedence over the
9779 // default attr, so we need to register the new editor/renderer for the string
9780 // data type in order to make setting a default editor/renderer appear to
9783 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9785 RegisterDataType(wxGRID_VALUE_STRING
,
9787 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9790 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9792 RegisterDataType(wxGRID_VALUE_STRING
,
9793 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9797 // ----------------------------------------------------------------------------
9798 // access to the default attributes
9799 // ----------------------------------------------------------------------------
9801 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
9803 return m_defaultCellAttr
->GetBackgroundColour();
9806 wxColour
wxGrid::GetDefaultCellTextColour() const
9808 return m_defaultCellAttr
->GetTextColour();
9811 wxFont
wxGrid::GetDefaultCellFont() const
9813 return m_defaultCellAttr
->GetFont();
9816 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
9818 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9821 bool wxGrid::GetDefaultCellOverflow() const
9823 return m_defaultCellAttr
->GetOverflow();
9826 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9828 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9831 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9833 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9836 // ----------------------------------------------------------------------------
9837 // access to cell attributes
9838 // ----------------------------------------------------------------------------
9840 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
9842 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9843 wxColour colour
= attr
->GetBackgroundColour();
9849 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
9851 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9852 wxColour colour
= attr
->GetTextColour();
9858 wxFont
wxGrid::GetCellFont( int row
, int col
) const
9860 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9861 wxFont font
= attr
->GetFont();
9867 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
9869 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9870 attr
->GetAlignment(horiz
, vert
);
9874 bool wxGrid::GetCellOverflow( int row
, int col
) const
9876 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9877 bool allow
= attr
->GetOverflow();
9883 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
9885 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9886 attr
->GetSize( num_rows
, num_cols
);
9890 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
9892 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9893 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9899 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
9901 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9902 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9908 bool wxGrid::IsReadOnly(int row
, int col
) const
9910 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9911 bool isReadOnly
= attr
->IsReadOnly();
9917 // ----------------------------------------------------------------------------
9918 // attribute support: cache, automatic provider creation, ...
9919 // ----------------------------------------------------------------------------
9921 bool wxGrid::CanHaveAttributes() const
9928 return m_table
->CanHaveAttributes();
9931 void wxGrid::ClearAttrCache()
9933 if ( m_attrCache
.row
!= -1 )
9935 wxSafeDecRef(m_attrCache
.attr
);
9936 m_attrCache
.attr
= NULL
;
9937 m_attrCache
.row
= -1;
9941 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9945 wxGrid
*self
= (wxGrid
*)this; // const_cast
9947 self
->ClearAttrCache();
9948 self
->m_attrCache
.row
= row
;
9949 self
->m_attrCache
.col
= col
;
9950 self
->m_attrCache
.attr
= attr
;
9955 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9957 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9959 *attr
= m_attrCache
.attr
;
9960 wxSafeIncRef(m_attrCache
.attr
);
9962 #ifdef DEBUG_ATTR_CACHE
9963 gs_nAttrCacheHits
++;
9970 #ifdef DEBUG_ATTR_CACHE
9971 gs_nAttrCacheMisses
++;
9978 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9980 wxGridCellAttr
*attr
= NULL
;
9981 // Additional test to avoid looking at the cache e.g. for
9982 // wxNoCellCoords, as this will confuse memory management.
9985 if ( !LookupAttr(row
, col
, &attr
) )
9987 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9988 : (wxGridCellAttr
*)NULL
;
9989 CacheAttr(row
, col
, attr
);
9995 attr
->SetDefAttr(m_defaultCellAttr
);
9999 attr
= m_defaultCellAttr
;
10006 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
10008 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
10009 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
10011 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
10012 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
10014 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
10017 attr
= new wxGridCellAttr(m_defaultCellAttr
);
10019 // artificially inc the ref count to match DecRef() in caller
10021 m_table
->SetAttr(attr
, row
, col
);
10027 // ----------------------------------------------------------------------------
10028 // setting column attributes (wrappers around SetColAttr)
10029 // ----------------------------------------------------------------------------
10031 void wxGrid::SetColFormatBool(int col
)
10033 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
10036 void wxGrid::SetColFormatNumber(int col
)
10038 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
10041 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
10043 wxString typeName
= wxGRID_VALUE_FLOAT
;
10044 if ( (width
!= -1) || (precision
!= -1) )
10046 typeName
<< _T(':') << width
<< _T(',') << precision
;
10049 SetColFormatCustom(col
, typeName
);
10052 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
10054 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
10056 attr
= new wxGridCellAttr
;
10057 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
10058 attr
->SetRenderer(renderer
);
10059 wxGridCellEditor
*editor
= GetDefaultEditorForType(typeName
);
10060 attr
->SetEditor(editor
);
10062 SetColAttr(col
, attr
);
10066 // ----------------------------------------------------------------------------
10067 // setting cell attributes: this is forwarded to the table
10068 // ----------------------------------------------------------------------------
10070 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
10072 if ( CanHaveAttributes() )
10074 m_table
->SetAttr(attr
, row
, col
);
10079 wxSafeDecRef(attr
);
10083 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
10085 if ( CanHaveAttributes() )
10087 m_table
->SetRowAttr(attr
, row
);
10092 wxSafeDecRef(attr
);
10096 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
10098 if ( CanHaveAttributes() )
10100 m_table
->SetColAttr(attr
, col
);
10105 wxSafeDecRef(attr
);
10109 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
10111 if ( CanHaveAttributes() )
10113 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10114 attr
->SetBackgroundColour(colour
);
10119 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
10121 if ( CanHaveAttributes() )
10123 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10124 attr
->SetTextColour(colour
);
10129 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
10131 if ( CanHaveAttributes() )
10133 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10134 attr
->SetFont(font
);
10139 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
10141 if ( CanHaveAttributes() )
10143 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10144 attr
->SetAlignment(horiz
, vert
);
10149 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
10151 if ( CanHaveAttributes() )
10153 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10154 attr
->SetOverflow(allow
);
10159 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
10161 if ( CanHaveAttributes() )
10163 int cell_rows
, cell_cols
;
10165 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10166 attr
->GetSize(&cell_rows
, &cell_cols
);
10167 attr
->SetSize(num_rows
, num_cols
);
10170 // Cannot set the size of a cell to 0 or negative values
10171 // While it is perfectly legal to do that, this function cannot
10172 // handle all the possibilies, do it by hand by getting the CellAttr.
10173 // You can only set the size of a cell to 1,1 or greater with this fn
10174 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
10175 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10176 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
10177 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10179 // if this was already a multicell then "turn off" the other cells first
10180 if ((cell_rows
> 1) || (cell_rows
> 1))
10183 for (j
=row
; j
< row
+ cell_rows
; j
++)
10185 for (i
=col
; i
< col
+ cell_cols
; i
++)
10187 if ((i
!= col
) || (j
!= row
))
10189 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10190 attr_stub
->SetSize( 1, 1 );
10191 attr_stub
->DecRef();
10197 // mark the cells that will be covered by this cell to
10198 // negative or zero values to point back at this cell
10199 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
10202 for (j
=row
; j
< row
+ num_rows
; j
++)
10204 for (i
=col
; i
< col
+ num_cols
; i
++)
10206 if ((i
!= col
) || (j
!= row
))
10208 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10209 attr_stub
->SetSize( row
- j
, col
- i
);
10210 attr_stub
->DecRef();
10218 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
10220 if ( CanHaveAttributes() )
10222 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10223 attr
->SetRenderer(renderer
);
10228 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
10230 if ( CanHaveAttributes() )
10232 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10233 attr
->SetEditor(editor
);
10238 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
10240 if ( CanHaveAttributes() )
10242 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10243 attr
->SetReadOnly(isReadOnly
);
10248 // ----------------------------------------------------------------------------
10249 // Data type registration
10250 // ----------------------------------------------------------------------------
10252 void wxGrid::RegisterDataType(const wxString
& typeName
,
10253 wxGridCellRenderer
* renderer
,
10254 wxGridCellEditor
* editor
)
10256 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
10260 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
10262 wxString typeName
= m_table
->GetTypeName(row
, col
);
10263 return GetDefaultEditorForType(typeName
);
10266 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
10268 wxString typeName
= m_table
->GetTypeName(row
, col
);
10269 return GetDefaultRendererForType(typeName
);
10272 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
10274 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10275 if ( index
== wxNOT_FOUND
)
10277 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10282 return m_typeRegistry
->GetEditor(index
);
10285 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
10287 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10288 if ( index
== wxNOT_FOUND
)
10290 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10295 return m_typeRegistry
->GetRenderer(index
);
10298 // ----------------------------------------------------------------------------
10300 // ----------------------------------------------------------------------------
10302 void wxGrid::EnableDragRowSize( bool enable
)
10304 m_canDragRowSize
= enable
;
10307 void wxGrid::EnableDragColSize( bool enable
)
10309 m_canDragColSize
= enable
;
10312 void wxGrid::EnableDragGridSize( bool enable
)
10314 m_canDragGridSize
= enable
;
10317 void wxGrid::EnableDragCell( bool enable
)
10319 m_canDragCell
= enable
;
10322 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
10324 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
10326 if ( resizeExistingRows
)
10328 // since we are resizing all rows to the default row size,
10329 // we can simply clear the row heights and row bottoms
10330 // arrays (which also allows us to take advantage of
10331 // some speed optimisations)
10332 m_rowHeights
.Empty();
10333 m_rowBottoms
.Empty();
10334 if ( !GetBatchCount() )
10339 void wxGrid::SetRowSize( int row
, int height
)
10341 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
10343 // See comment in SetColSize
10344 if ( height
< GetRowMinimalAcceptableHeight())
10347 if ( m_rowHeights
.IsEmpty() )
10349 // need to really create the array
10353 int h
= wxMax( 0, height
);
10354 int diff
= h
- m_rowHeights
[row
];
10356 m_rowHeights
[row
] = h
;
10357 for ( int i
= row
; i
< m_numRows
; i
++ )
10359 m_rowBottoms
[i
] += diff
;
10362 if ( !GetBatchCount() )
10366 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
10368 // we dont allow zero default column width
10369 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
10371 if ( resizeExistingCols
)
10373 // since we are resizing all columns to the default column size,
10374 // we can simply clear the col widths and col rights
10375 // arrays (which also allows us to take advantage of
10376 // some speed optimisations)
10377 m_colWidths
.Empty();
10378 m_colRights
.Empty();
10379 if ( !GetBatchCount() )
10384 void wxGrid::SetColSize( int col
, int width
)
10386 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
10388 // should we check that it's bigger than GetColMinimalWidth(col) here?
10390 // No, because it is reasonable to assume the library user know's
10391 // what he is doing. However we should test against the weaker
10392 // constraint of minimalAcceptableWidth, as this breaks rendering
10394 // This test then fixes sf.net bug #645734
10396 if ( width
< GetColMinimalAcceptableWidth() )
10399 if ( m_colWidths
.IsEmpty() )
10401 // need to really create the array
10405 // if < 0 then calculate new width from label
10409 wxArrayString lines
;
10410 wxClientDC
dc(m_colLabelWin
);
10411 dc
.SetFont(GetLabelFont());
10412 StringToLines(GetColLabelValue(col
), lines
);
10413 GetTextBoxSize(dc
, lines
, &w
, &h
);
10417 int w
= wxMax( 0, width
);
10418 int diff
= w
- m_colWidths
[col
];
10419 m_colWidths
[col
] = w
;
10421 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
10423 m_colRights
[GetColAt(colPos
)] += diff
;
10426 if ( !GetBatchCount() )
10430 void wxGrid::SetColMinimalWidth( int col
, int width
)
10432 if (width
> GetColMinimalAcceptableWidth())
10434 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10435 m_colMinWidths
[key
] = width
;
10439 void wxGrid::SetRowMinimalHeight( int row
, int width
)
10441 if (width
> GetRowMinimalAcceptableHeight())
10443 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10444 m_rowMinHeights
[key
] = width
;
10448 int wxGrid::GetColMinimalWidth(int col
) const
10450 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10451 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
10453 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
10456 int wxGrid::GetRowMinimalHeight(int row
) const
10458 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10459 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
10461 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
10464 void wxGrid::SetColMinimalAcceptableWidth( int width
)
10466 // We do allow a width of 0 since this gives us
10467 // an easy way to temporarily hiding columns.
10469 m_minAcceptableColWidth
= width
;
10472 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
10474 // We do allow a height of 0 since this gives us
10475 // an easy way to temporarily hiding rows.
10477 m_minAcceptableRowHeight
= height
;
10480 int wxGrid::GetColMinimalAcceptableWidth() const
10482 return m_minAcceptableColWidth
;
10485 int wxGrid::GetRowMinimalAcceptableHeight() const
10487 return m_minAcceptableRowHeight
;
10490 // ----------------------------------------------------------------------------
10492 // ----------------------------------------------------------------------------
10495 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
10497 const bool column
= direction
== wxGRID_COLUMN
;
10499 wxClientDC
dc(m_gridWin
);
10501 // cancel editing of cell
10502 HideCellEditControl();
10503 SaveEditControlValue();
10505 // init both of them to avoid compiler warnings, even if we only need one
10513 wxCoord extent
, extentMax
= 0;
10514 int max
= column
? m_numRows
: m_numCols
;
10515 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
10522 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
10523 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
10526 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
10527 extent
= column
? size
.x
: size
.y
;
10528 if ( extent
> extentMax
)
10529 extentMax
= extent
;
10531 renderer
->DecRef();
10537 // now also compare with the column label extent
10539 dc
.SetFont( GetLabelFont() );
10543 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
10544 if ( GetColLabelTextOrientation() == wxVERTICAL
)
10548 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
10550 extent
= column
? w
: h
;
10551 if ( extent
> extentMax
)
10552 extentMax
= extent
;
10556 // empty column - give default extent (notice that if extentMax is less
10557 // than default extent but != 0, it's OK)
10558 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
10563 // leave some space around text
10571 // Ensure automatic width is not less than minimal width. See the
10572 // comment in SetColSize() for explanation of why this isn't done
10573 // in SetColSize().
10575 extentMax
= wxMax(extentMax
, GetColMinimalWidth(col
));
10577 SetColSize( col
, extentMax
);
10578 if ( !GetBatchCount() )
10581 m_gridWin
->GetClientSize( &cw
, &ch
);
10582 wxRect
rect ( CellToRect( 0, col
) );
10584 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10585 rect
.width
= cw
- rect
.x
;
10586 rect
.height
= m_colLabelHeight
;
10587 m_colLabelWin
->Refresh( true, &rect
);
10592 // Ensure automatic width is not less than minimal height. See the
10593 // comment in SetColSize() for explanation of why this isn't done
10594 // in SetRowSize().
10596 extentMax
= wxMax(extentMax
, GetRowMinimalHeight(row
));
10598 SetRowSize(row
, extentMax
);
10599 if ( !GetBatchCount() )
10602 m_gridWin
->GetClientSize( &cw
, &ch
);
10603 wxRect
rect( CellToRect( row
, 0 ) );
10605 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10606 rect
.width
= m_rowLabelWidth
;
10607 rect
.height
= ch
- rect
.y
;
10608 m_rowLabelWin
->Refresh( true, &rect
);
10615 SetColMinimalWidth(col
, extentMax
);
10617 SetRowMinimalHeight(row
, extentMax
);
10621 wxCoord
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
10623 // calculate size for the rows or columns?
10624 const bool calcRows
= direction
== wxGRID_ROW
;
10626 wxClientDC
dc(calcRows
? GetGridRowLabelWindow()
10627 : GetGridColLabelWindow());
10628 dc
.SetFont(GetLabelFont());
10630 // which dimension should we take into account for calculations?
10632 // for columns, the text can be only horizontal so it's easy but for rows
10633 // we also have to take into account the text orientation
10635 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
10637 wxArrayString lines
;
10638 wxCoord extentMax
= 0;
10640 const int numRowsOrCols
= calcRows
? m_numRows
: m_numCols
;
10641 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
10645 wxString label
= calcRows
? GetRowLabelValue(rowOrCol
)
10646 : GetColLabelValue(rowOrCol
);
10647 StringToLines(label
, lines
);
10650 GetTextBoxSize(dc
, lines
, &w
, &h
);
10652 const wxCoord extent
= useWidth
? w
: h
;
10653 if ( extent
> extentMax
)
10654 extentMax
= extent
;
10659 // empty column - give default extent (notice that if extentMax is less
10660 // than default extent but != 0, it's OK)
10661 extentMax
= calcRows
? GetDefaultRowLabelSize()
10662 : GetDefaultColLabelSize();
10665 // leave some space around text (taken from AutoSizeColOrRow)
10674 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10676 int width
= m_rowLabelWidth
;
10678 wxGridUpdateLocker locker
;
10680 locker
.Create(this);
10682 for ( int col
= 0; col
< m_numCols
; col
++ )
10685 AutoSizeColumn(col
, setAsMin
);
10687 width
+= GetColWidth(col
);
10693 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10695 int height
= m_colLabelHeight
;
10697 wxGridUpdateLocker locker
;
10699 locker
.Create(this);
10701 for ( int row
= 0; row
< m_numRows
; row
++ )
10704 AutoSizeRow(row
, setAsMin
);
10706 height
+= GetRowHeight(row
);
10712 void wxGrid::AutoSize()
10714 wxGridUpdateLocker
locker(this);
10716 // we need to round up the size of the scrollable area to a multiple of
10717 // scroll step to ensure that we don't get the scrollbars when we're sized
10718 // exactly to fit our contents
10719 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
10720 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
10721 wxSize
sizeFit(GetScrollX(size
.x
) * GetScrollLineX(),
10722 GetScrollY(size
.y
) * GetScrollLineY());
10724 // distribute the extra space between the columns/rows to avoid having
10725 // extra white space
10726 wxCoord diff
= sizeFit
.x
- size
.x
;
10727 if ( diff
&& m_numCols
)
10729 // try to resize the columns uniformly
10730 wxCoord diffPerCol
= diff
/ m_numCols
;
10733 for ( int col
= 0; col
< m_numCols
; col
++ )
10735 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
10739 // add remaining amount to the last columns
10740 diff
-= diffPerCol
* m_numCols
;
10743 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
10745 SetColSize(col
, GetColWidth(col
) + 1);
10751 diff
= sizeFit
.y
- size
.y
;
10752 if ( diff
&& m_numRows
)
10754 // try to resize the columns uniformly
10755 wxCoord diffPerRow
= diff
/ m_numRows
;
10758 for ( int row
= 0; row
< m_numRows
; row
++ )
10760 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
10764 // add remaining amount to the last rows
10765 diff
-= diffPerRow
* m_numRows
;
10768 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
10770 SetRowSize(row
, GetRowHeight(row
) + 1);
10775 // we know that we're not going to have scrollbars so disable them now to
10776 // avoid trouble in SetClientSize() which can otherwise set the correct
10777 // client size but also leave space for (not needed any more) scrollbars
10778 SetScrollbars(0, 0, 0, 0, 0, 0, true);
10779 SetClientSize(sizeFit
.x
+ m_rowLabelWidth
, sizeFit
.y
+ m_colLabelHeight
);
10782 void wxGrid::AutoSizeRowLabelSize( int row
)
10784 wxArrayString lines
;
10787 // Hide the edit control, so it
10788 // won't interfere with drag-shrinking.
10789 if ( IsCellEditControlShown() )
10791 HideCellEditControl();
10792 SaveEditControlValue();
10795 // autosize row height depending on label text
10796 StringToLines( GetRowLabelValue( row
), lines
);
10797 wxClientDC
dc( m_rowLabelWin
);
10798 GetTextBoxSize( dc
, lines
, &w
, &h
);
10799 if ( h
< m_defaultRowHeight
)
10800 h
= m_defaultRowHeight
;
10801 SetRowSize(row
, h
);
10805 void wxGrid::AutoSizeColLabelSize( int col
)
10807 wxArrayString lines
;
10810 // Hide the edit control, so it
10811 // won't interfere with drag-shrinking.
10812 if ( IsCellEditControlShown() )
10814 HideCellEditControl();
10815 SaveEditControlValue();
10818 // autosize column width depending on label text
10819 StringToLines( GetColLabelValue( col
), lines
);
10820 wxClientDC
dc( m_colLabelWin
);
10821 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10822 GetTextBoxSize( dc
, lines
, &w
, &h
);
10824 GetTextBoxSize( dc
, lines
, &h
, &w
);
10825 if ( w
< m_defaultColWidth
)
10826 w
= m_defaultColWidth
;
10827 SetColSize(col
, w
);
10831 wxSize
wxGrid::DoGetBestSize() const
10833 wxGrid
*self
= (wxGrid
*)this; // const_cast
10835 // we do the same as in AutoSize() here with the exception that we don't
10836 // change the column/row sizes, only calculate them
10837 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
10838 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
10839 wxSize
sizeFit(GetScrollX(size
.x
) * GetScrollLineX(),
10840 GetScrollY(size
.y
) * GetScrollLineY());
10842 // NOTE: This size should be cached, but first we need to add calls to
10843 // InvalidateBestSize everywhere that could change the results of this
10845 // CacheBestSize(size);
10847 return wxSize(sizeFit
.x
+ m_rowLabelWidth
, sizeFit
.y
+ m_colLabelHeight
)
10848 + GetWindowBorderSize();
10856 wxPen
& wxGrid::GetDividerPen() const
10861 // ----------------------------------------------------------------------------
10862 // cell value accessor functions
10863 // ----------------------------------------------------------------------------
10865 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10869 m_table
->SetValue( row
, col
, s
);
10870 if ( !GetBatchCount() )
10873 wxRect
rect( CellToRect( row
, col
) );
10875 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10876 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10877 m_gridWin
->Refresh( false, &rect
);
10880 if ( m_currentCellCoords
.GetRow() == row
&&
10881 m_currentCellCoords
.GetCol() == col
&&
10882 IsCellEditControlShown())
10883 // Note: If we are using IsCellEditControlEnabled,
10884 // this interacts badly with calling SetCellValue from
10885 // an EVT_GRID_CELL_CHANGE handler.
10887 HideCellEditControl();
10888 ShowCellEditControl(); // will reread data from table
10893 // ----------------------------------------------------------------------------
10894 // block, row and column selection
10895 // ----------------------------------------------------------------------------
10897 void wxGrid::SelectRow( int row
, bool addToSelected
)
10899 if ( IsSelection() && !addToSelected
)
10903 m_selection
->SelectRow( row
, false, addToSelected
);
10906 void wxGrid::SelectCol( int col
, bool addToSelected
)
10908 if ( IsSelection() && !addToSelected
)
10912 m_selection
->SelectCol( col
, false, addToSelected
);
10915 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10916 bool addToSelected
)
10918 if ( IsSelection() && !addToSelected
)
10922 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10923 false, addToSelected
);
10926 void wxGrid::SelectAll()
10928 if ( m_numRows
> 0 && m_numCols
> 0 )
10931 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10935 // ----------------------------------------------------------------------------
10936 // cell, row and col deselection
10937 // ----------------------------------------------------------------------------
10939 void wxGrid::DeselectRow( int row
)
10941 if ( !m_selection
)
10944 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10946 if ( m_selection
->IsInSelection(row
, 0 ) )
10947 m_selection
->ToggleCellSelection(row
, 0);
10951 int nCols
= GetNumberCols();
10952 for ( int i
= 0; i
< nCols
; i
++ )
10954 if ( m_selection
->IsInSelection(row
, i
) )
10955 m_selection
->ToggleCellSelection(row
, i
);
10960 void wxGrid::DeselectCol( int col
)
10962 if ( !m_selection
)
10965 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10967 if ( m_selection
->IsInSelection(0, col
) )
10968 m_selection
->ToggleCellSelection(0, col
);
10972 int nRows
= GetNumberRows();
10973 for ( int i
= 0; i
< nRows
; i
++ )
10975 if ( m_selection
->IsInSelection(i
, col
) )
10976 m_selection
->ToggleCellSelection(i
, col
);
10981 void wxGrid::DeselectCell( int row
, int col
)
10983 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10984 m_selection
->ToggleCellSelection(row
, col
);
10987 bool wxGrid::IsSelection() const
10989 return ( m_selection
&& (m_selection
->IsSelection() ||
10990 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10991 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10994 bool wxGrid::IsInSelection( int row
, int col
) const
10996 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10997 ( row
>= m_selectingTopLeft
.GetRow() &&
10998 col
>= m_selectingTopLeft
.GetCol() &&
10999 row
<= m_selectingBottomRight
.GetRow() &&
11000 col
<= m_selectingBottomRight
.GetCol() )) );
11003 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
11007 wxGridCellCoordsArray a
;
11011 return m_selection
->m_cellSelection
;
11014 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
11018 wxGridCellCoordsArray a
;
11022 return m_selection
->m_blockSelectionTopLeft
;
11025 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
11029 wxGridCellCoordsArray a
;
11033 return m_selection
->m_blockSelectionBottomRight
;
11036 wxArrayInt
wxGrid::GetSelectedRows() const
11044 return m_selection
->m_rowSelection
;
11047 wxArrayInt
wxGrid::GetSelectedCols() const
11055 return m_selection
->m_colSelection
;
11058 void wxGrid::ClearSelection()
11060 wxRect r1
= BlockToDeviceRect( m_selectingTopLeft
, m_selectingBottomRight
);
11061 wxRect r2
= BlockToDeviceRect( m_currentCellCoords
, m_selectingKeyboard
);
11062 m_selectingTopLeft
=
11063 m_selectingBottomRight
=
11064 m_selectingKeyboard
= wxGridNoCellCoords
;
11065 Refresh( false, &r1
);
11066 Refresh( false, &r2
);
11068 m_selection
->ClearSelection();
11071 // This function returns the rectangle that encloses the given block
11072 // in device coords clipped to the client size of the grid window.
11074 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
11075 const wxGridCellCoords
& bottomRight
) const
11078 wxRect tempCellRect
= CellToRect(topLeft
);
11079 if ( tempCellRect
!= wxGridNoCellRect
)
11081 resultRect
= tempCellRect
;
11085 resultRect
= wxRect(0, 0, 0, 0);
11088 tempCellRect
= CellToRect(bottomRight
);
11089 if ( tempCellRect
!= wxGridNoCellRect
)
11091 resultRect
+= tempCellRect
;
11095 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
11096 return wxGridNoCellRect
;
11099 // Ensure that left/right and top/bottom pairs are in order.
11100 int left
= resultRect
.GetLeft();
11101 int top
= resultRect
.GetTop();
11102 int right
= resultRect
.GetRight();
11103 int bottom
= resultRect
.GetBottom();
11105 int leftCol
= topLeft
.GetCol();
11106 int topRow
= topLeft
.GetRow();
11107 int rightCol
= bottomRight
.GetCol();
11108 int bottomRow
= bottomRight
.GetRow();
11117 leftCol
= rightCol
;
11128 topRow
= bottomRow
;
11132 // The following loop is ONLY necessary to detect and handle merged cells.
11134 m_gridWin
->GetClientSize( &cw
, &ch
);
11136 // Get the origin coordinates: notice that they will be negative if the
11137 // grid is scrolled downwards/to the right.
11138 int gridOriginX
= 0;
11139 int gridOriginY
= 0;
11140 CalcScrolledPosition(gridOriginX
, gridOriginY
, &gridOriginX
, &gridOriginY
);
11142 int onScreenLeftmostCol
= internalXToCol(-gridOriginX
);
11143 int onScreenUppermostRow
= internalYToRow(-gridOriginY
);
11145 int onScreenRightmostCol
= internalXToCol(-gridOriginX
+ cw
);
11146 int onScreenBottommostRow
= internalYToRow(-gridOriginY
+ ch
);
11148 // Bound our loop so that we only examine the portion of the selected block
11149 // that is shown on screen. Therefore, we compare the Top-Left block values
11150 // to the Top-Left screen values, and the Bottom-Right block values to the
11151 // Bottom-Right screen values, choosing appropriately.
11152 const int visibleTopRow
= wxMax(topRow
, onScreenUppermostRow
);
11153 const int visibleBottomRow
= wxMin(bottomRow
, onScreenBottommostRow
);
11154 const int visibleLeftCol
= wxMax(leftCol
, onScreenLeftmostCol
);
11155 const int visibleRightCol
= wxMin(rightCol
, onScreenRightmostCol
);
11157 for ( int j
= visibleTopRow
; j
<= visibleBottomRow
; j
++ )
11159 for ( int i
= visibleLeftCol
; i
<= visibleRightCol
; i
++ )
11161 if ( (j
== visibleTopRow
) || (j
== visibleBottomRow
) ||
11162 (i
== visibleLeftCol
) || (i
== visibleRightCol
) )
11164 tempCellRect
= CellToRect( j
, i
);
11166 if (tempCellRect
.x
< left
)
11167 left
= tempCellRect
.x
;
11168 if (tempCellRect
.y
< top
)
11169 top
= tempCellRect
.y
;
11170 if (tempCellRect
.x
+ tempCellRect
.width
> right
)
11171 right
= tempCellRect
.x
+ tempCellRect
.width
;
11172 if (tempCellRect
.y
+ tempCellRect
.height
> bottom
)
11173 bottom
= tempCellRect
.y
+ tempCellRect
.height
;
11177 i
= visibleRightCol
; // jump over inner cells.
11182 // Convert to scrolled coords
11183 CalcScrolledPosition( left
, top
, &left
, &top
);
11184 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
11186 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
11187 return wxRect(0,0,0,0);
11189 resultRect
.SetLeft( wxMax(0, left
) );
11190 resultRect
.SetTop( wxMax(0, top
) );
11191 resultRect
.SetRight( wxMin(cw
, right
) );
11192 resultRect
.SetBottom( wxMin(ch
, bottom
) );
11197 // ----------------------------------------------------------------------------
11199 // ----------------------------------------------------------------------------
11201 #if wxUSE_DRAG_AND_DROP
11203 // this allow setting drop target directly on wxGrid
11204 void wxGrid::SetDropTarget(wxDropTarget
*dropTarget
)
11206 GetGridWindow()->SetDropTarget(dropTarget
);
11209 #endif // wxUSE_DRAG_AND_DROP
11211 // ----------------------------------------------------------------------------
11212 // grid event classes
11213 // ----------------------------------------------------------------------------
11215 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
11217 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
11218 int row
, int col
, int x
, int y
, bool sel
,
11219 bool control
, bool shift
, bool alt
, bool meta
)
11220 : wxNotifyEvent( type
, id
)
11227 m_control
= control
;
11232 SetEventObject(obj
);
11236 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
11238 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
11239 int rowOrCol
, int x
, int y
,
11240 bool control
, bool shift
, bool alt
, bool meta
)
11241 : wxNotifyEvent( type
, id
)
11243 m_rowOrCol
= rowOrCol
;
11246 m_control
= control
;
11251 SetEventObject(obj
);
11255 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
11257 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
11258 const wxGridCellCoords
& topLeft
,
11259 const wxGridCellCoords
& bottomRight
,
11260 bool sel
, bool control
,
11261 bool shift
, bool alt
, bool meta
)
11262 : wxNotifyEvent( type
, id
)
11264 m_topLeft
= topLeft
;
11265 m_bottomRight
= bottomRight
;
11267 m_control
= control
;
11272 SetEventObject(obj
);
11276 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
11278 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
11279 wxObject
* obj
, int row
,
11280 int col
, wxControl
* ctrl
)
11281 : wxCommandEvent(type
, id
)
11283 SetEventObject(obj
);
11289 #endif // wxUSE_GRID