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
*table
= grid
->GetTable();
1117 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1119 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1124 wxString sValue
= table
->GetValue(row
, col
);
1125 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.empty())
1127 wxFAIL_MSG( _T("this cell doesn't have float value") );
1132 DoBeginEdit(GetString());
1135 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1139 wxString
text(Text()->GetValue());
1141 if ( (text
.empty() || text
.ToDouble(&value
)) &&
1142 !wxIsSameDouble(value
, m_valueOld
) )
1144 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1145 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1147 grid
->GetTable()->SetValue(row
, col
, text
);
1155 void wxGridCellFloatEditor::Reset()
1157 DoReset(GetString());
1160 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1162 int keycode
= event
.GetKeyCode();
1164 tmpbuf
[0] = (char) keycode
;
1166 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1169 bool is_decimal_point
= ( strbuf
==
1170 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1172 bool is_decimal_point
= ( strbuf
== _T(".") );
1175 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1176 || is_decimal_point
)
1178 wxGridCellTextEditor::StartingKey(event
);
1180 // skip Skip() below
1187 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1198 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1202 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1204 m_precision
= (int)tmp
;
1206 // skip the error message below
1211 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1215 wxString
wxGridCellFloatEditor::GetString() const
1218 if ( m_precision
== -1 && m_width
!= -1)
1220 // default precision
1221 fmt
.Printf(_T("%%%d.f"), m_width
);
1223 else if ( m_precision
!= -1 && m_width
== -1)
1226 fmt
.Printf(_T("%%.%df"), m_precision
);
1228 else if ( m_precision
!= -1 && m_width
!= -1 )
1230 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1234 // default width/precision
1238 return wxString::Format(fmt
, m_valueOld
);
1241 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1243 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1245 const int keycode
= event
.GetKeyCode();
1246 if ( isascii(keycode
) )
1249 tmpbuf
[0] = (char) keycode
;
1251 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1254 const wxString decimalPoint
=
1255 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
1257 const wxString
decimalPoint(_T('.'));
1260 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1261 if ( wxIsdigit(keycode
) ||
1262 tolower(keycode
) == 'e' ||
1263 keycode
== decimalPoint
||
1275 #endif // wxUSE_TEXTCTRL
1279 // ----------------------------------------------------------------------------
1280 // wxGridCellBoolEditor
1281 // ----------------------------------------------------------------------------
1283 // the default values for GetValue()
1284 wxString
wxGridCellBoolEditor::ms_stringValues
[2] = { _T(""), _T("1") };
1286 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1288 wxEvtHandler
* evtHandler
)
1290 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1291 wxDefaultPosition
, wxDefaultSize
,
1294 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1297 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1299 bool resize
= false;
1300 wxSize size
= m_control
->GetSize();
1301 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1303 // check if the checkbox is not too big/small for this cell
1304 wxSize sizeBest
= m_control
->GetBestSize();
1305 if ( !(size
== sizeBest
) )
1307 // reset to default size if it had been made smaller
1313 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1315 // leave 1 pixel margin
1316 size
.x
= size
.y
= minSize
- 2;
1323 m_control
->SetSize(size
);
1326 // position it in the centre of the rectangle (TODO: support alignment?)
1328 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1329 // the checkbox without label still has some space to the right in wxGTK,
1330 // so shift it to the right
1332 #elif defined(__WXMSW__)
1333 // here too, but in other way
1338 int hAlign
= wxALIGN_CENTRE
;
1339 int vAlign
= wxALIGN_CENTRE
;
1341 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1344 if (hAlign
== wxALIGN_LEFT
)
1352 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1354 else if (hAlign
== wxALIGN_RIGHT
)
1356 x
= r
.x
+ r
.width
- size
.x
- 2;
1357 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1359 else if (hAlign
== wxALIGN_CENTRE
)
1361 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1362 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1365 m_control
->Move(x
, y
);
1368 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1370 m_control
->Show(show
);
1374 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1375 CBox()->SetBackgroundColour(colBg
);
1379 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1381 wxASSERT_MSG(m_control
,
1382 wxT("The wxGridCellEditor must be created first!"));
1384 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1386 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1390 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1392 if ( cellval
== ms_stringValues
[false] )
1393 m_startValue
= false;
1394 else if ( cellval
== ms_stringValues
[true] )
1395 m_startValue
= true;
1398 // do not try to be smart here and convert it to true or false
1399 // because we'll still overwrite it with something different and
1400 // this risks to be very surprising for the user code, let them
1402 wxFAIL_MSG( _T("invalid value for a cell with bool editor!") );
1406 CBox()->SetValue(m_startValue
);
1410 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1413 wxASSERT_MSG(m_control
,
1414 wxT("The wxGridCellEditor must be created first!"));
1416 bool changed
= false;
1417 bool value
= CBox()->GetValue();
1418 if ( value
!= m_startValue
)
1423 wxGridTableBase
* const table
= grid
->GetTable();
1424 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1425 table
->SetValueAsBool(row
, col
, value
);
1427 table
->SetValue(row
, col
, GetValue());
1433 void wxGridCellBoolEditor::Reset()
1435 wxASSERT_MSG(m_control
,
1436 wxT("The wxGridCellEditor must be created first!"));
1438 CBox()->SetValue(m_startValue
);
1441 void wxGridCellBoolEditor::StartingClick()
1443 CBox()->SetValue(!CBox()->GetValue());
1446 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1448 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1450 int keycode
= event
.GetKeyCode();
1463 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1465 int keycode
= event
.GetKeyCode();
1469 CBox()->SetValue(!CBox()->GetValue());
1473 CBox()->SetValue(true);
1477 CBox()->SetValue(false);
1482 wxString
wxGridCellBoolEditor::GetValue() const
1484 return ms_stringValues
[CBox()->GetValue()];
1488 wxGridCellBoolEditor::UseStringValues(const wxString
& valueTrue
,
1489 const wxString
& valueFalse
)
1491 ms_stringValues
[false] = valueFalse
;
1492 ms_stringValues
[true] = valueTrue
;
1496 wxGridCellBoolEditor::IsTrueValue(const wxString
& value
)
1498 return value
== ms_stringValues
[true];
1501 #endif // wxUSE_CHECKBOX
1505 // ----------------------------------------------------------------------------
1506 // wxGridCellChoiceEditor
1507 // ----------------------------------------------------------------------------
1509 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1511 : m_choices(choices
),
1512 m_allowOthers(allowOthers
) { }
1514 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1515 const wxString choices
[],
1517 : m_allowOthers(allowOthers
)
1521 m_choices
.Alloc(count
);
1522 for ( size_t n
= 0; n
< count
; n
++ )
1524 m_choices
.Add(choices
[n
]);
1529 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1531 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1532 editor
->m_allowOthers
= m_allowOthers
;
1533 editor
->m_choices
= m_choices
;
1538 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1540 wxEvtHandler
* evtHandler
)
1542 int style
= wxTE_PROCESS_ENTER
|
1546 if ( !m_allowOthers
)
1547 style
|= wxCB_READONLY
;
1548 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1549 wxDefaultPosition
, wxDefaultSize
,
1553 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1556 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1557 wxGridCellAttr
* attr
)
1559 // as we fill the entire client area, don't do anything here to minimize
1562 // TODO: It doesn't actually fill the client area since the height of a
1563 // combo always defaults to the standard. Until someone has time to
1564 // figure out the right rectangle to paint, just do it the normal way.
1565 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1568 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1570 wxASSERT_MSG(m_control
,
1571 wxT("The wxGridCellEditor must be created first!"));
1573 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1575 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1577 // Don't immediately end if we get a kill focus event within BeginEdit
1579 evtHandler
->SetInSetFocus(true);
1581 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1585 Combo()->SetValue(m_startValue
);
1586 Combo()->SetInsertionPointEnd();
1588 else // the combobox is read-only
1590 // find the right position, or default to the first if not found
1591 int pos
= Combo()->FindString(m_startValue
);
1592 if (pos
== wxNOT_FOUND
)
1594 Combo()->SetSelection(pos
);
1597 Combo()->SetFocus();
1601 // When dropping down the menu, a kill focus event
1602 // happens after this point, so we can't reset the flag yet.
1603 #if !defined(__WXGTK20__)
1604 evtHandler
->SetInSetFocus(false);
1609 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1612 wxString value
= Combo()->GetValue();
1613 if ( value
== m_startValue
)
1616 grid
->GetTable()->SetValue(row
, col
, value
);
1621 void wxGridCellChoiceEditor::Reset()
1623 Combo()->SetValue(m_startValue
);
1624 Combo()->SetInsertionPointEnd();
1627 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1637 wxStringTokenizer
tk(params
, _T(','));
1638 while ( tk
.HasMoreTokens() )
1640 m_choices
.Add(tk
.GetNextToken());
1644 // return the value in the text control
1645 wxString
wxGridCellChoiceEditor::GetValue() const
1647 return Combo()->GetValue();
1650 #endif // wxUSE_COMBOBOX
1652 // ----------------------------------------------------------------------------
1653 // wxGridCellEditorEvtHandler
1654 // ----------------------------------------------------------------------------
1656 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
1658 // Don't disable the cell if we're just starting to edit it
1663 m_grid
->DisableCellEditControl();
1668 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1670 switch ( event
.GetKeyCode() )
1674 m_grid
->DisableCellEditControl();
1678 m_grid
->GetEventHandler()->ProcessEvent( event
);
1682 case WXK_NUMPAD_ENTER
:
1683 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1684 m_editor
->HandleReturn(event
);
1693 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1695 int row
= m_grid
->GetGridCursorRow();
1696 int col
= m_grid
->GetGridCursorCol();
1697 wxRect rect
= m_grid
->CellToRect( row
, col
);
1699 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
1701 // if cell width is smaller than grid client area, cell is wholly visible
1702 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
1704 switch ( event
.GetKeyCode() )
1709 case WXK_NUMPAD_ENTER
:
1714 if ( wholeCellVisible
)
1716 // no special processing needed...
1721 // do special processing for partly visible cell...
1723 // get the widths of all cells previous to this one
1725 for ( int i
= 0; i
< col
; i
++ )
1727 colXPos
+= m_grid
->GetColSize(i
);
1730 int xUnit
= 1, yUnit
= 1;
1731 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1734 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1738 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
1746 if ( wholeCellVisible
)
1748 // no special processing needed...
1753 // do special processing for partly visible cell...
1756 wxString value
= m_grid
->GetCellValue(row
, col
);
1757 if ( wxEmptyString
!= value
)
1759 // get width of cell CONTENTS (text)
1761 wxFont font
= m_grid
->GetCellFont(row
, col
);
1762 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
1764 // try to RIGHT align the text by scrolling
1765 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
1767 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
1768 // otherwise the last part of the cell content might be hidden below the scroll bar
1769 // FIXME: maybe there is a more suitable correction?
1770 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
1771 if ( textWidth
< 0 )
1777 // get the widths of all cells previous to this one
1779 for ( int i
= 0; i
< col
; i
++ )
1781 colXPos
+= m_grid
->GetColSize(i
);
1784 // and add the (modified) text width of the cell contents
1785 // as we'd like to see the last part of the cell contents
1786 colXPos
+= textWidth
;
1788 int xUnit
= 1, yUnit
= 1;
1789 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1790 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1801 // ----------------------------------------------------------------------------
1802 // wxGridCellWorker is an (almost) empty common base class for
1803 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1804 // ----------------------------------------------------------------------------
1806 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1811 wxGridCellWorker::~wxGridCellWorker()
1815 // ============================================================================
1817 // ============================================================================
1819 // ----------------------------------------------------------------------------
1820 // wxGridCellRenderer
1821 // ----------------------------------------------------------------------------
1823 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1824 wxGridCellAttr
& attr
,
1827 int WXUNUSED(row
), int WXUNUSED(col
),
1830 dc
.SetBackgroundMode( wxBRUSHSTYLE_SOLID
);
1832 // grey out fields if the grid is disabled
1833 if ( grid
.IsEnabled() )
1838 if ( grid
.HasFocus() )
1839 clr
= grid
.GetSelectionBackground();
1841 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
1842 dc
.SetBrush( wxBrush(clr
, wxBRUSHSTYLE_SOLID
) );
1846 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxBRUSHSTYLE_SOLID
) );
1851 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
), wxBRUSHSTYLE_SOLID
));
1854 dc
.SetPen( *wxTRANSPARENT_PEN
);
1855 dc
.DrawRectangle(rect
);
1858 // ----------------------------------------------------------------------------
1859 // wxGridCellStringRenderer
1860 // ----------------------------------------------------------------------------
1862 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
1863 const wxGridCellAttr
& attr
,
1867 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
1869 // TODO some special colours for attr.IsReadOnly() case?
1871 // different coloured text when the grid is disabled
1872 if ( grid
.IsEnabled() )
1877 if ( grid
.HasFocus() )
1878 clr
= grid
.GetSelectionBackground();
1880 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
1881 dc
.SetTextBackground( clr
);
1882 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1886 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1887 dc
.SetTextForeground( attr
.GetTextColour() );
1892 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1893 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
1896 dc
.SetFont( attr
.GetFont() );
1899 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
1901 const wxString
& text
)
1903 wxCoord x
= 0, y
= 0, max_x
= 0;
1904 dc
.SetFont(attr
.GetFont());
1905 wxStringTokenizer
tk(text
, _T('\n'));
1906 while ( tk
.HasMoreTokens() )
1908 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1909 max_x
= wxMax(max_x
, x
);
1912 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1914 return wxSize(max_x
, y
);
1917 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1918 wxGridCellAttr
& attr
,
1922 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1925 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1926 wxGridCellAttr
& attr
,
1928 const wxRect
& rectCell
,
1932 wxRect rect
= rectCell
;
1935 // erase only this cells background, overflow cells should have been erased
1936 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1939 attr
.GetAlignment(&hAlign
, &vAlign
);
1941 int overflowCols
= 0;
1943 if (attr
.GetOverflow())
1945 int cols
= grid
.GetNumberCols();
1946 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1947 int cell_rows
, cell_cols
;
1948 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <= 0
1949 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1951 int i
, c_cols
, c_rows
;
1952 for (i
= col
+cell_cols
; i
< cols
; i
++)
1954 bool is_empty
= true;
1955 for (int j
=row
; j
< row
+ cell_rows
; j
++)
1957 // check w/ anchor cell for multicell block
1958 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1961 if (!grid
.GetTable()->IsEmptyCell(j
+ c_rows
, i
))
1970 rect
.width
+= grid
.GetColSize(i
);
1978 if (rect
.width
>= best_width
)
1982 overflowCols
= i
- col
- cell_cols
+ 1;
1983 if (overflowCols
>= cols
)
1984 overflowCols
= cols
- 1;
1987 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1989 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1991 clip
.x
+= rectCell
.width
;
1992 // draw each overflow cell individually
1993 int col_end
= col
+ cell_cols
+ overflowCols
;
1994 if (col_end
>= grid
.GetNumberCols())
1995 col_end
= grid
.GetNumberCols() - 1;
1996 for (int i
= col
+ cell_cols
; i
<= col_end
; i
++)
1998 clip
.width
= grid
.GetColSize(i
) - 1;
1999 dc
.DestroyClippingRegion();
2000 dc
.SetClippingRegion(clip
);
2002 SetTextColoursAndFont(grid
, attr
, dc
,
2003 grid
.IsInSelection(row
,i
));
2005 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
2006 rect
, hAlign
, vAlign
);
2007 clip
.x
+= grid
.GetColSize(i
) - 1;
2013 dc
.DestroyClippingRegion();
2017 // now we only have to draw the text
2018 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2020 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
2021 rect
, hAlign
, vAlign
);
2024 // ----------------------------------------------------------------------------
2025 // wxGridCellNumberRenderer
2026 // ----------------------------------------------------------------------------
2028 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2030 wxGridTableBase
*table
= grid
.GetTable();
2032 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
2034 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
2038 text
= table
->GetValue(row
, col
);
2044 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
2045 wxGridCellAttr
& attr
,
2047 const wxRect
& rectCell
,
2051 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2053 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2055 // draw the text right aligned by default
2057 attr
.GetAlignment(&hAlign
, &vAlign
);
2058 hAlign
= wxALIGN_RIGHT
;
2060 wxRect rect
= rectCell
;
2063 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2066 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
2067 wxGridCellAttr
& attr
,
2071 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2074 // ----------------------------------------------------------------------------
2075 // wxGridCellFloatRenderer
2076 // ----------------------------------------------------------------------------
2078 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
2081 SetPrecision(precision
);
2084 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
2086 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
2087 renderer
->m_width
= m_width
;
2088 renderer
->m_precision
= m_precision
;
2089 renderer
->m_format
= m_format
;
2094 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2096 wxGridTableBase
*table
= grid
.GetTable();
2101 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
2103 val
= table
->GetValueAsDouble(row
, col
);
2108 text
= table
->GetValue(row
, col
);
2109 hasDouble
= text
.ToDouble(&val
);
2116 if ( m_width
== -1 )
2118 if ( m_precision
== -1 )
2120 // default width/precision
2121 m_format
= _T("%f");
2125 m_format
.Printf(_T("%%.%df"), m_precision
);
2128 else if ( m_precision
== -1 )
2130 // default precision
2131 m_format
.Printf(_T("%%%d.f"), m_width
);
2135 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
2139 text
.Printf(m_format
, val
);
2142 //else: text already contains the string
2147 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
2148 wxGridCellAttr
& attr
,
2150 const wxRect
& rectCell
,
2154 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2156 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2158 // draw the text right aligned by default
2160 attr
.GetAlignment(&hAlign
, &vAlign
);
2161 hAlign
= wxALIGN_RIGHT
;
2163 wxRect rect
= rectCell
;
2166 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2169 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
2170 wxGridCellAttr
& attr
,
2174 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2177 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
2181 // reset to defaults
2187 wxString tmp
= params
.BeforeFirst(_T(','));
2191 if ( tmp
.ToLong(&width
) )
2193 SetWidth((int)width
);
2197 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
2201 tmp
= params
.AfterFirst(_T(','));
2205 if ( tmp
.ToLong(&precision
) )
2207 SetPrecision((int)precision
);
2211 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
2217 // ----------------------------------------------------------------------------
2218 // wxGridCellBoolRenderer
2219 // ----------------------------------------------------------------------------
2221 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
2223 // FIXME these checkbox size calculations are really ugly...
2225 // between checkmark and box
2226 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2228 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2229 wxGridCellAttr
& WXUNUSED(attr
),
2234 // compute it only once (no locks for MT safeness in GUI thread...)
2235 if ( !ms_sizeCheckMark
.x
)
2237 // get checkbox size
2238 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2239 wxSize size
= checkbox
->GetBestSize();
2240 wxCoord checkSize
= size
.y
+ 2 * wxGRID_CHECKMARK_MARGIN
;
2242 #if defined(__WXMOTIF__)
2243 checkSize
-= size
.y
/ 2;
2248 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2251 return ms_sizeCheckMark
;
2254 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2255 wxGridCellAttr
& attr
,
2261 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2263 // draw a check mark in the centre (ignoring alignment - TODO)
2264 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2266 // don't draw outside the cell
2267 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2268 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2270 // and even leave (at least) 1 pixel margin
2271 size
.x
= size
.y
= minSize
;
2274 // draw a border around checkmark
2276 attr
.GetAlignment(&hAlign
, &vAlign
);
2279 if (hAlign
== wxALIGN_CENTRE
)
2281 rectBorder
.x
= rect
.x
+ rect
.width
/ 2 - size
.x
/ 2;
2282 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2283 rectBorder
.width
= size
.x
;
2284 rectBorder
.height
= size
.y
;
2286 else if (hAlign
== wxALIGN_LEFT
)
2288 rectBorder
.x
= rect
.x
+ 2;
2289 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2290 rectBorder
.width
= size
.x
;
2291 rectBorder
.height
= size
.y
;
2293 else if (hAlign
== wxALIGN_RIGHT
)
2295 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2296 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2297 rectBorder
.width
= size
.x
;
2298 rectBorder
.height
= size
.y
;
2302 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2304 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2308 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2309 value
= wxGridCellBoolEditor::IsTrueValue(cellval
);
2314 flags
|= wxCONTROL_CHECKED
;
2316 wxRendererNative::Get().DrawCheckBox( &grid
, dc
, rectBorder
, flags
);
2319 // ----------------------------------------------------------------------------
2321 // ----------------------------------------------------------------------------
2323 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2327 m_isReadOnly
= Unset
;
2332 m_attrkind
= wxGridCellAttr::Cell
;
2334 m_sizeRows
= m_sizeCols
= 1;
2335 m_overflow
= UnsetOverflow
;
2337 SetDefAttr(attrDefault
);
2340 wxGridCellAttr
*wxGridCellAttr::Clone() const
2342 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2344 if ( HasTextColour() )
2345 attr
->SetTextColour(GetTextColour());
2346 if ( HasBackgroundColour() )
2347 attr
->SetBackgroundColour(GetBackgroundColour());
2349 attr
->SetFont(GetFont());
2350 if ( HasAlignment() )
2351 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2353 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2357 attr
->SetRenderer(m_renderer
);
2358 m_renderer
->IncRef();
2362 attr
->SetEditor(m_editor
);
2367 attr
->SetReadOnly();
2369 attr
->SetOverflow( m_overflow
== Overflow
);
2370 attr
->SetKind( m_attrkind
);
2375 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2377 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2378 SetTextColour(mergefrom
->GetTextColour());
2379 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2380 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2381 if ( !HasFont() && mergefrom
->HasFont() )
2382 SetFont(mergefrom
->GetFont());
2383 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2386 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2387 SetAlignment(hAlign
, vAlign
);
2389 if ( !HasSize() && mergefrom
->HasSize() )
2390 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2392 // Directly access member functions as GetRender/Editor don't just return
2393 // m_renderer/m_editor
2395 // Maybe add support for merge of Render and Editor?
2396 if (!HasRenderer() && mergefrom
->HasRenderer() )
2398 m_renderer
= mergefrom
->m_renderer
;
2399 m_renderer
->IncRef();
2401 if ( !HasEditor() && mergefrom
->HasEditor() )
2403 m_editor
= mergefrom
->m_editor
;
2406 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2407 SetReadOnly(mergefrom
->IsReadOnly());
2409 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2410 SetOverflow(mergefrom
->GetOverflow());
2412 SetDefAttr(mergefrom
->m_defGridAttr
);
2415 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2417 // The size of a cell is normally 1,1
2419 // If this cell is larger (2,2) then this is the top left cell
2420 // the other cells that will be covered (lower right cells) must be
2421 // set to negative or zero values such that
2422 // row + num_rows of the covered cell points to the larger cell (this cell)
2423 // same goes for the col + num_cols.
2425 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2427 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
2428 !((num_rows
<= 0) && (num_cols
> 0)) ||
2429 !((num_rows
== 0) && (num_cols
== 0))),
2430 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2432 m_sizeRows
= num_rows
;
2433 m_sizeCols
= num_cols
;
2436 const wxColour
& wxGridCellAttr::GetTextColour() const
2438 if (HasTextColour())
2442 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2444 return m_defGridAttr
->GetTextColour();
2448 wxFAIL_MSG(wxT("Missing default cell attribute"));
2449 return wxNullColour
;
2453 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2455 if (HasBackgroundColour())
2459 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2461 return m_defGridAttr
->GetBackgroundColour();
2465 wxFAIL_MSG(wxT("Missing default cell attribute"));
2466 return wxNullColour
;
2470 const wxFont
& wxGridCellAttr::GetFont() const
2476 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2478 return m_defGridAttr
->GetFont();
2482 wxFAIL_MSG(wxT("Missing default cell attribute"));
2487 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2496 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2498 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2502 wxFAIL_MSG(wxT("Missing default cell attribute"));
2506 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2509 *num_rows
= m_sizeRows
;
2511 *num_cols
= m_sizeCols
;
2514 // GetRenderer and GetEditor use a slightly different decision path about
2515 // which attribute to use. If a non-default attr object has one then it is
2516 // used, otherwise the default editor or renderer is fetched from the grid and
2517 // used. It should be the default for the data type of the cell. If it is
2518 // NULL (because the table has a type that the grid does not have in its
2519 // registry), then the grid's default editor or renderer is used.
2521 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(const wxGrid
* grid
, int row
, int col
) const
2523 wxGridCellRenderer
*renderer
= NULL
;
2525 if ( m_renderer
&& this != m_defGridAttr
)
2527 // use the cells renderer if it has one
2528 renderer
= m_renderer
;
2531 else // no non-default cell renderer
2533 // get default renderer for the data type
2536 // GetDefaultRendererForCell() will do IncRef() for us
2537 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2540 if ( renderer
== NULL
)
2542 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2544 // if we still don't have one then use the grid default
2545 // (no need for IncRef() here neither)
2546 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2548 else // default grid attr
2550 // use m_renderer which we had decided not to use initially
2551 renderer
= m_renderer
;
2558 // we're supposed to always find something
2559 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2564 // same as above, except for s/renderer/editor/g
2565 wxGridCellEditor
* wxGridCellAttr::GetEditor(const wxGrid
* grid
, int row
, int col
) const
2567 wxGridCellEditor
*editor
= NULL
;
2569 if ( m_editor
&& this != m_defGridAttr
)
2571 // use the cells editor if it has one
2575 else // no non default cell editor
2577 // get default editor for the data type
2580 // GetDefaultEditorForCell() will do IncRef() for us
2581 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2584 if ( editor
== NULL
)
2586 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2588 // if we still don't have one then use the grid default
2589 // (no need for IncRef() here neither)
2590 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2592 else // default grid attr
2594 // use m_editor which we had decided not to use initially
2602 // we're supposed to always find something
2603 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2608 // ----------------------------------------------------------------------------
2609 // wxGridCellAttrData
2610 // ----------------------------------------------------------------------------
2612 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2614 // Note: contrary to wxGridRowOrColAttrData::SetAttr, we must not
2615 // touch attribute's reference counting explicitly, since this
2616 // is managed by class wxGridCellWithAttr
2617 int n
= FindIndex(row
, col
);
2618 if ( n
== wxNOT_FOUND
)
2622 // add the attribute
2623 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2625 //else: nothing to do
2627 else // we already have an attribute for this cell
2631 // change the attribute
2632 m_attrs
[(size_t)n
].ChangeAttr(attr
);
2636 // remove this attribute
2637 m_attrs
.RemoveAt((size_t)n
);
2642 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2644 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2646 int n
= FindIndex(row
, col
);
2647 if ( n
!= wxNOT_FOUND
)
2649 attr
= m_attrs
[(size_t)n
].attr
;
2656 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2658 size_t count
= m_attrs
.GetCount();
2659 for ( size_t n
= 0; n
< count
; n
++ )
2661 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2662 wxCoord row
= coords
.GetRow();
2663 if ((size_t)row
>= pos
)
2667 // If rows inserted, include row counter where necessary
2668 coords
.SetRow(row
+ numRows
);
2670 else if (numRows
< 0)
2672 // If rows deleted ...
2673 if ((size_t)row
>= pos
- numRows
)
2675 // ...either decrement row counter (if row still exists)...
2676 coords
.SetRow(row
+ numRows
);
2680 // ...or remove the attribute
2681 m_attrs
.RemoveAt(n
);
2690 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2692 size_t count
= m_attrs
.GetCount();
2693 for ( size_t n
= 0; n
< count
; n
++ )
2695 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2696 wxCoord col
= coords
.GetCol();
2697 if ( (size_t)col
>= pos
)
2701 // If rows inserted, include row counter where necessary
2702 coords
.SetCol(col
+ numCols
);
2704 else if (numCols
< 0)
2706 // If rows deleted ...
2707 if ((size_t)col
>= pos
- numCols
)
2709 // ...either decrement row counter (if row still exists)...
2710 coords
.SetCol(col
+ numCols
);
2714 // ...or remove the attribute
2715 m_attrs
.RemoveAt(n
);
2724 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2726 size_t count
= m_attrs
.GetCount();
2727 for ( size_t n
= 0; n
< count
; n
++ )
2729 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2730 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2739 // ----------------------------------------------------------------------------
2740 // wxGridRowOrColAttrData
2741 // ----------------------------------------------------------------------------
2743 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2745 size_t count
= m_attrs
.GetCount();
2746 for ( size_t n
= 0; n
< count
; n
++ )
2748 m_attrs
[n
]->DecRef();
2752 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2754 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2756 int n
= m_rowsOrCols
.Index(rowOrCol
);
2757 if ( n
!= wxNOT_FOUND
)
2759 attr
= m_attrs
[(size_t)n
];
2766 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2768 int i
= m_rowsOrCols
.Index(rowOrCol
);
2769 if ( i
== wxNOT_FOUND
)
2773 // add the attribute - no need to do anything to reference count
2774 // since we take ownership of the attribute.
2775 m_rowsOrCols
.Add(rowOrCol
);
2778 // nothing to remove
2782 size_t n
= (size_t)i
;
2783 if ( m_attrs
[n
] == attr
)
2788 // change the attribute, handling reference count manually,
2789 // taking ownership of the new attribute.
2790 m_attrs
[n
]->DecRef();
2795 // remove this attribute, handling reference count manually
2796 m_attrs
[n
]->DecRef();
2797 m_rowsOrCols
.RemoveAt(n
);
2798 m_attrs
.RemoveAt(n
);
2803 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2805 size_t count
= m_attrs
.GetCount();
2806 for ( size_t n
= 0; n
< count
; n
++ )
2808 int & rowOrCol
= m_rowsOrCols
[n
];
2809 if ( (size_t)rowOrCol
>= pos
)
2811 if ( numRowsOrCols
> 0 )
2813 // If rows inserted, include row counter where necessary
2814 rowOrCol
+= numRowsOrCols
;
2816 else if ( numRowsOrCols
< 0)
2818 // If rows deleted, either decrement row counter (if row still exists)
2819 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2820 rowOrCol
+= numRowsOrCols
;
2823 m_rowsOrCols
.RemoveAt(n
);
2824 m_attrs
[n
]->DecRef();
2825 m_attrs
.RemoveAt(n
);
2834 // ----------------------------------------------------------------------------
2835 // wxGridCellAttrProvider
2836 // ----------------------------------------------------------------------------
2838 wxGridCellAttrProvider::wxGridCellAttrProvider()
2840 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2843 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2848 void wxGridCellAttrProvider::InitData()
2850 m_data
= new wxGridCellAttrProviderData
;
2853 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2854 wxGridCellAttr::wxAttrKind kind
) const
2856 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2861 case (wxGridCellAttr::Any
):
2862 // Get cached merge attributes.
2863 // Currently not used as no cache implemented as not mutable
2864 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2867 // Basically implement old version.
2868 // Also check merge cache, so we don't have to re-merge every time..
2869 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2870 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2871 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2873 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
2875 // Two or more are non NULL
2876 attr
= new wxGridCellAttr
;
2877 attr
->SetKind(wxGridCellAttr::Merged
);
2879 // Order is important..
2882 attr
->MergeWith(attrcell
);
2887 attr
->MergeWith(attrcol
);
2892 attr
->MergeWith(attrrow
);
2896 // store merge attr if cache implemented
2898 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2902 // one or none is non null return it or null.
2921 case (wxGridCellAttr::Cell
):
2922 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2925 case (wxGridCellAttr::Col
):
2926 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2929 case (wxGridCellAttr::Row
):
2930 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2935 // (wxGridCellAttr::Default):
2936 // (wxGridCellAttr::Merged):
2944 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2950 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2953 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2958 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2961 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2966 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2969 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2973 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2975 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2979 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2983 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2985 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2989 // ----------------------------------------------------------------------------
2990 // wxGridTypeRegistry
2991 // ----------------------------------------------------------------------------
2993 wxGridTypeRegistry::~wxGridTypeRegistry()
2995 size_t count
= m_typeinfo
.GetCount();
2996 for ( size_t i
= 0; i
< count
; i
++ )
2997 delete m_typeinfo
[i
];
3000 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
3001 wxGridCellRenderer
* renderer
,
3002 wxGridCellEditor
* editor
)
3004 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
3006 // is it already registered?
3007 int loc
= FindRegisteredDataType(typeName
);
3008 if ( loc
!= wxNOT_FOUND
)
3010 delete m_typeinfo
[loc
];
3011 m_typeinfo
[loc
] = info
;
3015 m_typeinfo
.Add(info
);
3019 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
3021 size_t count
= m_typeinfo
.GetCount();
3022 for ( size_t i
= 0; i
< count
; i
++ )
3024 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
3033 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
3035 int index
= FindRegisteredDataType(typeName
);
3036 if ( index
== wxNOT_FOUND
)
3038 // check whether this is one of the standard ones, in which case
3039 // register it "on the fly"
3041 if ( typeName
== wxGRID_VALUE_STRING
)
3043 RegisterDataType(wxGRID_VALUE_STRING
,
3044 new wxGridCellStringRenderer
,
3045 new wxGridCellTextEditor
);
3048 #endif // wxUSE_TEXTCTRL
3050 if ( typeName
== wxGRID_VALUE_BOOL
)
3052 RegisterDataType(wxGRID_VALUE_BOOL
,
3053 new wxGridCellBoolRenderer
,
3054 new wxGridCellBoolEditor
);
3057 #endif // wxUSE_CHECKBOX
3059 if ( typeName
== wxGRID_VALUE_NUMBER
)
3061 RegisterDataType(wxGRID_VALUE_NUMBER
,
3062 new wxGridCellNumberRenderer
,
3063 new wxGridCellNumberEditor
);
3065 else if ( typeName
== wxGRID_VALUE_FLOAT
)
3067 RegisterDataType(wxGRID_VALUE_FLOAT
,
3068 new wxGridCellFloatRenderer
,
3069 new wxGridCellFloatEditor
);
3072 #endif // wxUSE_TEXTCTRL
3074 if ( typeName
== wxGRID_VALUE_CHOICE
)
3076 RegisterDataType(wxGRID_VALUE_CHOICE
,
3077 new wxGridCellStringRenderer
,
3078 new wxGridCellChoiceEditor
);
3081 #endif // wxUSE_COMBOBOX
3086 // we get here only if just added the entry for this type, so return
3088 index
= m_typeinfo
.GetCount() - 1;
3094 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
3096 int index
= FindDataType(typeName
);
3097 if ( index
== wxNOT_FOUND
)
3099 // the first part of the typename is the "real" type, anything after ':'
3100 // are the parameters for the renderer
3101 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
3102 if ( index
== wxNOT_FOUND
)
3107 wxGridCellRenderer
*renderer
= GetRenderer(index
);
3108 wxGridCellRenderer
*rendererOld
= renderer
;
3109 renderer
= renderer
->Clone();
3110 rendererOld
->DecRef();
3112 wxGridCellEditor
*editor
= GetEditor(index
);
3113 wxGridCellEditor
*editorOld
= editor
;
3114 editor
= editor
->Clone();
3115 editorOld
->DecRef();
3117 // do it even if there are no parameters to reset them to defaults
3118 wxString params
= typeName
.AfterFirst(_T(':'));
3119 renderer
->SetParameters(params
);
3120 editor
->SetParameters(params
);
3122 // register the new typename
3123 RegisterDataType(typeName
, renderer
, editor
);
3125 // we just registered it, it's the last one
3126 index
= m_typeinfo
.GetCount() - 1;
3132 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
3134 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
3141 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
3143 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
3150 // ----------------------------------------------------------------------------
3152 // ----------------------------------------------------------------------------
3154 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
3156 wxGridTableBase::wxGridTableBase()
3158 m_view
= (wxGrid
*) NULL
;
3159 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
3162 wxGridTableBase::~wxGridTableBase()
3164 delete m_attrProvider
;
3167 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
3169 delete m_attrProvider
;
3170 m_attrProvider
= attrProvider
;
3173 bool wxGridTableBase::CanHaveAttributes()
3175 if ( ! GetAttrProvider() )
3177 // use the default attr provider by default
3178 SetAttrProvider(new wxGridCellAttrProvider
);
3184 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
3186 if ( m_attrProvider
)
3187 return m_attrProvider
->GetAttr(row
, col
, kind
);
3189 return (wxGridCellAttr
*)NULL
;
3192 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
3194 if ( m_attrProvider
)
3197 attr
->SetKind(wxGridCellAttr::Cell
);
3198 m_attrProvider
->SetAttr(attr
, row
, col
);
3202 // as we take ownership of the pointer and don't store it, we must
3208 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3210 if ( m_attrProvider
)
3212 attr
->SetKind(wxGridCellAttr::Row
);
3213 m_attrProvider
->SetRowAttr(attr
, row
);
3217 // as we take ownership of the pointer and don't store it, we must
3223 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3225 if ( m_attrProvider
)
3227 attr
->SetKind(wxGridCellAttr::Col
);
3228 m_attrProvider
->SetColAttr(attr
, col
);
3232 // as we take ownership of the pointer and don't store it, we must
3238 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3239 size_t WXUNUSED(numRows
) )
3241 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3246 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3248 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3253 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3254 size_t WXUNUSED(numRows
) )
3256 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3261 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3262 size_t WXUNUSED(numCols
) )
3264 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3269 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3271 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3276 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3277 size_t WXUNUSED(numCols
) )
3279 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3284 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3288 // RD: Starting the rows at zero confuses users,
3289 // no matter how much it makes sense to us geeks.
3295 wxString
wxGridTableBase::GetColLabelValue( int col
)
3297 // default col labels are:
3298 // cols 0 to 25 : A-Z
3299 // cols 26 to 675 : AA-ZZ
3304 for ( n
= 1; ; n
++ )
3306 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
3312 // reverse the string...
3314 for ( i
= 0; i
< n
; i
++ )
3322 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3324 return wxGRID_VALUE_STRING
;
3327 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3328 const wxString
& typeName
)
3330 return typeName
== wxGRID_VALUE_STRING
;
3333 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3335 return CanGetValueAs(row
, col
, typeName
);
3338 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3343 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3348 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3353 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3354 long WXUNUSED(value
) )
3358 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3359 double WXUNUSED(value
) )
3363 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3364 bool WXUNUSED(value
) )
3368 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3369 const wxString
& WXUNUSED(typeName
) )
3374 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3375 const wxString
& WXUNUSED(typeName
),
3376 void* WXUNUSED(value
) )
3380 //////////////////////////////////////////////////////////////////////
3382 // Message class for the grid table to send requests and notifications
3386 wxGridTableMessage::wxGridTableMessage()
3388 m_table
= (wxGridTableBase
*) NULL
;
3394 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3395 int commandInt1
, int commandInt2
)
3399 m_comInt1
= commandInt1
;
3400 m_comInt2
= commandInt2
;
3403 //////////////////////////////////////////////////////////////////////
3405 // A basic grid table for string data. An object of this class will
3406 // created by wxGrid if you don't specify an alternative table class.
3409 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3411 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3413 wxGridStringTable::wxGridStringTable()
3418 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3421 m_data
.Alloc( numRows
);
3424 sa
.Alloc( numCols
);
3425 sa
.Add( wxEmptyString
, numCols
);
3427 m_data
.Add( sa
, numRows
);
3430 wxGridStringTable::~wxGridStringTable()
3434 int wxGridStringTable::GetNumberRows()
3436 return m_data
.GetCount();
3439 int wxGridStringTable::GetNumberCols()
3441 if ( m_data
.GetCount() > 0 )
3442 return m_data
[0].GetCount();
3447 wxString
wxGridStringTable::GetValue( int row
, int col
)
3449 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3451 _T("invalid row or column index in wxGridStringTable") );
3453 return m_data
[row
][col
];
3456 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3458 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3459 _T("invalid row or column index in wxGridStringTable") );
3461 m_data
[row
][col
] = value
;
3464 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3466 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3468 _T("invalid row or column index in wxGridStringTable") );
3470 return (m_data
[row
][col
] == wxEmptyString
);
3473 void wxGridStringTable::Clear()
3476 int numRows
, numCols
;
3478 numRows
= m_data
.GetCount();
3481 numCols
= m_data
[0].GetCount();
3483 for ( row
= 0; row
< numRows
; row
++ )
3485 for ( col
= 0; col
< numCols
; col
++ )
3487 m_data
[row
][col
] = wxEmptyString
;
3493 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3495 size_t curNumRows
= m_data
.GetCount();
3496 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3497 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3499 if ( pos
>= curNumRows
)
3501 return AppendRows( numRows
);
3505 sa
.Alloc( curNumCols
);
3506 sa
.Add( wxEmptyString
, curNumCols
);
3507 m_data
.Insert( sa
, pos
, numRows
);
3511 wxGridTableMessage
msg( this,
3512 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3516 GetView()->ProcessTableMessage( msg
);
3522 bool wxGridStringTable::AppendRows( size_t numRows
)
3524 size_t curNumRows
= m_data
.GetCount();
3525 size_t curNumCols
= ( curNumRows
> 0
3526 ? m_data
[0].GetCount()
3527 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3530 if ( curNumCols
> 0 )
3532 sa
.Alloc( curNumCols
);
3533 sa
.Add( wxEmptyString
, curNumCols
);
3536 m_data
.Add( sa
, numRows
);
3540 wxGridTableMessage
msg( this,
3541 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3544 GetView()->ProcessTableMessage( msg
);
3550 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3552 size_t curNumRows
= m_data
.GetCount();
3554 if ( pos
>= curNumRows
)
3556 wxFAIL_MSG( wxString::Format
3558 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3560 (unsigned long)numRows
,
3561 (unsigned long)curNumRows
3567 if ( numRows
> curNumRows
- pos
)
3569 numRows
= curNumRows
- pos
;
3572 if ( numRows
>= curNumRows
)
3578 m_data
.RemoveAt( pos
, numRows
);
3583 wxGridTableMessage
msg( this,
3584 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3588 GetView()->ProcessTableMessage( msg
);
3594 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3598 size_t curNumRows
= m_data
.GetCount();
3599 size_t curNumCols
= ( curNumRows
> 0
3600 ? m_data
[0].GetCount()
3601 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3603 if ( pos
>= curNumCols
)
3605 return AppendCols( numCols
);
3608 if ( !m_colLabels
.IsEmpty() )
3610 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
3613 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
3614 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
3617 for ( row
= 0; row
< curNumRows
; row
++ )
3619 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3621 m_data
[row
].Insert( wxEmptyString
, col
);
3627 wxGridTableMessage
msg( this,
3628 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3632 GetView()->ProcessTableMessage( msg
);
3638 bool wxGridStringTable::AppendCols( size_t numCols
)
3642 size_t curNumRows
= m_data
.GetCount();
3647 // TODO: something better than this ?
3649 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3654 for ( row
= 0; row
< curNumRows
; row
++ )
3656 m_data
[row
].Add( wxEmptyString
, numCols
);
3661 wxGridTableMessage
msg( this,
3662 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3665 GetView()->ProcessTableMessage( msg
);
3671 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3675 size_t curNumRows
= m_data
.GetCount();
3676 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3677 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3679 if ( pos
>= curNumCols
)
3681 wxFAIL_MSG( wxString::Format
3683 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3685 (unsigned long)numCols
,
3686 (unsigned long)curNumCols
3693 colID
= GetView()->GetColAt( pos
);
3697 if ( numCols
> curNumCols
- colID
)
3699 numCols
= curNumCols
- colID
;
3702 if ( !m_colLabels
.IsEmpty() )
3704 // m_colLabels stores just as many elements as it needs, e.g. if only
3705 // the label of the first column had been set it would have only one
3706 // element and not numCols, so account for it
3707 int nToRm
= m_colLabels
.size() - colID
;
3709 m_colLabels
.RemoveAt( colID
, nToRm
);
3712 for ( row
= 0; row
< curNumRows
; row
++ )
3714 if ( numCols
>= curNumCols
)
3716 m_data
[row
].Clear();
3720 m_data
[row
].RemoveAt( colID
, numCols
);
3726 wxGridTableMessage
msg( this,
3727 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3731 GetView()->ProcessTableMessage( msg
);
3737 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3739 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3741 // using default label
3743 return wxGridTableBase::GetRowLabelValue( row
);
3747 return m_rowLabels
[row
];
3751 wxString
wxGridStringTable::GetColLabelValue( int col
)
3753 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3755 // using default label
3757 return wxGridTableBase::GetColLabelValue( col
);
3761 return m_colLabels
[col
];
3765 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3767 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3769 int n
= m_rowLabels
.GetCount();
3772 for ( i
= n
; i
<= row
; i
++ )
3774 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3778 m_rowLabels
[row
] = value
;
3781 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3783 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3785 int n
= m_colLabels
.GetCount();
3788 for ( i
= n
; i
<= col
; i
++ )
3790 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3794 m_colLabels
[col
] = value
;
3798 //////////////////////////////////////////////////////////////////////
3799 //////////////////////////////////////////////////////////////////////
3801 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
)
3802 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost
)
3805 void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
3807 m_owner
->CancelMouseCapture();
3810 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3812 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow
)
3813 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3814 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3815 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3818 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3820 const wxPoint
&pos
, const wxSize
&size
)
3821 : wxGridSubwindow(parent
, id
, pos
, size
)
3826 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3830 // NO - don't do this because it will set both the x and y origin
3831 // coords to match the parent scrolled window and we just want to
3832 // set the y coord - MB
3834 // m_owner->PrepareDC( dc );
3837 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3838 wxPoint pt
= dc
.GetDeviceOrigin();
3839 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
3841 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3842 m_owner
->DrawRowLabels( dc
, rows
);
3845 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3847 m_owner
->ProcessRowLabelMouseEvent( event
);
3850 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3852 m_owner
->GetEventHandler()->ProcessEvent( event
);
3855 //////////////////////////////////////////////////////////////////////
3857 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3859 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow
)
3860 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3861 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3862 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3865 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3867 const wxPoint
&pos
, const wxSize
&size
)
3868 : wxGridSubwindow(parent
, id
, pos
, size
)
3873 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3877 // NO - don't do this because it will set both the x and y origin
3878 // coords to match the parent scrolled window and we just want to
3879 // set the x coord - MB
3881 // m_owner->PrepareDC( dc );
3884 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3885 wxPoint pt
= dc
.GetDeviceOrigin();
3886 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3887 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
3889 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
3891 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3892 m_owner
->DrawColLabels( dc
, cols
);
3895 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3897 m_owner
->ProcessColLabelMouseEvent( event
);
3900 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3902 m_owner
->GetEventHandler()->ProcessEvent( event
);
3905 //////////////////////////////////////////////////////////////////////
3907 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3909 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow
)
3910 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3911 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3912 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3915 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3917 const wxPoint
&pos
, const wxSize
&size
)
3918 : wxGridSubwindow(parent
, id
, pos
, size
)
3923 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3927 int client_height
= 0;
3928 int client_width
= 0;
3929 GetClientSize( &client_width
, &client_height
);
3931 // VZ: any reason for this ifdef? (FIXME)
3937 rect
.SetWidth( client_width
- 2 );
3938 rect
.SetHeight( client_height
- 2 );
3940 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3942 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxPENSTYLE_SOLID
) );
3943 dc
.DrawLine( client_width
- 1, client_height
- 1, client_width
- 1, 0 );
3944 dc
.DrawLine( client_width
- 1, client_height
- 1, 0, client_height
- 1 );
3945 dc
.DrawLine( 0, 0, client_width
, 0 );
3946 dc
.DrawLine( 0, 0, 0, client_height
);
3948 dc
.SetPen( *wxWHITE_PEN
);
3949 dc
.DrawLine( 1, 1, client_width
- 1, 1 );
3950 dc
.DrawLine( 1, 1, 1, client_height
- 1 );
3954 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3956 m_owner
->ProcessCornerLabelMouseEvent( event
);
3959 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3961 m_owner
->GetEventHandler()->ProcessEvent(event
);
3964 //////////////////////////////////////////////////////////////////////
3966 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3968 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow
)
3969 EVT_PAINT( wxGridWindow::OnPaint
)
3970 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3971 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3972 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3973 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3974 EVT_CHAR( wxGridWindow::OnChar
)
3975 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3976 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3977 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3980 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3981 wxGridRowLabelWindow
*rowLblWin
,
3982 wxGridColLabelWindow
*colLblWin
,
3985 const wxSize
&size
)
3986 : wxGridSubwindow(parent
, id
, pos
, size
,
3987 wxWANTS_CHARS
| wxCLIP_CHILDREN
,
3988 wxT("grid window") )
3991 m_rowLabelWin
= rowLblWin
;
3992 m_colLabelWin
= colLblWin
;
3995 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3997 wxPaintDC
dc( this );
3998 m_owner
->PrepareDC( dc
);
3999 wxRegion reg
= GetUpdateRegion();
4000 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
4001 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
4003 #if WXGRID_DRAW_LINES
4004 m_owner
->DrawAllGridLines( dc
, reg
);
4007 m_owner
->DrawGridSpace( dc
);
4008 m_owner
->DrawHighlight( dc
, dirtyCells
);
4011 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
4013 wxWindow::ScrollWindow( dx
, dy
, rect
);
4014 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
4015 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
4018 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
4020 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
4023 m_owner
->ProcessGridCellMouseEvent( event
);
4026 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
4028 m_owner
->GetEventHandler()->ProcessEvent( event
);
4031 // This seems to be required for wxMotif/wxGTK otherwise the mouse
4032 // cursor must be in the cell edit control to get key events
4034 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
4036 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4040 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
4042 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4046 void wxGridWindow::OnChar( wxKeyEvent
& event
)
4048 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4052 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
4056 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
4058 // and if we have any selection, it has to be repainted, because it
4059 // uses different colour when the grid is not focused:
4060 if ( m_owner
->IsSelection() )
4066 // NB: Note that this code is in "else" branch only because the other
4067 // branch refreshes everything and so there's no point in calling
4068 // Refresh() again, *not* because it should only be done if
4069 // !IsSelection(). If the above code is ever optimized to refresh
4070 // only selected area, this needs to be moved out of the "else"
4071 // branch so that it's always executed.
4073 // current cell cursor {dis,re}appears on focus change:
4074 const wxGridCellCoords
cursorCoords(m_owner
->GetGridCursorRow(),
4075 m_owner
->GetGridCursorCol());
4076 const wxRect cursor
=
4077 m_owner
->BlockToDeviceRect(cursorCoords
, cursorCoords
);
4078 Refresh(true, &cursor
);
4081 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4085 //////////////////////////////////////////////////////////////////////
4087 // Internal Helper function for computing row or column from some
4088 // (unscrolled) coordinate value, using either
4089 // m_defaultRowHeight/m_defaultColWidth or binary search on array
4090 // of m_rowBottoms/m_ColRights to speed up the search!
4092 // Internal helper macros for simpler use of that function
4094 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
4095 const wxArrayInt
& BorderArray
, int nMax
,
4098 #define internalXToCol(x) XToCol(x, true)
4099 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
4100 m_minAcceptableRowHeight, \
4101 m_rowBottoms, m_numRows, true)
4103 /////////////////////////////////////////////////////////////////////
4105 #if wxUSE_EXTENDED_RTTI
4106 WX_DEFINE_FLAGS( wxGridStyle
)
4108 wxBEGIN_FLAGS( wxGridStyle
)
4109 // new style border flags, we put them first to
4110 // use them for streaming out
4111 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4112 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4113 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4114 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4115 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4116 wxFLAGS_MEMBER(wxBORDER_NONE
)
4118 // old style border flags
4119 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4120 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4121 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4122 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4123 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4124 wxFLAGS_MEMBER(wxBORDER
)
4126 // standard window styles
4127 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4128 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4129 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4130 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4131 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4132 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4133 wxFLAGS_MEMBER(wxVSCROLL
)
4134 wxFLAGS_MEMBER(wxHSCROLL
)
4136 wxEND_FLAGS( wxGridStyle
)
4138 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4140 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4141 wxHIDE_PROPERTY( Children
)
4142 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4143 wxEND_PROPERTIES_TABLE()
4145 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4146 wxEND_HANDLERS_TABLE()
4148 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4151 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
4154 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4157 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4158 EVT_PAINT( wxGrid::OnPaint
)
4159 EVT_SIZE( wxGrid::OnSize
)
4160 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
4161 EVT_KEY_UP( wxGrid::OnKeyUp
)
4162 EVT_CHAR ( wxGrid::OnChar
)
4163 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
4171 wxGrid::wxGrid( wxWindow
*parent
,
4176 const wxString
& name
)
4179 Create(parent
, id
, pos
, size
, style
, name
);
4182 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
4183 const wxPoint
& pos
, const wxSize
& size
,
4184 long style
, const wxString
& name
)
4186 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
4187 style
| wxWANTS_CHARS
, name
))
4190 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4191 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4194 SetInitialSize(size
);
4202 // Must do this or ~wxScrollHelper will pop the wrong event handler
4203 SetTargetWindow(this);
4205 wxSafeDecRef(m_defaultCellAttr
);
4207 #ifdef DEBUG_ATTR_CACHE
4208 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4209 wxPrintf(_T("wxGrid attribute cache statistics: "
4210 "total: %u, hits: %u (%u%%)\n"),
4211 total
, gs_nAttrCacheHits
,
4212 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4215 // if we own the table, just delete it, otherwise at least don't leave it
4216 // with dangling view pointer
4219 else if ( m_table
&& m_table
->GetView() == this )
4220 m_table
->SetView(NULL
);
4222 delete m_typeRegistry
;
4227 // ----- internal init and update functions
4230 // NOTE: If using the default visual attributes works everywhere then this can
4231 // be removed as well as the #else cases below.
4232 #define _USE_VISATTR 0
4234 void wxGrid::Create()
4236 // create the type registry
4237 m_typeRegistry
= new wxGridTypeRegistry
;
4239 m_cellEditCtrlEnabled
= false;
4241 m_defaultCellAttr
= new wxGridCellAttr();
4243 // Set default cell attributes
4244 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4245 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4246 m_defaultCellAttr
->SetFont(GetFont());
4247 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4248 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4249 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4252 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4253 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4255 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4256 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4259 m_defaultCellAttr
->SetTextColour(
4260 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4261 m_defaultCellAttr
->SetBackgroundColour(
4262 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4267 m_currentCellCoords
= wxGridNoCellCoords
;
4269 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4270 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4272 // subwindow components that make up the wxGrid
4273 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4278 m_colLabelWin
= new wxGridColLabelWindow( this,
4283 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4288 m_gridWin
= new wxGridWindow( this,
4295 SetTargetWindow( m_gridWin
);
4298 wxColour gfg
= gva
.colFg
;
4299 wxColour gbg
= gva
.colBg
;
4300 wxColour lfg
= lva
.colFg
;
4301 wxColour lbg
= lva
.colBg
;
4303 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4304 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4305 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4306 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4309 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4310 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4311 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4312 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4313 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4314 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4316 m_gridWin
->SetOwnForegroundColour(gfg
);
4317 m_gridWin
->SetOwnBackgroundColour(gbg
);
4322 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4323 wxGrid::wxGridSelectionModes selmode
)
4325 wxCHECK_MSG( !m_created
,
4327 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4329 m_numRows
= numRows
;
4330 m_numCols
= numCols
;
4332 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4333 m_table
->SetView( this );
4335 m_selection
= new wxGridSelection( this, selmode
);
4344 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4346 wxCHECK_RET( m_created
,
4347 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4349 m_selection
->SetSelectionMode( selmode
);
4352 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4354 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4355 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4357 return m_selection
->GetSelectionMode();
4360 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4361 wxGrid::wxGridSelectionModes selmode
)
4363 bool checkSelection
= false;
4366 // stop all processing
4371 m_table
->SetView(0);
4383 checkSelection
= true;
4385 // kill row and column size arrays
4386 m_colWidths
.Empty();
4387 m_colRights
.Empty();
4388 m_rowHeights
.Empty();
4389 m_rowBottoms
.Empty();
4394 m_numRows
= table
->GetNumberRows();
4395 m_numCols
= table
->GetNumberCols();
4398 m_table
->SetView( this );
4399 m_ownTable
= takeOwnership
;
4400 m_selection
= new wxGridSelection( this, selmode
);
4403 // If the newly set table is smaller than the
4404 // original one current cell and selection regions
4405 // might be invalid,
4406 m_selectingKeyboard
= wxGridNoCellCoords
;
4407 m_currentCellCoords
=
4408 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
4409 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
4410 if (m_selectingTopLeft
.GetRow() >= m_numRows
||
4411 m_selectingTopLeft
.GetCol() >= m_numCols
)
4413 m_selectingTopLeft
= wxGridNoCellCoords
;
4414 m_selectingBottomRight
= wxGridNoCellCoords
;
4417 m_selectingBottomRight
=
4418 wxGridCellCoords(wxMin(m_numRows
,
4419 m_selectingBottomRight
.GetRow()),
4421 m_selectingBottomRight
.GetCol()));
4431 void wxGrid::InitVars()
4435 m_cornerLabelWin
= NULL
;
4436 m_rowLabelWin
= NULL
;
4437 m_colLabelWin
= NULL
;
4444 m_defaultCellAttr
= NULL
;
4445 m_typeRegistry
= NULL
;
4446 m_winCapture
= NULL
;
4451 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4452 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4454 if ( m_rowLabelWin
)
4456 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4460 m_labelBackgroundColour
= *wxWHITE
;
4463 m_labelTextColour
= *wxBLACK
;
4466 m_attrCache
.row
= -1;
4467 m_attrCache
.col
= -1;
4468 m_attrCache
.attr
= NULL
;
4470 // TODO: something better than this ?
4472 m_labelFont
= this->GetFont();
4473 m_labelFont
.SetWeight( wxBOLD
);
4475 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4476 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4478 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4479 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4480 m_colLabelTextOrientation
= wxHORIZONTAL
;
4482 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4483 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4485 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4486 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4488 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4489 m_defaultRowHeight
+= 8;
4491 m_defaultRowHeight
+= 4;
4494 m_gridLineColour
= wxColour( 192,192,192 );
4495 m_gridLinesEnabled
= true;
4496 m_cellHighlightColour
= *wxBLACK
;
4497 m_cellHighlightPenWidth
= 2;
4498 m_cellHighlightROPenWidth
= 1;
4500 m_canDragColMove
= false;
4502 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4503 m_winCapture
= (wxWindow
*)NULL
;
4504 m_canDragRowSize
= true;
4505 m_canDragColSize
= true;
4506 m_canDragGridSize
= true;
4507 m_canDragCell
= false;
4509 m_dragRowOrCol
= -1;
4510 m_isDragging
= false;
4511 m_startDragPos
= wxDefaultPosition
;
4512 m_nativeColumnLabels
= false;
4514 m_waitForSlowClick
= false;
4516 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4517 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4519 m_currentCellCoords
= wxGridNoCellCoords
;
4523 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4524 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4526 m_editable
= true; // default for whole grid
4528 m_inOnKeyDown
= false;
4534 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4535 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4538 // ----------------------------------------------------------------------------
4539 // the idea is to call these functions only when necessary because they create
4540 // quite big arrays which eat memory mostly unnecessary - in particular, if
4541 // default widths/heights are used for all rows/columns, we may not use these
4544 // with some extra code, it should be possible to only store the widths/heights
4545 // different from default ones (resulting in space savings for huge grids) but
4546 // this is not done currently
4547 // ----------------------------------------------------------------------------
4549 void wxGrid::InitRowHeights()
4551 m_rowHeights
.Empty();
4552 m_rowBottoms
.Empty();
4554 m_rowHeights
.Alloc( m_numRows
);
4555 m_rowBottoms
.Alloc( m_numRows
);
4557 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4560 for ( int i
= 0; i
< m_numRows
; i
++ )
4562 rowBottom
+= m_defaultRowHeight
;
4563 m_rowBottoms
.Add( rowBottom
);
4567 void wxGrid::InitColWidths()
4569 m_colWidths
.Empty();
4570 m_colRights
.Empty();
4572 m_colWidths
.Alloc( m_numCols
);
4573 m_colRights
.Alloc( m_numCols
);
4575 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4578 for ( int i
= 0; i
< m_numCols
; i
++ )
4580 colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
4581 m_colRights
.Add( colRight
);
4585 int wxGrid::GetColWidth(int col
) const
4587 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4590 int wxGrid::GetColLeft(int col
) const
4592 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
4593 : m_colRights
[col
] - m_colWidths
[col
];
4596 int wxGrid::GetColRight(int col
) const
4598 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
4602 int wxGrid::GetRowHeight(int row
) const
4604 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4607 int wxGrid::GetRowTop(int row
) const
4609 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4610 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4613 int wxGrid::GetRowBottom(int row
) const
4615 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4616 : m_rowBottoms
[row
];
4619 void wxGrid::CalcDimensions()
4621 // compute the size of the scrollable area
4622 int w
= m_numCols
> 0 ? GetColRight(GetColAt(m_numCols
- 1)) : 0;
4623 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
4628 // take into account editor if shown
4629 if ( IsCellEditControlShown() )
4632 int r
= m_currentCellCoords
.GetRow();
4633 int c
= m_currentCellCoords
.GetCol();
4634 int x
= GetColLeft(c
);
4635 int y
= GetRowTop(r
);
4637 // how big is the editor
4638 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4639 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4640 editor
->GetControl()->GetSize(&w2
, &h2
);
4651 // preserve (more or less) the previous position
4653 GetViewStart( &x
, &y
);
4655 // ensure the position is valid for the new scroll ranges
4657 x
= wxMax( w
- 1, 0 );
4659 y
= wxMax( h
- 1, 0 );
4661 // do set scrollbar parameters
4662 SetScrollbars( m_scrollLineX
, m_scrollLineY
,
4663 GetScrollX(w
), GetScrollY(h
),
4665 GetBatchCount() != 0);
4667 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4668 // still must reposition the children
4672 void wxGrid::CalcWindowSizes()
4674 // escape if the window is has not been fully created yet
4676 if ( m_cornerLabelWin
== NULL
)
4680 GetClientSize( &cw
, &ch
);
4682 // this block of code tries to work around the following problem: the grid
4683 // could have been just resized to have enough space to show the full grid
4684 // window contents without the scrollbars, but its client size could be
4685 // not big enough because the grid has the scrollbars right now and so the
4686 // scrollbars would remain even though we don't need them any more
4688 // to prevent this from happening, check if we have enough space for
4689 // everything without the scrollbars and explicitly disable them then
4690 wxSize size
= GetSize() - GetWindowBorderSize();
4691 if ( size
!= wxSize(cw
, ch
) )
4693 // check if we have enough space for grid window after accounting for
4694 // the fixed size elements
4695 size
.x
-= m_rowLabelWidth
;
4696 size
.y
-= m_colLabelHeight
;
4698 const wxSize vsize
= m_gridWin
->GetVirtualSize();
4700 if ( size
.x
>= vsize
.x
&& size
.y
>= vsize
.y
)
4702 // yes, we do, so remove the scrollbars and use the new client size
4703 // (which should be the same as full window size - borders now)
4704 SetScrollbars(0, 0, 0, 0);
4705 GetClientSize(&cw
, &ch
);
4709 // the grid may be too small to have enough space for the labels yet, don't
4710 // size the windows to negative sizes in this case
4711 int gw
= cw
- m_rowLabelWidth
;
4712 int gh
= ch
- m_colLabelHeight
;
4718 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4719 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4721 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4722 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, gw
, m_colLabelHeight
);
4724 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4725 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, gh
);
4727 if ( m_gridWin
&& m_gridWin
->IsShown() )
4728 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, gw
, gh
);
4731 // this is called when the grid table sends a message
4732 // to indicate that it has been redimensioned
4734 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4737 bool result
= false;
4739 // Clear the attribute cache as the attribute might refer to a different
4740 // cell than stored in the cache after adding/removing rows/columns.
4743 // By the same reasoning, the editor should be dismissed if columns are
4744 // added or removed. And for consistency, it should IMHO always be
4745 // removed, not only if the cell "underneath" it actually changes.
4746 // For now, I intentionally do not save the editor's content as the
4747 // cell it might want to save that stuff to might no longer exist.
4748 HideCellEditControl();
4751 // if we were using the default widths/heights so far, we must change them
4753 if ( m_colWidths
.IsEmpty() )
4758 if ( m_rowHeights
.IsEmpty() )
4764 switch ( msg
.GetId() )
4766 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4768 size_t pos
= msg
.GetCommandInt();
4769 int numRows
= msg
.GetCommandInt2();
4771 m_numRows
+= numRows
;
4773 if ( !m_rowHeights
.IsEmpty() )
4775 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4776 m_rowBottoms
.Insert( 0, pos
, numRows
);
4780 bottom
= m_rowBottoms
[pos
- 1];
4782 for ( i
= pos
; i
< m_numRows
; i
++ )
4784 bottom
+= m_rowHeights
[i
];
4785 m_rowBottoms
[i
] = bottom
;
4789 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4791 // if we have just inserted cols into an empty grid the current
4792 // cell will be undefined...
4794 SetCurrentCell( 0, 0 );
4798 m_selection
->UpdateRows( pos
, numRows
);
4799 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4801 attrProvider
->UpdateAttrRows( pos
, numRows
);
4803 if ( !GetBatchCount() )
4806 m_rowLabelWin
->Refresh();
4812 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4814 int numRows
= msg
.GetCommandInt();
4815 int oldNumRows
= m_numRows
;
4816 m_numRows
+= numRows
;
4818 if ( !m_rowHeights
.IsEmpty() )
4820 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4821 m_rowBottoms
.Add( 0, numRows
);
4824 if ( oldNumRows
> 0 )
4825 bottom
= m_rowBottoms
[oldNumRows
- 1];
4827 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4829 bottom
+= m_rowHeights
[i
];
4830 m_rowBottoms
[i
] = bottom
;
4834 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4836 // if we have just inserted cols into an empty grid the current
4837 // cell will be undefined...
4839 SetCurrentCell( 0, 0 );
4842 if ( !GetBatchCount() )
4845 m_rowLabelWin
->Refresh();
4851 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4853 size_t pos
= msg
.GetCommandInt();
4854 int numRows
= msg
.GetCommandInt2();
4855 m_numRows
-= numRows
;
4857 if ( !m_rowHeights
.IsEmpty() )
4859 m_rowHeights
.RemoveAt( pos
, numRows
);
4860 m_rowBottoms
.RemoveAt( pos
, numRows
);
4863 for ( i
= 0; i
< m_numRows
; i
++ )
4865 h
+= m_rowHeights
[i
];
4866 m_rowBottoms
[i
] = h
;
4872 m_currentCellCoords
= wxGridNoCellCoords
;
4876 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4877 m_currentCellCoords
.Set( 0, 0 );
4881 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4882 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4885 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4887 // ifdef'd out following patch from Paul Gammans
4889 // No need to touch column attributes, unless we
4890 // removed _all_ rows, in this case, we remove
4891 // all column attributes.
4892 // I hate to do this here, but the
4893 // needed data is not available inside UpdateAttrRows.
4894 if ( !GetNumberRows() )
4895 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4899 if ( !GetBatchCount() )
4902 m_rowLabelWin
->Refresh();
4908 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4910 size_t pos
= msg
.GetCommandInt();
4911 int numCols
= msg
.GetCommandInt2();
4912 m_numCols
+= numCols
;
4914 if ( !m_colAt
.IsEmpty() )
4916 //Shift the column IDs
4918 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
4920 if ( m_colAt
[i
] >= (int)pos
)
4921 m_colAt
[i
] += numCols
;
4924 m_colAt
.Insert( pos
, pos
, numCols
);
4926 //Set the new columns' positions
4927 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
4933 if ( !m_colWidths
.IsEmpty() )
4935 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4936 m_colRights
.Insert( 0, pos
, numCols
);
4940 right
= m_colRights
[GetColAt( pos
- 1 )];
4943 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
4945 i
= GetColAt( colPos
);
4947 right
+= m_colWidths
[i
];
4948 m_colRights
[i
] = right
;
4952 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4954 // if we have just inserted cols into an empty grid the current
4955 // cell will be undefined...
4957 SetCurrentCell( 0, 0 );
4961 m_selection
->UpdateCols( pos
, numCols
);
4962 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4964 attrProvider
->UpdateAttrCols( pos
, numCols
);
4965 if ( !GetBatchCount() )
4968 m_colLabelWin
->Refresh();
4974 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4976 int numCols
= msg
.GetCommandInt();
4977 int oldNumCols
= m_numCols
;
4978 m_numCols
+= numCols
;
4980 if ( !m_colAt
.IsEmpty() )
4982 m_colAt
.Add( 0, numCols
);
4984 //Set the new columns' positions
4986 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4992 if ( !m_colWidths
.IsEmpty() )
4994 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4995 m_colRights
.Add( 0, numCols
);
4998 if ( oldNumCols
> 0 )
4999 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
5002 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
5004 i
= GetColAt( colPos
);
5006 right
+= m_colWidths
[i
];
5007 m_colRights
[i
] = right
;
5011 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5013 // if we have just inserted cols into an empty grid the current
5014 // cell will be undefined...
5016 SetCurrentCell( 0, 0 );
5018 if ( !GetBatchCount() )
5021 m_colLabelWin
->Refresh();
5027 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5029 size_t pos
= msg
.GetCommandInt();
5030 int numCols
= msg
.GetCommandInt2();
5031 m_numCols
-= numCols
;
5033 if ( !m_colAt
.IsEmpty() )
5035 int colID
= GetColAt( pos
);
5037 m_colAt
.RemoveAt( pos
, numCols
);
5039 //Shift the column IDs
5041 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5043 if ( m_colAt
[colPos
] > colID
)
5044 m_colAt
[colPos
] -= numCols
;
5048 if ( !m_colWidths
.IsEmpty() )
5050 m_colWidths
.RemoveAt( pos
, numCols
);
5051 m_colRights
.RemoveAt( pos
, numCols
);
5055 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5057 i
= GetColAt( colPos
);
5059 w
+= m_colWidths
[i
];
5066 m_currentCellCoords
= wxGridNoCellCoords
;
5070 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
5071 m_currentCellCoords
.Set( 0, 0 );
5075 m_selection
->UpdateCols( pos
, -((int)numCols
) );
5076 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5079 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
5081 // ifdef'd out following patch from Paul Gammans
5083 // No need to touch row attributes, unless we
5084 // removed _all_ columns, in this case, we remove
5085 // all row attributes.
5086 // I hate to do this here, but the
5087 // needed data is not available inside UpdateAttrCols.
5088 if ( !GetNumberCols() )
5089 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
5093 if ( !GetBatchCount() )
5096 m_colLabelWin
->Refresh();
5103 if (result
&& !GetBatchCount() )
5104 m_gridWin
->Refresh();
5109 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
) const
5111 wxRegionIterator
iter( reg
);
5114 wxArrayInt rowlabels
;
5121 // TODO: remove this when we can...
5122 // There is a bug in wxMotif that gives garbage update
5123 // rectangles if you jump-scroll a long way by clicking the
5124 // scrollbar with middle button. This is a work-around
5126 #if defined(__WXMOTIF__)
5128 m_gridWin
->GetClientSize( &cw
, &ch
);
5129 if ( r
.GetTop() > ch
)
5131 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5134 // logical bounds of update region
5137 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
5138 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
5140 // find the row labels within these bounds
5143 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5145 if ( GetRowBottom(row
) < top
)
5148 if ( GetRowTop(row
) > bottom
)
5151 rowlabels
.Add( row
);
5160 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
) const
5162 wxRegionIterator
iter( reg
);
5165 wxArrayInt colLabels
;
5172 // TODO: remove this when we can...
5173 // There is a bug in wxMotif that gives garbage update
5174 // rectangles if you jump-scroll a long way by clicking the
5175 // scrollbar with middle button. This is a work-around
5177 #if defined(__WXMOTIF__)
5179 m_gridWin
->GetClientSize( &cw
, &ch
);
5180 if ( r
.GetLeft() > cw
)
5182 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5185 // logical bounds of update region
5188 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
5189 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
5191 // find the cells within these bounds
5195 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5197 col
= GetColAt( colPos
);
5199 if ( GetColRight(col
) < left
)
5202 if ( GetColLeft(col
) > right
)
5205 colLabels
.Add( col
);
5214 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
) const
5216 wxRegionIterator
iter( reg
);
5219 wxGridCellCoordsArray cellsExposed
;
5221 int left
, top
, right
, bottom
;
5226 // TODO: remove this when we can...
5227 // There is a bug in wxMotif that gives garbage update
5228 // rectangles if you jump-scroll a long way by clicking the
5229 // scrollbar with middle button. This is a work-around
5231 #if defined(__WXMOTIF__)
5233 m_gridWin
->GetClientSize( &cw
, &ch
);
5234 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5235 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5236 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5237 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5240 // logical bounds of update region
5242 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5243 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5245 // find the cells within these bounds
5248 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5250 if ( GetRowBottom(row
) <= top
)
5253 if ( GetRowTop(row
) > bottom
)
5257 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5259 col
= GetColAt( colPos
);
5261 if ( GetColRight(col
) <= left
)
5264 if ( GetColLeft(col
) > right
)
5267 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
5274 return cellsExposed
;
5278 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5281 wxPoint
pos( event
.GetPosition() );
5282 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5284 if ( event
.Dragging() )
5288 m_isDragging
= true;
5289 m_rowLabelWin
->CaptureMouse();
5292 if ( event
.LeftIsDown() )
5294 switch ( m_cursorMode
)
5296 case WXGRID_CURSOR_RESIZE_ROW
:
5298 int cw
, ch
, left
, dummy
;
5299 m_gridWin
->GetClientSize( &cw
, &ch
);
5300 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5302 wxClientDC
dc( m_gridWin
);
5305 GetRowTop(m_dragRowOrCol
) +
5306 GetRowMinimalHeight(m_dragRowOrCol
) );
5307 dc
.SetLogicalFunction(wxINVERT
);
5308 if ( m_dragLastPos
>= 0 )
5310 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5312 dc
.DrawLine( left
, y
, left
+cw
, y
);
5317 case WXGRID_CURSOR_SELECT_ROW
:
5319 if ( (row
= YToRow( y
)) >= 0 )
5323 m_selection
->SelectRow( row
,
5324 event
.ControlDown(),
5333 // default label to suppress warnings about "enumeration value
5334 // 'xxx' not handled in switch
5342 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5347 if (m_rowLabelWin
->HasCapture())
5348 m_rowLabelWin
->ReleaseMouse();
5349 m_isDragging
= false;
5352 // ------------ Entering or leaving the window
5354 if ( event
.Entering() || event
.Leaving() )
5356 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5359 // ------------ Left button pressed
5361 else if ( event
.LeftDown() )
5363 // don't send a label click event for a hit on the
5364 // edge of the row label - this is probably the user
5365 // wanting to resize the row
5367 if ( YToEdgeOfRow(y
) < 0 )
5371 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5373 if ( !event
.ShiftDown() && !event
.CmdDown() )
5377 if ( event
.ShiftDown() )
5379 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5382 GetNumberCols() - 1,
5383 event
.ControlDown(),
5390 m_selection
->SelectRow( row
,
5391 event
.ControlDown(),
5398 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5403 // starting to drag-resize a row
5404 if ( CanDragRowSize() )
5405 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5409 // ------------ Left double click
5411 else if (event
.LeftDClick() )
5413 row
= YToEdgeOfRow(y
);
5418 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5420 // no default action at the moment
5425 // adjust row height depending on label text
5426 AutoSizeRowLabelSize( row
);
5428 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5433 // ------------ Left button released
5435 else if ( event
.LeftUp() )
5437 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5439 DoEndDragResizeRow();
5441 // Note: we are ending the event *after* doing
5442 // default processing in this case
5444 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5447 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5451 // ------------ Right button down
5453 else if ( event
.RightDown() )
5457 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5459 // no default action at the moment
5463 // ------------ Right double click
5465 else if ( event
.RightDClick() )
5469 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5471 // no default action at the moment
5475 // ------------ No buttons down and mouse moving
5477 else if ( event
.Moving() )
5479 m_dragRowOrCol
= YToEdgeOfRow( y
);
5480 if ( m_dragRowOrCol
>= 0 )
5482 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5484 // don't capture the mouse yet
5485 if ( CanDragRowSize() )
5486 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5489 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5491 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5496 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5499 wxPoint
pos( event
.GetPosition() );
5500 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5502 if ( event
.Dragging() )
5506 m_isDragging
= true;
5507 m_colLabelWin
->CaptureMouse();
5509 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
)
5510 m_dragRowOrCol
= XToCol( x
);
5513 if ( event
.LeftIsDown() )
5515 switch ( m_cursorMode
)
5517 case WXGRID_CURSOR_RESIZE_COL
:
5519 int cw
, ch
, dummy
, top
;
5520 m_gridWin
->GetClientSize( &cw
, &ch
);
5521 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5523 wxClientDC
dc( m_gridWin
);
5526 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5527 GetColMinimalWidth(m_dragRowOrCol
));
5528 dc
.SetLogicalFunction(wxINVERT
);
5529 if ( m_dragLastPos
>= 0 )
5531 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5533 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5538 case WXGRID_CURSOR_SELECT_COL
:
5540 if ( (col
= XToCol( x
)) >= 0 )
5544 m_selection
->SelectCol( col
,
5545 event
.ControlDown(),
5554 case WXGRID_CURSOR_MOVE_COL
:
5557 m_moveToCol
= GetColAt( 0 );
5559 m_moveToCol
= XToCol( x
);
5563 if ( m_moveToCol
< 0 )
5564 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5566 markerX
= GetColLeft( m_moveToCol
);
5568 if ( markerX
!= m_dragLastPos
)
5570 wxClientDC
dc( m_colLabelWin
);
5573 m_colLabelWin
->GetClientSize( &cw
, &ch
);
5577 //Clean up the last indicator
5578 if ( m_dragLastPos
>= 0 )
5580 wxPen
pen( m_colLabelWin
->GetBackgroundColour(), 2 );
5582 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
5583 dc
.SetPen(wxNullPen
);
5585 if ( XToCol( m_dragLastPos
) != -1 )
5586 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
5589 //Moving to the same place? Don't draw a marker
5590 if ( (m_moveToCol
== m_dragRowOrCol
)
5591 || (GetColPos( m_moveToCol
) == GetColPos( m_dragRowOrCol
) + 1)
5592 || (m_moveToCol
< 0 && m_dragRowOrCol
== GetColAt( m_numCols
- 1 )))
5599 wxPen
pen( *wxBLUE
, 2 );
5602 dc
.DrawLine( markerX
, 0, markerX
, ch
);
5604 dc
.SetPen(wxNullPen
);
5606 m_dragLastPos
= markerX
- 1;
5611 // default label to suppress warnings about "enumeration value
5612 // 'xxx' not handled in switch
5620 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5625 if (m_colLabelWin
->HasCapture())
5626 m_colLabelWin
->ReleaseMouse();
5627 m_isDragging
= false;
5630 // ------------ Entering or leaving the window
5632 if ( event
.Entering() || event
.Leaving() )
5634 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5637 // ------------ Left button pressed
5639 else if ( event
.LeftDown() )
5641 // don't send a label click event for a hit on the
5642 // edge of the col label - this is probably the user
5643 // wanting to resize the col
5645 if ( XToEdgeOfCol(x
) < 0 )
5649 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5651 if ( m_canDragColMove
)
5653 //Show button as pressed
5654 wxClientDC
dc( m_colLabelWin
);
5655 int colLeft
= GetColLeft( col
);
5656 int colRight
= GetColRight( col
) - 1;
5657 dc
.SetPen( wxPen( m_colLabelWin
->GetBackgroundColour(), 1 ) );
5658 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
5659 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
5661 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, m_colLabelWin
);
5665 if ( !event
.ShiftDown() && !event
.CmdDown() )
5669 if ( event
.ShiftDown() )
5671 m_selection
->SelectBlock( 0,
5672 m_currentCellCoords
.GetCol(),
5673 GetNumberRows() - 1, col
,
5674 event
.ControlDown(),
5681 m_selection
->SelectCol( col
,
5682 event
.ControlDown(),
5689 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5695 // starting to drag-resize a col
5697 if ( CanDragColSize() )
5698 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5702 // ------------ Left double click
5704 if ( event
.LeftDClick() )
5706 col
= XToEdgeOfCol(x
);
5711 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5713 // no default action at the moment
5718 // adjust column width depending on label text
5719 AutoSizeColLabelSize( col
);
5721 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5726 // ------------ Left button released
5728 else if ( event
.LeftUp() )
5730 switch ( m_cursorMode
)
5732 case WXGRID_CURSOR_RESIZE_COL
:
5733 DoEndDragResizeCol();
5735 // Note: we are ending the event *after* doing
5736 // default processing in this case
5738 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5741 case WXGRID_CURSOR_MOVE_COL
:
5744 SendEvent( wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
, event
);
5747 case WXGRID_CURSOR_SELECT_COL
:
5748 case WXGRID_CURSOR_SELECT_CELL
:
5749 case WXGRID_CURSOR_RESIZE_ROW
:
5750 case WXGRID_CURSOR_SELECT_ROW
:
5751 // nothing to do (?)
5755 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5759 // ------------ Right button down
5761 else if ( event
.RightDown() )
5765 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5767 // no default action at the moment
5771 // ------------ Right double click
5773 else if ( event
.RightDClick() )
5777 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5779 // no default action at the moment
5783 // ------------ No buttons down and mouse moving
5785 else if ( event
.Moving() )
5787 m_dragRowOrCol
= XToEdgeOfCol( x
);
5788 if ( m_dragRowOrCol
>= 0 )
5790 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5792 // don't capture the cursor yet
5793 if ( CanDragColSize() )
5794 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5797 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5799 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5804 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5806 if ( event
.LeftDown() )
5808 // indicate corner label by having both row and
5811 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5816 else if ( event
.LeftDClick() )
5818 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5820 else if ( event
.RightDown() )
5822 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5824 // no default action at the moment
5827 else if ( event
.RightDClick() )
5829 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5831 // no default action at the moment
5836 void wxGrid::CancelMouseCapture()
5838 // cancel operation currently in progress, whatever it is
5841 m_isDragging
= false;
5842 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
5843 m_winCapture
->SetCursor( *wxSTANDARD_CURSOR
);
5844 m_winCapture
= NULL
;
5846 // remove traces of whatever we drew on screen
5851 void wxGrid::ChangeCursorMode(CursorMode mode
,
5856 static const wxChar
*cursorModes
[] =
5866 wxLogTrace(_T("grid"),
5867 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5868 win
== m_colLabelWin
? _T("colLabelWin")
5869 : win
? _T("rowLabelWin")
5871 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5874 if ( mode
== m_cursorMode
&&
5875 win
== m_winCapture
&&
5876 captureMouse
== (m_winCapture
!= NULL
))
5881 // by default use the grid itself
5887 if (m_winCapture
->HasCapture())
5888 m_winCapture
->ReleaseMouse();
5889 m_winCapture
= (wxWindow
*)NULL
;
5892 m_cursorMode
= mode
;
5894 switch ( m_cursorMode
)
5896 case WXGRID_CURSOR_RESIZE_ROW
:
5897 win
->SetCursor( m_rowResizeCursor
);
5900 case WXGRID_CURSOR_RESIZE_COL
:
5901 win
->SetCursor( m_colResizeCursor
);
5904 case WXGRID_CURSOR_MOVE_COL
:
5905 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
5909 win
->SetCursor( *wxSTANDARD_CURSOR
);
5913 // we need to capture mouse when resizing
5914 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5915 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5917 if ( captureMouse
&& resize
)
5919 win
->CaptureMouse();
5924 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5927 wxPoint
pos( event
.GetPosition() );
5928 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5930 wxGridCellCoords coords
;
5931 XYToCell( x
, y
, coords
);
5933 int cell_rows
, cell_cols
;
5934 bool isFirstDrag
= !m_isDragging
;
5935 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5936 if ((cell_rows
< 0) || (cell_cols
< 0))
5938 coords
.SetRow(coords
.GetRow() + cell_rows
);
5939 coords
.SetCol(coords
.GetCol() + cell_cols
);
5942 if ( event
.Dragging() )
5944 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5946 // Don't start doing anything until the mouse has been dragged at
5947 // least 3 pixels in any direction...
5950 if (m_startDragPos
== wxDefaultPosition
)
5952 m_startDragPos
= pos
;
5955 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5959 m_isDragging
= true;
5960 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5962 // Hide the edit control, so it
5963 // won't interfere with drag-shrinking.
5964 if ( IsCellEditControlShown() )
5966 HideCellEditControl();
5967 SaveEditControlValue();
5970 if ( coords
!= wxGridNoCellCoords
)
5972 if ( event
.CmdDown() )
5974 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5975 m_selectingKeyboard
= coords
;
5976 HighlightBlock( m_selectingKeyboard
, coords
);
5978 else if ( CanDragCell() )
5982 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5983 m_selectingKeyboard
= coords
;
5985 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5994 if ( !IsSelection() )
5996 HighlightBlock( coords
, coords
);
6000 HighlightBlock( m_currentCellCoords
, coords
);
6004 if (! IsVisible(coords
))
6006 MakeCellVisible(coords
);
6007 // TODO: need to introduce a delay or something here. The
6008 // scrolling is way to fast, at least on MSW - also on GTK.
6011 // Have we captured the mouse yet?
6014 m_winCapture
= m_gridWin
;
6015 m_winCapture
->CaptureMouse();
6020 else if ( event
.LeftIsDown() &&
6021 m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6023 int cw
, ch
, left
, dummy
;
6024 m_gridWin
->GetClientSize( &cw
, &ch
);
6025 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6027 wxClientDC
dc( m_gridWin
);
6029 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
6030 GetRowMinimalHeight(m_dragRowOrCol
) );
6031 dc
.SetLogicalFunction(wxINVERT
);
6032 if ( m_dragLastPos
>= 0 )
6034 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
6036 dc
.DrawLine( left
, y
, left
+cw
, y
);
6039 else if ( event
.LeftIsDown() &&
6040 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6042 int cw
, ch
, dummy
, top
;
6043 m_gridWin
->GetClientSize( &cw
, &ch
);
6044 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6046 wxClientDC
dc( m_gridWin
);
6048 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
6049 GetColMinimalWidth(m_dragRowOrCol
) );
6050 dc
.SetLogicalFunction(wxINVERT
);
6051 if ( m_dragLastPos
>= 0 )
6053 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6055 dc
.DrawLine( x
, top
, x
, top
+ ch
);
6062 m_isDragging
= false;
6063 m_startDragPos
= wxDefaultPosition
;
6065 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
6066 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
6069 if ( event
.Entering() || event
.Leaving() )
6071 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6072 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
6077 // ------------ Left button pressed
6079 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
6081 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
6086 if ( !event
.CmdDown() )
6088 if ( event
.ShiftDown() )
6092 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
6093 m_currentCellCoords
.GetCol(),
6096 event
.ControlDown(),
6102 else if ( XToEdgeOfCol(x
) < 0 &&
6103 YToEdgeOfRow(y
) < 0 )
6105 DisableCellEditControl();
6106 MakeCellVisible( coords
);
6108 if ( event
.CmdDown() )
6112 m_selection
->ToggleCellSelection( coords
.GetRow(),
6114 event
.ControlDown(),
6119 m_selectingTopLeft
= wxGridNoCellCoords
;
6120 m_selectingBottomRight
= wxGridNoCellCoords
;
6121 m_selectingKeyboard
= coords
;
6125 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
6126 SetCurrentCell( coords
);
6129 if ( m_selection
->GetSelectionMode() !=
6130 wxGrid::wxGridSelectCells
)
6132 HighlightBlock( coords
, coords
);
6140 // ------------ Left double click
6142 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
6144 DisableCellEditControl();
6146 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
6148 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
6153 // we want double click to select a cell and start editing
6154 // (i.e. to behave in same way as sequence of two slow clicks):
6155 m_waitForSlowClick
= true;
6160 // ------------ Left button released
6162 else if ( event
.LeftUp() )
6164 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6168 if (m_winCapture
->HasCapture())
6169 m_winCapture
->ReleaseMouse();
6170 m_winCapture
= NULL
;
6173 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
6176 EnableCellEditControl();
6178 wxGridCellAttr
*attr
= GetCellAttr(coords
);
6179 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
6180 editor
->StartingClick();
6184 m_waitForSlowClick
= false;
6186 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6187 m_selectingBottomRight
!= wxGridNoCellCoords
)
6191 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6192 m_selectingTopLeft
.GetCol(),
6193 m_selectingBottomRight
.GetRow(),
6194 m_selectingBottomRight
.GetCol(),
6195 event
.ControlDown(),
6201 m_selectingTopLeft
= wxGridNoCellCoords
;
6202 m_selectingBottomRight
= wxGridNoCellCoords
;
6204 // Show the edit control, if it has been hidden for
6206 ShowCellEditControl();
6209 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6211 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6212 DoEndDragResizeRow();
6214 // Note: we are ending the event *after* doing
6215 // default processing in this case
6217 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
6219 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6221 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6222 DoEndDragResizeCol();
6224 // Note: we are ending the event *after* doing
6225 // default processing in this case
6227 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
6233 // ------------ Right button down
6235 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
6237 DisableCellEditControl();
6238 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
6243 // no default action at the moment
6247 // ------------ Right double click
6249 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
6251 DisableCellEditControl();
6252 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
6257 // no default action at the moment
6261 // ------------ Moving and no button action
6263 else if ( event
.Moving() && !event
.IsButton() )
6265 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
6267 // out of grid cell area
6268 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6272 int dragRow
= YToEdgeOfRow( y
);
6273 int dragCol
= XToEdgeOfCol( x
);
6275 // Dragging on the corner of a cell to resize in both
6276 // directions is not implemented yet...
6278 if ( dragRow
>= 0 && dragCol
>= 0 )
6280 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6286 m_dragRowOrCol
= dragRow
;
6288 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6290 if ( CanDragRowSize() && CanDragGridSize() )
6291 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false);
6294 else if ( dragCol
>= 0 )
6296 m_dragRowOrCol
= dragCol
;
6298 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6300 if ( CanDragColSize() && CanDragGridSize() )
6301 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false);
6304 else // Neither on a row or col edge
6306 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6308 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6314 void wxGrid::DoEndDragResizeRow()
6316 if ( m_dragLastPos
>= 0 )
6318 // erase the last line and resize the row
6320 int cw
, ch
, left
, dummy
;
6321 m_gridWin
->GetClientSize( &cw
, &ch
);
6322 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6324 wxClientDC
dc( m_gridWin
);
6326 dc
.SetLogicalFunction( wxINVERT
);
6327 dc
.DrawLine( left
, m_dragLastPos
, left
+ cw
, m_dragLastPos
);
6328 HideCellEditControl();
6329 SaveEditControlValue();
6331 int rowTop
= GetRowTop(m_dragRowOrCol
);
6332 SetRowSize( m_dragRowOrCol
,
6333 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
6335 if ( !GetBatchCount() )
6337 // Only needed to get the correct rect.y:
6338 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
6340 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6341 rect
.width
= m_rowLabelWidth
;
6342 rect
.height
= ch
- rect
.y
;
6343 m_rowLabelWin
->Refresh( true, &rect
);
6346 // if there is a multicell block, paint all of it
6349 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
6350 int leftCol
= XToCol(left
);
6351 int rightCol
= internalXToCol(left
+ cw
);
6354 for (i
=leftCol
; i
<rightCol
; i
++)
6356 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
6357 if (cell_rows
< subtract_rows
)
6358 subtract_rows
= cell_rows
;
6360 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
6361 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6362 rect
.height
= ch
- rect
.y
;
6365 m_gridWin
->Refresh( false, &rect
);
6368 ShowCellEditControl();
6373 void wxGrid::DoEndDragResizeCol()
6375 if ( m_dragLastPos
>= 0 )
6377 // erase the last line and resize the col
6379 int cw
, ch
, dummy
, top
;
6380 m_gridWin
->GetClientSize( &cw
, &ch
);
6381 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6383 wxClientDC
dc( m_gridWin
);
6385 dc
.SetLogicalFunction( wxINVERT
);
6386 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6387 HideCellEditControl();
6388 SaveEditControlValue();
6390 int colLeft
= GetColLeft(m_dragRowOrCol
);
6391 SetColSize( m_dragRowOrCol
,
6392 wxMax( m_dragLastPos
- colLeft
,
6393 GetColMinimalWidth(m_dragRowOrCol
) ) );
6395 if ( !GetBatchCount() )
6397 // Only needed to get the correct rect.x:
6398 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
6400 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6401 rect
.width
= cw
- rect
.x
;
6402 rect
.height
= m_colLabelHeight
;
6403 m_colLabelWin
->Refresh( true, &rect
);
6406 // if there is a multicell block, paint all of it
6409 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
6410 int topRow
= YToRow(top
);
6411 int bottomRow
= internalYToRow(top
+ cw
);
6414 for (i
=topRow
; i
<bottomRow
; i
++)
6416 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
6417 if (cell_cols
< subtract_cols
)
6418 subtract_cols
= cell_cols
;
6421 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
6422 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6423 rect
.width
= cw
- rect
.x
;
6427 m_gridWin
->Refresh( false, &rect
);
6430 ShowCellEditControl();
6434 void wxGrid::DoEndDragMoveCol()
6436 //The user clicked on the column but didn't actually drag
6437 if ( m_dragLastPos
< 0 )
6439 m_colLabelWin
->Refresh(); //Do this to "unpress" the column
6444 if ( m_moveToCol
== -1 )
6445 newPos
= m_numCols
- 1;
6448 newPos
= GetColPos( m_moveToCol
);
6449 if ( newPos
> GetColPos( m_dragRowOrCol
) )
6453 SetColPos( m_dragRowOrCol
, newPos
);
6456 void wxGrid::SetColPos( int colID
, int newPos
)
6458 if ( m_colAt
.IsEmpty() )
6460 m_colAt
.Alloc( m_numCols
);
6463 for ( i
= 0; i
< m_numCols
; i
++ )
6469 int oldPos
= GetColPos( colID
);
6471 //Reshuffle the m_colAt array
6472 if ( newPos
> oldPos
)
6475 for ( i
= oldPos
; i
< newPos
; i
++ )
6477 m_colAt
[i
] = m_colAt
[i
+1];
6483 for ( i
= oldPos
; i
> newPos
; i
-- )
6485 m_colAt
[i
] = m_colAt
[i
-1];
6489 m_colAt
[newPos
] = colID
;
6491 //Recalculate the column rights
6492 if ( !m_colWidths
.IsEmpty() )
6496 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6498 int colID
= GetColAt( colPos
);
6500 colRight
+= m_colWidths
[colID
];
6501 m_colRights
[colID
] = colRight
;
6505 m_colLabelWin
->Refresh();
6506 m_gridWin
->Refresh();
6511 void wxGrid::EnableDragColMove( bool enable
)
6513 if ( m_canDragColMove
== enable
)
6516 m_canDragColMove
= enable
;
6518 if ( !m_canDragColMove
)
6522 //Recalculate the column rights
6523 if ( !m_colWidths
.IsEmpty() )
6527 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6529 colRight
+= m_colWidths
[colPos
];
6530 m_colRights
[colPos
] = colRight
;
6534 m_colLabelWin
->Refresh();
6535 m_gridWin
->Refresh();
6541 // ------ interaction with data model
6543 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6545 switch ( msg
.GetId() )
6547 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6548 return GetModelValues();
6550 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6551 return SetModelValues();
6553 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6554 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6555 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6556 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6557 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6558 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6559 return Redimension( msg
);
6566 // The behaviour of this function depends on the grid table class
6567 // Clear() function. For the default wxGridStringTable class the
6568 // behavious is to replace all cell contents with wxEmptyString but
6569 // not to change the number of rows or cols.
6571 void wxGrid::ClearGrid()
6575 if (IsCellEditControlEnabled())
6576 DisableCellEditControl();
6579 if (!GetBatchCount())
6580 m_gridWin
->Refresh();
6584 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6586 // TODO: something with updateLabels flag
6590 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6596 if (IsCellEditControlEnabled())
6597 DisableCellEditControl();
6599 bool done
= m_table
->InsertRows( pos
, numRows
);
6602 // the table will have sent the results of the insert row
6603 // operation to this view object as a grid table message
6609 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6611 // TODO: something with updateLabels flag
6615 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6621 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6624 // the table will have sent the results of the append row
6625 // operation to this view object as a grid table message
6631 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6633 // TODO: something with updateLabels flag
6637 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6643 if (IsCellEditControlEnabled())
6644 DisableCellEditControl();
6646 bool done
= m_table
->DeleteRows( pos
, numRows
);
6648 // the table will have sent the results of the delete row
6649 // operation to this view object as a grid table message
6655 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6657 // TODO: something with updateLabels flag
6661 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6667 if (IsCellEditControlEnabled())
6668 DisableCellEditControl();
6670 bool done
= m_table
->InsertCols( pos
, numCols
);
6672 // the table will have sent the results of the insert col
6673 // operation to this view object as a grid table message
6679 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6681 // TODO: something with updateLabels flag
6685 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6691 bool done
= m_table
->AppendCols( numCols
);
6693 // the table will have sent the results of the append col
6694 // operation to this view object as a grid table message
6700 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6702 // TODO: something with updateLabels flag
6706 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6712 if (IsCellEditControlEnabled())
6713 DisableCellEditControl();
6715 bool done
= m_table
->DeleteCols( pos
, numCols
);
6717 // the table will have sent the results of the delete col
6718 // operation to this view object as a grid table message
6725 // ----- event handlers
6728 // Generate a grid event based on a mouse event and
6729 // return the result of ProcessEvent()
6731 int wxGrid::SendEvent( const wxEventType type
,
6733 wxMouseEvent
& mouseEv
)
6735 bool claimed
, vetoed
;
6737 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6739 int rowOrCol
= (row
== -1 ? col
: row
);
6741 wxGridSizeEvent
gridEvt( GetId(),
6745 mouseEv
.GetX() + GetRowLabelSize(),
6746 mouseEv
.GetY() + GetColLabelSize(),
6747 mouseEv
.ControlDown(),
6748 mouseEv
.ShiftDown(),
6750 mouseEv
.MetaDown() );
6752 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6753 vetoed
= !gridEvt
.IsAllowed();
6755 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6757 // Right now, it should _never_ end up here!
6758 wxGridRangeSelectEvent
gridEvt( GetId(),
6762 m_selectingBottomRight
,
6764 mouseEv
.ControlDown(),
6765 mouseEv
.ShiftDown(),
6767 mouseEv
.MetaDown() );
6769 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6770 vetoed
= !gridEvt
.IsAllowed();
6772 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
6773 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
6774 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
6775 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
6777 wxPoint pos
= mouseEv
.GetPosition();
6779 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
6780 pos
.y
+= GetColLabelSize();
6781 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
6782 pos
.x
+= GetRowLabelSize();
6784 wxGridEvent
gridEvt( GetId(),
6791 mouseEv
.ControlDown(),
6792 mouseEv
.ShiftDown(),
6794 mouseEv
.MetaDown() );
6795 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6796 vetoed
= !gridEvt
.IsAllowed();
6800 wxGridEvent
gridEvt( GetId(),
6804 mouseEv
.GetX() + GetRowLabelSize(),
6805 mouseEv
.GetY() + GetColLabelSize(),
6807 mouseEv
.ControlDown(),
6808 mouseEv
.ShiftDown(),
6810 mouseEv
.MetaDown() );
6811 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6812 vetoed
= !gridEvt
.IsAllowed();
6815 // A Veto'd event may not be `claimed' so test this first
6819 return claimed
? 1 : 0;
6822 // Generate a grid event of specified type and return the result
6823 // of ProcessEvent().
6825 int wxGrid::SendEvent( const wxEventType type
,
6828 bool claimed
, vetoed
;
6830 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6832 int rowOrCol
= (row
== -1 ? col
: row
);
6834 wxGridSizeEvent
gridEvt( GetId(), type
, this, rowOrCol
);
6836 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6837 vetoed
= !gridEvt
.IsAllowed();
6841 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
6843 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6844 vetoed
= !gridEvt
.IsAllowed();
6847 // A Veto'd event may not be `claimed' so test this first
6851 return claimed
? 1 : 0;
6854 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6856 // needed to prevent zillions of paint events on MSW
6860 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6862 // Don't do anything if between Begin/EndBatch...
6863 // EndBatch() will do all this on the last nested one anyway.
6864 if ( m_created
&& !GetBatchCount() )
6866 // Refresh to get correct scrolled position:
6867 wxScrolledWindow::Refresh(eraseb
, rect
);
6871 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6872 int width_label
, width_cell
, height_label
, height_cell
;
6875 // Copy rectangle can get scroll offsets..
6876 rect_x
= rect
->GetX();
6877 rect_y
= rect
->GetY();
6878 rectWidth
= rect
->GetWidth();
6879 rectHeight
= rect
->GetHeight();
6881 width_label
= m_rowLabelWidth
- rect_x
;
6882 if (width_label
> rectWidth
)
6883 width_label
= rectWidth
;
6885 height_label
= m_colLabelHeight
- rect_y
;
6886 if (height_label
> rectHeight
)
6887 height_label
= rectHeight
;
6889 if (rect_x
> m_rowLabelWidth
)
6891 x
= rect_x
- m_rowLabelWidth
;
6892 width_cell
= rectWidth
;
6897 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6900 if (rect_y
> m_colLabelHeight
)
6902 y
= rect_y
- m_colLabelHeight
;
6903 height_cell
= rectHeight
;
6908 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6911 // Paint corner label part intersecting rect.
6912 if ( width_label
> 0 && height_label
> 0 )
6914 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6915 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6918 // Paint col labels part intersecting rect.
6919 if ( width_cell
> 0 && height_label
> 0 )
6921 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6922 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6925 // Paint row labels part intersecting rect.
6926 if ( width_label
> 0 && height_cell
> 0 )
6928 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6929 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6932 // Paint cell area part intersecting rect.
6933 if ( width_cell
> 0 && height_cell
> 0 )
6935 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6936 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6941 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6942 m_colLabelWin
->Refresh(eraseb
, NULL
);
6943 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6944 m_gridWin
->Refresh(eraseb
, NULL
);
6949 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
))
6951 if (m_targetWindow
!= this) // check whether initialisation has been done
6953 // update our children window positions and scrollbars
6958 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6960 if ( m_inOnKeyDown
)
6962 // shouldn't be here - we are going round in circles...
6964 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6967 m_inOnKeyDown
= true;
6969 // propagate the event up and see if it gets processed
6970 wxWindow
*parent
= GetParent();
6971 wxKeyEvent
keyEvt( event
);
6972 keyEvt
.SetEventObject( parent
);
6974 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6976 if (GetLayoutDirection() == wxLayout_RightToLeft
)
6978 if (event
.GetKeyCode() == WXK_RIGHT
)
6979 event
.m_keyCode
= WXK_LEFT
;
6980 else if (event
.GetKeyCode() == WXK_LEFT
)
6981 event
.m_keyCode
= WXK_RIGHT
;
6984 // try local handlers
6985 switch ( event
.GetKeyCode() )
6988 if ( event
.ControlDown() )
6989 MoveCursorUpBlock( event
.ShiftDown() );
6991 MoveCursorUp( event
.ShiftDown() );
6995 if ( event
.ControlDown() )
6996 MoveCursorDownBlock( event
.ShiftDown() );
6998 MoveCursorDown( event
.ShiftDown() );
7002 if ( event
.ControlDown() )
7003 MoveCursorLeftBlock( event
.ShiftDown() );
7005 MoveCursorLeft( event
.ShiftDown() );
7009 if ( event
.ControlDown() )
7010 MoveCursorRightBlock( event
.ShiftDown() );
7012 MoveCursorRight( event
.ShiftDown() );
7016 case WXK_NUMPAD_ENTER
:
7017 if ( event
.ControlDown() )
7019 event
.Skip(); // to let the edit control have the return
7023 if ( GetGridCursorRow() < GetNumberRows()-1 )
7025 MoveCursorDown( event
.ShiftDown() );
7029 // at the bottom of a column
7030 DisableCellEditControl();
7040 if (event
.ShiftDown())
7042 if ( GetGridCursorCol() > 0 )
7044 MoveCursorLeft( false );
7049 DisableCellEditControl();
7054 if ( GetGridCursorCol() < GetNumberCols() - 1 )
7056 MoveCursorRight( false );
7061 DisableCellEditControl();
7067 if ( event
.ControlDown() )
7069 MakeCellVisible( 0, 0 );
7070 SetCurrentCell( 0, 0 );
7079 if ( event
.ControlDown() )
7081 MakeCellVisible( m_numRows
- 1, m_numCols
- 1 );
7082 SetCurrentCell( m_numRows
- 1, m_numCols
- 1 );
7099 if ( event
.ControlDown() )
7103 m_selection
->ToggleCellSelection(
7104 m_currentCellCoords
.GetRow(),
7105 m_currentCellCoords
.GetCol(),
7106 event
.ControlDown(),
7114 if ( !IsEditable() )
7115 MoveCursorRight( false );
7126 m_inOnKeyDown
= false;
7129 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
7131 // try local handlers
7133 if ( event
.GetKeyCode() == WXK_SHIFT
)
7135 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
7136 m_selectingBottomRight
!= wxGridNoCellCoords
)
7140 m_selection
->SelectBlock(
7141 m_selectingTopLeft
.GetRow(),
7142 m_selectingTopLeft
.GetCol(),
7143 m_selectingBottomRight
.GetRow(),
7144 m_selectingBottomRight
.GetCol(),
7145 event
.ControlDown(),
7152 m_selectingTopLeft
= wxGridNoCellCoords
;
7153 m_selectingBottomRight
= wxGridNoCellCoords
;
7154 m_selectingKeyboard
= wxGridNoCellCoords
;
7158 void wxGrid::OnChar( wxKeyEvent
& event
)
7160 // is it possible to edit the current cell at all?
7161 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7163 // yes, now check whether the cells editor accepts the key
7164 int row
= m_currentCellCoords
.GetRow();
7165 int col
= m_currentCellCoords
.GetCol();
7166 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7167 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7169 // <F2> is special and will always start editing, for
7170 // other keys - ask the editor itself
7171 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
7172 || editor
->IsAcceptedKey(event
) )
7174 // ensure cell is visble
7175 MakeCellVisible(row
, col
);
7176 EnableCellEditControl();
7178 // a problem can arise if the cell is not completely
7179 // visible (even after calling MakeCellVisible the
7180 // control is not created and calling StartingKey will
7182 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
7183 editor
->StartingKey(event
);
7199 void wxGrid::OnEraseBackground(wxEraseEvent
&)
7203 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
7205 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
7207 // the event has been intercepted - do nothing
7211 #if !defined(__WXMAC__)
7212 wxClientDC
dc( m_gridWin
);
7216 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
7218 DisableCellEditControl();
7220 if ( IsVisible( m_currentCellCoords
, false ) )
7223 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
7224 if ( !m_gridLinesEnabled
)
7232 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
7234 // Otherwise refresh redraws the highlight!
7235 m_currentCellCoords
= coords
;
7237 #if defined(__WXMAC__)
7238 m_gridWin
->Refresh(true /*, & r */);
7240 DrawGridCellArea( dc
, cells
);
7241 DrawAllGridLines( dc
, r
);
7246 m_currentCellCoords
= coords
;
7248 wxGridCellAttr
*attr
= GetCellAttr( coords
);
7249 #if !defined(__WXMAC__)
7250 DrawCellHighlight( dc
, attr
);
7255 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7258 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7262 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
7265 rightCol
= GetNumberCols() - 1;
7267 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
7270 bottomRow
= GetNumberRows() - 1;
7274 if ( topRow
> bottomRow
)
7281 if ( leftCol
> rightCol
)
7288 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7289 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7291 // First the case that we selected a completely new area
7292 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
7293 m_selectingBottomRight
== wxGridNoCellCoords
)
7296 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
7297 wxGridCellCoords ( bottomRow
, rightCol
) );
7298 m_gridWin
->Refresh( false, &rect
);
7301 // Now handle changing an existing selection area.
7302 else if ( m_selectingTopLeft
!= updateTopLeft
||
7303 m_selectingBottomRight
!= updateBottomRight
)
7305 // Compute two optimal update rectangles:
7306 // Either one rectangle is a real subset of the
7307 // other, or they are (almost) disjoint!
7309 bool need_refresh
[4];
7313 need_refresh
[3] = false;
7316 // Store intermediate values
7317 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
7318 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
7319 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
7320 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
7322 // Determine the outer/inner coordinates.
7323 if (oldLeft
> leftCol
)
7329 if (oldTop
> topRow
)
7335 if (oldRight
< rightCol
)
7338 oldRight
= rightCol
;
7341 if (oldBottom
< bottomRow
)
7344 oldBottom
= bottomRow
;
7348 // Now, either the stuff marked old is the outer
7349 // rectangle or we don't have a situation where one
7350 // is contained in the other.
7352 if ( oldLeft
< leftCol
)
7354 // Refresh the newly selected or deselected
7355 // area to the left of the old or new selection.
7356 need_refresh
[0] = true;
7357 rect
[0] = BlockToDeviceRect(
7358 wxGridCellCoords( oldTop
, oldLeft
),
7359 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
7362 if ( oldTop
< topRow
)
7364 // Refresh the newly selected or deselected
7365 // area above the old or new selection.
7366 need_refresh
[1] = true;
7367 rect
[1] = BlockToDeviceRect(
7368 wxGridCellCoords( oldTop
, leftCol
),
7369 wxGridCellCoords( topRow
- 1, rightCol
) );
7372 if ( oldRight
> rightCol
)
7374 // Refresh the newly selected or deselected
7375 // area to the right of the old or new selection.
7376 need_refresh
[2] = true;
7377 rect
[2] = BlockToDeviceRect(
7378 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
7379 wxGridCellCoords( oldBottom
, oldRight
) );
7382 if ( oldBottom
> bottomRow
)
7384 // Refresh the newly selected or deselected
7385 // area below the old or new selection.
7386 need_refresh
[3] = true;
7387 rect
[3] = BlockToDeviceRect(
7388 wxGridCellCoords( bottomRow
+ 1, leftCol
),
7389 wxGridCellCoords( oldBottom
, rightCol
) );
7392 // various Refresh() calls
7393 for (i
= 0; i
< 4; i
++ )
7394 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7395 m_gridWin
->Refresh( false, &(rect
[i
]) );
7399 m_selectingTopLeft
= updateTopLeft
;
7400 m_selectingBottomRight
= updateBottomRight
;
7404 // ------ functions to get/send data (see also public functions)
7407 bool wxGrid::GetModelValues()
7409 // Hide the editor, so it won't hide a changed value.
7410 HideCellEditControl();
7414 // all we need to do is repaint the grid
7416 m_gridWin
->Refresh();
7423 bool wxGrid::SetModelValues()
7427 // Disable the editor, so it won't hide a changed value.
7428 // Do we also want to save the current value of the editor first?
7430 DisableCellEditControl();
7434 for ( row
= 0; row
< m_numRows
; row
++ )
7436 for ( col
= 0; col
< m_numCols
; col
++ )
7438 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
7448 // Note - this function only draws cells that are in the list of
7449 // exposed cells (usually set from the update region by
7450 // CalcExposedCells)
7452 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7454 if ( !m_numRows
|| !m_numCols
)
7457 int i
, numCells
= cells
.GetCount();
7458 int row
, col
, cell_rows
, cell_cols
;
7459 wxGridCellCoordsArray redrawCells
;
7461 for ( i
= numCells
- 1; i
>= 0; i
-- )
7463 row
= cells
[i
].GetRow();
7464 col
= cells
[i
].GetCol();
7465 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7467 // If this cell is part of a multicell block, find owner for repaint
7468 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7470 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
7471 bool marked
= false;
7472 for ( int j
= 0; j
< numCells
; j
++ )
7474 if ( cell
== cells
[j
] )
7483 int count
= redrawCells
.GetCount();
7484 for (int j
= 0; j
< count
; j
++)
7486 if ( cell
== redrawCells
[j
] )
7494 redrawCells
.Add( cell
);
7497 // don't bother drawing this cell
7501 // If this cell is empty, find cell to left that might want to overflow
7502 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7504 for ( int l
= 0; l
< cell_rows
; l
++ )
7506 // find a cell in this row to leave already marked for repaint
7508 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7509 if ((redrawCells
[k
].GetCol() < left
) &&
7510 (redrawCells
[k
].GetRow() == row
))
7512 left
= redrawCells
[k
].GetCol();
7516 left
= 0; // oh well
7518 for (int j
= col
- 1; j
>= left
; j
--)
7520 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7522 if (GetCellOverflow(row
+ l
, j
))
7524 wxGridCellCoords
cell(row
+ l
, j
);
7525 bool marked
= false;
7527 for (int k
= 0; k
< numCells
; k
++)
7529 if ( cell
== cells
[k
] )
7538 int count
= redrawCells
.GetCount();
7539 for (int k
= 0; k
< count
; k
++)
7541 if ( cell
== redrawCells
[k
] )
7548 redrawCells
.Add( cell
);
7557 DrawCell( dc
, cells
[i
] );
7560 numCells
= redrawCells
.GetCount();
7562 for ( i
= numCells
- 1; i
>= 0; i
-- )
7564 DrawCell( dc
, redrawCells
[i
] );
7568 void wxGrid::DrawGridSpace( wxDC
& dc
)
7571 m_gridWin
->GetClientSize( &cw
, &ch
);
7574 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7576 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
7577 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7579 if ( right
> rightCol
|| bottom
> bottomRow
)
7582 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7584 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxBRUSHSTYLE_SOLID
) );
7585 dc
.SetPen( *wxTRANSPARENT_PEN
);
7587 if ( right
> rightCol
)
7589 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7592 if ( bottom
> bottomRow
)
7594 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7599 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7601 int row
= coords
.GetRow();
7602 int col
= coords
.GetCol();
7604 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7607 // we draw the cell border ourselves
7608 #if !WXGRID_DRAW_LINES
7609 if ( m_gridLinesEnabled
)
7610 DrawCellBorder( dc
, coords
);
7613 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7615 bool isCurrent
= coords
== m_currentCellCoords
;
7617 wxRect rect
= CellToRect( row
, col
);
7619 // if the editor is shown, we should use it and not the renderer
7620 // Note: However, only if it is really _shown_, i.e. not hidden!
7621 if ( isCurrent
&& IsCellEditControlShown() )
7623 // NB: this "#if..." is temporary and fixes a problem where the
7624 // edit control is erased by this code after being rendered.
7625 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7626 // implicitly, causing this out-of order render.
7627 #if !defined(__WXMAC__)
7628 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7629 editor
->PaintBackground(rect
, attr
);
7635 // but all the rest is drawn by the cell renderer and hence may be customized
7636 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7637 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7644 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7646 // don't show highlight when the grid doesn't have focus
7650 int row
= m_currentCellCoords
.GetRow();
7651 int col
= m_currentCellCoords
.GetCol();
7653 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7656 wxRect rect
= CellToRect(row
, col
);
7658 // hmmm... what could we do here to show that the cell is disabled?
7659 // for now, I just draw a thinner border than for the other ones, but
7660 // it doesn't look really good
7662 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7666 // The center of the drawn line is where the position/width/height of
7667 // the rectangle is actually at (on wxMSW at least), so the
7668 // size of the rectangle is reduced to compensate for the thickness of
7669 // the line. If this is too strange on non-wxMSW platforms then
7670 // please #ifdef this appropriately.
7671 rect
.x
+= penWidth
/ 2;
7672 rect
.y
+= penWidth
/ 2;
7673 rect
.width
-= penWidth
- 1;
7674 rect
.height
-= penWidth
- 1;
7676 // Now draw the rectangle
7677 // use the cellHighlightColour if the cell is inside a selection, this
7678 // will ensure the cell is always visible.
7679 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
: m_cellHighlightColour
, penWidth
, wxPENSTYLE_SOLID
));
7680 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7681 dc
.DrawRectangle(rect
);
7685 // VZ: my experiments with 3D borders...
7687 // how to properly set colours for arbitrary bg?
7688 wxCoord x1
= rect
.x
,
7690 x2
= rect
.x
+ rect
.width
- 1,
7691 y2
= rect
.y
+ rect
.height
- 1;
7693 dc
.SetPen(*wxWHITE_PEN
);
7694 dc
.DrawLine(x1
, y1
, x2
, y1
);
7695 dc
.DrawLine(x1
, y1
, x1
, y2
);
7697 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7698 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7700 dc
.SetPen(*wxBLACK_PEN
);
7701 dc
.DrawLine(x1
, y2
, x2
, y2
);
7702 dc
.DrawLine(x2
, y1
, x2
, y2
+ 1);
7706 wxPen
wxGrid::GetDefaultGridLinePen()
7708 return wxPen(GetGridLineColour(), 1, wxPENSTYLE_SOLID
);
7711 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
7713 return GetDefaultGridLinePen();
7716 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
7718 return GetDefaultGridLinePen();
7721 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7723 int row
= coords
.GetRow();
7724 int col
= coords
.GetCol();
7725 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7729 wxRect rect
= CellToRect( row
, col
);
7731 // right hand border
7732 dc
.SetPen( GetColGridLinePen(col
) );
7733 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7734 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7737 dc
.SetPen( GetRowGridLinePen(row
) );
7738 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7739 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7742 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7744 // This if block was previously in wxGrid::OnPaint but that doesn't
7745 // seem to get called under wxGTK - MB
7747 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7748 m_numRows
&& m_numCols
)
7750 m_currentCellCoords
.Set(0, 0);
7753 if ( IsCellEditControlShown() )
7755 // don't show highlight when the edit control is shown
7759 // if the active cell was repainted, repaint its highlight too because it
7760 // might have been damaged by the grid lines
7761 size_t count
= cells
.GetCount();
7762 for ( size_t n
= 0; n
< count
; n
++ )
7764 if ( cells
[n
] == m_currentCellCoords
)
7766 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7767 DrawCellHighlight(dc
, attr
);
7775 // TODO: remove this ???
7776 // This is used to redraw all grid lines e.g. when the grid line colour
7779 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7781 #if !WXGRID_DRAW_LINES
7785 if ( !m_gridLinesEnabled
|| !m_numRows
|| !m_numCols
)
7788 int top
, bottom
, left
, right
;
7790 #if 0 //#ifndef __WXGTK__
7794 m_gridWin
->GetClientSize(&cw
, &ch
);
7796 // virtual coords of visible area
7798 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7799 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7804 reg
.GetBox(x
, y
, w
, h
);
7805 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7806 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7810 m_gridWin
->GetClientSize(&cw
, &ch
);
7811 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7812 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7815 // avoid drawing grid lines past the last row and col
7817 right
= wxMin( right
, GetColRight(GetColAt( m_numCols
- 1 )) );
7818 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7820 // no gridlines inside multicells, clip them out
7821 int leftCol
= GetColPos( internalXToCol(left
) );
7822 int topRow
= internalYToRow(top
);
7823 int rightCol
= GetColPos( internalXToCol(right
) );
7824 int bottomRow
= internalYToRow(bottom
);
7826 wxRegion
clippedcells(0, 0, cw
, ch
);
7828 int i
, j
, cell_rows
, cell_cols
;
7831 for (j
=topRow
; j
<=bottomRow
; j
++)
7834 for (colPos
=leftCol
; colPos
<=rightCol
; colPos
++)
7836 i
= GetColAt( colPos
);
7838 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7839 if ((cell_rows
> 1) || (cell_cols
> 1))
7841 rect
= CellToRect(j
,i
);
7842 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7843 clippedcells
.Subtract(rect
);
7845 else if ((cell_rows
< 0) || (cell_cols
< 0))
7847 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7848 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7849 clippedcells
.Subtract(rect
);
7854 dc
.SetDeviceClippingRegion( clippedcells
);
7857 // horizontal grid lines
7859 // already declared above - int i;
7860 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7862 int bot
= GetRowBottom(i
) - 1;
7871 dc
.SetPen( GetRowGridLinePen(i
) );
7872 dc
.DrawLine( left
, bot
, right
, bot
);
7876 // vertical grid lines
7879 for ( colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
7881 i
= GetColAt( colPos
);
7883 int colRight
= GetColRight(i
);
7885 if (GetLayoutDirection() != wxLayout_RightToLeft
)
7889 if ( colRight
> right
)
7894 if ( colRight
>= left
)
7896 dc
.SetPen( GetColGridLinePen(i
) );
7897 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7901 dc
.DestroyClippingRegion();
7904 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
7910 size_t numLabels
= rows
.GetCount();
7912 for ( i
= 0; i
< numLabels
; i
++ )
7914 DrawRowLabel( dc
, rows
[i
] );
7918 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7920 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7925 int rowTop
= GetRowTop(row
),
7926 rowBottom
= GetRowBottom(row
) - 1;
7928 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxPENSTYLE_SOLID
) );
7929 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
7930 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7931 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7933 dc
.SetPen( *wxWHITE_PEN
);
7934 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7935 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
7937 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
7938 dc
.SetTextForeground( GetLabelTextColour() );
7939 dc
.SetFont( GetLabelFont() );
7942 GetRowLabelAlignment( &hAlign
, &vAlign
);
7945 rect
.SetY( GetRowTop(row
) + 2 );
7946 rect
.SetWidth( m_rowLabelWidth
- 4 );
7947 rect
.SetHeight( GetRowHeight(row
) - 4 );
7948 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7951 void wxGrid::SetUseNativeColLabels( bool native
)
7953 m_nativeColumnLabels
= native
;
7956 int height
= wxRendererNative::Get().GetHeaderButtonHeight( this );
7957 SetColLabelSize( height
);
7960 m_colLabelWin
->Refresh();
7963 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7969 size_t numLabels
= cols
.GetCount();
7971 for ( i
= 0; i
< numLabels
; i
++ )
7973 DrawColLabel( dc
, cols
[i
] );
7977 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7979 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7982 int colLeft
= GetColLeft(col
);
7986 if (m_nativeColumnLabels
)
7988 rect
.SetX( colLeft
);
7990 rect
.SetWidth( GetColWidth(col
));
7991 rect
.SetHeight( m_colLabelHeight
);
7993 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7994 wxRendererNative::Get().DrawHeaderButton( win_dc
->GetWindow(), dc
, rect
, 0 );
7998 int colRight
= GetColRight(col
) - 1;
8000 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxPENSTYLE_SOLID
) );
8001 dc
.DrawLine( colRight
, 0, colRight
, m_colLabelHeight
- 1 );
8002 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
8003 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
8004 colRight
+ 1, m_colLabelHeight
- 1 );
8006 dc
.SetPen( *wxWHITE_PEN
);
8007 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
8008 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
8011 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
8012 dc
.SetTextForeground( GetLabelTextColour() );
8013 dc
.SetFont( GetLabelFont() );
8015 int hAlign
, vAlign
, orient
;
8016 GetColLabelAlignment( &hAlign
, &vAlign
);
8017 orient
= GetColLabelTextOrientation();
8019 rect
.SetX( colLeft
+ 2 );
8021 rect
.SetWidth( GetColWidth(col
) - 4 );
8022 rect
.SetHeight( m_colLabelHeight
- 4 );
8023 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
8026 void wxGrid::DrawTextRectangle( wxDC
& dc
,
8027 const wxString
& value
,
8031 int textOrientation
)
8033 wxArrayString lines
;
8035 StringToLines( value
, lines
);
8037 // Forward to new API.
8038 DrawTextRectangle( dc
,
8046 // VZ: this should be replaced with wxDC::DrawLabel() to which we just have to
8047 // add textOrientation support
8048 void wxGrid::DrawTextRectangle(wxDC
& dc
,
8049 const wxArrayString
& lines
,
8053 int textOrientation
)
8055 if ( lines
.empty() )
8058 wxDCClipper
clip(dc
, rect
);
8063 if ( textOrientation
== wxHORIZONTAL
)
8064 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
8066 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
8070 switch ( vertAlign
)
8072 case wxALIGN_BOTTOM
:
8073 if ( textOrientation
== wxHORIZONTAL
)
8074 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
8076 x
= rect
.x
+ rect
.width
- textWidth
;
8079 case wxALIGN_CENTRE
:
8080 if ( textOrientation
== wxHORIZONTAL
)
8081 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
8083 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
8088 if ( textOrientation
== wxHORIZONTAL
)
8095 // Align each line of a multi-line label
8096 size_t nLines
= lines
.GetCount();
8097 for ( size_t l
= 0; l
< nLines
; l
++ )
8099 const wxString
& line
= lines
[l
];
8103 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
8107 wxCoord lineWidth
= 0,
8109 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
8111 switch ( horizAlign
)
8114 if ( textOrientation
== wxHORIZONTAL
)
8115 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
8117 y
= rect
.y
+ lineWidth
+ 1;
8120 case wxALIGN_CENTRE
:
8121 if ( textOrientation
== wxHORIZONTAL
)
8122 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
8124 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
8129 if ( textOrientation
== wxHORIZONTAL
)
8132 y
= rect
.y
+ rect
.height
- 1;
8136 if ( textOrientation
== wxHORIZONTAL
)
8138 dc
.DrawText( line
, x
, y
);
8143 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
8149 // Split multi-line text up into an array of strings.
8150 // Any existing contents of the string array are preserved.
8152 // TODO: refactor wxTextFile::Read() and reuse the same code from here
8153 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
8157 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
8158 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
8160 while ( startPos
< (int)tVal
.length() )
8162 pos
= tVal
.Mid(startPos
).Find( eol
);
8167 else if ( pos
== 0 )
8169 lines
.Add( wxEmptyString
);
8173 lines
.Add( tVal
.Mid(startPos
, pos
) );
8176 startPos
+= pos
+ 1;
8179 if ( startPos
< (int)tVal
.length() )
8181 lines
.Add( tVal
.Mid( startPos
) );
8185 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
8186 const wxArrayString
& lines
,
8187 long *width
, long *height
) const
8191 wxCoord lineW
= 0, lineH
= 0;
8194 for ( i
= 0; i
< lines
.GetCount(); i
++ )
8196 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
8197 w
= wxMax( w
, lineW
);
8206 // ------ Batch processing.
8208 void wxGrid::EndBatch()
8210 if ( m_batchCount
> 0 )
8213 if ( !m_batchCount
)
8216 m_rowLabelWin
->Refresh();
8217 m_colLabelWin
->Refresh();
8218 m_cornerLabelWin
->Refresh();
8219 m_gridWin
->Refresh();
8224 // Use this, rather than wxWindow::Refresh(), to force an immediate
8225 // repainting of the grid. Has no effect if you are already inside a
8226 // BeginBatch / EndBatch block.
8228 void wxGrid::ForceRefresh()
8234 bool wxGrid::Enable(bool enable
)
8236 if ( !wxScrolledWindow::Enable(enable
) )
8239 // redraw in the new state
8240 m_gridWin
->Refresh();
8246 // ------ Edit control functions
8249 void wxGrid::EnableEditing( bool edit
)
8251 // TODO: improve this ?
8253 if ( edit
!= m_editable
)
8256 EnableCellEditControl(edit
);
8261 void wxGrid::EnableCellEditControl( bool enable
)
8266 if ( enable
!= m_cellEditCtrlEnabled
)
8270 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
8273 // this should be checked by the caller!
8274 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8276 // do it before ShowCellEditControl()
8277 m_cellEditCtrlEnabled
= enable
;
8279 ShowCellEditControl();
8283 //FIXME:add veto support
8284 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
8286 HideCellEditControl();
8287 SaveEditControlValue();
8289 // do it after HideCellEditControl()
8290 m_cellEditCtrlEnabled
= enable
;
8295 bool wxGrid::IsCurrentCellReadOnly() const
8298 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
8299 bool readonly
= attr
->IsReadOnly();
8305 bool wxGrid::CanEnableCellControl() const
8307 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
8308 !IsCurrentCellReadOnly();
8311 bool wxGrid::IsCellEditControlEnabled() const
8313 // the cell edit control might be disable for all cells or just for the
8314 // current one if it's read only
8315 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
8318 bool wxGrid::IsCellEditControlShown() const
8320 bool isShown
= false;
8322 if ( m_cellEditCtrlEnabled
)
8324 int row
= m_currentCellCoords
.GetRow();
8325 int col
= m_currentCellCoords
.GetCol();
8326 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8327 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
8332 if ( editor
->IsCreated() )
8334 isShown
= editor
->GetControl()->IsShown();
8344 void wxGrid::ShowCellEditControl()
8346 if ( IsCellEditControlEnabled() )
8348 if ( !IsVisible( m_currentCellCoords
, false ) )
8350 m_cellEditCtrlEnabled
= false;
8355 wxRect rect
= CellToRect( m_currentCellCoords
);
8356 int row
= m_currentCellCoords
.GetRow();
8357 int col
= m_currentCellCoords
.GetCol();
8359 // if this is part of a multicell, find owner (topleft)
8360 int cell_rows
, cell_cols
;
8361 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8362 if ( cell_rows
<= 0 || cell_cols
<= 0 )
8366 m_currentCellCoords
.SetRow( row
);
8367 m_currentCellCoords
.SetCol( col
);
8370 // erase the highlight and the cell contents because the editor
8371 // might not cover the entire cell
8372 wxClientDC
dc( m_gridWin
);
8374 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8375 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxBRUSHSTYLE_SOLID
));
8376 dc
.SetPen(*wxTRANSPARENT_PEN
);
8377 dc
.DrawRectangle(rect
);
8379 // convert to scrolled coords
8380 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8386 // cell is shifted by one pixel
8387 // However, don't allow x or y to become negative
8388 // since the SetSize() method interprets that as
8395 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8396 if ( !editor
->IsCreated() )
8398 editor
->Create(m_gridWin
, wxID_ANY
,
8399 new wxGridCellEditorEvtHandler(this, editor
));
8401 wxGridEditorCreatedEvent
evt(GetId(),
8402 wxEVT_GRID_EDITOR_CREATED
,
8406 editor
->GetControl());
8407 GetEventHandler()->ProcessEvent(evt
);
8410 // resize editor to overflow into righthand cells if allowed
8411 int maxWidth
= rect
.width
;
8412 wxString value
= GetCellValue(row
, col
);
8413 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
8416 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
8417 if (maxWidth
< rect
.width
)
8418 maxWidth
= rect
.width
;
8421 int client_right
= m_gridWin
->GetClientSize().GetWidth();
8422 if (rect
.x
+ maxWidth
> client_right
)
8423 maxWidth
= client_right
- rect
.x
;
8425 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
8427 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8428 // may have changed earlier
8429 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
8432 GetCellSize( row
, i
, &c_rows
, &c_cols
);
8434 // looks weird going over a multicell
8435 if (m_table
->IsEmptyCell( row
, i
) &&
8436 (rect
.width
< maxWidth
) && (c_rows
== 1))
8438 rect
.width
+= GetColWidth( i
);
8444 if (rect
.GetRight() > client_right
)
8445 rect
.SetRight( client_right
- 1 );
8448 editor
->SetCellAttr( attr
);
8449 editor
->SetSize( rect
);
8451 editor
->GetControl()->Move(
8452 editor
->GetControl()->GetPosition().x
+ nXMove
,
8453 editor
->GetControl()->GetPosition().y
);
8454 editor
->Show( true, attr
);
8456 // recalc dimensions in case we need to
8457 // expand the scrolled window to account for editor
8460 editor
->BeginEdit(row
, col
, this);
8461 editor
->SetCellAttr(NULL
);
8469 void wxGrid::HideCellEditControl()
8471 if ( IsCellEditControlEnabled() )
8473 int row
= m_currentCellCoords
.GetRow();
8474 int col
= m_currentCellCoords
.GetCol();
8476 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8477 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
8478 editor
->Show( false );
8482 m_gridWin
->SetFocus();
8484 // refresh whole row to the right
8485 wxRect
rect( CellToRect(row
, col
) );
8486 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8487 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8490 // ensure that the pixels under the focus ring get refreshed as well
8491 rect
.Inflate(10, 10);
8494 m_gridWin
->Refresh( false, &rect
);
8498 void wxGrid::SaveEditControlValue()
8500 if ( IsCellEditControlEnabled() )
8502 int row
= m_currentCellCoords
.GetRow();
8503 int col
= m_currentCellCoords
.GetCol();
8505 wxString oldval
= GetCellValue(row
, col
);
8507 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8508 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8509 bool changed
= editor
->EndEdit(row
, col
, this);
8516 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
8517 m_currentCellCoords
.GetRow(),
8518 m_currentCellCoords
.GetCol() ) < 0 )
8520 // Event has been vetoed, set the data back.
8521 SetCellValue(row
, col
, oldval
);
8528 // ------ Grid location functions
8529 // Note that all of these functions work with the logical coordinates of
8530 // grid cells and labels so you will need to convert from device
8531 // coordinates for mouse events etc.
8534 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
) const
8536 int row
= YToRow(y
);
8537 int col
= XToCol(x
);
8539 if ( row
== -1 || col
== -1 )
8541 coords
= wxGridNoCellCoords
;
8545 coords
.Set( row
, col
);
8549 // Internal Helper function for computing row or column from some
8550 // (unscrolled) coordinate value, using either
8551 // m_defaultRowHeight/m_defaultColWidth or binary search on array
8552 // of m_rowBottoms/m_ColRights to speed up the search!
8554 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
8555 const wxArrayInt
& BorderArray
, int nMax
,
8559 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
8564 size_t i_max
= coord
/ defaultDist
,
8567 if (BorderArray
.IsEmpty())
8569 if ((int) i_max
< nMax
)
8571 return clipToMinMax
? nMax
- 1 : -1;
8574 if ( i_max
>= BorderArray
.GetCount())
8576 i_max
= BorderArray
.GetCount() - 1;
8580 if ( coord
>= BorderArray
[i_max
])
8584 i_max
= coord
/ minDist
;
8586 i_max
= BorderArray
.GetCount() - 1;
8589 if ( i_max
>= BorderArray
.GetCount())
8590 i_max
= BorderArray
.GetCount() - 1;
8593 if ( coord
>= BorderArray
[i_max
])
8594 return clipToMinMax
? (int)i_max
: -1;
8595 if ( coord
< BorderArray
[0] )
8598 while ( i_max
- i_min
> 0 )
8600 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
8601 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8602 if (coord
>= BorderArray
[ i_max
- 1])
8606 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
8607 if (coord
< BorderArray
[median
])
8616 int wxGrid::YToRow( int y
) const
8618 return CoordToRowOrCol(y
, m_defaultRowHeight
,
8619 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8622 int wxGrid::XToCol( int x
, bool clipToMinMax
) const
8625 return clipToMinMax
&& (m_numCols
> 0) ? GetColAt( 0 ) : -1;
8627 wxASSERT_MSG(m_defaultColWidth
> 0, wxT("Default column width can not be zero"));
8629 int maxPos
= x
/ m_defaultColWidth
;
8632 if (m_colRights
.IsEmpty())
8634 if(maxPos
< m_numCols
)
8635 return GetColAt( maxPos
);
8636 return clipToMinMax
? GetColAt( m_numCols
- 1 ) : -1;
8639 if ( maxPos
>= m_numCols
)
8640 maxPos
= m_numCols
- 1;
8643 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8646 if (m_minAcceptableColWidth
)
8647 maxPos
= x
/ m_minAcceptableColWidth
;
8649 maxPos
= m_numCols
- 1;
8651 if ( maxPos
>= m_numCols
)
8652 maxPos
= m_numCols
- 1;
8655 //X is beyond the last column
8656 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8657 return clipToMinMax
? GetColAt( maxPos
) : -1;
8659 //X is before the first column
8660 if ( x
< m_colRights
[GetColAt( 0 )] )
8661 return GetColAt( 0 );
8663 //Perform a binary search
8664 while ( maxPos
- minPos
> 0 )
8666 wxCHECK_MSG(m_colRights
[GetColAt( minPos
)] <= x
&& x
< m_colRights
[GetColAt( maxPos
)],
8667 0, _T("wxGrid: internal error in XToCol"));
8669 if (x
>= m_colRights
[GetColAt( maxPos
- 1 )])
8670 return GetColAt( maxPos
);
8673 int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
8674 if (x
< m_colRights
[GetColAt( median
)])
8679 return GetColAt( maxPos
);
8682 // return the row number that that the y coord is near
8683 // the edge of, or -1 if not near an edge.
8684 // coords can only possibly be near an edge if
8685 // (a) the row/column is large enough to still allow for an "inner" area
8686 // that is _not_ nead the edge (i.e., if the height/width is smaller
8687 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8690 // (b) resizing rows/columns (the thing for which edge detection is
8691 // relevant at all) is enabled.
8693 int wxGrid::YToEdgeOfRow( int y
) const
8696 i
= internalYToRow(y
);
8698 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragRowSize() )
8700 // We know that we are in row i, test whether we are
8701 // close enough to lower or upper border, respectively.
8702 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8704 else if ( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8711 // return the col number that that the x coord is near the edge of, or
8712 // -1 if not near an edge
8713 // See comment at YToEdgeOfRow for conditions on edge detection.
8715 int wxGrid::XToEdgeOfCol( int x
) const
8718 i
= internalXToCol(x
);
8720 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragColSize() )
8722 // We know that we are in column i; test whether we are
8723 // close enough to right or left border, respectively.
8724 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8726 else if ( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8733 wxRect
wxGrid::CellToRect( int row
, int col
) const
8735 wxRect
rect( -1, -1, -1, -1 );
8737 if ( row
>= 0 && row
< m_numRows
&&
8738 col
>= 0 && col
< m_numCols
)
8740 int i
, cell_rows
, cell_cols
;
8741 rect
.width
= rect
.height
= 0;
8742 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8743 // if negative then find multicell owner
8748 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8750 rect
.x
= GetColLeft(col
);
8751 rect
.y
= GetRowTop(row
);
8752 for (i
=col
; i
< col
+ cell_cols
; i
++)
8753 rect
.width
+= GetColWidth(i
);
8754 for (i
=row
; i
< row
+ cell_rows
; i
++)
8755 rect
.height
+= GetRowHeight(i
);
8758 // if grid lines are enabled, then the area of the cell is a bit smaller
8759 if (m_gridLinesEnabled
)
8768 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
8770 // get the cell rectangle in logical coords
8772 wxRect
r( CellToRect( row
, col
) );
8774 // convert to device coords
8776 int left
, top
, right
, bottom
;
8777 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8778 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8780 // check against the client area of the grid window
8782 m_gridWin
->GetClientSize( &cw
, &ch
);
8784 if ( wholeCellVisible
)
8786 // is the cell wholly visible ?
8787 return ( left
>= 0 && right
<= cw
&&
8788 top
>= 0 && bottom
<= ch
);
8792 // is the cell partly visible ?
8794 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8795 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8799 // make the specified cell location visible by doing a minimal amount
8802 void wxGrid::MakeCellVisible( int row
, int col
)
8805 int xpos
= -1, ypos
= -1;
8807 if ( row
>= 0 && row
< m_numRows
&&
8808 col
>= 0 && col
< m_numCols
)
8810 // get the cell rectangle in logical coords
8811 wxRect
r( CellToRect( row
, col
) );
8813 // convert to device coords
8814 int left
, top
, right
, bottom
;
8815 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8816 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8819 m_gridWin
->GetClientSize( &cw
, &ch
);
8825 else if ( bottom
> ch
)
8827 int h
= r
.GetHeight();
8829 for ( i
= row
- 1; i
>= 0; i
-- )
8831 int rowHeight
= GetRowHeight(i
);
8832 if ( h
+ rowHeight
> ch
)
8839 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8840 // have rounding errors (this is important, because if we do,
8841 // we might not scroll at all and some cells won't be redrawn)
8843 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
8844 // so just add a full scroll unit...
8845 ypos
+= m_scrollLineY
;
8848 // special handling for wide cells - show always left part of the cell!
8849 // Otherwise, e.g. when stepping from row to row, it would jump between
8850 // left and right part of the cell on every step!
8852 if ( left
< 0 || (right
- left
) >= cw
)
8856 else if ( right
> cw
)
8858 // position the view so that the cell is on the right
8860 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8861 xpos
= x0
+ (right
- cw
);
8863 // see comment for ypos above
8864 xpos
+= m_scrollLineX
;
8867 if ( xpos
!= -1 || ypos
!= -1 )
8870 xpos
/= m_scrollLineX
;
8872 ypos
/= m_scrollLineY
;
8873 Scroll( xpos
, ypos
);
8880 // ------ Grid cursor movement functions
8883 bool wxGrid::MoveCursorUp( bool expandSelection
)
8885 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8886 m_currentCellCoords
.GetRow() >= 0 )
8888 if ( expandSelection
)
8890 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8891 m_selectingKeyboard
= m_currentCellCoords
;
8892 if ( m_selectingKeyboard
.GetRow() > 0 )
8894 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8895 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8896 m_selectingKeyboard
.GetCol() );
8897 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8900 else if ( m_currentCellCoords
.GetRow() > 0 )
8902 int row
= m_currentCellCoords
.GetRow() - 1;
8903 int col
= m_currentCellCoords
.GetCol();
8905 MakeCellVisible( row
, col
);
8906 SetCurrentCell( row
, col
);
8917 bool wxGrid::MoveCursorDown( bool expandSelection
)
8919 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8920 m_currentCellCoords
.GetRow() < m_numRows
)
8922 if ( expandSelection
)
8924 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8925 m_selectingKeyboard
= m_currentCellCoords
;
8926 if ( m_selectingKeyboard
.GetRow() < m_numRows
- 1 )
8928 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8929 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8930 m_selectingKeyboard
.GetCol() );
8931 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8934 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8936 int row
= m_currentCellCoords
.GetRow() + 1;
8937 int col
= m_currentCellCoords
.GetCol();
8939 MakeCellVisible( row
, col
);
8940 SetCurrentCell( row
, col
);
8951 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8953 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8954 m_currentCellCoords
.GetCol() >= 0 )
8956 if ( expandSelection
)
8958 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8959 m_selectingKeyboard
= m_currentCellCoords
;
8960 if ( m_selectingKeyboard
.GetCol() > 0 )
8962 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8963 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8964 m_selectingKeyboard
.GetCol() );
8965 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8968 else if ( GetColPos( m_currentCellCoords
.GetCol() ) > 0 )
8970 int row
= m_currentCellCoords
.GetRow();
8971 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) - 1 );
8974 MakeCellVisible( row
, col
);
8975 SetCurrentCell( row
, col
);
8986 bool wxGrid::MoveCursorRight( bool expandSelection
)
8988 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8989 m_currentCellCoords
.GetCol() < m_numCols
)
8991 if ( expandSelection
)
8993 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8994 m_selectingKeyboard
= m_currentCellCoords
;
8995 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8997 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8998 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8999 m_selectingKeyboard
.GetCol() );
9000 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9003 else if ( GetColPos( m_currentCellCoords
.GetCol() ) < m_numCols
- 1 )
9005 int row
= m_currentCellCoords
.GetRow();
9006 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) + 1 );
9009 MakeCellVisible( row
, col
);
9010 SetCurrentCell( row
, col
);
9021 bool wxGrid::MovePageUp()
9023 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9026 int row
= m_currentCellCoords
.GetRow();
9030 m_gridWin
->GetClientSize( &cw
, &ch
);
9032 int y
= GetRowTop(row
);
9033 int newRow
= internalYToRow( y
- ch
+ 1 );
9035 if ( newRow
== row
)
9037 // row > 0, so newRow can never be less than 0 here.
9041 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
9042 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
9050 bool wxGrid::MovePageDown()
9052 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9055 int row
= m_currentCellCoords
.GetRow();
9056 if ( (row
+ 1) < m_numRows
)
9059 m_gridWin
->GetClientSize( &cw
, &ch
);
9061 int y
= GetRowTop(row
);
9062 int newRow
= internalYToRow( y
+ ch
);
9063 if ( newRow
== row
)
9065 // row < m_numRows, so newRow can't overflow here.
9069 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
9070 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
9078 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
9081 m_currentCellCoords
!= wxGridNoCellCoords
&&
9082 m_currentCellCoords
.GetRow() > 0 )
9084 int row
= m_currentCellCoords
.GetRow();
9085 int col
= m_currentCellCoords
.GetCol();
9087 if ( m_table
->IsEmptyCell(row
, col
) )
9089 // starting in an empty cell: find the next block of
9095 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9099 else if ( m_table
->IsEmptyCell(row
- 1, col
) )
9101 // starting at the top of a block: find the next block
9107 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9113 // starting within a block: find the top of the block
9118 if ( m_table
->IsEmptyCell(row
, col
) )
9126 MakeCellVisible( row
, col
);
9127 if ( expandSelection
)
9129 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9130 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9135 SetCurrentCell( row
, col
);
9144 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
9147 m_currentCellCoords
!= wxGridNoCellCoords
&&
9148 m_currentCellCoords
.GetRow() < m_numRows
- 1 )
9150 int row
= m_currentCellCoords
.GetRow();
9151 int col
= m_currentCellCoords
.GetCol();
9153 if ( m_table
->IsEmptyCell(row
, col
) )
9155 // starting in an empty cell: find the next block of
9158 while ( row
< m_numRows
- 1 )
9161 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9165 else if ( m_table
->IsEmptyCell(row
+ 1, col
) )
9167 // starting at the bottom of a block: find the next block
9170 while ( row
< m_numRows
- 1 )
9173 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9179 // starting within a block: find the bottom of the block
9181 while ( row
< m_numRows
- 1 )
9184 if ( m_table
->IsEmptyCell(row
, col
) )
9192 MakeCellVisible( row
, col
);
9193 if ( expandSelection
)
9195 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9196 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9201 SetCurrentCell( row
, col
);
9210 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
9213 m_currentCellCoords
!= wxGridNoCellCoords
&&
9214 m_currentCellCoords
.GetCol() > 0 )
9216 int row
= m_currentCellCoords
.GetRow();
9217 int col
= m_currentCellCoords
.GetCol();
9219 if ( m_table
->IsEmptyCell(row
, col
) )
9221 // starting in an empty cell: find the next block of
9227 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9231 else if ( m_table
->IsEmptyCell(row
, col
- 1) )
9233 // starting at the left of a block: find the next block
9239 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9245 // starting within a block: find the left of the block
9250 if ( m_table
->IsEmptyCell(row
, col
) )
9258 MakeCellVisible( row
, col
);
9259 if ( expandSelection
)
9261 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9262 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9267 SetCurrentCell( row
, col
);
9276 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
9279 m_currentCellCoords
!= wxGridNoCellCoords
&&
9280 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
9282 int row
= m_currentCellCoords
.GetRow();
9283 int col
= m_currentCellCoords
.GetCol();
9285 if ( m_table
->IsEmptyCell(row
, col
) )
9287 // starting in an empty cell: find the next block of
9290 while ( col
< m_numCols
- 1 )
9293 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9297 else if ( m_table
->IsEmptyCell(row
, col
+ 1) )
9299 // starting at the right of a block: find the next block
9302 while ( col
< m_numCols
- 1 )
9305 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9311 // starting within a block: find the right of the block
9313 while ( col
< m_numCols
- 1 )
9316 if ( m_table
->IsEmptyCell(row
, col
) )
9324 MakeCellVisible( row
, col
);
9325 if ( expandSelection
)
9327 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9328 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9333 SetCurrentCell( row
, col
);
9343 // ------ Label values and formatting
9346 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
9349 *horiz
= m_rowLabelHorizAlign
;
9351 *vert
= m_rowLabelVertAlign
;
9354 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
9357 *horiz
= m_colLabelHorizAlign
;
9359 *vert
= m_colLabelVertAlign
;
9362 int wxGrid::GetColLabelTextOrientation() const
9364 return m_colLabelTextOrientation
;
9367 wxString
wxGrid::GetRowLabelValue( int row
) const
9371 return m_table
->GetRowLabelValue( row
);
9381 wxString
wxGrid::GetColLabelValue( int col
) const
9385 return m_table
->GetColLabelValue( col
);
9395 void wxGrid::SetRowLabelSize( int width
)
9397 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
9399 if ( width
== wxGRID_AUTOSIZE
)
9401 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
9404 if ( width
!= m_rowLabelWidth
)
9408 m_rowLabelWin
->Show( false );
9409 m_cornerLabelWin
->Show( false );
9411 else if ( m_rowLabelWidth
== 0 )
9413 m_rowLabelWin
->Show( true );
9414 if ( m_colLabelHeight
> 0 )
9415 m_cornerLabelWin
->Show( true );
9418 m_rowLabelWidth
= width
;
9420 wxScrolledWindow::Refresh( true );
9424 void wxGrid::SetColLabelSize( int height
)
9426 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
9428 if ( height
== wxGRID_AUTOSIZE
)
9430 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
9433 if ( height
!= m_colLabelHeight
)
9437 m_colLabelWin
->Show( false );
9438 m_cornerLabelWin
->Show( false );
9440 else if ( m_colLabelHeight
== 0 )
9442 m_colLabelWin
->Show( true );
9443 if ( m_rowLabelWidth
> 0 )
9444 m_cornerLabelWin
->Show( true );
9447 m_colLabelHeight
= height
;
9449 wxScrolledWindow::Refresh( true );
9453 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
9455 if ( m_labelBackgroundColour
!= colour
)
9457 m_labelBackgroundColour
= colour
;
9458 m_rowLabelWin
->SetBackgroundColour( colour
);
9459 m_colLabelWin
->SetBackgroundColour( colour
);
9460 m_cornerLabelWin
->SetBackgroundColour( colour
);
9462 if ( !GetBatchCount() )
9464 m_rowLabelWin
->Refresh();
9465 m_colLabelWin
->Refresh();
9466 m_cornerLabelWin
->Refresh();
9471 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
9473 if ( m_labelTextColour
!= colour
)
9475 m_labelTextColour
= colour
;
9476 if ( !GetBatchCount() )
9478 m_rowLabelWin
->Refresh();
9479 m_colLabelWin
->Refresh();
9484 void wxGrid::SetLabelFont( const wxFont
& font
)
9487 if ( !GetBatchCount() )
9489 m_rowLabelWin
->Refresh();
9490 m_colLabelWin
->Refresh();
9494 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
9496 // allow old (incorrect) defs to be used
9499 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9500 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9501 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9506 case wxTOP
: vert
= wxALIGN_TOP
; break;
9507 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9508 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9511 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9513 m_rowLabelHorizAlign
= horiz
;
9516 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9518 m_rowLabelVertAlign
= vert
;
9521 if ( !GetBatchCount() )
9523 m_rowLabelWin
->Refresh();
9527 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
9529 // allow old (incorrect) defs to be used
9532 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9533 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9534 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9539 case wxTOP
: vert
= wxALIGN_TOP
; break;
9540 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9541 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9544 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9546 m_colLabelHorizAlign
= horiz
;
9549 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9551 m_colLabelVertAlign
= vert
;
9554 if ( !GetBatchCount() )
9556 m_colLabelWin
->Refresh();
9560 // Note: under MSW, the default column label font must be changed because it
9561 // does not support vertical printing
9563 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9564 // pGrid->SetLabelFont(font);
9565 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9567 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9569 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9570 m_colLabelTextOrientation
= textOrientation
;
9572 if ( !GetBatchCount() )
9573 m_colLabelWin
->Refresh();
9576 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9580 m_table
->SetRowLabelValue( row
, s
);
9581 if ( !GetBatchCount() )
9583 wxRect rect
= CellToRect( row
, 0 );
9584 if ( rect
.height
> 0 )
9586 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9588 rect
.width
= m_rowLabelWidth
;
9589 m_rowLabelWin
->Refresh( true, &rect
);
9595 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9599 m_table
->SetColLabelValue( col
, s
);
9600 if ( !GetBatchCount() )
9602 wxRect rect
= CellToRect( 0, col
);
9603 if ( rect
.width
> 0 )
9605 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9607 rect
.height
= m_colLabelHeight
;
9608 m_colLabelWin
->Refresh( true, &rect
);
9614 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9616 if ( m_gridLineColour
!= colour
)
9618 m_gridLineColour
= colour
;
9620 wxClientDC
dc( m_gridWin
);
9622 DrawAllGridLines( dc
, wxRegion() );
9626 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9628 if ( m_cellHighlightColour
!= colour
)
9630 m_cellHighlightColour
= colour
;
9632 wxClientDC
dc( m_gridWin
);
9634 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9635 DrawCellHighlight(dc
, attr
);
9640 void wxGrid::SetCellHighlightPenWidth(int width
)
9642 if (m_cellHighlightPenWidth
!= width
)
9644 m_cellHighlightPenWidth
= width
;
9646 // Just redrawing the cell highlight is not enough since that won't
9647 // make any visible change if the the thickness is getting smaller.
9648 int row
= m_currentCellCoords
.GetRow();
9649 int col
= m_currentCellCoords
.GetCol();
9650 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9653 wxRect rect
= CellToRect(row
, col
);
9654 m_gridWin
->Refresh(true, &rect
);
9658 void wxGrid::SetCellHighlightROPenWidth(int width
)
9660 if (m_cellHighlightROPenWidth
!= width
)
9662 m_cellHighlightROPenWidth
= 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 ||
9669 GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9672 wxRect rect
= CellToRect(row
, col
);
9673 m_gridWin
->Refresh(true, &rect
);
9677 void wxGrid::EnableGridLines( bool enable
)
9679 if ( enable
!= m_gridLinesEnabled
)
9681 m_gridLinesEnabled
= enable
;
9683 if ( !GetBatchCount() )
9687 wxClientDC
dc( m_gridWin
);
9689 DrawAllGridLines( dc
, wxRegion() );
9693 m_gridWin
->Refresh();
9699 int wxGrid::GetDefaultRowSize() const
9701 return m_defaultRowHeight
;
9704 int wxGrid::GetRowSize( int row
) const
9706 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9708 return GetRowHeight(row
);
9711 int wxGrid::GetDefaultColSize() const
9713 return m_defaultColWidth
;
9716 int wxGrid::GetColSize( int col
) const
9718 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9720 return GetColWidth(col
);
9723 // ============================================================================
9724 // access to the grid attributes: each of them has a default value in the grid
9725 // itself and may be overidden on a per-cell basis
9726 // ============================================================================
9728 // ----------------------------------------------------------------------------
9729 // setting default attributes
9730 // ----------------------------------------------------------------------------
9732 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9734 m_defaultCellAttr
->SetBackgroundColour(col
);
9736 m_gridWin
->SetBackgroundColour(col
);
9740 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9742 m_defaultCellAttr
->SetTextColour(col
);
9745 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9747 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9750 void wxGrid::SetDefaultCellOverflow( bool allow
)
9752 m_defaultCellAttr
->SetOverflow(allow
);
9755 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9757 m_defaultCellAttr
->SetFont(font
);
9760 // For editors and renderers the type registry takes precedence over the
9761 // default attr, so we need to register the new editor/renderer for the string
9762 // data type in order to make setting a default editor/renderer appear to
9765 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9767 RegisterDataType(wxGRID_VALUE_STRING
,
9769 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9772 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9774 RegisterDataType(wxGRID_VALUE_STRING
,
9775 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9779 // ----------------------------------------------------------------------------
9780 // access to the default attributes
9781 // ----------------------------------------------------------------------------
9783 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
9785 return m_defaultCellAttr
->GetBackgroundColour();
9788 wxColour
wxGrid::GetDefaultCellTextColour() const
9790 return m_defaultCellAttr
->GetTextColour();
9793 wxFont
wxGrid::GetDefaultCellFont() const
9795 return m_defaultCellAttr
->GetFont();
9798 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
9800 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9803 bool wxGrid::GetDefaultCellOverflow() const
9805 return m_defaultCellAttr
->GetOverflow();
9808 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9810 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9813 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9815 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9818 // ----------------------------------------------------------------------------
9819 // access to cell attributes
9820 // ----------------------------------------------------------------------------
9822 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
9824 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9825 wxColour colour
= attr
->GetBackgroundColour();
9831 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
9833 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9834 wxColour colour
= attr
->GetTextColour();
9840 wxFont
wxGrid::GetCellFont( int row
, int col
) const
9842 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9843 wxFont font
= attr
->GetFont();
9849 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
9851 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9852 attr
->GetAlignment(horiz
, vert
);
9856 bool wxGrid::GetCellOverflow( int row
, int col
) const
9858 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9859 bool allow
= attr
->GetOverflow();
9865 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
9867 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9868 attr
->GetSize( num_rows
, num_cols
);
9872 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
9874 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9875 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9881 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
9883 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9884 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9890 bool wxGrid::IsReadOnly(int row
, int col
) const
9892 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9893 bool isReadOnly
= attr
->IsReadOnly();
9899 // ----------------------------------------------------------------------------
9900 // attribute support: cache, automatic provider creation, ...
9901 // ----------------------------------------------------------------------------
9903 bool wxGrid::CanHaveAttributes() const
9910 return m_table
->CanHaveAttributes();
9913 void wxGrid::ClearAttrCache()
9915 if ( m_attrCache
.row
!= -1 )
9917 wxSafeDecRef(m_attrCache
.attr
);
9918 m_attrCache
.attr
= NULL
;
9919 m_attrCache
.row
= -1;
9923 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9927 wxGrid
*self
= (wxGrid
*)this; // const_cast
9929 self
->ClearAttrCache();
9930 self
->m_attrCache
.row
= row
;
9931 self
->m_attrCache
.col
= col
;
9932 self
->m_attrCache
.attr
= attr
;
9937 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9939 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9941 *attr
= m_attrCache
.attr
;
9942 wxSafeIncRef(m_attrCache
.attr
);
9944 #ifdef DEBUG_ATTR_CACHE
9945 gs_nAttrCacheHits
++;
9952 #ifdef DEBUG_ATTR_CACHE
9953 gs_nAttrCacheMisses
++;
9960 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9962 wxGridCellAttr
*attr
= NULL
;
9963 // Additional test to avoid looking at the cache e.g. for
9964 // wxNoCellCoords, as this will confuse memory management.
9967 if ( !LookupAttr(row
, col
, &attr
) )
9969 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9970 : (wxGridCellAttr
*)NULL
;
9971 CacheAttr(row
, col
, attr
);
9977 attr
->SetDefAttr(m_defaultCellAttr
);
9981 attr
= m_defaultCellAttr
;
9988 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9990 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9991 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9993 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9994 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9996 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9999 attr
= new wxGridCellAttr(m_defaultCellAttr
);
10001 // artificially inc the ref count to match DecRef() in caller
10003 m_table
->SetAttr(attr
, row
, col
);
10009 // ----------------------------------------------------------------------------
10010 // setting column attributes (wrappers around SetColAttr)
10011 // ----------------------------------------------------------------------------
10013 void wxGrid::SetColFormatBool(int col
)
10015 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
10018 void wxGrid::SetColFormatNumber(int col
)
10020 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
10023 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
10025 wxString typeName
= wxGRID_VALUE_FLOAT
;
10026 if ( (width
!= -1) || (precision
!= -1) )
10028 typeName
<< _T(':') << width
<< _T(',') << precision
;
10031 SetColFormatCustom(col
, typeName
);
10034 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
10036 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
10038 attr
= new wxGridCellAttr
;
10039 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
10040 attr
->SetRenderer(renderer
);
10041 wxGridCellEditor
*editor
= GetDefaultEditorForType(typeName
);
10042 attr
->SetEditor(editor
);
10044 SetColAttr(col
, attr
);
10048 // ----------------------------------------------------------------------------
10049 // setting cell attributes: this is forwarded to the table
10050 // ----------------------------------------------------------------------------
10052 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
10054 if ( CanHaveAttributes() )
10056 m_table
->SetAttr(attr
, row
, col
);
10061 wxSafeDecRef(attr
);
10065 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
10067 if ( CanHaveAttributes() )
10069 m_table
->SetRowAttr(attr
, row
);
10074 wxSafeDecRef(attr
);
10078 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
10080 if ( CanHaveAttributes() )
10082 m_table
->SetColAttr(attr
, col
);
10087 wxSafeDecRef(attr
);
10091 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
10093 if ( CanHaveAttributes() )
10095 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10096 attr
->SetBackgroundColour(colour
);
10101 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
10103 if ( CanHaveAttributes() )
10105 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10106 attr
->SetTextColour(colour
);
10111 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
10113 if ( CanHaveAttributes() )
10115 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10116 attr
->SetFont(font
);
10121 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
10123 if ( CanHaveAttributes() )
10125 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10126 attr
->SetAlignment(horiz
, vert
);
10131 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
10133 if ( CanHaveAttributes() )
10135 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10136 attr
->SetOverflow(allow
);
10141 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
10143 if ( CanHaveAttributes() )
10145 int cell_rows
, cell_cols
;
10147 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10148 attr
->GetSize(&cell_rows
, &cell_cols
);
10149 attr
->SetSize(num_rows
, num_cols
);
10152 // Cannot set the size of a cell to 0 or negative values
10153 // While it is perfectly legal to do that, this function cannot
10154 // handle all the possibilies, do it by hand by getting the CellAttr.
10155 // You can only set the size of a cell to 1,1 or greater with this fn
10156 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
10157 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10158 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
10159 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10161 // if this was already a multicell then "turn off" the other cells first
10162 if ((cell_rows
> 1) || (cell_rows
> 1))
10165 for (j
=row
; j
< row
+ cell_rows
; j
++)
10167 for (i
=col
; i
< col
+ cell_cols
; i
++)
10169 if ((i
!= col
) || (j
!= row
))
10171 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10172 attr_stub
->SetSize( 1, 1 );
10173 attr_stub
->DecRef();
10179 // mark the cells that will be covered by this cell to
10180 // negative or zero values to point back at this cell
10181 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
10184 for (j
=row
; j
< row
+ num_rows
; j
++)
10186 for (i
=col
; i
< col
+ num_cols
; i
++)
10188 if ((i
!= col
) || (j
!= row
))
10190 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10191 attr_stub
->SetSize( row
- j
, col
- i
);
10192 attr_stub
->DecRef();
10200 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
10202 if ( CanHaveAttributes() )
10204 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10205 attr
->SetRenderer(renderer
);
10210 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
10212 if ( CanHaveAttributes() )
10214 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10215 attr
->SetEditor(editor
);
10220 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
10222 if ( CanHaveAttributes() )
10224 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10225 attr
->SetReadOnly(isReadOnly
);
10230 // ----------------------------------------------------------------------------
10231 // Data type registration
10232 // ----------------------------------------------------------------------------
10234 void wxGrid::RegisterDataType(const wxString
& typeName
,
10235 wxGridCellRenderer
* renderer
,
10236 wxGridCellEditor
* editor
)
10238 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
10242 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
10244 wxString typeName
= m_table
->GetTypeName(row
, col
);
10245 return GetDefaultEditorForType(typeName
);
10248 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
10250 wxString typeName
= m_table
->GetTypeName(row
, col
);
10251 return GetDefaultRendererForType(typeName
);
10254 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
10256 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10257 if ( index
== wxNOT_FOUND
)
10259 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10264 return m_typeRegistry
->GetEditor(index
);
10267 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
10269 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10270 if ( index
== wxNOT_FOUND
)
10272 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10277 return m_typeRegistry
->GetRenderer(index
);
10280 // ----------------------------------------------------------------------------
10282 // ----------------------------------------------------------------------------
10284 void wxGrid::EnableDragRowSize( bool enable
)
10286 m_canDragRowSize
= enable
;
10289 void wxGrid::EnableDragColSize( bool enable
)
10291 m_canDragColSize
= enable
;
10294 void wxGrid::EnableDragGridSize( bool enable
)
10296 m_canDragGridSize
= enable
;
10299 void wxGrid::EnableDragCell( bool enable
)
10301 m_canDragCell
= enable
;
10304 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
10306 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
10308 if ( resizeExistingRows
)
10310 // since we are resizing all rows to the default row size,
10311 // we can simply clear the row heights and row bottoms
10312 // arrays (which also allows us to take advantage of
10313 // some speed optimisations)
10314 m_rowHeights
.Empty();
10315 m_rowBottoms
.Empty();
10316 if ( !GetBatchCount() )
10321 void wxGrid::SetRowSize( int row
, int height
)
10323 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
10325 // See comment in SetColSize
10326 if ( height
< GetRowMinimalAcceptableHeight())
10329 if ( m_rowHeights
.IsEmpty() )
10331 // need to really create the array
10335 int h
= wxMax( 0, height
);
10336 int diff
= h
- m_rowHeights
[row
];
10338 m_rowHeights
[row
] = h
;
10339 for ( int i
= row
; i
< m_numRows
; i
++ )
10341 m_rowBottoms
[i
] += diff
;
10344 if ( !GetBatchCount() )
10348 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
10350 // we dont allow zero default column width
10351 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
10353 if ( resizeExistingCols
)
10355 // since we are resizing all columns to the default column size,
10356 // we can simply clear the col widths and col rights
10357 // arrays (which also allows us to take advantage of
10358 // some speed optimisations)
10359 m_colWidths
.Empty();
10360 m_colRights
.Empty();
10361 if ( !GetBatchCount() )
10366 void wxGrid::SetColSize( int col
, int width
)
10368 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
10370 // should we check that it's bigger than GetColMinimalWidth(col) here?
10372 // No, because it is reasonable to assume the library user know's
10373 // what he is doing. However we should test against the weaker
10374 // constraint of minimalAcceptableWidth, as this breaks rendering
10376 // This test then fixes sf.net bug #645734
10378 if ( width
< GetColMinimalAcceptableWidth() )
10381 if ( m_colWidths
.IsEmpty() )
10383 // need to really create the array
10387 // if < 0 then calculate new width from label
10391 wxArrayString lines
;
10392 wxClientDC
dc(m_colLabelWin
);
10393 dc
.SetFont(GetLabelFont());
10394 StringToLines(GetColLabelValue(col
), lines
);
10395 GetTextBoxSize(dc
, lines
, &w
, &h
);
10399 int w
= wxMax( 0, width
);
10400 int diff
= w
- m_colWidths
[col
];
10401 m_colWidths
[col
] = w
;
10403 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
10405 m_colRights
[GetColAt(colPos
)] += diff
;
10408 if ( !GetBatchCount() )
10412 void wxGrid::SetColMinimalWidth( int col
, int width
)
10414 if (width
> GetColMinimalAcceptableWidth())
10416 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10417 m_colMinWidths
[key
] = width
;
10421 void wxGrid::SetRowMinimalHeight( int row
, int width
)
10423 if (width
> GetRowMinimalAcceptableHeight())
10425 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10426 m_rowMinHeights
[key
] = width
;
10430 int wxGrid::GetColMinimalWidth(int col
) const
10432 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10433 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
10435 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
10438 int wxGrid::GetRowMinimalHeight(int row
) const
10440 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10441 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
10443 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
10446 void wxGrid::SetColMinimalAcceptableWidth( int width
)
10448 // We do allow a width of 0 since this gives us
10449 // an easy way to temporarily hiding columns.
10451 m_minAcceptableColWidth
= width
;
10454 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
10456 // We do allow a height of 0 since this gives us
10457 // an easy way to temporarily hiding rows.
10459 m_minAcceptableRowHeight
= height
;
10462 int wxGrid::GetColMinimalAcceptableWidth() const
10464 return m_minAcceptableColWidth
;
10467 int wxGrid::GetRowMinimalAcceptableHeight() const
10469 return m_minAcceptableRowHeight
;
10472 // ----------------------------------------------------------------------------
10474 // ----------------------------------------------------------------------------
10477 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
10479 const bool column
= direction
== wxGRID_COLUMN
;
10481 wxClientDC
dc(m_gridWin
);
10483 // cancel editing of cell
10484 HideCellEditControl();
10485 SaveEditControlValue();
10487 // init both of them to avoid compiler warnings, even if we only need one
10495 wxCoord extent
, extentMax
= 0;
10496 int max
= column
? m_numRows
: m_numCols
;
10497 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
10504 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
10505 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
10508 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
10509 extent
= column
? size
.x
: size
.y
;
10510 if ( extent
> extentMax
)
10511 extentMax
= extent
;
10513 renderer
->DecRef();
10519 // now also compare with the column label extent
10521 dc
.SetFont( GetLabelFont() );
10525 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
10526 if ( GetColLabelTextOrientation() == wxVERTICAL
)
10530 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
10532 extent
= column
? w
: h
;
10533 if ( extent
> extentMax
)
10534 extentMax
= extent
;
10538 // empty column - give default extent (notice that if extentMax is less
10539 // than default extent but != 0, it's OK)
10540 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
10545 // leave some space around text
10553 // Ensure automatic width is not less than minimal width. See the
10554 // comment in SetColSize() for explanation of why this isn't done
10555 // in SetColSize().
10557 extentMax
= wxMax(extentMax
, GetColMinimalWidth(col
));
10559 SetColSize( col
, extentMax
);
10560 if ( !GetBatchCount() )
10563 m_gridWin
->GetClientSize( &cw
, &ch
);
10564 wxRect
rect ( CellToRect( 0, col
) );
10566 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10567 rect
.width
= cw
- rect
.x
;
10568 rect
.height
= m_colLabelHeight
;
10569 m_colLabelWin
->Refresh( true, &rect
);
10574 // Ensure automatic width is not less than minimal height. See the
10575 // comment in SetColSize() for explanation of why this isn't done
10576 // in SetRowSize().
10578 extentMax
= wxMax(extentMax
, GetRowMinimalHeight(row
));
10580 SetRowSize(row
, extentMax
);
10581 if ( !GetBatchCount() )
10584 m_gridWin
->GetClientSize( &cw
, &ch
);
10585 wxRect
rect( CellToRect( row
, 0 ) );
10587 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10588 rect
.width
= m_rowLabelWidth
;
10589 rect
.height
= ch
- rect
.y
;
10590 m_rowLabelWin
->Refresh( true, &rect
);
10597 SetColMinimalWidth(col
, extentMax
);
10599 SetRowMinimalHeight(row
, extentMax
);
10603 wxCoord
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
10605 // calculate size for the rows or columns?
10606 const bool calcRows
= direction
== wxGRID_ROW
;
10608 wxClientDC
dc(calcRows
? GetGridRowLabelWindow()
10609 : GetGridColLabelWindow());
10610 dc
.SetFont(GetLabelFont());
10612 // which dimension should we take into account for calculations?
10614 // for columns, the text can be only horizontal so it's easy but for rows
10615 // we also have to take into account the text orientation
10617 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
10619 wxArrayString lines
;
10620 wxCoord extentMax
= 0;
10622 const int numRowsOrCols
= calcRows
? m_numRows
: m_numCols
;
10623 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
10627 wxString label
= calcRows
? GetRowLabelValue(rowOrCol
)
10628 : GetColLabelValue(rowOrCol
);
10629 StringToLines(label
, lines
);
10632 GetTextBoxSize(dc
, lines
, &w
, &h
);
10634 const wxCoord extent
= useWidth
? w
: h
;
10635 if ( extent
> extentMax
)
10636 extentMax
= extent
;
10641 // empty column - give default extent (notice that if extentMax is less
10642 // than default extent but != 0, it's OK)
10643 extentMax
= calcRows
? GetDefaultRowLabelSize()
10644 : GetDefaultColLabelSize();
10647 // leave some space around text (taken from AutoSizeColOrRow)
10656 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10658 int width
= m_rowLabelWidth
;
10660 wxGridUpdateLocker locker
;
10662 locker
.Create(this);
10664 for ( int col
= 0; col
< m_numCols
; col
++ )
10667 AutoSizeColumn(col
, setAsMin
);
10669 width
+= GetColWidth(col
);
10675 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10677 int height
= m_colLabelHeight
;
10679 wxGridUpdateLocker locker
;
10681 locker
.Create(this);
10683 for ( int row
= 0; row
< m_numRows
; row
++ )
10686 AutoSizeRow(row
, setAsMin
);
10688 height
+= GetRowHeight(row
);
10694 void wxGrid::AutoSize()
10696 wxGridUpdateLocker
locker(this);
10698 // we need to round up the size of the scrollable area to a multiple of
10699 // scroll step to ensure that we don't get the scrollbars when we're sized
10700 // exactly to fit our contents
10701 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
10702 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
10703 wxSize
sizeFit(GetScrollX(size
.x
) * GetScrollLineX(),
10704 GetScrollY(size
.y
) * GetScrollLineY());
10706 // distribute the extra space between the columns/rows to avoid having
10707 // extra white space
10708 wxCoord diff
= sizeFit
.x
- size
.x
;
10709 if ( diff
&& m_numCols
)
10711 // try to resize the columns uniformly
10712 wxCoord diffPerCol
= diff
/ m_numCols
;
10715 for ( int col
= 0; col
< m_numCols
; col
++ )
10717 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
10721 // add remaining amount to the last columns
10722 diff
-= diffPerCol
* m_numCols
;
10725 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
10727 SetColSize(col
, GetColWidth(col
) + 1);
10733 diff
= sizeFit
.y
- size
.y
;
10734 if ( diff
&& m_numRows
)
10736 // try to resize the columns uniformly
10737 wxCoord diffPerRow
= diff
/ m_numRows
;
10740 for ( int row
= 0; row
< m_numRows
; row
++ )
10742 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
10746 // add remaining amount to the last rows
10747 diff
-= diffPerRow
* m_numRows
;
10750 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
10752 SetRowSize(row
, GetRowHeight(row
) + 1);
10757 // we know that we're not going to have scrollbars so disable them now to
10758 // avoid trouble in SetClientSize() which can otherwise set the correct
10759 // client size but also leave space for (not needed any more) scrollbars
10760 SetScrollbars(0, 0, 0, 0, 0, 0, true);
10761 SetClientSize(sizeFit
.x
+ m_rowLabelWidth
, sizeFit
.y
+ m_colLabelHeight
);
10764 void wxGrid::AutoSizeRowLabelSize( int row
)
10766 wxArrayString lines
;
10769 // Hide the edit control, so it
10770 // won't interfere with drag-shrinking.
10771 if ( IsCellEditControlShown() )
10773 HideCellEditControl();
10774 SaveEditControlValue();
10777 // autosize row height depending on label text
10778 StringToLines( GetRowLabelValue( row
), lines
);
10779 wxClientDC
dc( m_rowLabelWin
);
10780 GetTextBoxSize( dc
, lines
, &w
, &h
);
10781 if ( h
< m_defaultRowHeight
)
10782 h
= m_defaultRowHeight
;
10783 SetRowSize(row
, h
);
10787 void wxGrid::AutoSizeColLabelSize( int col
)
10789 wxArrayString lines
;
10792 // Hide the edit control, so it
10793 // won't interfere with drag-shrinking.
10794 if ( IsCellEditControlShown() )
10796 HideCellEditControl();
10797 SaveEditControlValue();
10800 // autosize column width depending on label text
10801 StringToLines( GetColLabelValue( col
), lines
);
10802 wxClientDC
dc( m_colLabelWin
);
10803 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10804 GetTextBoxSize( dc
, lines
, &w
, &h
);
10806 GetTextBoxSize( dc
, lines
, &h
, &w
);
10807 if ( w
< m_defaultColWidth
)
10808 w
= m_defaultColWidth
;
10809 SetColSize(col
, w
);
10813 wxSize
wxGrid::DoGetBestSize() const
10815 wxGrid
*self
= (wxGrid
*)this; // const_cast
10817 // we do the same as in AutoSize() here with the exception that we don't
10818 // change the column/row sizes, only calculate them
10819 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
10820 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
10821 wxSize
sizeFit(GetScrollX(size
.x
) * GetScrollLineX(),
10822 GetScrollY(size
.y
) * GetScrollLineY());
10824 // NOTE: This size should be cached, but first we need to add calls to
10825 // InvalidateBestSize everywhere that could change the results of this
10827 // CacheBestSize(size);
10829 return wxSize(sizeFit
.x
+ m_rowLabelWidth
, sizeFit
.y
+ m_colLabelHeight
)
10830 + GetWindowBorderSize();
10838 wxPen
& wxGrid::GetDividerPen() const
10843 // ----------------------------------------------------------------------------
10844 // cell value accessor functions
10845 // ----------------------------------------------------------------------------
10847 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10851 m_table
->SetValue( row
, col
, s
);
10852 if ( !GetBatchCount() )
10855 wxRect
rect( CellToRect( row
, col
) );
10857 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10858 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10859 m_gridWin
->Refresh( false, &rect
);
10862 if ( m_currentCellCoords
.GetRow() == row
&&
10863 m_currentCellCoords
.GetCol() == col
&&
10864 IsCellEditControlShown())
10865 // Note: If we are using IsCellEditControlEnabled,
10866 // this interacts badly with calling SetCellValue from
10867 // an EVT_GRID_CELL_CHANGE handler.
10869 HideCellEditControl();
10870 ShowCellEditControl(); // will reread data from table
10875 // ----------------------------------------------------------------------------
10876 // block, row and column selection
10877 // ----------------------------------------------------------------------------
10879 void wxGrid::SelectRow( int row
, bool addToSelected
)
10881 if ( IsSelection() && !addToSelected
)
10885 m_selection
->SelectRow( row
, false, addToSelected
);
10888 void wxGrid::SelectCol( int col
, bool addToSelected
)
10890 if ( IsSelection() && !addToSelected
)
10894 m_selection
->SelectCol( col
, false, addToSelected
);
10897 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10898 bool addToSelected
)
10900 if ( IsSelection() && !addToSelected
)
10904 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10905 false, addToSelected
);
10908 void wxGrid::SelectAll()
10910 if ( m_numRows
> 0 && m_numCols
> 0 )
10913 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10917 // ----------------------------------------------------------------------------
10918 // cell, row and col deselection
10919 // ----------------------------------------------------------------------------
10921 void wxGrid::DeselectRow( int row
)
10923 if ( !m_selection
)
10926 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10928 if ( m_selection
->IsInSelection(row
, 0 ) )
10929 m_selection
->ToggleCellSelection(row
, 0);
10933 int nCols
= GetNumberCols();
10934 for ( int i
= 0; i
< nCols
; i
++ )
10936 if ( m_selection
->IsInSelection(row
, i
) )
10937 m_selection
->ToggleCellSelection(row
, i
);
10942 void wxGrid::DeselectCol( int col
)
10944 if ( !m_selection
)
10947 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10949 if ( m_selection
->IsInSelection(0, col
) )
10950 m_selection
->ToggleCellSelection(0, col
);
10954 int nRows
= GetNumberRows();
10955 for ( int i
= 0; i
< nRows
; i
++ )
10957 if ( m_selection
->IsInSelection(i
, col
) )
10958 m_selection
->ToggleCellSelection(i
, col
);
10963 void wxGrid::DeselectCell( int row
, int col
)
10965 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10966 m_selection
->ToggleCellSelection(row
, col
);
10969 bool wxGrid::IsSelection() const
10971 return ( m_selection
&& (m_selection
->IsSelection() ||
10972 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10973 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10976 bool wxGrid::IsInSelection( int row
, int col
) const
10978 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10979 ( row
>= m_selectingTopLeft
.GetRow() &&
10980 col
>= m_selectingTopLeft
.GetCol() &&
10981 row
<= m_selectingBottomRight
.GetRow() &&
10982 col
<= m_selectingBottomRight
.GetCol() )) );
10985 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10989 wxGridCellCoordsArray a
;
10993 return m_selection
->m_cellSelection
;
10996 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
11000 wxGridCellCoordsArray a
;
11004 return m_selection
->m_blockSelectionTopLeft
;
11007 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
11011 wxGridCellCoordsArray a
;
11015 return m_selection
->m_blockSelectionBottomRight
;
11018 wxArrayInt
wxGrid::GetSelectedRows() const
11026 return m_selection
->m_rowSelection
;
11029 wxArrayInt
wxGrid::GetSelectedCols() const
11037 return m_selection
->m_colSelection
;
11040 void wxGrid::ClearSelection()
11042 wxRect r1
= BlockToDeviceRect( m_selectingTopLeft
, m_selectingBottomRight
);
11043 wxRect r2
= BlockToDeviceRect( m_currentCellCoords
, m_selectingKeyboard
);
11044 m_selectingTopLeft
=
11045 m_selectingBottomRight
=
11046 m_selectingKeyboard
= wxGridNoCellCoords
;
11047 Refresh( false, &r1
);
11048 Refresh( false, &r2
);
11050 m_selection
->ClearSelection();
11053 // This function returns the rectangle that encloses the given block
11054 // in device coords clipped to the client size of the grid window.
11056 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
11057 const wxGridCellCoords
& bottomRight
) const
11060 wxRect tempCellRect
= CellToRect(topLeft
);
11061 if ( tempCellRect
!= wxGridNoCellRect
)
11063 resultRect
= tempCellRect
;
11067 resultRect
= wxRect(0, 0, 0, 0);
11070 tempCellRect
= CellToRect(bottomRight
);
11071 if ( tempCellRect
!= wxGridNoCellRect
)
11073 resultRect
+= tempCellRect
;
11077 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
11078 return wxGridNoCellRect
;
11081 // Ensure that left/right and top/bottom pairs are in order.
11082 int left
= resultRect
.GetLeft();
11083 int top
= resultRect
.GetTop();
11084 int right
= resultRect
.GetRight();
11085 int bottom
= resultRect
.GetBottom();
11087 int leftCol
= topLeft
.GetCol();
11088 int topRow
= topLeft
.GetRow();
11089 int rightCol
= bottomRight
.GetCol();
11090 int bottomRow
= bottomRight
.GetRow();
11099 leftCol
= rightCol
;
11110 topRow
= bottomRow
;
11114 // The following loop is ONLY necessary to detect and handle merged cells.
11116 m_gridWin
->GetClientSize( &cw
, &ch
);
11118 // Get the origin coordinates: notice that they will be negative if the
11119 // grid is scrolled downwards/to the right.
11120 int gridOriginX
= 0;
11121 int gridOriginY
= 0;
11122 CalcScrolledPosition(gridOriginX
, gridOriginY
, &gridOriginX
, &gridOriginY
);
11124 int onScreenLeftmostCol
= internalXToCol(-gridOriginX
);
11125 int onScreenUppermostRow
= internalYToRow(-gridOriginY
);
11127 int onScreenRightmostCol
= internalXToCol(-gridOriginX
+ cw
);
11128 int onScreenBottommostRow
= internalYToRow(-gridOriginY
+ ch
);
11130 // Bound our loop so that we only examine the portion of the selected block
11131 // that is shown on screen. Therefore, we compare the Top-Left block values
11132 // to the Top-Left screen values, and the Bottom-Right block values to the
11133 // Bottom-Right screen values, choosing appropriately.
11134 const int visibleTopRow
= wxMax(topRow
, onScreenUppermostRow
);
11135 const int visibleBottomRow
= wxMin(bottomRow
, onScreenBottommostRow
);
11136 const int visibleLeftCol
= wxMax(leftCol
, onScreenLeftmostCol
);
11137 const int visibleRightCol
= wxMin(rightCol
, onScreenRightmostCol
);
11139 for ( int j
= visibleTopRow
; j
<= visibleBottomRow
; j
++ )
11141 for ( int i
= visibleLeftCol
; i
<= visibleRightCol
; i
++ )
11143 if ( (j
== visibleTopRow
) || (j
== visibleBottomRow
) ||
11144 (i
== visibleLeftCol
) || (i
== visibleRightCol
) )
11146 tempCellRect
= CellToRect( j
, i
);
11148 if (tempCellRect
.x
< left
)
11149 left
= tempCellRect
.x
;
11150 if (tempCellRect
.y
< top
)
11151 top
= tempCellRect
.y
;
11152 if (tempCellRect
.x
+ tempCellRect
.width
> right
)
11153 right
= tempCellRect
.x
+ tempCellRect
.width
;
11154 if (tempCellRect
.y
+ tempCellRect
.height
> bottom
)
11155 bottom
= tempCellRect
.y
+ tempCellRect
.height
;
11159 i
= visibleRightCol
; // jump over inner cells.
11164 // Convert to scrolled coords
11165 CalcScrolledPosition( left
, top
, &left
, &top
);
11166 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
11168 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
11169 return wxRect(0,0,0,0);
11171 resultRect
.SetLeft( wxMax(0, left
) );
11172 resultRect
.SetTop( wxMax(0, top
) );
11173 resultRect
.SetRight( wxMin(cw
, right
) );
11174 resultRect
.SetBottom( wxMin(ch
, bottom
) );
11179 // ----------------------------------------------------------------------------
11181 // ----------------------------------------------------------------------------
11183 #if wxUSE_DRAG_AND_DROP
11185 // this allow setting drop target directly on wxGrid
11186 void wxGrid::SetDropTarget(wxDropTarget
*dropTarget
)
11188 GetGridWindow()->SetDropTarget(dropTarget
);
11191 #endif // wxUSE_DRAG_AND_DROP
11193 // ----------------------------------------------------------------------------
11194 // grid event classes
11195 // ----------------------------------------------------------------------------
11197 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
11199 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
11200 int row
, int col
, int x
, int y
, bool sel
,
11201 bool control
, bool shift
, bool alt
, bool meta
)
11202 : wxNotifyEvent( type
, id
)
11209 m_control
= control
;
11214 SetEventObject(obj
);
11218 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
11220 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
11221 int rowOrCol
, int x
, int y
,
11222 bool control
, bool shift
, bool alt
, bool meta
)
11223 : wxNotifyEvent( type
, id
)
11225 m_rowOrCol
= rowOrCol
;
11228 m_control
= control
;
11233 SetEventObject(obj
);
11237 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
11239 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
11240 const wxGridCellCoords
& topLeft
,
11241 const wxGridCellCoords
& bottomRight
,
11242 bool sel
, bool control
,
11243 bool shift
, bool alt
, bool meta
)
11244 : wxNotifyEvent( type
, id
)
11246 m_topLeft
= topLeft
;
11247 m_bottomRight
= bottomRight
;
11249 m_control
= control
;
11254 SetEventObject(obj
);
11258 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
11260 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
11261 wxObject
* obj
, int row
,
11262 int col
, wxControl
* ctrl
)
11263 : wxCommandEvent(type
, id
)
11265 SetEventObject(obj
);
11271 #endif // wxUSE_GRID