1 ///////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
5 // Modified by: Robin Dunn, Vadim Zeitlin, Santiago Palacios
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
25 #include "wx/dcclient.h"
26 #include "wx/settings.h"
28 #include "wx/textctrl.h"
29 #include "wx/checkbox.h"
30 #include "wx/combobox.h"
31 #include "wx/valtext.h"
34 #include "wx/listbox.h"
37 #include "wx/textfile.h"
38 #include "wx/spinctrl.h"
39 #include "wx/tokenzr.h"
40 #include "wx/renderer.h"
42 #include "wx/generic/gridsel.h"
44 const wxChar wxGridNameStr
[] = wxT("grid");
46 #if defined(__WXMOTIF__)
47 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
49 #define WXUNUSED_MOTIF(identifier) identifier
52 #if defined(__WXGTK__)
53 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
55 #define WXUNUSED_GTK(identifier) identifier
58 // Required for wxIs... functions
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr
*, wxArrayAttrs
,
66 class WXDLLIMPEXP_ADV
);
68 struct wxGridCellWithAttr
70 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
71 : coords(row
, col
), attr(attr_
)
76 wxGridCellWithAttr(const wxGridCellWithAttr
& other
)
77 : coords(other
.coords
),
83 wxGridCellWithAttr
& operator=(const wxGridCellWithAttr
& other
)
85 coords
= other
.coords
;
86 if (attr
!= other
.attr
)
95 void ChangeAttr(wxGridCellAttr
* new_attr
)
99 // "Delete" (i.e. DecRef) the old attribute.
102 // Take ownership of the new attribute, i.e. no IncRef.
106 ~wxGridCellWithAttr()
111 wxGridCellCoords coords
;
112 wxGridCellAttr
*attr
;
115 WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr
, wxGridCellWithAttrArray
,
116 class WXDLLIMPEXP_ADV
);
118 #include "wx/arrimpl.cpp"
120 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
121 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
123 // ----------------------------------------------------------------------------
125 // ----------------------------------------------------------------------------
127 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
128 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
129 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
130 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
131 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_BEGIN_DRAG
)
132 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
133 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
134 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
135 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
136 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
137 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
138 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_MOVE
)
139 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
140 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
141 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
142 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
143 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
144 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
146 // ----------------------------------------------------------------------------
148 // ----------------------------------------------------------------------------
150 // common base class for various grid subwindows
151 class WXDLLIMPEXP_ADV wxGridSubwindow
: public wxWindow
154 wxGridSubwindow() { m_owner
= NULL
; }
155 wxGridSubwindow(wxGrid
*owner
,
159 int additionalStyle
= 0,
160 const wxString
& name
= wxPanelNameStr
)
161 : wxWindow(owner
, id
, pos
, size
,
162 wxBORDER_NONE
| additionalStyle
,
168 virtual bool AcceptsFocus() const { return false; }
170 wxGrid
*GetOwner() { return m_owner
; }
173 void OnMouseCaptureLost(wxMouseCaptureLostEvent
& event
);
177 DECLARE_EVENT_TABLE()
178 DECLARE_NO_COPY_CLASS(wxGridSubwindow
)
181 class WXDLLIMPEXP_ADV wxGridRowLabelWindow
: public wxGridSubwindow
184 wxGridRowLabelWindow() { }
185 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
186 const wxPoint
&pos
, const wxSize
&size
);
189 void OnPaint( wxPaintEvent
& event
);
190 void OnMouseEvent( wxMouseEvent
& event
);
191 void OnMouseWheel( wxMouseEvent
& event
);
193 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
194 DECLARE_EVENT_TABLE()
195 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow
)
199 class WXDLLIMPEXP_ADV wxGridColLabelWindow
: public wxGridSubwindow
202 wxGridColLabelWindow() { }
203 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
204 const wxPoint
&pos
, const wxSize
&size
);
207 void OnPaint( wxPaintEvent
& event
);
208 void OnMouseEvent( wxMouseEvent
& event
);
209 void OnMouseWheel( wxMouseEvent
& event
);
211 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
212 DECLARE_EVENT_TABLE()
213 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow
)
217 class WXDLLIMPEXP_ADV wxGridCornerLabelWindow
: public wxGridSubwindow
220 wxGridCornerLabelWindow() { }
221 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
222 const wxPoint
&pos
, const wxSize
&size
);
225 void OnMouseEvent( wxMouseEvent
& event
);
226 void OnMouseWheel( wxMouseEvent
& event
);
227 void OnPaint( wxPaintEvent
& event
);
229 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
230 DECLARE_EVENT_TABLE()
231 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow
)
234 class WXDLLIMPEXP_ADV wxGridWindow
: public wxGridSubwindow
239 m_rowLabelWin
= NULL
;
240 m_colLabelWin
= NULL
;
243 wxGridWindow( wxGrid
*parent
,
244 wxGridRowLabelWindow
*rowLblWin
,
245 wxGridColLabelWindow
*colLblWin
,
246 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
248 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
250 virtual bool AcceptsFocus() const { return true; }
253 wxGridRowLabelWindow
*m_rowLabelWin
;
254 wxGridColLabelWindow
*m_colLabelWin
;
256 void OnPaint( wxPaintEvent
&event
);
257 void OnMouseWheel( wxMouseEvent
& event
);
258 void OnMouseEvent( wxMouseEvent
& event
);
259 void OnKeyDown( wxKeyEvent
& );
260 void OnKeyUp( wxKeyEvent
& );
261 void OnChar( wxKeyEvent
& );
262 void OnEraseBackground( wxEraseEvent
& );
263 void OnFocus( wxFocusEvent
& );
265 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
266 DECLARE_EVENT_TABLE()
267 DECLARE_NO_COPY_CLASS(wxGridWindow
)
271 class wxGridCellEditorEvtHandler
: public wxEvtHandler
274 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
281 void OnKillFocus(wxFocusEvent
& event
);
282 void OnKeyDown(wxKeyEvent
& event
);
283 void OnChar(wxKeyEvent
& event
);
285 void SetInSetFocus(bool inSetFocus
) { m_inSetFocus
= inSetFocus
; }
289 wxGridCellEditor
*m_editor
;
291 // Work around the fact that a focus kill event can be sent to
292 // a combobox within a set focus event.
295 DECLARE_EVENT_TABLE()
296 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
297 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler
)
301 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler
, wxEvtHandler
)
303 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
304 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus
)
305 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
306 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
310 // ----------------------------------------------------------------------------
311 // the internal data representation used by wxGridCellAttrProvider
312 // ----------------------------------------------------------------------------
314 // this class stores attributes set for cells
315 class WXDLLIMPEXP_ADV wxGridCellAttrData
318 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
319 wxGridCellAttr
*GetAttr(int row
, int col
) const;
320 void UpdateAttrRows( size_t pos
, int numRows
);
321 void UpdateAttrCols( size_t pos
, int numCols
);
324 // searches for the attr for given cell, returns wxNOT_FOUND if not found
325 int FindIndex(int row
, int col
) const;
327 wxGridCellWithAttrArray m_attrs
;
330 // this class stores attributes set for rows or columns
331 class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
334 // empty ctor to suppress warnings
335 wxGridRowOrColAttrData() {}
336 ~wxGridRowOrColAttrData();
338 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
339 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
340 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
343 wxArrayInt m_rowsOrCols
;
344 wxArrayAttrs m_attrs
;
347 // NB: this is just a wrapper around 3 objects: one which stores cell
348 // attributes, and 2 others for row/col ones
349 class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
352 wxGridCellAttrData m_cellAttrs
;
353 wxGridRowOrColAttrData m_rowAttrs
,
358 // ----------------------------------------------------------------------------
359 // data structures used for the data type registry
360 // ----------------------------------------------------------------------------
362 struct wxGridDataTypeInfo
364 wxGridDataTypeInfo(const wxString
& typeName
,
365 wxGridCellRenderer
* renderer
,
366 wxGridCellEditor
* editor
)
367 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
370 ~wxGridDataTypeInfo()
372 wxSafeDecRef(m_renderer
);
373 wxSafeDecRef(m_editor
);
377 wxGridCellRenderer
* m_renderer
;
378 wxGridCellEditor
* m_editor
;
380 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo
)
384 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
,
385 class WXDLLIMPEXP_ADV
);
388 class WXDLLIMPEXP_ADV wxGridTypeRegistry
391 wxGridTypeRegistry() {}
392 ~wxGridTypeRegistry();
394 void RegisterDataType(const wxString
& typeName
,
395 wxGridCellRenderer
* renderer
,
396 wxGridCellEditor
* editor
);
398 // find one of already registered data types
399 int FindRegisteredDataType(const wxString
& typeName
);
401 // try to FindRegisteredDataType(), if this fails and typeName is one of
402 // standard typenames, register it and return its index
403 int FindDataType(const wxString
& typeName
);
405 // try to FindDataType(), if it fails see if it is not one of already
406 // registered data types with some params in which case clone the
407 // registered data type and set params for it
408 int FindOrCloneDataType(const wxString
& typeName
);
410 wxGridCellRenderer
* GetRenderer(int index
);
411 wxGridCellEditor
* GetEditor(int index
);
414 wxGridDataTypeInfoArray m_typeinfo
;
418 // ----------------------------------------------------------------------------
419 // conditional compilation
420 // ----------------------------------------------------------------------------
422 #ifndef WXGRID_DRAW_LINES
423 #define WXGRID_DRAW_LINES 1
426 // ----------------------------------------------------------------------------
428 // ----------------------------------------------------------------------------
430 //#define DEBUG_ATTR_CACHE
431 #ifdef DEBUG_ATTR_CACHE
432 static size_t gs_nAttrCacheHits
= 0;
433 static size_t gs_nAttrCacheMisses
= 0;
436 // ----------------------------------------------------------------------------
438 // ----------------------------------------------------------------------------
440 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
441 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
444 // TODO: this doesn't work at all, grid cells have different sizes and approx
445 // calculations don't work as because of the size mismatch scrollbars
446 // sometimes fail to be shown when they should be or vice versa
448 // The scroll bars may be a little flakey once in a while, but that is
449 // surely much less horrible than having scroll lines of only 1!!!
452 // Well, it's still seriously broken so it might be better but needs
455 static const size_t GRID_SCROLL_LINE_X
= 15; // 1;
456 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
458 // the size of hash tables used a bit everywhere (the max number of elements
459 // in these hash tables is the number of rows/columns)
460 static const int GRID_HASH_SIZE
= 100;
463 // ----------------------------------------------------------------------------
465 // ----------------------------------------------------------------------------
467 static inline int GetScrollX(int x
)
469 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
472 static inline int GetScrollY(int y
)
474 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
478 // ============================================================================
480 // ============================================================================
482 // ----------------------------------------------------------------------------
484 // ----------------------------------------------------------------------------
486 wxGridCellEditor::wxGridCellEditor()
492 wxGridCellEditor::~wxGridCellEditor()
497 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
498 wxWindowID
WXUNUSED(id
),
499 wxEvtHandler
* evtHandler
)
502 m_control
->PushEventHandler(evtHandler
);
505 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
506 wxGridCellAttr
*attr
)
508 // erase the background because we might not fill the cell
509 wxClientDC
dc(m_control
->GetParent());
510 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
512 gridWindow
->GetOwner()->PrepareDC(dc
);
514 dc
.SetPen(*wxTRANSPARENT_PEN
);
515 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxBRUSHSTYLE_SOLID
));
516 dc
.DrawRectangle(rectCell
);
518 // redraw the control we just painted over
519 m_control
->Refresh();
522 void wxGridCellEditor::Destroy()
526 m_control
->PopEventHandler( true /* delete it*/ );
528 m_control
->Destroy();
533 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
535 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
537 m_control
->Show(show
);
541 // set the colours/fonts if we have any
544 m_colFgOld
= m_control
->GetForegroundColour();
545 m_control
->SetForegroundColour(attr
->GetTextColour());
547 m_colBgOld
= m_control
->GetBackgroundColour();
548 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
550 // Workaround for GTK+1 font setting problem on some platforms
551 #if !defined(__WXGTK__) || defined(__WXGTK20__)
552 m_fontOld
= m_control
->GetFont();
553 m_control
->SetFont(attr
->GetFont());
556 // can't do anything more in the base class version, the other
557 // attributes may only be used by the derived classes
562 // restore the standard colours fonts
563 if ( m_colFgOld
.Ok() )
565 m_control
->SetForegroundColour(m_colFgOld
);
566 m_colFgOld
= wxNullColour
;
569 if ( m_colBgOld
.Ok() )
571 m_control
->SetBackgroundColour(m_colBgOld
);
572 m_colBgOld
= wxNullColour
;
575 // Workaround for GTK+1 font setting problem on some platforms
576 #if !defined(__WXGTK__) || defined(__WXGTK20__)
577 if ( m_fontOld
.Ok() )
579 m_control
->SetFont(m_fontOld
);
580 m_fontOld
= wxNullFont
;
586 void wxGridCellEditor::SetSize(const wxRect
& rect
)
588 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
590 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
593 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
598 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
600 bool ctrl
= event
.ControlDown();
601 bool alt
= event
.AltDown();
604 // On the Mac the Alt key is more like shift and is used for entry of
605 // valid characters, so check for Ctrl and Meta instead.
606 alt
= event
.MetaDown();
609 // Assume it's not a valid char if ctrl or alt is down, but if both are
610 // down then it may be because of an AltGr key combination, so let them
611 // through in that case.
612 if ((ctrl
|| alt
) && !(ctrl
&& alt
))
619 // If it's a F-Key or other special key then it shouldn't start the
621 if (event
.GetKeyCode() >= WXK_START
)
625 // if the unicode key code is not really a unicode character (it may
626 // be a function key or etc., the platforms appear to always give us a
627 // small value in this case) then fallback to the ASCII key code but
628 // don't do anything for function keys or etc.
629 key
= event
.GetUnicodeKey();
632 key
= event
.GetKeyCode();
633 keyOk
= (key
<= 127);
636 key
= event
.GetKeyCode();
637 keyOk
= (key
<= 255);
643 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
648 void wxGridCellEditor::StartingClick()
654 // ----------------------------------------------------------------------------
655 // wxGridCellTextEditor
656 // ----------------------------------------------------------------------------
658 wxGridCellTextEditor::wxGridCellTextEditor()
663 void wxGridCellTextEditor::Create(wxWindow
* parent
,
665 wxEvtHandler
* evtHandler
)
667 DoCreate(parent
, id
, evtHandler
);
670 void wxGridCellTextEditor::DoCreate(wxWindow
* parent
,
672 wxEvtHandler
* evtHandler
,
675 style
|= wxTE_PROCESS_ENTER
| wxTE_PROCESS_TAB
| wxNO_BORDER
;
677 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
678 wxDefaultPosition
, wxDefaultSize
,
681 // set max length allowed in the textctrl, if the parameter was set
682 if ( m_maxChars
!= 0 )
684 Text()->SetMaxLength(m_maxChars
);
687 wxGridCellEditor::Create(parent
, id
, evtHandler
);
690 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
691 wxGridCellAttr
* WXUNUSED(attr
))
693 // as we fill the entire client area,
694 // don't do anything here to minimize flicker
697 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
699 wxRect
rect(rectOrig
);
701 // Make the edit control large enough to allow for internal margins
703 // TODO: remove this if the text ctrl sizing is improved esp. for unix
705 #if defined(__WXGTK__)
713 #elif defined(__WXMSW__)
727 int extra_x
= ( rect
.x
> 2 ) ? 2 : 1;
728 int extra_y
= ( rect
.y
> 2 ) ? 2 : 1;
730 #if defined(__WXMOTIF__)
735 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
736 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
737 rect
.SetRight( rect
.GetRight() + 2 * extra_x
);
738 rect
.SetBottom( rect
.GetBottom() + 2 * extra_y
);
741 wxGridCellEditor::SetSize(rect
);
744 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
746 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
748 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
750 DoBeginEdit(m_startValue
);
753 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
755 Text()->SetValue(startValue
);
756 Text()->SetInsertionPointEnd();
757 Text()->SetSelection(-1, -1);
761 bool wxGridCellTextEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
763 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
765 bool changed
= false;
766 wxString value
= Text()->GetValue();
767 if (value
!= m_startValue
)
771 grid
->GetTable()->SetValue(row
, col
, value
);
773 m_startValue
= wxEmptyString
;
775 // No point in setting the text of the hidden control
776 //Text()->SetValue(m_startValue);
781 void wxGridCellTextEditor::Reset()
783 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
785 DoReset(m_startValue
);
788 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
790 Text()->SetValue(startValue
);
791 Text()->SetInsertionPointEnd();
794 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
796 return wxGridCellEditor::IsAcceptedKey(event
);
799 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
801 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
802 // longer an appropriate way to get the character into the text control.
803 // Do it ourselves instead. We know that if we get this far that we have
804 // a valid character, so not a whole lot of testing needs to be done.
806 wxTextCtrl
* tc
= Text();
811 ch
= event
.GetUnicodeKey();
813 ch
= (wxChar
)event
.GetKeyCode();
815 ch
= (wxChar
)event
.GetKeyCode();
821 // delete the character at the cursor
822 pos
= tc
->GetInsertionPoint();
823 if (pos
< tc
->GetLastPosition())
824 tc
->Remove(pos
, pos
+ 1);
828 // delete the character before the cursor
829 pos
= tc
->GetInsertionPoint();
831 tc
->Remove(pos
- 1, pos
);
840 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
841 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
843 #if defined(__WXMOTIF__) || defined(__WXGTK__)
844 // wxMotif needs a little extra help...
845 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
846 wxString
s( Text()->GetValue() );
847 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
849 Text()->SetInsertionPoint( pos
);
851 // the other ports can handle a Return key press
857 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
867 if ( params
.ToLong(&tmp
) )
869 m_maxChars
= (size_t)tmp
;
873 wxLogDebug( _T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str() );
878 // return the value in the text control
879 wxString
wxGridCellTextEditor::GetValue() const
881 return Text()->GetValue();
884 // ----------------------------------------------------------------------------
885 // wxGridCellNumberEditor
886 // ----------------------------------------------------------------------------
888 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
894 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
896 wxEvtHandler
* evtHandler
)
901 // create a spin ctrl
902 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
903 wxDefaultPosition
, wxDefaultSize
,
907 wxGridCellEditor::Create(parent
, id
, evtHandler
);
912 // just a text control
913 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
916 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
921 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
923 // first get the value
924 wxGridTableBase
*table
= grid
->GetTable();
925 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
927 m_valueOld
= table
->GetValueAsLong(row
, col
);
932 wxString sValue
= table
->GetValue(row
, col
);
933 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty())
935 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
943 Spin()->SetValue((int)m_valueOld
);
949 DoBeginEdit(GetString());
953 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
963 value
= Spin()->GetValue();
964 changed
= value
!= m_valueOld
;
966 text
= wxString::Format(wxT("%ld"), value
);
971 text
= Text()->GetValue();
972 changed
= (text
.empty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
977 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
978 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
980 grid
->GetTable()->SetValue(row
, col
, text
);
986 void wxGridCellNumberEditor::Reset()
991 Spin()->SetValue((int)m_valueOld
);
996 DoReset(GetString());
1000 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
1002 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1004 int keycode
= event
.GetKeyCode();
1005 if ( (keycode
< 128) &&
1006 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
1015 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
1017 int keycode
= event
.GetKeyCode();
1020 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
1022 wxGridCellTextEditor::StartingKey(event
);
1024 // skip Skip() below
1031 if ( wxIsdigit(keycode
) )
1033 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
1034 spin
->SetValue(keycode
- '0');
1035 spin
->SetSelection(1,1);
1044 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
1055 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1059 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1063 // skip the error message below
1068 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1072 // return the value in the spin control if it is there (the text control otherwise)
1073 wxString
wxGridCellNumberEditor::GetValue() const
1080 long value
= Spin()->GetValue();
1081 s
.Printf(wxT("%ld"), value
);
1086 s
= Text()->GetValue();
1092 // ----------------------------------------------------------------------------
1093 // wxGridCellFloatEditor
1094 // ----------------------------------------------------------------------------
1096 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1099 m_precision
= precision
;
1102 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1104 wxEvtHandler
* evtHandler
)
1106 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1108 #if wxUSE_VALIDATORS
1109 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1113 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1115 // first get the value
1116 wxGridTableBase
* const table
= grid
->GetTable();
1117 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1119 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1125 const wxString value
= table
->GetValue(row
, col
);
1126 if ( !value
.empty() )
1128 if ( !value
.ToDouble(&m_valueOld
) )
1130 wxFAIL_MSG( _T("this cell doesn't have float value") );
1136 DoBeginEdit(GetString());
1139 bool wxGridCellFloatEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
1141 const wxString
text(Text()->GetValue()),
1142 textOld(grid
->GetCellValue(row
, col
));
1145 if ( !text
.empty() )
1147 if ( !text
.ToDouble(&value
) )
1150 else // new value is empty string
1152 if ( textOld
.empty() )
1153 return false; // nothing changed
1158 // the test for empty strings ensures that we don't skip the value setting
1159 // when "" is replaced by "0" or vice versa as "" numeric value is also 0.
1160 if ( wxIsSameDouble(value
, m_valueOld
) && !text
.empty() && !textOld
.empty() )
1161 return false; // nothing changed
1163 wxGridTableBase
* const table
= grid
->GetTable();
1165 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1166 table
->SetValueAsDouble(row
, col
, value
);
1168 table
->SetValue(row
, col
, text
);
1173 void wxGridCellFloatEditor::Reset()
1175 DoReset(GetString());
1178 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1180 int keycode
= event
.GetKeyCode();
1182 tmpbuf
[0] = (char) keycode
;
1184 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1187 bool is_decimal_point
= ( strbuf
==
1188 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1190 bool is_decimal_point
= ( strbuf
== _T(".") );
1193 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1194 || is_decimal_point
)
1196 wxGridCellTextEditor::StartingKey(event
);
1198 // skip Skip() below
1205 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1216 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1220 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1222 m_precision
= (int)tmp
;
1224 // skip the error message below
1229 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1233 wxString
wxGridCellFloatEditor::GetString() const
1236 if ( m_precision
== -1 && m_width
!= -1)
1238 // default precision
1239 fmt
.Printf(_T("%%%d.f"), m_width
);
1241 else if ( m_precision
!= -1 && m_width
== -1)
1244 fmt
.Printf(_T("%%.%df"), m_precision
);
1246 else if ( m_precision
!= -1 && m_width
!= -1 )
1248 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1252 // default width/precision
1256 return wxString::Format(fmt
, m_valueOld
);
1259 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1261 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1263 const int keycode
= event
.GetKeyCode();
1264 if ( isascii(keycode
) )
1267 tmpbuf
[0] = (char) keycode
;
1269 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1272 const wxString decimalPoint
=
1273 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
1275 const wxString
decimalPoint(_T('.'));
1278 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1279 if ( wxIsdigit(keycode
) ||
1280 tolower(keycode
) == 'e' ||
1281 keycode
== decimalPoint
||
1293 #endif // wxUSE_TEXTCTRL
1297 // ----------------------------------------------------------------------------
1298 // wxGridCellBoolEditor
1299 // ----------------------------------------------------------------------------
1301 // the default values for GetValue()
1302 wxString
wxGridCellBoolEditor::ms_stringValues
[2] = { _T(""), _T("1") };
1304 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1306 wxEvtHandler
* evtHandler
)
1308 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1309 wxDefaultPosition
, wxDefaultSize
,
1312 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1315 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1317 bool resize
= false;
1318 wxSize size
= m_control
->GetSize();
1319 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1321 // check if the checkbox is not too big/small for this cell
1322 wxSize sizeBest
= m_control
->GetBestSize();
1323 if ( !(size
== sizeBest
) )
1325 // reset to default size if it had been made smaller
1331 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1333 // leave 1 pixel margin
1334 size
.x
= size
.y
= minSize
- 2;
1341 m_control
->SetSize(size
);
1344 // position it in the centre of the rectangle (TODO: support alignment?)
1346 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1347 // the checkbox without label still has some space to the right in wxGTK,
1348 // so shift it to the right
1350 #elif defined(__WXMSW__)
1351 // here too, but in other way
1356 int hAlign
= wxALIGN_CENTRE
;
1357 int vAlign
= wxALIGN_CENTRE
;
1359 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1362 if (hAlign
== wxALIGN_LEFT
)
1370 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1372 else if (hAlign
== wxALIGN_RIGHT
)
1374 x
= r
.x
+ r
.width
- size
.x
- 2;
1375 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1377 else if (hAlign
== wxALIGN_CENTRE
)
1379 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1380 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1383 m_control
->Move(x
, y
);
1386 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1388 m_control
->Show(show
);
1392 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1393 CBox()->SetBackgroundColour(colBg
);
1397 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1399 wxASSERT_MSG(m_control
,
1400 wxT("The wxGridCellEditor must be created first!"));
1402 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1404 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1408 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1410 if ( cellval
== ms_stringValues
[false] )
1411 m_startValue
= false;
1412 else if ( cellval
== ms_stringValues
[true] )
1413 m_startValue
= true;
1416 // do not try to be smart here and convert it to true or false
1417 // because we'll still overwrite it with something different and
1418 // this risks to be very surprising for the user code, let them
1420 wxFAIL_MSG( _T("invalid value for a cell with bool editor!") );
1424 CBox()->SetValue(m_startValue
);
1428 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1431 wxASSERT_MSG(m_control
,
1432 wxT("The wxGridCellEditor must be created first!"));
1434 bool changed
= false;
1435 bool value
= CBox()->GetValue();
1436 if ( value
!= m_startValue
)
1441 wxGridTableBase
* const table
= grid
->GetTable();
1442 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1443 table
->SetValueAsBool(row
, col
, value
);
1445 table
->SetValue(row
, col
, GetValue());
1451 void wxGridCellBoolEditor::Reset()
1453 wxASSERT_MSG(m_control
,
1454 wxT("The wxGridCellEditor must be created first!"));
1456 CBox()->SetValue(m_startValue
);
1459 void wxGridCellBoolEditor::StartingClick()
1461 CBox()->SetValue(!CBox()->GetValue());
1464 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1466 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1468 int keycode
= event
.GetKeyCode();
1481 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1483 int keycode
= event
.GetKeyCode();
1487 CBox()->SetValue(!CBox()->GetValue());
1491 CBox()->SetValue(true);
1495 CBox()->SetValue(false);
1500 wxString
wxGridCellBoolEditor::GetValue() const
1502 return ms_stringValues
[CBox()->GetValue()];
1506 wxGridCellBoolEditor::UseStringValues(const wxString
& valueTrue
,
1507 const wxString
& valueFalse
)
1509 ms_stringValues
[false] = valueFalse
;
1510 ms_stringValues
[true] = valueTrue
;
1514 wxGridCellBoolEditor::IsTrueValue(const wxString
& value
)
1516 return value
== ms_stringValues
[true];
1519 #endif // wxUSE_CHECKBOX
1523 // ----------------------------------------------------------------------------
1524 // wxGridCellChoiceEditor
1525 // ----------------------------------------------------------------------------
1527 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1529 : m_choices(choices
),
1530 m_allowOthers(allowOthers
) { }
1532 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1533 const wxString choices
[],
1535 : m_allowOthers(allowOthers
)
1539 m_choices
.Alloc(count
);
1540 for ( size_t n
= 0; n
< count
; n
++ )
1542 m_choices
.Add(choices
[n
]);
1547 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1549 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1550 editor
->m_allowOthers
= m_allowOthers
;
1551 editor
->m_choices
= m_choices
;
1556 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1558 wxEvtHandler
* evtHandler
)
1560 int style
= wxTE_PROCESS_ENTER
|
1564 if ( !m_allowOthers
)
1565 style
|= wxCB_READONLY
;
1566 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1567 wxDefaultPosition
, wxDefaultSize
,
1571 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1574 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1575 wxGridCellAttr
* attr
)
1577 // as we fill the entire client area, don't do anything here to minimize
1580 // TODO: It doesn't actually fill the client area since the height of a
1581 // combo always defaults to the standard. Until someone has time to
1582 // figure out the right rectangle to paint, just do it the normal way.
1583 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1586 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1588 wxASSERT_MSG(m_control
,
1589 wxT("The wxGridCellEditor must be created first!"));
1591 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1593 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1595 // Don't immediately end if we get a kill focus event within BeginEdit
1597 evtHandler
->SetInSetFocus(true);
1599 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1601 Reset(); // this updates combo box to correspond to m_startValue
1603 Combo()->SetFocus();
1607 // When dropping down the menu, a kill focus event
1608 // happens after this point, so we can't reset the flag yet.
1609 #if !defined(__WXGTK20__)
1610 evtHandler
->SetInSetFocus(false);
1615 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1618 wxString value
= Combo()->GetValue();
1619 if ( value
== m_startValue
)
1622 grid
->GetTable()->SetValue(row
, col
, value
);
1627 void wxGridCellChoiceEditor::Reset()
1631 Combo()->SetValue(m_startValue
);
1632 Combo()->SetInsertionPointEnd();
1634 else // the combobox is read-only
1636 // find the right position, or default to the first if not found
1637 int pos
= Combo()->FindString(m_startValue
);
1638 if (pos
== wxNOT_FOUND
)
1640 Combo()->SetSelection(pos
);
1644 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1654 wxStringTokenizer
tk(params
, _T(','));
1655 while ( tk
.HasMoreTokens() )
1657 m_choices
.Add(tk
.GetNextToken());
1661 // return the value in the text control
1662 wxString
wxGridCellChoiceEditor::GetValue() const
1664 return Combo()->GetValue();
1667 #endif // wxUSE_COMBOBOX
1669 // ----------------------------------------------------------------------------
1670 // wxGridCellEditorEvtHandler
1671 // ----------------------------------------------------------------------------
1673 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
1675 // Don't disable the cell if we're just starting to edit it
1680 m_grid
->DisableCellEditControl();
1685 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1687 switch ( event
.GetKeyCode() )
1691 m_grid
->DisableCellEditControl();
1695 m_grid
->GetEventHandler()->ProcessEvent( event
);
1699 case WXK_NUMPAD_ENTER
:
1700 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1701 m_editor
->HandleReturn(event
);
1710 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1712 int row
= m_grid
->GetGridCursorRow();
1713 int col
= m_grid
->GetGridCursorCol();
1714 wxRect rect
= m_grid
->CellToRect( row
, col
);
1716 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
1718 // if cell width is smaller than grid client area, cell is wholly visible
1719 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
1721 switch ( event
.GetKeyCode() )
1726 case WXK_NUMPAD_ENTER
:
1731 if ( wholeCellVisible
)
1733 // no special processing needed...
1738 // do special processing for partly visible cell...
1740 // get the widths of all cells previous to this one
1742 for ( int i
= 0; i
< col
; i
++ )
1744 colXPos
+= m_grid
->GetColSize(i
);
1747 int xUnit
= 1, yUnit
= 1;
1748 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1751 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1755 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
1763 if ( wholeCellVisible
)
1765 // no special processing needed...
1770 // do special processing for partly visible cell...
1773 wxString value
= m_grid
->GetCellValue(row
, col
);
1774 if ( wxEmptyString
!= value
)
1776 // get width of cell CONTENTS (text)
1778 wxFont font
= m_grid
->GetCellFont(row
, col
);
1779 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
1781 // try to RIGHT align the text by scrolling
1782 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
1784 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
1785 // otherwise the last part of the cell content might be hidden below the scroll bar
1786 // FIXME: maybe there is a more suitable correction?
1787 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
1788 if ( textWidth
< 0 )
1794 // get the widths of all cells previous to this one
1796 for ( int i
= 0; i
< col
; i
++ )
1798 colXPos
+= m_grid
->GetColSize(i
);
1801 // and add the (modified) text width of the cell contents
1802 // as we'd like to see the last part of the cell contents
1803 colXPos
+= textWidth
;
1805 int xUnit
= 1, yUnit
= 1;
1806 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1807 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1818 // ----------------------------------------------------------------------------
1819 // wxGridCellWorker is an (almost) empty common base class for
1820 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1821 // ----------------------------------------------------------------------------
1823 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1828 wxGridCellWorker::~wxGridCellWorker()
1832 // ============================================================================
1834 // ============================================================================
1836 // ----------------------------------------------------------------------------
1837 // wxGridCellRenderer
1838 // ----------------------------------------------------------------------------
1840 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1841 wxGridCellAttr
& attr
,
1844 int WXUNUSED(row
), int WXUNUSED(col
),
1847 dc
.SetBackgroundMode( wxBRUSHSTYLE_SOLID
);
1849 // grey out fields if the grid is disabled
1850 if ( grid
.IsEnabled() )
1855 if ( grid
.HasFocus() )
1856 clr
= grid
.GetSelectionBackground();
1858 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
1859 dc
.SetBrush( wxBrush(clr
, wxBRUSHSTYLE_SOLID
) );
1863 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxBRUSHSTYLE_SOLID
) );
1868 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
), wxBRUSHSTYLE_SOLID
));
1871 dc
.SetPen( *wxTRANSPARENT_PEN
);
1872 dc
.DrawRectangle(rect
);
1875 // ----------------------------------------------------------------------------
1876 // wxGridCellStringRenderer
1877 // ----------------------------------------------------------------------------
1879 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
1880 const wxGridCellAttr
& attr
,
1884 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
1886 // TODO some special colours for attr.IsReadOnly() case?
1888 // different coloured text when the grid is disabled
1889 if ( grid
.IsEnabled() )
1894 if ( grid
.HasFocus() )
1895 clr
= grid
.GetSelectionBackground();
1897 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
1898 dc
.SetTextBackground( clr
);
1899 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1903 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1904 dc
.SetTextForeground( attr
.GetTextColour() );
1909 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1910 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
1913 dc
.SetFont( attr
.GetFont() );
1916 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
1918 const wxString
& text
)
1920 wxCoord x
= 0, y
= 0, max_x
= 0;
1921 dc
.SetFont(attr
.GetFont());
1922 wxStringTokenizer
tk(text
, _T('\n'));
1923 while ( tk
.HasMoreTokens() )
1925 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1926 max_x
= wxMax(max_x
, x
);
1929 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1931 return wxSize(max_x
, y
);
1934 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1935 wxGridCellAttr
& attr
,
1939 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1942 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1943 wxGridCellAttr
& attr
,
1945 const wxRect
& rectCell
,
1949 wxRect rect
= rectCell
;
1952 // erase only this cells background, overflow cells should have been erased
1953 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1956 attr
.GetAlignment(&hAlign
, &vAlign
);
1958 int overflowCols
= 0;
1960 if (attr
.GetOverflow())
1962 int cols
= grid
.GetNumberCols();
1963 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1964 int cell_rows
, cell_cols
;
1965 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <= 0
1966 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1968 int i
, c_cols
, c_rows
;
1969 for (i
= col
+cell_cols
; i
< cols
; i
++)
1971 bool is_empty
= true;
1972 for (int j
=row
; j
< row
+ cell_rows
; j
++)
1974 // check w/ anchor cell for multicell block
1975 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1978 if (!grid
.GetTable()->IsEmptyCell(j
+ c_rows
, i
))
1987 rect
.width
+= grid
.GetColSize(i
);
1995 if (rect
.width
>= best_width
)
1999 overflowCols
= i
- col
- cell_cols
+ 1;
2000 if (overflowCols
>= cols
)
2001 overflowCols
= cols
- 1;
2004 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
2006 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
2008 clip
.x
+= rectCell
.width
;
2009 // draw each overflow cell individually
2010 int col_end
= col
+ cell_cols
+ overflowCols
;
2011 if (col_end
>= grid
.GetNumberCols())
2012 col_end
= grid
.GetNumberCols() - 1;
2013 for (int i
= col
+ cell_cols
; i
<= col_end
; i
++)
2015 clip
.width
= grid
.GetColSize(i
) - 1;
2016 dc
.DestroyClippingRegion();
2017 dc
.SetClippingRegion(clip
);
2019 SetTextColoursAndFont(grid
, attr
, dc
,
2020 grid
.IsInSelection(row
,i
));
2022 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
2023 rect
, hAlign
, vAlign
);
2024 clip
.x
+= grid
.GetColSize(i
) - 1;
2030 dc
.DestroyClippingRegion();
2034 // now we only have to draw the text
2035 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2037 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
2038 rect
, hAlign
, vAlign
);
2041 // ----------------------------------------------------------------------------
2042 // wxGridCellNumberRenderer
2043 // ----------------------------------------------------------------------------
2045 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2047 wxGridTableBase
*table
= grid
.GetTable();
2049 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
2051 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
2055 text
= table
->GetValue(row
, col
);
2061 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
2062 wxGridCellAttr
& attr
,
2064 const wxRect
& rectCell
,
2068 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2070 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2072 // draw the text right aligned by default
2074 attr
.GetAlignment(&hAlign
, &vAlign
);
2075 hAlign
= wxALIGN_RIGHT
;
2077 wxRect rect
= rectCell
;
2080 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2083 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
2084 wxGridCellAttr
& attr
,
2088 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2091 // ----------------------------------------------------------------------------
2092 // wxGridCellFloatRenderer
2093 // ----------------------------------------------------------------------------
2095 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
2098 SetPrecision(precision
);
2101 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
2103 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
2104 renderer
->m_width
= m_width
;
2105 renderer
->m_precision
= m_precision
;
2106 renderer
->m_format
= m_format
;
2111 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2113 wxGridTableBase
*table
= grid
.GetTable();
2118 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
2120 val
= table
->GetValueAsDouble(row
, col
);
2125 text
= table
->GetValue(row
, col
);
2126 hasDouble
= text
.ToDouble(&val
);
2133 if ( m_width
== -1 )
2135 if ( m_precision
== -1 )
2137 // default width/precision
2138 m_format
= _T("%f");
2142 m_format
.Printf(_T("%%.%df"), m_precision
);
2145 else if ( m_precision
== -1 )
2147 // default precision
2148 m_format
.Printf(_T("%%%d.f"), m_width
);
2152 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
2156 text
.Printf(m_format
, val
);
2159 //else: text already contains the string
2164 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
2165 wxGridCellAttr
& attr
,
2167 const wxRect
& rectCell
,
2171 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2173 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2175 // draw the text right aligned by default
2177 attr
.GetAlignment(&hAlign
, &vAlign
);
2178 hAlign
= wxALIGN_RIGHT
;
2180 wxRect rect
= rectCell
;
2183 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2186 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
2187 wxGridCellAttr
& attr
,
2191 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2194 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
2198 // reset to defaults
2204 wxString tmp
= params
.BeforeFirst(_T(','));
2208 if ( tmp
.ToLong(&width
) )
2210 SetWidth((int)width
);
2214 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
2218 tmp
= params
.AfterFirst(_T(','));
2222 if ( tmp
.ToLong(&precision
) )
2224 SetPrecision((int)precision
);
2228 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
2234 // ----------------------------------------------------------------------------
2235 // wxGridCellBoolRenderer
2236 // ----------------------------------------------------------------------------
2238 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
2240 // FIXME these checkbox size calculations are really ugly...
2242 // between checkmark and box
2243 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2245 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2246 wxGridCellAttr
& WXUNUSED(attr
),
2251 // compute it only once (no locks for MT safeness in GUI thread...)
2252 if ( !ms_sizeCheckMark
.x
)
2254 // get checkbox size
2255 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2256 wxSize size
= checkbox
->GetBestSize();
2257 wxCoord checkSize
= size
.y
+ 2 * wxGRID_CHECKMARK_MARGIN
;
2259 #if defined(__WXMOTIF__)
2260 checkSize
-= size
.y
/ 2;
2265 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2268 return ms_sizeCheckMark
;
2271 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2272 wxGridCellAttr
& attr
,
2278 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2280 // draw a check mark in the centre (ignoring alignment - TODO)
2281 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2283 // don't draw outside the cell
2284 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2285 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2287 // and even leave (at least) 1 pixel margin
2288 size
.x
= size
.y
= minSize
;
2291 // draw a border around checkmark
2293 attr
.GetAlignment(&hAlign
, &vAlign
);
2296 if (hAlign
== wxALIGN_CENTRE
)
2298 rectBorder
.x
= rect
.x
+ rect
.width
/ 2 - size
.x
/ 2;
2299 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2300 rectBorder
.width
= size
.x
;
2301 rectBorder
.height
= size
.y
;
2303 else if (hAlign
== wxALIGN_LEFT
)
2305 rectBorder
.x
= rect
.x
+ 2;
2306 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2307 rectBorder
.width
= size
.x
;
2308 rectBorder
.height
= size
.y
;
2310 else if (hAlign
== wxALIGN_RIGHT
)
2312 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2313 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2314 rectBorder
.width
= size
.x
;
2315 rectBorder
.height
= size
.y
;
2319 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2321 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2325 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2326 value
= wxGridCellBoolEditor::IsTrueValue(cellval
);
2331 flags
|= wxCONTROL_CHECKED
;
2333 wxRendererNative::Get().DrawCheckBox( &grid
, dc
, rectBorder
, flags
);
2336 // ----------------------------------------------------------------------------
2338 // ----------------------------------------------------------------------------
2340 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2344 m_isReadOnly
= Unset
;
2349 m_attrkind
= wxGridCellAttr::Cell
;
2351 m_sizeRows
= m_sizeCols
= 1;
2352 m_overflow
= UnsetOverflow
;
2354 SetDefAttr(attrDefault
);
2357 wxGridCellAttr
*wxGridCellAttr::Clone() const
2359 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2361 if ( HasTextColour() )
2362 attr
->SetTextColour(GetTextColour());
2363 if ( HasBackgroundColour() )
2364 attr
->SetBackgroundColour(GetBackgroundColour());
2366 attr
->SetFont(GetFont());
2367 if ( HasAlignment() )
2368 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2370 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2374 attr
->SetRenderer(m_renderer
);
2375 m_renderer
->IncRef();
2379 attr
->SetEditor(m_editor
);
2384 attr
->SetReadOnly();
2386 attr
->SetOverflow( m_overflow
== Overflow
);
2387 attr
->SetKind( m_attrkind
);
2392 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2394 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2395 SetTextColour(mergefrom
->GetTextColour());
2396 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2397 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2398 if ( !HasFont() && mergefrom
->HasFont() )
2399 SetFont(mergefrom
->GetFont());
2400 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2403 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2404 SetAlignment(hAlign
, vAlign
);
2406 if ( !HasSize() && mergefrom
->HasSize() )
2407 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2409 // Directly access member functions as GetRender/Editor don't just return
2410 // m_renderer/m_editor
2412 // Maybe add support for merge of Render and Editor?
2413 if (!HasRenderer() && mergefrom
->HasRenderer() )
2415 m_renderer
= mergefrom
->m_renderer
;
2416 m_renderer
->IncRef();
2418 if ( !HasEditor() && mergefrom
->HasEditor() )
2420 m_editor
= mergefrom
->m_editor
;
2423 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2424 SetReadOnly(mergefrom
->IsReadOnly());
2426 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2427 SetOverflow(mergefrom
->GetOverflow());
2429 SetDefAttr(mergefrom
->m_defGridAttr
);
2432 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2434 // The size of a cell is normally 1,1
2436 // If this cell is larger (2,2) then this is the top left cell
2437 // the other cells that will be covered (lower right cells) must be
2438 // set to negative or zero values such that
2439 // row + num_rows of the covered cell points to the larger cell (this cell)
2440 // same goes for the col + num_cols.
2442 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2444 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
2445 !((num_rows
<= 0) && (num_cols
> 0)) ||
2446 !((num_rows
== 0) && (num_cols
== 0))),
2447 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2449 m_sizeRows
= num_rows
;
2450 m_sizeCols
= num_cols
;
2453 const wxColour
& wxGridCellAttr::GetTextColour() const
2455 if (HasTextColour())
2459 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2461 return m_defGridAttr
->GetTextColour();
2465 wxFAIL_MSG(wxT("Missing default cell attribute"));
2466 return wxNullColour
;
2470 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2472 if (HasBackgroundColour())
2476 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2478 return m_defGridAttr
->GetBackgroundColour();
2482 wxFAIL_MSG(wxT("Missing default cell attribute"));
2483 return wxNullColour
;
2487 const wxFont
& wxGridCellAttr::GetFont() const
2493 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2495 return m_defGridAttr
->GetFont();
2499 wxFAIL_MSG(wxT("Missing default cell attribute"));
2504 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2513 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2515 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2519 wxFAIL_MSG(wxT("Missing default cell attribute"));
2523 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2526 *num_rows
= m_sizeRows
;
2528 *num_cols
= m_sizeCols
;
2531 // GetRenderer and GetEditor use a slightly different decision path about
2532 // which attribute to use. If a non-default attr object has one then it is
2533 // used, otherwise the default editor or renderer is fetched from the grid and
2534 // used. It should be the default for the data type of the cell. If it is
2535 // NULL (because the table has a type that the grid does not have in its
2536 // registry), then the grid's default editor or renderer is used.
2538 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(const wxGrid
* grid
, int row
, int col
) const
2540 wxGridCellRenderer
*renderer
= NULL
;
2542 if ( m_renderer
&& this != m_defGridAttr
)
2544 // use the cells renderer if it has one
2545 renderer
= m_renderer
;
2548 else // no non-default cell renderer
2550 // get default renderer for the data type
2553 // GetDefaultRendererForCell() will do IncRef() for us
2554 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2557 if ( renderer
== NULL
)
2559 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2561 // if we still don't have one then use the grid default
2562 // (no need for IncRef() here neither)
2563 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2565 else // default grid attr
2567 // use m_renderer which we had decided not to use initially
2568 renderer
= m_renderer
;
2575 // we're supposed to always find something
2576 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2581 // same as above, except for s/renderer/editor/g
2582 wxGridCellEditor
* wxGridCellAttr::GetEditor(const wxGrid
* grid
, int row
, int col
) const
2584 wxGridCellEditor
*editor
= NULL
;
2586 if ( m_editor
&& this != m_defGridAttr
)
2588 // use the cells editor if it has one
2592 else // no non default cell editor
2594 // get default editor for the data type
2597 // GetDefaultEditorForCell() will do IncRef() for us
2598 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2601 if ( editor
== NULL
)
2603 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2605 // if we still don't have one then use the grid default
2606 // (no need for IncRef() here neither)
2607 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2609 else // default grid attr
2611 // use m_editor which we had decided not to use initially
2619 // we're supposed to always find something
2620 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2625 // ----------------------------------------------------------------------------
2626 // wxGridCellAttrData
2627 // ----------------------------------------------------------------------------
2629 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2631 // Note: contrary to wxGridRowOrColAttrData::SetAttr, we must not
2632 // touch attribute's reference counting explicitly, since this
2633 // is managed by class wxGridCellWithAttr
2634 int n
= FindIndex(row
, col
);
2635 if ( n
== wxNOT_FOUND
)
2639 // add the attribute
2640 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2642 //else: nothing to do
2644 else // we already have an attribute for this cell
2648 // change the attribute
2649 m_attrs
[(size_t)n
].ChangeAttr(attr
);
2653 // remove this attribute
2654 m_attrs
.RemoveAt((size_t)n
);
2659 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2661 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2663 int n
= FindIndex(row
, col
);
2664 if ( n
!= wxNOT_FOUND
)
2666 attr
= m_attrs
[(size_t)n
].attr
;
2673 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2675 size_t count
= m_attrs
.GetCount();
2676 for ( size_t n
= 0; n
< count
; n
++ )
2678 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2679 wxCoord row
= coords
.GetRow();
2680 if ((size_t)row
>= pos
)
2684 // If rows inserted, include row counter where necessary
2685 coords
.SetRow(row
+ numRows
);
2687 else if (numRows
< 0)
2689 // If rows deleted ...
2690 if ((size_t)row
>= pos
- numRows
)
2692 // ...either decrement row counter (if row still exists)...
2693 coords
.SetRow(row
+ numRows
);
2697 // ...or remove the attribute
2698 m_attrs
.RemoveAt(n
);
2707 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2709 size_t count
= m_attrs
.GetCount();
2710 for ( size_t n
= 0; n
< count
; n
++ )
2712 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2713 wxCoord col
= coords
.GetCol();
2714 if ( (size_t)col
>= pos
)
2718 // If rows inserted, include row counter where necessary
2719 coords
.SetCol(col
+ numCols
);
2721 else if (numCols
< 0)
2723 // If rows deleted ...
2724 if ((size_t)col
>= pos
- numCols
)
2726 // ...either decrement row counter (if row still exists)...
2727 coords
.SetCol(col
+ numCols
);
2731 // ...or remove the attribute
2732 m_attrs
.RemoveAt(n
);
2741 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2743 size_t count
= m_attrs
.GetCount();
2744 for ( size_t n
= 0; n
< count
; n
++ )
2746 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2747 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2756 // ----------------------------------------------------------------------------
2757 // wxGridRowOrColAttrData
2758 // ----------------------------------------------------------------------------
2760 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2762 size_t count
= m_attrs
.GetCount();
2763 for ( size_t n
= 0; n
< count
; n
++ )
2765 m_attrs
[n
]->DecRef();
2769 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2771 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2773 int n
= m_rowsOrCols
.Index(rowOrCol
);
2774 if ( n
!= wxNOT_FOUND
)
2776 attr
= m_attrs
[(size_t)n
];
2783 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2785 int i
= m_rowsOrCols
.Index(rowOrCol
);
2786 if ( i
== wxNOT_FOUND
)
2790 // add the attribute - no need to do anything to reference count
2791 // since we take ownership of the attribute.
2792 m_rowsOrCols
.Add(rowOrCol
);
2795 // nothing to remove
2799 size_t n
= (size_t)i
;
2800 if ( m_attrs
[n
] == attr
)
2805 // change the attribute, handling reference count manually,
2806 // taking ownership of the new attribute.
2807 m_attrs
[n
]->DecRef();
2812 // remove this attribute, handling reference count manually
2813 m_attrs
[n
]->DecRef();
2814 m_rowsOrCols
.RemoveAt(n
);
2815 m_attrs
.RemoveAt(n
);
2820 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2822 size_t count
= m_attrs
.GetCount();
2823 for ( size_t n
= 0; n
< count
; n
++ )
2825 int & rowOrCol
= m_rowsOrCols
[n
];
2826 if ( (size_t)rowOrCol
>= pos
)
2828 if ( numRowsOrCols
> 0 )
2830 // If rows inserted, include row counter where necessary
2831 rowOrCol
+= numRowsOrCols
;
2833 else if ( numRowsOrCols
< 0)
2835 // If rows deleted, either decrement row counter (if row still exists)
2836 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2837 rowOrCol
+= numRowsOrCols
;
2840 m_rowsOrCols
.RemoveAt(n
);
2841 m_attrs
[n
]->DecRef();
2842 m_attrs
.RemoveAt(n
);
2851 // ----------------------------------------------------------------------------
2852 // wxGridCellAttrProvider
2853 // ----------------------------------------------------------------------------
2855 wxGridCellAttrProvider::wxGridCellAttrProvider()
2857 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2860 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2865 void wxGridCellAttrProvider::InitData()
2867 m_data
= new wxGridCellAttrProviderData
;
2870 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2871 wxGridCellAttr::wxAttrKind kind
) const
2873 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2878 case (wxGridCellAttr::Any
):
2879 // Get cached merge attributes.
2880 // Currently not used as no cache implemented as not mutable
2881 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2884 // Basically implement old version.
2885 // Also check merge cache, so we don't have to re-merge every time..
2886 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2887 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2888 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2890 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
2892 // Two or more are non NULL
2893 attr
= new wxGridCellAttr
;
2894 attr
->SetKind(wxGridCellAttr::Merged
);
2896 // Order is important..
2899 attr
->MergeWith(attrcell
);
2904 attr
->MergeWith(attrcol
);
2909 attr
->MergeWith(attrrow
);
2913 // store merge attr if cache implemented
2915 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2919 // one or none is non null return it or null.
2938 case (wxGridCellAttr::Cell
):
2939 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2942 case (wxGridCellAttr::Col
):
2943 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2946 case (wxGridCellAttr::Row
):
2947 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2952 // (wxGridCellAttr::Default):
2953 // (wxGridCellAttr::Merged):
2961 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2967 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2970 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2975 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2978 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2983 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2986 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2990 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2992 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2996 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
3000 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
3002 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
3006 // ----------------------------------------------------------------------------
3007 // wxGridTypeRegistry
3008 // ----------------------------------------------------------------------------
3010 wxGridTypeRegistry::~wxGridTypeRegistry()
3012 size_t count
= m_typeinfo
.GetCount();
3013 for ( size_t i
= 0; i
< count
; i
++ )
3014 delete m_typeinfo
[i
];
3017 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
3018 wxGridCellRenderer
* renderer
,
3019 wxGridCellEditor
* editor
)
3021 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
3023 // is it already registered?
3024 int loc
= FindRegisteredDataType(typeName
);
3025 if ( loc
!= wxNOT_FOUND
)
3027 delete m_typeinfo
[loc
];
3028 m_typeinfo
[loc
] = info
;
3032 m_typeinfo
.Add(info
);
3036 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
3038 size_t count
= m_typeinfo
.GetCount();
3039 for ( size_t i
= 0; i
< count
; i
++ )
3041 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
3050 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
3052 int index
= FindRegisteredDataType(typeName
);
3053 if ( index
== wxNOT_FOUND
)
3055 // check whether this is one of the standard ones, in which case
3056 // register it "on the fly"
3058 if ( typeName
== wxGRID_VALUE_STRING
)
3060 RegisterDataType(wxGRID_VALUE_STRING
,
3061 new wxGridCellStringRenderer
,
3062 new wxGridCellTextEditor
);
3065 #endif // wxUSE_TEXTCTRL
3067 if ( typeName
== wxGRID_VALUE_BOOL
)
3069 RegisterDataType(wxGRID_VALUE_BOOL
,
3070 new wxGridCellBoolRenderer
,
3071 new wxGridCellBoolEditor
);
3074 #endif // wxUSE_CHECKBOX
3076 if ( typeName
== wxGRID_VALUE_NUMBER
)
3078 RegisterDataType(wxGRID_VALUE_NUMBER
,
3079 new wxGridCellNumberRenderer
,
3080 new wxGridCellNumberEditor
);
3082 else if ( typeName
== wxGRID_VALUE_FLOAT
)
3084 RegisterDataType(wxGRID_VALUE_FLOAT
,
3085 new wxGridCellFloatRenderer
,
3086 new wxGridCellFloatEditor
);
3089 #endif // wxUSE_TEXTCTRL
3091 if ( typeName
== wxGRID_VALUE_CHOICE
)
3093 RegisterDataType(wxGRID_VALUE_CHOICE
,
3094 new wxGridCellStringRenderer
,
3095 new wxGridCellChoiceEditor
);
3098 #endif // wxUSE_COMBOBOX
3103 // we get here only if just added the entry for this type, so return
3105 index
= m_typeinfo
.GetCount() - 1;
3111 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
3113 int index
= FindDataType(typeName
);
3114 if ( index
== wxNOT_FOUND
)
3116 // the first part of the typename is the "real" type, anything after ':'
3117 // are the parameters for the renderer
3118 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
3119 if ( index
== wxNOT_FOUND
)
3124 wxGridCellRenderer
*renderer
= GetRenderer(index
);
3125 wxGridCellRenderer
*rendererOld
= renderer
;
3126 renderer
= renderer
->Clone();
3127 rendererOld
->DecRef();
3129 wxGridCellEditor
*editor
= GetEditor(index
);
3130 wxGridCellEditor
*editorOld
= editor
;
3131 editor
= editor
->Clone();
3132 editorOld
->DecRef();
3134 // do it even if there are no parameters to reset them to defaults
3135 wxString params
= typeName
.AfterFirst(_T(':'));
3136 renderer
->SetParameters(params
);
3137 editor
->SetParameters(params
);
3139 // register the new typename
3140 RegisterDataType(typeName
, renderer
, editor
);
3142 // we just registered it, it's the last one
3143 index
= m_typeinfo
.GetCount() - 1;
3149 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
3151 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
3158 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
3160 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
3167 // ----------------------------------------------------------------------------
3169 // ----------------------------------------------------------------------------
3171 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
3173 wxGridTableBase::wxGridTableBase()
3175 m_view
= (wxGrid
*) NULL
;
3176 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
3179 wxGridTableBase::~wxGridTableBase()
3181 delete m_attrProvider
;
3184 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
3186 delete m_attrProvider
;
3187 m_attrProvider
= attrProvider
;
3190 bool wxGridTableBase::CanHaveAttributes()
3192 if ( ! GetAttrProvider() )
3194 // use the default attr provider by default
3195 SetAttrProvider(new wxGridCellAttrProvider
);
3201 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
3203 if ( m_attrProvider
)
3204 return m_attrProvider
->GetAttr(row
, col
, kind
);
3206 return (wxGridCellAttr
*)NULL
;
3209 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
3211 if ( m_attrProvider
)
3214 attr
->SetKind(wxGridCellAttr::Cell
);
3215 m_attrProvider
->SetAttr(attr
, row
, col
);
3219 // as we take ownership of the pointer and don't store it, we must
3225 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3227 if ( m_attrProvider
)
3229 attr
->SetKind(wxGridCellAttr::Row
);
3230 m_attrProvider
->SetRowAttr(attr
, row
);
3234 // as we take ownership of the pointer and don't store it, we must
3240 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3242 if ( m_attrProvider
)
3244 attr
->SetKind(wxGridCellAttr::Col
);
3245 m_attrProvider
->SetColAttr(attr
, col
);
3249 // as we take ownership of the pointer and don't store it, we must
3255 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3256 size_t WXUNUSED(numRows
) )
3258 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3263 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3265 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3270 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3271 size_t WXUNUSED(numRows
) )
3273 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3278 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3279 size_t WXUNUSED(numCols
) )
3281 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3286 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3288 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3293 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3294 size_t WXUNUSED(numCols
) )
3296 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3301 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3305 // RD: Starting the rows at zero confuses users,
3306 // no matter how much it makes sense to us geeks.
3312 wxString
wxGridTableBase::GetColLabelValue( int col
)
3314 // default col labels are:
3315 // cols 0 to 25 : A-Z
3316 // cols 26 to 675 : AA-ZZ
3321 for ( n
= 1; ; n
++ )
3323 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
3329 // reverse the string...
3331 for ( i
= 0; i
< n
; i
++ )
3339 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3341 return wxGRID_VALUE_STRING
;
3344 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3345 const wxString
& typeName
)
3347 return typeName
== wxGRID_VALUE_STRING
;
3350 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3352 return CanGetValueAs(row
, col
, typeName
);
3355 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3360 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3365 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3370 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3371 long WXUNUSED(value
) )
3375 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3376 double WXUNUSED(value
) )
3380 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3381 bool WXUNUSED(value
) )
3385 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3386 const wxString
& WXUNUSED(typeName
) )
3391 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3392 const wxString
& WXUNUSED(typeName
),
3393 void* WXUNUSED(value
) )
3397 //////////////////////////////////////////////////////////////////////
3399 // Message class for the grid table to send requests and notifications
3403 wxGridTableMessage::wxGridTableMessage()
3405 m_table
= (wxGridTableBase
*) NULL
;
3411 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3412 int commandInt1
, int commandInt2
)
3416 m_comInt1
= commandInt1
;
3417 m_comInt2
= commandInt2
;
3420 //////////////////////////////////////////////////////////////////////
3422 // A basic grid table for string data. An object of this class will
3423 // created by wxGrid if you don't specify an alternative table class.
3426 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3428 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3430 wxGridStringTable::wxGridStringTable()
3435 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3438 m_data
.Alloc( numRows
);
3441 sa
.Alloc( numCols
);
3442 sa
.Add( wxEmptyString
, numCols
);
3444 m_data
.Add( sa
, numRows
);
3447 wxGridStringTable::~wxGridStringTable()
3451 int wxGridStringTable::GetNumberRows()
3453 return m_data
.GetCount();
3456 int wxGridStringTable::GetNumberCols()
3458 if ( m_data
.GetCount() > 0 )
3459 return m_data
[0].GetCount();
3464 wxString
wxGridStringTable::GetValue( 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
];
3473 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3475 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3476 _T("invalid row or column index in wxGridStringTable") );
3478 m_data
[row
][col
] = value
;
3481 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3483 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3485 _T("invalid row or column index in wxGridStringTable") );
3487 return (m_data
[row
][col
] == wxEmptyString
);
3490 void wxGridStringTable::Clear()
3493 int numRows
, numCols
;
3495 numRows
= m_data
.GetCount();
3498 numCols
= m_data
[0].GetCount();
3500 for ( row
= 0; row
< numRows
; row
++ )
3502 for ( col
= 0; col
< numCols
; col
++ )
3504 m_data
[row
][col
] = wxEmptyString
;
3510 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3512 size_t curNumRows
= m_data
.GetCount();
3513 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3514 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3516 if ( pos
>= curNumRows
)
3518 return AppendRows( numRows
);
3522 sa
.Alloc( curNumCols
);
3523 sa
.Add( wxEmptyString
, curNumCols
);
3524 m_data
.Insert( sa
, pos
, numRows
);
3528 wxGridTableMessage
msg( this,
3529 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3533 GetView()->ProcessTableMessage( msg
);
3539 bool wxGridStringTable::AppendRows( size_t numRows
)
3541 size_t curNumRows
= m_data
.GetCount();
3542 size_t curNumCols
= ( curNumRows
> 0
3543 ? m_data
[0].GetCount()
3544 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3547 if ( curNumCols
> 0 )
3549 sa
.Alloc( curNumCols
);
3550 sa
.Add( wxEmptyString
, curNumCols
);
3553 m_data
.Add( sa
, numRows
);
3557 wxGridTableMessage
msg( this,
3558 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3561 GetView()->ProcessTableMessage( msg
);
3567 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3569 size_t curNumRows
= m_data
.GetCount();
3571 if ( pos
>= curNumRows
)
3573 wxFAIL_MSG( wxString::Format
3575 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3577 (unsigned long)numRows
,
3578 (unsigned long)curNumRows
3584 if ( numRows
> curNumRows
- pos
)
3586 numRows
= curNumRows
- pos
;
3589 if ( numRows
>= curNumRows
)
3595 m_data
.RemoveAt( pos
, numRows
);
3600 wxGridTableMessage
msg( this,
3601 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3605 GetView()->ProcessTableMessage( msg
);
3611 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3615 size_t curNumRows
= m_data
.GetCount();
3616 size_t curNumCols
= ( curNumRows
> 0
3617 ? m_data
[0].GetCount()
3618 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3620 if ( pos
>= curNumCols
)
3622 return AppendCols( numCols
);
3625 if ( !m_colLabels
.IsEmpty() )
3627 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
3630 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
3631 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
3634 for ( row
= 0; row
< curNumRows
; row
++ )
3636 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3638 m_data
[row
].Insert( wxEmptyString
, col
);
3644 wxGridTableMessage
msg( this,
3645 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3649 GetView()->ProcessTableMessage( msg
);
3655 bool wxGridStringTable::AppendCols( size_t numCols
)
3659 size_t curNumRows
= m_data
.GetCount();
3664 // TODO: something better than this ?
3666 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3671 for ( row
= 0; row
< curNumRows
; row
++ )
3673 m_data
[row
].Add( wxEmptyString
, numCols
);
3678 wxGridTableMessage
msg( this,
3679 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3682 GetView()->ProcessTableMessage( msg
);
3688 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3692 size_t curNumRows
= m_data
.GetCount();
3693 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3694 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3696 if ( pos
>= curNumCols
)
3698 wxFAIL_MSG( wxString::Format
3700 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3702 (unsigned long)numCols
,
3703 (unsigned long)curNumCols
3710 colID
= GetView()->GetColAt( pos
);
3714 if ( numCols
> curNumCols
- colID
)
3716 numCols
= curNumCols
- colID
;
3719 if ( !m_colLabels
.IsEmpty() )
3721 // m_colLabels stores just as many elements as it needs, e.g. if only
3722 // the label of the first column had been set it would have only one
3723 // element and not numCols, so account for it
3724 int nToRm
= m_colLabels
.size() - colID
;
3726 m_colLabels
.RemoveAt( colID
, nToRm
);
3729 for ( row
= 0; row
< curNumRows
; row
++ )
3731 if ( numCols
>= curNumCols
)
3733 m_data
[row
].Clear();
3737 m_data
[row
].RemoveAt( colID
, numCols
);
3743 wxGridTableMessage
msg( this,
3744 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3748 GetView()->ProcessTableMessage( msg
);
3754 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3756 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3758 // using default label
3760 return wxGridTableBase::GetRowLabelValue( row
);
3764 return m_rowLabels
[row
];
3768 wxString
wxGridStringTable::GetColLabelValue( int col
)
3770 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3772 // using default label
3774 return wxGridTableBase::GetColLabelValue( col
);
3778 return m_colLabels
[col
];
3782 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3784 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3786 int n
= m_rowLabels
.GetCount();
3789 for ( i
= n
; i
<= row
; i
++ )
3791 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3795 m_rowLabels
[row
] = value
;
3798 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3800 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3802 int n
= m_colLabels
.GetCount();
3805 for ( i
= n
; i
<= col
; i
++ )
3807 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3811 m_colLabels
[col
] = value
;
3815 //////////////////////////////////////////////////////////////////////
3816 //////////////////////////////////////////////////////////////////////
3818 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
)
3819 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost
)
3822 void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
3824 m_owner
->CancelMouseCapture();
3827 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3829 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow
)
3830 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3831 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3832 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3835 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3837 const wxPoint
&pos
, const wxSize
&size
)
3838 : wxGridSubwindow(parent
, id
, pos
, size
)
3843 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3847 // NO - don't do this because it will set both the x and y origin
3848 // coords to match the parent scrolled window and we just want to
3849 // set the y coord - MB
3851 // m_owner->PrepareDC( dc );
3854 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3855 wxPoint pt
= dc
.GetDeviceOrigin();
3856 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
3858 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3859 m_owner
->DrawRowLabels( dc
, rows
);
3862 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3864 m_owner
->ProcessRowLabelMouseEvent( event
);
3867 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3869 m_owner
->GetEventHandler()->ProcessEvent( event
);
3872 //////////////////////////////////////////////////////////////////////
3874 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3876 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow
)
3877 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3878 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3879 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3882 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3884 const wxPoint
&pos
, const wxSize
&size
)
3885 : wxGridSubwindow(parent
, id
, pos
, size
)
3890 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3894 // NO - don't do this because it will set both the x and y origin
3895 // coords to match the parent scrolled window and we just want to
3896 // set the x coord - MB
3898 // m_owner->PrepareDC( dc );
3901 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3902 wxPoint pt
= dc
.GetDeviceOrigin();
3903 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3904 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
3906 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
3908 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3909 m_owner
->DrawColLabels( dc
, cols
);
3912 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3914 m_owner
->ProcessColLabelMouseEvent( event
);
3917 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3919 m_owner
->GetEventHandler()->ProcessEvent( event
);
3922 //////////////////////////////////////////////////////////////////////
3924 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3926 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow
)
3927 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3928 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3929 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3932 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3934 const wxPoint
&pos
, const wxSize
&size
)
3935 : wxGridSubwindow(parent
, id
, pos
, size
)
3940 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3944 int client_height
= 0;
3945 int client_width
= 0;
3946 GetClientSize( &client_width
, &client_height
);
3948 // VZ: any reason for this ifdef? (FIXME)
3954 rect
.SetWidth( client_width
- 2 );
3955 rect
.SetHeight( client_height
- 2 );
3957 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3959 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxPENSTYLE_SOLID
) );
3960 dc
.DrawLine( client_width
- 1, client_height
- 1, client_width
- 1, 0 );
3961 dc
.DrawLine( client_width
- 1, client_height
- 1, 0, client_height
- 1 );
3962 dc
.DrawLine( 0, 0, client_width
, 0 );
3963 dc
.DrawLine( 0, 0, 0, client_height
);
3965 dc
.SetPen( *wxWHITE_PEN
);
3966 dc
.DrawLine( 1, 1, client_width
- 1, 1 );
3967 dc
.DrawLine( 1, 1, 1, client_height
- 1 );
3971 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3973 m_owner
->ProcessCornerLabelMouseEvent( event
);
3976 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3978 m_owner
->GetEventHandler()->ProcessEvent(event
);
3981 //////////////////////////////////////////////////////////////////////
3983 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3985 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow
)
3986 EVT_PAINT( wxGridWindow::OnPaint
)
3987 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3988 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3989 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3990 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3991 EVT_CHAR( wxGridWindow::OnChar
)
3992 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3993 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3994 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3997 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3998 wxGridRowLabelWindow
*rowLblWin
,
3999 wxGridColLabelWindow
*colLblWin
,
4002 const wxSize
&size
)
4003 : wxGridSubwindow(parent
, id
, pos
, size
,
4004 wxWANTS_CHARS
| wxCLIP_CHILDREN
,
4005 wxT("grid window") )
4008 m_rowLabelWin
= rowLblWin
;
4009 m_colLabelWin
= colLblWin
;
4012 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
4014 wxPaintDC
dc( this );
4015 m_owner
->PrepareDC( dc
);
4016 wxRegion reg
= GetUpdateRegion();
4017 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
4018 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
4020 #if WXGRID_DRAW_LINES
4021 m_owner
->DrawAllGridLines( dc
, reg
);
4024 m_owner
->DrawGridSpace( dc
);
4025 m_owner
->DrawHighlight( dc
, dirtyCells
);
4028 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
4030 wxWindow::ScrollWindow( dx
, dy
, rect
);
4031 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
4032 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
4035 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
4037 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
4040 m_owner
->ProcessGridCellMouseEvent( event
);
4043 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
4045 m_owner
->GetEventHandler()->ProcessEvent( event
);
4048 // This seems to be required for wxMotif/wxGTK otherwise the mouse
4049 // cursor must be in the cell edit control to get key events
4051 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
4053 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4057 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
4059 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4063 void wxGridWindow::OnChar( wxKeyEvent
& event
)
4065 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4069 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
4073 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
4075 // and if we have any selection, it has to be repainted, because it
4076 // uses different colour when the grid is not focused:
4077 if ( m_owner
->IsSelection() )
4083 // NB: Note that this code is in "else" branch only because the other
4084 // branch refreshes everything and so there's no point in calling
4085 // Refresh() again, *not* because it should only be done if
4086 // !IsSelection(). If the above code is ever optimized to refresh
4087 // only selected area, this needs to be moved out of the "else"
4088 // branch so that it's always executed.
4090 // current cell cursor {dis,re}appears on focus change:
4091 const wxGridCellCoords
cursorCoords(m_owner
->GetGridCursorRow(),
4092 m_owner
->GetGridCursorCol());
4093 const wxRect cursor
=
4094 m_owner
->BlockToDeviceRect(cursorCoords
, cursorCoords
);
4095 Refresh(true, &cursor
);
4098 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4102 //////////////////////////////////////////////////////////////////////
4104 // Internal Helper function for computing row or column from some
4105 // (unscrolled) coordinate value, using either
4106 // m_defaultRowHeight/m_defaultColWidth or binary search on array
4107 // of m_rowBottoms/m_ColRights to speed up the search!
4109 // Internal helper macros for simpler use of that function
4111 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
4112 const wxArrayInt
& BorderArray
, int nMax
,
4115 #define internalXToCol(x) XToCol(x, true)
4116 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
4117 m_minAcceptableRowHeight, \
4118 m_rowBottoms, m_numRows, true)
4120 /////////////////////////////////////////////////////////////////////
4122 #if wxUSE_EXTENDED_RTTI
4123 WX_DEFINE_FLAGS( wxGridStyle
)
4125 wxBEGIN_FLAGS( wxGridStyle
)
4126 // new style border flags, we put them first to
4127 // use them for streaming out
4128 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4129 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4130 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4131 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4132 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4133 wxFLAGS_MEMBER(wxBORDER_NONE
)
4135 // old style border flags
4136 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4137 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4138 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4139 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4140 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4141 wxFLAGS_MEMBER(wxBORDER
)
4143 // standard window styles
4144 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4145 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4146 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4147 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4148 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4149 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4150 wxFLAGS_MEMBER(wxVSCROLL
)
4151 wxFLAGS_MEMBER(wxHSCROLL
)
4153 wxEND_FLAGS( wxGridStyle
)
4155 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4157 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4158 wxHIDE_PROPERTY( Children
)
4159 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4160 wxEND_PROPERTIES_TABLE()
4162 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4163 wxEND_HANDLERS_TABLE()
4165 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4168 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
4171 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4174 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4175 EVT_PAINT( wxGrid::OnPaint
)
4176 EVT_SIZE( wxGrid::OnSize
)
4177 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
4178 EVT_KEY_UP( wxGrid::OnKeyUp
)
4179 EVT_CHAR ( wxGrid::OnChar
)
4180 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
4188 wxGrid::wxGrid( wxWindow
*parent
,
4193 const wxString
& name
)
4196 Create(parent
, id
, pos
, size
, style
, name
);
4199 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
4200 const wxPoint
& pos
, const wxSize
& size
,
4201 long style
, const wxString
& name
)
4203 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
4204 style
| wxWANTS_CHARS
, name
))
4207 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4208 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4211 SetInitialSize(size
);
4219 // Must do this or ~wxScrollHelper will pop the wrong event handler
4220 SetTargetWindow(this);
4222 wxSafeDecRef(m_defaultCellAttr
);
4224 #ifdef DEBUG_ATTR_CACHE
4225 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4226 wxPrintf(_T("wxGrid attribute cache statistics: "
4227 "total: %u, hits: %u (%u%%)\n"),
4228 total
, gs_nAttrCacheHits
,
4229 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4232 // if we own the table, just delete it, otherwise at least don't leave it
4233 // with dangling view pointer
4236 else if ( m_table
&& m_table
->GetView() == this )
4237 m_table
->SetView(NULL
);
4239 delete m_typeRegistry
;
4244 // ----- internal init and update functions
4247 // NOTE: If using the default visual attributes works everywhere then this can
4248 // be removed as well as the #else cases below.
4249 #define _USE_VISATTR 0
4251 void wxGrid::Create()
4253 // create the type registry
4254 m_typeRegistry
= new wxGridTypeRegistry
;
4256 m_cellEditCtrlEnabled
= false;
4258 m_defaultCellAttr
= new wxGridCellAttr();
4260 // Set default cell attributes
4261 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4262 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4263 m_defaultCellAttr
->SetFont(GetFont());
4264 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4265 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4266 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4269 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4270 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4272 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4273 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4276 m_defaultCellAttr
->SetTextColour(
4277 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4278 m_defaultCellAttr
->SetBackgroundColour(
4279 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4284 m_currentCellCoords
= wxGridNoCellCoords
;
4286 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4287 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4289 // subwindow components that make up the wxGrid
4290 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4295 m_colLabelWin
= new wxGridColLabelWindow( this,
4300 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4305 m_gridWin
= new wxGridWindow( this,
4312 SetTargetWindow( m_gridWin
);
4315 wxColour gfg
= gva
.colFg
;
4316 wxColour gbg
= gva
.colBg
;
4317 wxColour lfg
= lva
.colFg
;
4318 wxColour lbg
= lva
.colBg
;
4320 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4321 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4322 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4323 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4326 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4327 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4328 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4329 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4330 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4331 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4333 m_gridWin
->SetOwnForegroundColour(gfg
);
4334 m_gridWin
->SetOwnBackgroundColour(gbg
);
4339 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4340 wxGrid::wxGridSelectionModes selmode
)
4342 wxCHECK_MSG( !m_created
,
4344 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4346 m_numRows
= numRows
;
4347 m_numCols
= numCols
;
4349 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4350 m_table
->SetView( this );
4352 m_selection
= new wxGridSelection( this, selmode
);
4361 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4363 wxCHECK_RET( m_created
,
4364 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4366 m_selection
->SetSelectionMode( selmode
);
4369 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4371 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4372 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4374 return m_selection
->GetSelectionMode();
4377 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4378 wxGrid::wxGridSelectionModes selmode
)
4380 bool checkSelection
= false;
4383 // stop all processing
4388 m_table
->SetView(0);
4400 checkSelection
= true;
4402 // kill row and column size arrays
4403 m_colWidths
.Empty();
4404 m_colRights
.Empty();
4405 m_rowHeights
.Empty();
4406 m_rowBottoms
.Empty();
4411 m_numRows
= table
->GetNumberRows();
4412 m_numCols
= table
->GetNumberCols();
4415 m_table
->SetView( this );
4416 m_ownTable
= takeOwnership
;
4417 m_selection
= new wxGridSelection( this, selmode
);
4420 // If the newly set table is smaller than the
4421 // original one current cell and selection regions
4422 // might be invalid,
4423 m_selectingKeyboard
= wxGridNoCellCoords
;
4424 m_currentCellCoords
=
4425 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
4426 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
4427 if (m_selectingTopLeft
.GetRow() >= m_numRows
||
4428 m_selectingTopLeft
.GetCol() >= m_numCols
)
4430 m_selectingTopLeft
= wxGridNoCellCoords
;
4431 m_selectingBottomRight
= wxGridNoCellCoords
;
4434 m_selectingBottomRight
=
4435 wxGridCellCoords(wxMin(m_numRows
,
4436 m_selectingBottomRight
.GetRow()),
4438 m_selectingBottomRight
.GetCol()));
4448 void wxGrid::InitVars()
4452 m_cornerLabelWin
= NULL
;
4453 m_rowLabelWin
= NULL
;
4454 m_colLabelWin
= NULL
;
4461 m_defaultCellAttr
= NULL
;
4462 m_typeRegistry
= NULL
;
4463 m_winCapture
= NULL
;
4468 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4469 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4471 if ( m_rowLabelWin
)
4473 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4477 m_labelBackgroundColour
= *wxWHITE
;
4480 m_labelTextColour
= *wxBLACK
;
4483 m_attrCache
.row
= -1;
4484 m_attrCache
.col
= -1;
4485 m_attrCache
.attr
= NULL
;
4487 // TODO: something better than this ?
4489 m_labelFont
= this->GetFont();
4490 m_labelFont
.SetWeight( wxBOLD
);
4492 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4493 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4495 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4496 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4497 m_colLabelTextOrientation
= wxHORIZONTAL
;
4499 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4500 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4502 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4503 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4505 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4506 m_defaultRowHeight
+= 8;
4508 m_defaultRowHeight
+= 4;
4511 m_gridLineColour
= wxColour( 192,192,192 );
4512 m_gridLinesEnabled
= true;
4513 m_cellHighlightColour
= *wxBLACK
;
4514 m_cellHighlightPenWidth
= 2;
4515 m_cellHighlightROPenWidth
= 1;
4517 m_canDragColMove
= false;
4519 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4520 m_winCapture
= (wxWindow
*)NULL
;
4521 m_canDragRowSize
= true;
4522 m_canDragColSize
= true;
4523 m_canDragGridSize
= true;
4524 m_canDragCell
= false;
4526 m_dragRowOrCol
= -1;
4527 m_isDragging
= false;
4528 m_startDragPos
= wxDefaultPosition
;
4529 m_nativeColumnLabels
= false;
4531 m_waitForSlowClick
= false;
4533 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4534 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4536 m_currentCellCoords
= wxGridNoCellCoords
;
4540 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4541 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4543 m_editable
= true; // default for whole grid
4545 m_inOnKeyDown
= false;
4551 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4552 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4555 // ----------------------------------------------------------------------------
4556 // the idea is to call these functions only when necessary because they create
4557 // quite big arrays which eat memory mostly unnecessary - in particular, if
4558 // default widths/heights are used for all rows/columns, we may not use these
4561 // with some extra code, it should be possible to only store the widths/heights
4562 // different from default ones (resulting in space savings for huge grids) but
4563 // this is not done currently
4564 // ----------------------------------------------------------------------------
4566 void wxGrid::InitRowHeights()
4568 m_rowHeights
.Empty();
4569 m_rowBottoms
.Empty();
4571 m_rowHeights
.Alloc( m_numRows
);
4572 m_rowBottoms
.Alloc( m_numRows
);
4574 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4577 for ( int i
= 0; i
< m_numRows
; i
++ )
4579 rowBottom
+= m_defaultRowHeight
;
4580 m_rowBottoms
.Add( rowBottom
);
4584 void wxGrid::InitColWidths()
4586 m_colWidths
.Empty();
4587 m_colRights
.Empty();
4589 m_colWidths
.Alloc( m_numCols
);
4590 m_colRights
.Alloc( m_numCols
);
4592 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4595 for ( int i
= 0; i
< m_numCols
; i
++ )
4597 colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
4598 m_colRights
.Add( colRight
);
4602 int wxGrid::GetColWidth(int col
) const
4604 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4607 int wxGrid::GetColLeft(int col
) const
4609 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
4610 : m_colRights
[col
] - m_colWidths
[col
];
4613 int wxGrid::GetColRight(int col
) const
4615 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
4619 int wxGrid::GetRowHeight(int row
) const
4621 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4624 int wxGrid::GetRowTop(int row
) const
4626 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4627 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4630 int wxGrid::GetRowBottom(int row
) const
4632 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4633 : m_rowBottoms
[row
];
4636 void wxGrid::CalcDimensions()
4638 // compute the size of the scrollable area
4639 int w
= m_numCols
> 0 ? GetColRight(GetColAt(m_numCols
- 1)) : 0;
4640 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
4645 // take into account editor if shown
4646 if ( IsCellEditControlShown() )
4649 int r
= m_currentCellCoords
.GetRow();
4650 int c
= m_currentCellCoords
.GetCol();
4651 int x
= GetColLeft(c
);
4652 int y
= GetRowTop(r
);
4654 // how big is the editor
4655 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4656 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4657 editor
->GetControl()->GetSize(&w2
, &h2
);
4668 // preserve (more or less) the previous position
4670 GetViewStart( &x
, &y
);
4672 // ensure the position is valid for the new scroll ranges
4674 x
= wxMax( w
- 1, 0 );
4676 y
= wxMax( h
- 1, 0 );
4678 // do set scrollbar parameters
4679 SetScrollbars( m_scrollLineX
, m_scrollLineY
,
4680 GetScrollX(w
), GetScrollY(h
),
4682 GetBatchCount() != 0);
4684 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4685 // still must reposition the children
4689 void wxGrid::CalcWindowSizes()
4691 // escape if the window is has not been fully created yet
4693 if ( m_cornerLabelWin
== NULL
)
4697 GetClientSize( &cw
, &ch
);
4699 // this block of code tries to work around the following problem: the grid
4700 // could have been just resized to have enough space to show the full grid
4701 // window contents without the scrollbars, but its client size could be
4702 // not big enough because the grid has the scrollbars right now and so the
4703 // scrollbars would remain even though we don't need them any more
4705 // to prevent this from happening, check if we have enough space for
4706 // everything without the scrollbars and explicitly disable them then
4707 wxSize size
= GetSize() - GetWindowBorderSize();
4708 if ( size
!= wxSize(cw
, ch
) )
4710 // check if we have enough space for grid window after accounting for
4711 // the fixed size elements
4712 size
.x
-= m_rowLabelWidth
;
4713 size
.y
-= m_colLabelHeight
;
4715 const wxSize vsize
= m_gridWin
->GetVirtualSize();
4717 if ( size
.x
>= vsize
.x
&& size
.y
>= vsize
.y
)
4719 // yes, we do, so remove the scrollbars and use the new client size
4720 // (which should be the same as full window size - borders now)
4721 SetScrollbars(0, 0, 0, 0);
4722 GetClientSize(&cw
, &ch
);
4726 // the grid may be too small to have enough space for the labels yet, don't
4727 // size the windows to negative sizes in this case
4728 int gw
= cw
- m_rowLabelWidth
;
4729 int gh
= ch
- m_colLabelHeight
;
4735 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4736 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4738 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4739 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, gw
, m_colLabelHeight
);
4741 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4742 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, gh
);
4744 if ( m_gridWin
&& m_gridWin
->IsShown() )
4745 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, gw
, gh
);
4748 // this is called when the grid table sends a message
4749 // to indicate that it has been redimensioned
4751 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4754 bool result
= false;
4756 // Clear the attribute cache as the attribute might refer to a different
4757 // cell than stored in the cache after adding/removing rows/columns.
4760 // By the same reasoning, the editor should be dismissed if columns are
4761 // added or removed. And for consistency, it should IMHO always be
4762 // removed, not only if the cell "underneath" it actually changes.
4763 // For now, I intentionally do not save the editor's content as the
4764 // cell it might want to save that stuff to might no longer exist.
4765 HideCellEditControl();
4768 // if we were using the default widths/heights so far, we must change them
4770 if ( m_colWidths
.IsEmpty() )
4775 if ( m_rowHeights
.IsEmpty() )
4781 switch ( msg
.GetId() )
4783 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4785 size_t pos
= msg
.GetCommandInt();
4786 int numRows
= msg
.GetCommandInt2();
4788 m_numRows
+= numRows
;
4790 if ( !m_rowHeights
.IsEmpty() )
4792 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4793 m_rowBottoms
.Insert( 0, pos
, numRows
);
4797 bottom
= m_rowBottoms
[pos
- 1];
4799 for ( i
= pos
; i
< m_numRows
; i
++ )
4801 bottom
+= m_rowHeights
[i
];
4802 m_rowBottoms
[i
] = bottom
;
4806 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4808 // if we have just inserted cols into an empty grid the current
4809 // cell will be undefined...
4811 SetCurrentCell( 0, 0 );
4815 m_selection
->UpdateRows( pos
, numRows
);
4816 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4818 attrProvider
->UpdateAttrRows( pos
, numRows
);
4820 if ( !GetBatchCount() )
4823 m_rowLabelWin
->Refresh();
4829 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4831 int numRows
= msg
.GetCommandInt();
4832 int oldNumRows
= m_numRows
;
4833 m_numRows
+= numRows
;
4835 if ( !m_rowHeights
.IsEmpty() )
4837 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4838 m_rowBottoms
.Add( 0, numRows
);
4841 if ( oldNumRows
> 0 )
4842 bottom
= m_rowBottoms
[oldNumRows
- 1];
4844 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4846 bottom
+= m_rowHeights
[i
];
4847 m_rowBottoms
[i
] = bottom
;
4851 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4853 // if we have just inserted cols into an empty grid the current
4854 // cell will be undefined...
4856 SetCurrentCell( 0, 0 );
4859 if ( !GetBatchCount() )
4862 m_rowLabelWin
->Refresh();
4868 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4870 size_t pos
= msg
.GetCommandInt();
4871 int numRows
= msg
.GetCommandInt2();
4872 m_numRows
-= numRows
;
4874 if ( !m_rowHeights
.IsEmpty() )
4876 m_rowHeights
.RemoveAt( pos
, numRows
);
4877 m_rowBottoms
.RemoveAt( pos
, numRows
);
4880 for ( i
= 0; i
< m_numRows
; i
++ )
4882 h
+= m_rowHeights
[i
];
4883 m_rowBottoms
[i
] = h
;
4889 m_currentCellCoords
= wxGridNoCellCoords
;
4893 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4894 m_currentCellCoords
.Set( 0, 0 );
4898 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4899 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4902 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4904 // ifdef'd out following patch from Paul Gammans
4906 // No need to touch column attributes, unless we
4907 // removed _all_ rows, in this case, we remove
4908 // all column attributes.
4909 // I hate to do this here, but the
4910 // needed data is not available inside UpdateAttrRows.
4911 if ( !GetNumberRows() )
4912 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4916 if ( !GetBatchCount() )
4919 m_rowLabelWin
->Refresh();
4925 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4927 size_t pos
= msg
.GetCommandInt();
4928 int numCols
= msg
.GetCommandInt2();
4929 m_numCols
+= numCols
;
4931 if ( !m_colAt
.IsEmpty() )
4933 //Shift the column IDs
4935 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
4937 if ( m_colAt
[i
] >= (int)pos
)
4938 m_colAt
[i
] += numCols
;
4941 m_colAt
.Insert( pos
, pos
, numCols
);
4943 //Set the new columns' positions
4944 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
4950 if ( !m_colWidths
.IsEmpty() )
4952 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4953 m_colRights
.Insert( 0, pos
, numCols
);
4957 right
= m_colRights
[GetColAt( pos
- 1 )];
4960 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
4962 i
= GetColAt( colPos
);
4964 right
+= m_colWidths
[i
];
4965 m_colRights
[i
] = right
;
4969 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4971 // if we have just inserted cols into an empty grid the current
4972 // cell will be undefined...
4974 SetCurrentCell( 0, 0 );
4978 m_selection
->UpdateCols( pos
, numCols
);
4979 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4981 attrProvider
->UpdateAttrCols( pos
, numCols
);
4982 if ( !GetBatchCount() )
4985 m_colLabelWin
->Refresh();
4991 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4993 int numCols
= msg
.GetCommandInt();
4994 int oldNumCols
= m_numCols
;
4995 m_numCols
+= numCols
;
4997 if ( !m_colAt
.IsEmpty() )
4999 m_colAt
.Add( 0, numCols
);
5001 //Set the new columns' positions
5003 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
5009 if ( !m_colWidths
.IsEmpty() )
5011 m_colWidths
.Add( m_defaultColWidth
, numCols
);
5012 m_colRights
.Add( 0, numCols
);
5015 if ( oldNumCols
> 0 )
5016 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
5019 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
5021 i
= GetColAt( colPos
);
5023 right
+= m_colWidths
[i
];
5024 m_colRights
[i
] = right
;
5028 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5030 // if we have just inserted cols into an empty grid the current
5031 // cell will be undefined...
5033 SetCurrentCell( 0, 0 );
5035 if ( !GetBatchCount() )
5038 m_colLabelWin
->Refresh();
5044 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5046 size_t pos
= msg
.GetCommandInt();
5047 int numCols
= msg
.GetCommandInt2();
5048 m_numCols
-= numCols
;
5050 if ( !m_colAt
.IsEmpty() )
5052 int colID
= GetColAt( pos
);
5054 m_colAt
.RemoveAt( pos
, numCols
);
5056 //Shift the column IDs
5058 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5060 if ( m_colAt
[colPos
] > colID
)
5061 m_colAt
[colPos
] -= numCols
;
5065 if ( !m_colWidths
.IsEmpty() )
5067 m_colWidths
.RemoveAt( pos
, numCols
);
5068 m_colRights
.RemoveAt( pos
, numCols
);
5072 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5074 i
= GetColAt( colPos
);
5076 w
+= m_colWidths
[i
];
5083 m_currentCellCoords
= wxGridNoCellCoords
;
5087 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
5088 m_currentCellCoords
.Set( 0, 0 );
5092 m_selection
->UpdateCols( pos
, -((int)numCols
) );
5093 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5096 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
5098 // ifdef'd out following patch from Paul Gammans
5100 // No need to touch row attributes, unless we
5101 // removed _all_ columns, in this case, we remove
5102 // all row attributes.
5103 // I hate to do this here, but the
5104 // needed data is not available inside UpdateAttrCols.
5105 if ( !GetNumberCols() )
5106 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
5110 if ( !GetBatchCount() )
5113 m_colLabelWin
->Refresh();
5120 if (result
&& !GetBatchCount() )
5121 m_gridWin
->Refresh();
5126 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
) const
5128 wxRegionIterator
iter( reg
);
5131 wxArrayInt rowlabels
;
5138 // TODO: remove this when we can...
5139 // There is a bug in wxMotif that gives garbage update
5140 // rectangles if you jump-scroll a long way by clicking the
5141 // scrollbar with middle button. This is a work-around
5143 #if defined(__WXMOTIF__)
5145 m_gridWin
->GetClientSize( &cw
, &ch
);
5146 if ( r
.GetTop() > ch
)
5148 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5151 // logical bounds of update region
5154 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
5155 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
5157 // find the row labels within these bounds
5160 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5162 if ( GetRowBottom(row
) < top
)
5165 if ( GetRowTop(row
) > bottom
)
5168 rowlabels
.Add( row
);
5177 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
) const
5179 wxRegionIterator
iter( reg
);
5182 wxArrayInt colLabels
;
5189 // TODO: remove this when we can...
5190 // There is a bug in wxMotif that gives garbage update
5191 // rectangles if you jump-scroll a long way by clicking the
5192 // scrollbar with middle button. This is a work-around
5194 #if defined(__WXMOTIF__)
5196 m_gridWin
->GetClientSize( &cw
, &ch
);
5197 if ( r
.GetLeft() > cw
)
5199 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5202 // logical bounds of update region
5205 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
5206 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
5208 // find the cells within these bounds
5212 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5214 col
= GetColAt( colPos
);
5216 if ( GetColRight(col
) < left
)
5219 if ( GetColLeft(col
) > right
)
5222 colLabels
.Add( col
);
5231 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
) const
5233 wxRegionIterator
iter( reg
);
5236 wxGridCellCoordsArray cellsExposed
;
5238 int left
, top
, right
, bottom
;
5243 // TODO: remove this when we can...
5244 // There is a bug in wxMotif that gives garbage update
5245 // rectangles if you jump-scroll a long way by clicking the
5246 // scrollbar with middle button. This is a work-around
5248 #if defined(__WXMOTIF__)
5250 m_gridWin
->GetClientSize( &cw
, &ch
);
5251 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5252 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5253 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5254 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5257 // logical bounds of update region
5259 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5260 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5262 // find the cells within these bounds
5265 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5267 if ( GetRowBottom(row
) <= top
)
5270 if ( GetRowTop(row
) > bottom
)
5274 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5276 col
= GetColAt( colPos
);
5278 if ( GetColRight(col
) <= left
)
5281 if ( GetColLeft(col
) > right
)
5284 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
5291 return cellsExposed
;
5295 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5298 wxPoint
pos( event
.GetPosition() );
5299 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5301 if ( event
.Dragging() )
5305 m_isDragging
= true;
5306 m_rowLabelWin
->CaptureMouse();
5309 if ( event
.LeftIsDown() )
5311 switch ( m_cursorMode
)
5313 case WXGRID_CURSOR_RESIZE_ROW
:
5315 int cw
, ch
, left
, dummy
;
5316 m_gridWin
->GetClientSize( &cw
, &ch
);
5317 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5319 wxClientDC
dc( m_gridWin
);
5322 GetRowTop(m_dragRowOrCol
) +
5323 GetRowMinimalHeight(m_dragRowOrCol
) );
5324 dc
.SetLogicalFunction(wxINVERT
);
5325 if ( m_dragLastPos
>= 0 )
5327 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5329 dc
.DrawLine( left
, y
, left
+cw
, y
);
5334 case WXGRID_CURSOR_SELECT_ROW
:
5336 if ( (row
= YToRow( y
)) >= 0 )
5340 m_selection
->SelectRow( row
,
5341 event
.ControlDown(),
5350 // default label to suppress warnings about "enumeration value
5351 // 'xxx' not handled in switch
5359 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5364 if (m_rowLabelWin
->HasCapture())
5365 m_rowLabelWin
->ReleaseMouse();
5366 m_isDragging
= false;
5369 // ------------ Entering or leaving the window
5371 if ( event
.Entering() || event
.Leaving() )
5373 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5376 // ------------ Left button pressed
5378 else if ( event
.LeftDown() )
5380 // don't send a label click event for a hit on the
5381 // edge of the row label - this is probably the user
5382 // wanting to resize the row
5384 if ( YToEdgeOfRow(y
) < 0 )
5388 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5390 if ( !event
.ShiftDown() && !event
.CmdDown() )
5394 if ( event
.ShiftDown() )
5396 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5399 GetNumberCols() - 1,
5400 event
.ControlDown(),
5407 m_selection
->SelectRow( row
,
5408 event
.ControlDown(),
5415 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5420 // starting to drag-resize a row
5421 if ( CanDragRowSize() )
5422 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5426 // ------------ Left double click
5428 else if (event
.LeftDClick() )
5430 row
= YToEdgeOfRow(y
);
5435 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5437 // no default action at the moment
5442 // adjust row height depending on label text
5443 AutoSizeRowLabelSize( row
);
5445 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5450 // ------------ Left button released
5452 else if ( event
.LeftUp() )
5454 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5456 DoEndDragResizeRow();
5458 // Note: we are ending the event *after* doing
5459 // default processing in this case
5461 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5464 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5468 // ------------ Right button down
5470 else if ( event
.RightDown() )
5474 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5476 // no default action at the moment
5480 // ------------ Right double click
5482 else if ( event
.RightDClick() )
5486 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5488 // no default action at the moment
5492 // ------------ No buttons down and mouse moving
5494 else if ( event
.Moving() )
5496 m_dragRowOrCol
= YToEdgeOfRow( y
);
5497 if ( m_dragRowOrCol
>= 0 )
5499 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5501 // don't capture the mouse yet
5502 if ( CanDragRowSize() )
5503 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5506 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5508 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5513 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5516 wxPoint
pos( event
.GetPosition() );
5517 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5519 if ( event
.Dragging() )
5523 m_isDragging
= true;
5524 m_colLabelWin
->CaptureMouse();
5526 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
)
5527 m_dragRowOrCol
= XToCol( x
);
5530 if ( event
.LeftIsDown() )
5532 switch ( m_cursorMode
)
5534 case WXGRID_CURSOR_RESIZE_COL
:
5536 int cw
, ch
, dummy
, top
;
5537 m_gridWin
->GetClientSize( &cw
, &ch
);
5538 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5540 wxClientDC
dc( m_gridWin
);
5543 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5544 GetColMinimalWidth(m_dragRowOrCol
));
5545 dc
.SetLogicalFunction(wxINVERT
);
5546 if ( m_dragLastPos
>= 0 )
5548 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5550 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5555 case WXGRID_CURSOR_SELECT_COL
:
5557 if ( (col
= XToCol( x
)) >= 0 )
5561 m_selection
->SelectCol( col
,
5562 event
.ControlDown(),
5571 case WXGRID_CURSOR_MOVE_COL
:
5574 m_moveToCol
= GetColAt( 0 );
5576 m_moveToCol
= XToCol( x
);
5580 if ( m_moveToCol
< 0 )
5581 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5583 markerX
= GetColLeft( m_moveToCol
);
5585 if ( markerX
!= m_dragLastPos
)
5587 wxClientDC
dc( m_colLabelWin
);
5590 m_colLabelWin
->GetClientSize( &cw
, &ch
);
5594 //Clean up the last indicator
5595 if ( m_dragLastPos
>= 0 )
5597 wxPen
pen( m_colLabelWin
->GetBackgroundColour(), 2 );
5599 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
5600 dc
.SetPen(wxNullPen
);
5602 if ( XToCol( m_dragLastPos
) != -1 )
5603 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
5606 //Moving to the same place? Don't draw a marker
5607 if ( (m_moveToCol
== m_dragRowOrCol
)
5608 || (GetColPos( m_moveToCol
) == GetColPos( m_dragRowOrCol
) + 1)
5609 || (m_moveToCol
< 0 && m_dragRowOrCol
== GetColAt( m_numCols
- 1 )))
5616 wxPen
pen( *wxBLUE
, 2 );
5619 dc
.DrawLine( markerX
, 0, markerX
, ch
);
5621 dc
.SetPen(wxNullPen
);
5623 m_dragLastPos
= markerX
- 1;
5628 // default label to suppress warnings about "enumeration value
5629 // 'xxx' not handled in switch
5637 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5642 if (m_colLabelWin
->HasCapture())
5643 m_colLabelWin
->ReleaseMouse();
5644 m_isDragging
= false;
5647 // ------------ Entering or leaving the window
5649 if ( event
.Entering() || event
.Leaving() )
5651 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5654 // ------------ Left button pressed
5656 else if ( event
.LeftDown() )
5658 // don't send a label click event for a hit on the
5659 // edge of the col label - this is probably the user
5660 // wanting to resize the col
5662 if ( XToEdgeOfCol(x
) < 0 )
5666 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5668 if ( m_canDragColMove
)
5670 //Show button as pressed
5671 wxClientDC
dc( m_colLabelWin
);
5672 int colLeft
= GetColLeft( col
);
5673 int colRight
= GetColRight( col
) - 1;
5674 dc
.SetPen( wxPen( m_colLabelWin
->GetBackgroundColour(), 1 ) );
5675 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
5676 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
5678 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, m_colLabelWin
);
5682 if ( !event
.ShiftDown() && !event
.CmdDown() )
5686 if ( event
.ShiftDown() )
5688 m_selection
->SelectBlock( 0,
5689 m_currentCellCoords
.GetCol(),
5690 GetNumberRows() - 1, col
,
5691 event
.ControlDown(),
5698 m_selection
->SelectCol( col
,
5699 event
.ControlDown(),
5706 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5712 // starting to drag-resize a col
5714 if ( CanDragColSize() )
5715 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5719 // ------------ Left double click
5721 if ( event
.LeftDClick() )
5723 col
= XToEdgeOfCol(x
);
5728 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5730 // no default action at the moment
5735 // adjust column width depending on label text
5736 AutoSizeColLabelSize( col
);
5738 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5743 // ------------ Left button released
5745 else if ( event
.LeftUp() )
5747 switch ( m_cursorMode
)
5749 case WXGRID_CURSOR_RESIZE_COL
:
5750 DoEndDragResizeCol();
5752 // Note: we are ending the event *after* doing
5753 // default processing in this case
5755 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5758 case WXGRID_CURSOR_MOVE_COL
:
5761 SendEvent( wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
, event
);
5764 case WXGRID_CURSOR_SELECT_COL
:
5765 case WXGRID_CURSOR_SELECT_CELL
:
5766 case WXGRID_CURSOR_RESIZE_ROW
:
5767 case WXGRID_CURSOR_SELECT_ROW
:
5768 // nothing to do (?)
5772 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5776 // ------------ Right button down
5778 else if ( event
.RightDown() )
5782 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5784 // no default action at the moment
5788 // ------------ Right double click
5790 else if ( event
.RightDClick() )
5794 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5796 // no default action at the moment
5800 // ------------ No buttons down and mouse moving
5802 else if ( event
.Moving() )
5804 m_dragRowOrCol
= XToEdgeOfCol( x
);
5805 if ( m_dragRowOrCol
>= 0 )
5807 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5809 // don't capture the cursor yet
5810 if ( CanDragColSize() )
5811 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5814 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5816 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5821 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5823 if ( event
.LeftDown() )
5825 // indicate corner label by having both row and
5828 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5833 else if ( event
.LeftDClick() )
5835 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5837 else if ( event
.RightDown() )
5839 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5841 // no default action at the moment
5844 else if ( event
.RightDClick() )
5846 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5848 // no default action at the moment
5853 void wxGrid::CancelMouseCapture()
5855 // cancel operation currently in progress, whatever it is
5858 m_isDragging
= false;
5859 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
5860 m_winCapture
->SetCursor( *wxSTANDARD_CURSOR
);
5861 m_winCapture
= NULL
;
5863 // remove traces of whatever we drew on screen
5868 void wxGrid::ChangeCursorMode(CursorMode mode
,
5873 static const wxChar
*cursorModes
[] =
5883 wxLogTrace(_T("grid"),
5884 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5885 win
== m_colLabelWin
? _T("colLabelWin")
5886 : win
? _T("rowLabelWin")
5888 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5891 if ( mode
== m_cursorMode
&&
5892 win
== m_winCapture
&&
5893 captureMouse
== (m_winCapture
!= NULL
))
5898 // by default use the grid itself
5904 if (m_winCapture
->HasCapture())
5905 m_winCapture
->ReleaseMouse();
5906 m_winCapture
= (wxWindow
*)NULL
;
5909 m_cursorMode
= mode
;
5911 switch ( m_cursorMode
)
5913 case WXGRID_CURSOR_RESIZE_ROW
:
5914 win
->SetCursor( m_rowResizeCursor
);
5917 case WXGRID_CURSOR_RESIZE_COL
:
5918 win
->SetCursor( m_colResizeCursor
);
5921 case WXGRID_CURSOR_MOVE_COL
:
5922 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
5926 win
->SetCursor( *wxSTANDARD_CURSOR
);
5930 // we need to capture mouse when resizing
5931 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5932 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5934 if ( captureMouse
&& resize
)
5936 win
->CaptureMouse();
5941 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5944 wxPoint
pos( event
.GetPosition() );
5945 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5947 wxGridCellCoords coords
;
5948 XYToCell( x
, y
, coords
);
5950 int cell_rows
, cell_cols
;
5951 bool isFirstDrag
= !m_isDragging
;
5952 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5953 if ((cell_rows
< 0) || (cell_cols
< 0))
5955 coords
.SetRow(coords
.GetRow() + cell_rows
);
5956 coords
.SetCol(coords
.GetCol() + cell_cols
);
5959 if ( event
.Dragging() )
5961 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5963 // Don't start doing anything until the mouse has been dragged at
5964 // least 3 pixels in any direction...
5967 if (m_startDragPos
== wxDefaultPosition
)
5969 m_startDragPos
= pos
;
5972 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5976 m_isDragging
= true;
5977 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5979 // Hide the edit control, so it
5980 // won't interfere with drag-shrinking.
5981 if ( IsCellEditControlShown() )
5983 HideCellEditControl();
5984 SaveEditControlValue();
5987 if ( coords
!= wxGridNoCellCoords
)
5989 if ( event
.CmdDown() )
5991 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5992 m_selectingKeyboard
= coords
;
5993 HighlightBlock( m_selectingKeyboard
, coords
);
5995 else if ( CanDragCell() )
5999 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6000 m_selectingKeyboard
= coords
;
6002 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
6011 if ( !IsSelection() )
6013 HighlightBlock( coords
, coords
);
6017 HighlightBlock( m_currentCellCoords
, coords
);
6021 if (! IsVisible(coords
))
6023 MakeCellVisible(coords
);
6024 // TODO: need to introduce a delay or something here. The
6025 // scrolling is way to fast, at least on MSW - also on GTK.
6028 // Have we captured the mouse yet?
6031 m_winCapture
= m_gridWin
;
6032 m_winCapture
->CaptureMouse();
6037 else if ( event
.LeftIsDown() &&
6038 m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6040 int cw
, ch
, left
, dummy
;
6041 m_gridWin
->GetClientSize( &cw
, &ch
);
6042 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6044 wxClientDC
dc( m_gridWin
);
6046 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
6047 GetRowMinimalHeight(m_dragRowOrCol
) );
6048 dc
.SetLogicalFunction(wxINVERT
);
6049 if ( m_dragLastPos
>= 0 )
6051 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
6053 dc
.DrawLine( left
, y
, left
+cw
, y
);
6056 else if ( event
.LeftIsDown() &&
6057 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6059 int cw
, ch
, dummy
, top
;
6060 m_gridWin
->GetClientSize( &cw
, &ch
);
6061 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6063 wxClientDC
dc( m_gridWin
);
6065 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
6066 GetColMinimalWidth(m_dragRowOrCol
) );
6067 dc
.SetLogicalFunction(wxINVERT
);
6068 if ( m_dragLastPos
>= 0 )
6070 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6072 dc
.DrawLine( x
, top
, x
, top
+ ch
);
6079 m_isDragging
= false;
6080 m_startDragPos
= wxDefaultPosition
;
6082 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
6083 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
6086 if ( event
.Entering() || event
.Leaving() )
6088 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6089 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
6094 // ------------ Left button pressed
6096 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
6098 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
6103 if ( !event
.CmdDown() )
6105 if ( event
.ShiftDown() )
6109 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
6110 m_currentCellCoords
.GetCol(),
6113 event
.ControlDown(),
6119 else if ( XToEdgeOfCol(x
) < 0 &&
6120 YToEdgeOfRow(y
) < 0 )
6122 DisableCellEditControl();
6123 MakeCellVisible( coords
);
6125 if ( event
.CmdDown() )
6129 m_selection
->ToggleCellSelection( coords
.GetRow(),
6131 event
.ControlDown(),
6136 m_selectingTopLeft
= wxGridNoCellCoords
;
6137 m_selectingBottomRight
= wxGridNoCellCoords
;
6138 m_selectingKeyboard
= coords
;
6142 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
6143 SetCurrentCell( coords
);
6146 if ( m_selection
->GetSelectionMode() !=
6147 wxGrid::wxGridSelectCells
)
6149 HighlightBlock( coords
, coords
);
6157 // ------------ Left double click
6159 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
6161 DisableCellEditControl();
6163 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
6165 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
6170 // we want double click to select a cell and start editing
6171 // (i.e. to behave in same way as sequence of two slow clicks):
6172 m_waitForSlowClick
= true;
6177 // ------------ Left button released
6179 else if ( event
.LeftUp() )
6181 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6185 if (m_winCapture
->HasCapture())
6186 m_winCapture
->ReleaseMouse();
6187 m_winCapture
= NULL
;
6190 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
6193 EnableCellEditControl();
6195 wxGridCellAttr
*attr
= GetCellAttr(coords
);
6196 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
6197 editor
->StartingClick();
6201 m_waitForSlowClick
= false;
6203 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6204 m_selectingBottomRight
!= wxGridNoCellCoords
)
6208 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6209 m_selectingTopLeft
.GetCol(),
6210 m_selectingBottomRight
.GetRow(),
6211 m_selectingBottomRight
.GetCol(),
6212 event
.ControlDown(),
6218 m_selectingTopLeft
= wxGridNoCellCoords
;
6219 m_selectingBottomRight
= wxGridNoCellCoords
;
6221 // Show the edit control, if it has been hidden for
6223 ShowCellEditControl();
6226 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6228 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6229 DoEndDragResizeRow();
6231 // Note: we are ending the event *after* doing
6232 // default processing in this case
6234 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
6236 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6238 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6239 DoEndDragResizeCol();
6241 // Note: we are ending the event *after* doing
6242 // default processing in this case
6244 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
6250 // ------------ Right button down
6252 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
6254 DisableCellEditControl();
6255 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
6260 // no default action at the moment
6264 // ------------ Right double click
6266 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
6268 DisableCellEditControl();
6269 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
6274 // no default action at the moment
6278 // ------------ Moving and no button action
6280 else if ( event
.Moving() && !event
.IsButton() )
6282 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
6284 // out of grid cell area
6285 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6289 int dragRow
= YToEdgeOfRow( y
);
6290 int dragCol
= XToEdgeOfCol( x
);
6292 // Dragging on the corner of a cell to resize in both
6293 // directions is not implemented yet...
6295 if ( dragRow
>= 0 && dragCol
>= 0 )
6297 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6303 m_dragRowOrCol
= dragRow
;
6305 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6307 if ( CanDragRowSize() && CanDragGridSize() )
6308 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false);
6311 else if ( dragCol
>= 0 )
6313 m_dragRowOrCol
= dragCol
;
6315 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6317 if ( CanDragColSize() && CanDragGridSize() )
6318 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false);
6321 else // Neither on a row or col edge
6323 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6325 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6331 void wxGrid::DoEndDragResizeRow()
6333 if ( m_dragLastPos
>= 0 )
6335 // erase the last line and resize the row
6337 int cw
, ch
, left
, dummy
;
6338 m_gridWin
->GetClientSize( &cw
, &ch
);
6339 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6341 wxClientDC
dc( m_gridWin
);
6343 dc
.SetLogicalFunction( wxINVERT
);
6344 dc
.DrawLine( left
, m_dragLastPos
, left
+ cw
, m_dragLastPos
);
6345 HideCellEditControl();
6346 SaveEditControlValue();
6348 int rowTop
= GetRowTop(m_dragRowOrCol
);
6349 SetRowSize( m_dragRowOrCol
,
6350 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
6352 if ( !GetBatchCount() )
6354 // Only needed to get the correct rect.y:
6355 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
6357 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6358 rect
.width
= m_rowLabelWidth
;
6359 rect
.height
= ch
- rect
.y
;
6360 m_rowLabelWin
->Refresh( true, &rect
);
6363 // if there is a multicell block, paint all of it
6366 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
6367 int leftCol
= XToCol(left
);
6368 int rightCol
= internalXToCol(left
+ cw
);
6371 for (i
=leftCol
; i
<rightCol
; i
++)
6373 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
6374 if (cell_rows
< subtract_rows
)
6375 subtract_rows
= cell_rows
;
6377 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
6378 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6379 rect
.height
= ch
- rect
.y
;
6382 m_gridWin
->Refresh( false, &rect
);
6385 ShowCellEditControl();
6390 void wxGrid::DoEndDragResizeCol()
6392 if ( m_dragLastPos
>= 0 )
6394 // erase the last line and resize the col
6396 int cw
, ch
, dummy
, top
;
6397 m_gridWin
->GetClientSize( &cw
, &ch
);
6398 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6400 wxClientDC
dc( m_gridWin
);
6402 dc
.SetLogicalFunction( wxINVERT
);
6403 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6404 HideCellEditControl();
6405 SaveEditControlValue();
6407 int colLeft
= GetColLeft(m_dragRowOrCol
);
6408 SetColSize( m_dragRowOrCol
,
6409 wxMax( m_dragLastPos
- colLeft
,
6410 GetColMinimalWidth(m_dragRowOrCol
) ) );
6412 if ( !GetBatchCount() )
6414 // Only needed to get the correct rect.x:
6415 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
6417 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6418 rect
.width
= cw
- rect
.x
;
6419 rect
.height
= m_colLabelHeight
;
6420 m_colLabelWin
->Refresh( true, &rect
);
6423 // if there is a multicell block, paint all of it
6426 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
6427 int topRow
= YToRow(top
);
6428 int bottomRow
= internalYToRow(top
+ cw
);
6431 for (i
=topRow
; i
<bottomRow
; i
++)
6433 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
6434 if (cell_cols
< subtract_cols
)
6435 subtract_cols
= cell_cols
;
6438 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
6439 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6440 rect
.width
= cw
- rect
.x
;
6444 m_gridWin
->Refresh( false, &rect
);
6447 ShowCellEditControl();
6451 void wxGrid::DoEndDragMoveCol()
6453 //The user clicked on the column but didn't actually drag
6454 if ( m_dragLastPos
< 0 )
6456 m_colLabelWin
->Refresh(); //Do this to "unpress" the column
6461 if ( m_moveToCol
== -1 )
6462 newPos
= m_numCols
- 1;
6465 newPos
= GetColPos( m_moveToCol
);
6466 if ( newPos
> GetColPos( m_dragRowOrCol
) )
6470 SetColPos( m_dragRowOrCol
, newPos
);
6473 void wxGrid::SetColPos( int colID
, int newPos
)
6475 if ( m_colAt
.IsEmpty() )
6477 m_colAt
.Alloc( m_numCols
);
6480 for ( i
= 0; i
< m_numCols
; i
++ )
6486 int oldPos
= GetColPos( colID
);
6488 //Reshuffle the m_colAt array
6489 if ( newPos
> oldPos
)
6492 for ( i
= oldPos
; i
< newPos
; i
++ )
6494 m_colAt
[i
] = m_colAt
[i
+1];
6500 for ( i
= oldPos
; i
> newPos
; i
-- )
6502 m_colAt
[i
] = m_colAt
[i
-1];
6506 m_colAt
[newPos
] = colID
;
6508 //Recalculate the column rights
6509 if ( !m_colWidths
.IsEmpty() )
6513 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6515 int colID
= GetColAt( colPos
);
6517 colRight
+= m_colWidths
[colID
];
6518 m_colRights
[colID
] = colRight
;
6522 m_colLabelWin
->Refresh();
6523 m_gridWin
->Refresh();
6528 void wxGrid::EnableDragColMove( bool enable
)
6530 if ( m_canDragColMove
== enable
)
6533 m_canDragColMove
= enable
;
6535 if ( !m_canDragColMove
)
6539 //Recalculate the column rights
6540 if ( !m_colWidths
.IsEmpty() )
6544 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6546 colRight
+= m_colWidths
[colPos
];
6547 m_colRights
[colPos
] = colRight
;
6551 m_colLabelWin
->Refresh();
6552 m_gridWin
->Refresh();
6558 // ------ interaction with data model
6560 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6562 switch ( msg
.GetId() )
6564 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6565 return GetModelValues();
6567 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6568 return SetModelValues();
6570 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6571 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6572 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6573 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6574 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6575 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6576 return Redimension( msg
);
6583 // The behaviour of this function depends on the grid table class
6584 // Clear() function. For the default wxGridStringTable class the
6585 // behavious is to replace all cell contents with wxEmptyString but
6586 // not to change the number of rows or cols.
6588 void wxGrid::ClearGrid()
6592 if (IsCellEditControlEnabled())
6593 DisableCellEditControl();
6596 if (!GetBatchCount())
6597 m_gridWin
->Refresh();
6601 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6603 // TODO: something with updateLabels flag
6607 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6613 if (IsCellEditControlEnabled())
6614 DisableCellEditControl();
6616 bool done
= m_table
->InsertRows( pos
, numRows
);
6619 // the table will have sent the results of the insert row
6620 // operation to this view object as a grid table message
6626 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6628 // TODO: something with updateLabels flag
6632 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6638 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6641 // the table will have sent the results of the append row
6642 // operation to this view object as a grid table message
6648 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6650 // TODO: something with updateLabels flag
6654 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6660 if (IsCellEditControlEnabled())
6661 DisableCellEditControl();
6663 bool done
= m_table
->DeleteRows( pos
, numRows
);
6665 // the table will have sent the results of the delete row
6666 // operation to this view object as a grid table message
6672 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6674 // TODO: something with updateLabels flag
6678 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6684 if (IsCellEditControlEnabled())
6685 DisableCellEditControl();
6687 bool done
= m_table
->InsertCols( pos
, numCols
);
6689 // the table will have sent the results of the insert col
6690 // operation to this view object as a grid table message
6696 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6698 // TODO: something with updateLabels flag
6702 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6708 bool done
= m_table
->AppendCols( numCols
);
6710 // the table will have sent the results of the append col
6711 // operation to this view object as a grid table message
6717 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6719 // TODO: something with updateLabels flag
6723 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6729 if (IsCellEditControlEnabled())
6730 DisableCellEditControl();
6732 bool done
= m_table
->DeleteCols( pos
, numCols
);
6734 // the table will have sent the results of the delete col
6735 // operation to this view object as a grid table message
6742 // ----- event handlers
6745 // Generate a grid event based on a mouse event and
6746 // return the result of ProcessEvent()
6748 int wxGrid::SendEvent( const wxEventType type
,
6750 wxMouseEvent
& mouseEv
)
6752 bool claimed
, vetoed
;
6754 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6756 int rowOrCol
= (row
== -1 ? col
: row
);
6758 wxGridSizeEvent
gridEvt( GetId(),
6762 mouseEv
.GetX() + GetRowLabelSize(),
6763 mouseEv
.GetY() + GetColLabelSize(),
6764 mouseEv
.ControlDown(),
6765 mouseEv
.ShiftDown(),
6767 mouseEv
.MetaDown() );
6769 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6770 vetoed
= !gridEvt
.IsAllowed();
6772 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6774 // Right now, it should _never_ end up here!
6775 wxGridRangeSelectEvent
gridEvt( GetId(),
6779 m_selectingBottomRight
,
6781 mouseEv
.ControlDown(),
6782 mouseEv
.ShiftDown(),
6784 mouseEv
.MetaDown() );
6786 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6787 vetoed
= !gridEvt
.IsAllowed();
6789 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
6790 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
6791 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
6792 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
6794 wxPoint pos
= mouseEv
.GetPosition();
6796 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
6797 pos
.y
+= GetColLabelSize();
6798 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
6799 pos
.x
+= GetRowLabelSize();
6801 wxGridEvent
gridEvt( GetId(),
6808 mouseEv
.ControlDown(),
6809 mouseEv
.ShiftDown(),
6811 mouseEv
.MetaDown() );
6812 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6813 vetoed
= !gridEvt
.IsAllowed();
6817 wxGridEvent
gridEvt( GetId(),
6821 mouseEv
.GetX() + GetRowLabelSize(),
6822 mouseEv
.GetY() + GetColLabelSize(),
6824 mouseEv
.ControlDown(),
6825 mouseEv
.ShiftDown(),
6827 mouseEv
.MetaDown() );
6828 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6829 vetoed
= !gridEvt
.IsAllowed();
6832 // A Veto'd event may not be `claimed' so test this first
6836 return claimed
? 1 : 0;
6839 // Generate a grid event of specified type and return the result
6840 // of ProcessEvent().
6842 int wxGrid::SendEvent( const wxEventType type
,
6845 bool claimed
, vetoed
;
6847 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6849 int rowOrCol
= (row
== -1 ? col
: row
);
6851 wxGridSizeEvent
gridEvt( GetId(), type
, this, rowOrCol
);
6853 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6854 vetoed
= !gridEvt
.IsAllowed();
6858 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
6860 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6861 vetoed
= !gridEvt
.IsAllowed();
6864 // A Veto'd event may not be `claimed' so test this first
6868 return claimed
? 1 : 0;
6871 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6873 // needed to prevent zillions of paint events on MSW
6877 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6879 // Don't do anything if between Begin/EndBatch...
6880 // EndBatch() will do all this on the last nested one anyway.
6881 if ( m_created
&& !GetBatchCount() )
6883 // Refresh to get correct scrolled position:
6884 wxScrolledWindow::Refresh(eraseb
, rect
);
6888 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6889 int width_label
, width_cell
, height_label
, height_cell
;
6892 // Copy rectangle can get scroll offsets..
6893 rect_x
= rect
->GetX();
6894 rect_y
= rect
->GetY();
6895 rectWidth
= rect
->GetWidth();
6896 rectHeight
= rect
->GetHeight();
6898 width_label
= m_rowLabelWidth
- rect_x
;
6899 if (width_label
> rectWidth
)
6900 width_label
= rectWidth
;
6902 height_label
= m_colLabelHeight
- rect_y
;
6903 if (height_label
> rectHeight
)
6904 height_label
= rectHeight
;
6906 if (rect_x
> m_rowLabelWidth
)
6908 x
= rect_x
- m_rowLabelWidth
;
6909 width_cell
= rectWidth
;
6914 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6917 if (rect_y
> m_colLabelHeight
)
6919 y
= rect_y
- m_colLabelHeight
;
6920 height_cell
= rectHeight
;
6925 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6928 // Paint corner label part intersecting rect.
6929 if ( width_label
> 0 && height_label
> 0 )
6931 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6932 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6935 // Paint col labels part intersecting rect.
6936 if ( width_cell
> 0 && height_label
> 0 )
6938 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6939 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6942 // Paint row labels part intersecting rect.
6943 if ( width_label
> 0 && height_cell
> 0 )
6945 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6946 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6949 // Paint cell area part intersecting rect.
6950 if ( width_cell
> 0 && height_cell
> 0 )
6952 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6953 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6958 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6959 m_colLabelWin
->Refresh(eraseb
, NULL
);
6960 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6961 m_gridWin
->Refresh(eraseb
, NULL
);
6966 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
))
6968 if (m_targetWindow
!= this) // check whether initialisation has been done
6970 // update our children window positions and scrollbars
6975 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6977 if ( m_inOnKeyDown
)
6979 // shouldn't be here - we are going round in circles...
6981 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6984 m_inOnKeyDown
= true;
6986 // propagate the event up and see if it gets processed
6987 wxWindow
*parent
= GetParent();
6988 wxKeyEvent
keyEvt( event
);
6989 keyEvt
.SetEventObject( parent
);
6991 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6993 if (GetLayoutDirection() == wxLayout_RightToLeft
)
6995 if (event
.GetKeyCode() == WXK_RIGHT
)
6996 event
.m_keyCode
= WXK_LEFT
;
6997 else if (event
.GetKeyCode() == WXK_LEFT
)
6998 event
.m_keyCode
= WXK_RIGHT
;
7001 // try local handlers
7002 switch ( event
.GetKeyCode() )
7005 if ( event
.ControlDown() )
7006 MoveCursorUpBlock( event
.ShiftDown() );
7008 MoveCursorUp( event
.ShiftDown() );
7012 if ( event
.ControlDown() )
7013 MoveCursorDownBlock( event
.ShiftDown() );
7015 MoveCursorDown( event
.ShiftDown() );
7019 if ( event
.ControlDown() )
7020 MoveCursorLeftBlock( event
.ShiftDown() );
7022 MoveCursorLeft( event
.ShiftDown() );
7026 if ( event
.ControlDown() )
7027 MoveCursorRightBlock( event
.ShiftDown() );
7029 MoveCursorRight( event
.ShiftDown() );
7033 case WXK_NUMPAD_ENTER
:
7034 if ( event
.ControlDown() )
7036 event
.Skip(); // to let the edit control have the return
7040 if ( GetGridCursorRow() < GetNumberRows()-1 )
7042 MoveCursorDown( event
.ShiftDown() );
7046 // at the bottom of a column
7047 DisableCellEditControl();
7057 if (event
.ShiftDown())
7059 if ( GetGridCursorCol() > 0 )
7061 MoveCursorLeft( false );
7066 DisableCellEditControl();
7071 if ( GetGridCursorCol() < GetNumberCols() - 1 )
7073 MoveCursorRight( false );
7078 DisableCellEditControl();
7084 if ( event
.ControlDown() )
7086 MakeCellVisible( 0, 0 );
7087 SetCurrentCell( 0, 0 );
7096 if ( event
.ControlDown() )
7098 MakeCellVisible( m_numRows
- 1, m_numCols
- 1 );
7099 SetCurrentCell( m_numRows
- 1, m_numCols
- 1 );
7116 if ( event
.ControlDown() )
7120 m_selection
->ToggleCellSelection(
7121 m_currentCellCoords
.GetRow(),
7122 m_currentCellCoords
.GetCol(),
7123 event
.ControlDown(),
7131 if ( !IsEditable() )
7132 MoveCursorRight( false );
7143 m_inOnKeyDown
= false;
7146 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
7148 // try local handlers
7150 if ( event
.GetKeyCode() == WXK_SHIFT
)
7152 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
7153 m_selectingBottomRight
!= wxGridNoCellCoords
)
7157 m_selection
->SelectBlock(
7158 m_selectingTopLeft
.GetRow(),
7159 m_selectingTopLeft
.GetCol(),
7160 m_selectingBottomRight
.GetRow(),
7161 m_selectingBottomRight
.GetCol(),
7162 event
.ControlDown(),
7169 m_selectingTopLeft
= wxGridNoCellCoords
;
7170 m_selectingBottomRight
= wxGridNoCellCoords
;
7171 m_selectingKeyboard
= wxGridNoCellCoords
;
7175 void wxGrid::OnChar( wxKeyEvent
& event
)
7177 // is it possible to edit the current cell at all?
7178 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7180 // yes, now check whether the cells editor accepts the key
7181 int row
= m_currentCellCoords
.GetRow();
7182 int col
= m_currentCellCoords
.GetCol();
7183 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7184 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7186 // <F2> is special and will always start editing, for
7187 // other keys - ask the editor itself
7188 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
7189 || editor
->IsAcceptedKey(event
) )
7191 // ensure cell is visble
7192 MakeCellVisible(row
, col
);
7193 EnableCellEditControl();
7195 // a problem can arise if the cell is not completely
7196 // visible (even after calling MakeCellVisible the
7197 // control is not created and calling StartingKey will
7199 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
7200 editor
->StartingKey(event
);
7216 void wxGrid::OnEraseBackground(wxEraseEvent
&)
7220 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
7222 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
7224 // the event has been intercepted - do nothing
7228 #if !defined(__WXMAC__)
7229 wxClientDC
dc( m_gridWin
);
7233 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
7235 DisableCellEditControl();
7237 if ( IsVisible( m_currentCellCoords
, false ) )
7240 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
7241 if ( !m_gridLinesEnabled
)
7249 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
7251 // Otherwise refresh redraws the highlight!
7252 m_currentCellCoords
= coords
;
7254 #if defined(__WXMAC__)
7255 m_gridWin
->Refresh(true /*, & r */);
7257 DrawGridCellArea( dc
, cells
);
7258 DrawAllGridLines( dc
, r
);
7263 m_currentCellCoords
= coords
;
7265 wxGridCellAttr
*attr
= GetCellAttr( coords
);
7266 #if !defined(__WXMAC__)
7267 DrawCellHighlight( dc
, attr
);
7272 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7275 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7279 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
7282 rightCol
= GetNumberCols() - 1;
7284 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
7287 bottomRow
= GetNumberRows() - 1;
7291 if ( topRow
> bottomRow
)
7298 if ( leftCol
> rightCol
)
7305 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7306 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7308 // First the case that we selected a completely new area
7309 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
7310 m_selectingBottomRight
== wxGridNoCellCoords
)
7313 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
7314 wxGridCellCoords ( bottomRow
, rightCol
) );
7315 m_gridWin
->Refresh( false, &rect
);
7318 // Now handle changing an existing selection area.
7319 else if ( m_selectingTopLeft
!= updateTopLeft
||
7320 m_selectingBottomRight
!= updateBottomRight
)
7322 // Compute two optimal update rectangles:
7323 // Either one rectangle is a real subset of the
7324 // other, or they are (almost) disjoint!
7326 bool need_refresh
[4];
7330 need_refresh
[3] = false;
7333 // Store intermediate values
7334 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
7335 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
7336 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
7337 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
7339 // Determine the outer/inner coordinates.
7340 if (oldLeft
> leftCol
)
7346 if (oldTop
> topRow
)
7352 if (oldRight
< rightCol
)
7355 oldRight
= rightCol
;
7358 if (oldBottom
< bottomRow
)
7361 oldBottom
= bottomRow
;
7365 // Now, either the stuff marked old is the outer
7366 // rectangle or we don't have a situation where one
7367 // is contained in the other.
7369 if ( oldLeft
< leftCol
)
7371 // Refresh the newly selected or deselected
7372 // area to the left of the old or new selection.
7373 need_refresh
[0] = true;
7374 rect
[0] = BlockToDeviceRect(
7375 wxGridCellCoords( oldTop
, oldLeft
),
7376 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
7379 if ( oldTop
< topRow
)
7381 // Refresh the newly selected or deselected
7382 // area above the old or new selection.
7383 need_refresh
[1] = true;
7384 rect
[1] = BlockToDeviceRect(
7385 wxGridCellCoords( oldTop
, leftCol
),
7386 wxGridCellCoords( topRow
- 1, rightCol
) );
7389 if ( oldRight
> rightCol
)
7391 // Refresh the newly selected or deselected
7392 // area to the right of the old or new selection.
7393 need_refresh
[2] = true;
7394 rect
[2] = BlockToDeviceRect(
7395 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
7396 wxGridCellCoords( oldBottom
, oldRight
) );
7399 if ( oldBottom
> bottomRow
)
7401 // Refresh the newly selected or deselected
7402 // area below the old or new selection.
7403 need_refresh
[3] = true;
7404 rect
[3] = BlockToDeviceRect(
7405 wxGridCellCoords( bottomRow
+ 1, leftCol
),
7406 wxGridCellCoords( oldBottom
, rightCol
) );
7409 // various Refresh() calls
7410 for (i
= 0; i
< 4; i
++ )
7411 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7412 m_gridWin
->Refresh( false, &(rect
[i
]) );
7416 m_selectingTopLeft
= updateTopLeft
;
7417 m_selectingBottomRight
= updateBottomRight
;
7421 // ------ functions to get/send data (see also public functions)
7424 bool wxGrid::GetModelValues()
7426 // Hide the editor, so it won't hide a changed value.
7427 HideCellEditControl();
7431 // all we need to do is repaint the grid
7433 m_gridWin
->Refresh();
7440 bool wxGrid::SetModelValues()
7444 // Disable the editor, so it won't hide a changed value.
7445 // Do we also want to save the current value of the editor first?
7447 DisableCellEditControl();
7451 for ( row
= 0; row
< m_numRows
; row
++ )
7453 for ( col
= 0; col
< m_numCols
; col
++ )
7455 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
7465 // Note - this function only draws cells that are in the list of
7466 // exposed cells (usually set from the update region by
7467 // CalcExposedCells)
7469 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7471 if ( !m_numRows
|| !m_numCols
)
7474 int i
, numCells
= cells
.GetCount();
7475 int row
, col
, cell_rows
, cell_cols
;
7476 wxGridCellCoordsArray redrawCells
;
7478 for ( i
= numCells
- 1; i
>= 0; i
-- )
7480 row
= cells
[i
].GetRow();
7481 col
= cells
[i
].GetCol();
7482 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7484 // If this cell is part of a multicell block, find owner for repaint
7485 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7487 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
7488 bool marked
= false;
7489 for ( int j
= 0; j
< numCells
; j
++ )
7491 if ( cell
== cells
[j
] )
7500 int count
= redrawCells
.GetCount();
7501 for (int j
= 0; j
< count
; j
++)
7503 if ( cell
== redrawCells
[j
] )
7511 redrawCells
.Add( cell
);
7514 // don't bother drawing this cell
7518 // If this cell is empty, find cell to left that might want to overflow
7519 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7521 for ( int l
= 0; l
< cell_rows
; l
++ )
7523 // find a cell in this row to leave already marked for repaint
7525 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7526 if ((redrawCells
[k
].GetCol() < left
) &&
7527 (redrawCells
[k
].GetRow() == row
))
7529 left
= redrawCells
[k
].GetCol();
7533 left
= 0; // oh well
7535 for (int j
= col
- 1; j
>= left
; j
--)
7537 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7539 if (GetCellOverflow(row
+ l
, j
))
7541 wxGridCellCoords
cell(row
+ l
, j
);
7542 bool marked
= false;
7544 for (int k
= 0; k
< numCells
; k
++)
7546 if ( cell
== cells
[k
] )
7555 int count
= redrawCells
.GetCount();
7556 for (int k
= 0; k
< count
; k
++)
7558 if ( cell
== redrawCells
[k
] )
7565 redrawCells
.Add( cell
);
7574 DrawCell( dc
, cells
[i
] );
7577 numCells
= redrawCells
.GetCount();
7579 for ( i
= numCells
- 1; i
>= 0; i
-- )
7581 DrawCell( dc
, redrawCells
[i
] );
7585 void wxGrid::DrawGridSpace( wxDC
& dc
)
7588 m_gridWin
->GetClientSize( &cw
, &ch
);
7591 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7593 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
7594 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7596 if ( right
> rightCol
|| bottom
> bottomRow
)
7599 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7601 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxBRUSHSTYLE_SOLID
) );
7602 dc
.SetPen( *wxTRANSPARENT_PEN
);
7604 if ( right
> rightCol
)
7606 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7609 if ( bottom
> bottomRow
)
7611 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7616 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7618 int row
= coords
.GetRow();
7619 int col
= coords
.GetCol();
7621 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7624 // we draw the cell border ourselves
7625 #if !WXGRID_DRAW_LINES
7626 if ( m_gridLinesEnabled
)
7627 DrawCellBorder( dc
, coords
);
7630 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7632 bool isCurrent
= coords
== m_currentCellCoords
;
7634 wxRect rect
= CellToRect( row
, col
);
7636 // if the editor is shown, we should use it and not the renderer
7637 // Note: However, only if it is really _shown_, i.e. not hidden!
7638 if ( isCurrent
&& IsCellEditControlShown() )
7640 // NB: this "#if..." is temporary and fixes a problem where the
7641 // edit control is erased by this code after being rendered.
7642 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7643 // implicitly, causing this out-of order render.
7644 #if !defined(__WXMAC__)
7645 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7646 editor
->PaintBackground(rect
, attr
);
7652 // but all the rest is drawn by the cell renderer and hence may be customized
7653 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7654 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7661 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7663 // don't show highlight when the grid doesn't have focus
7667 int row
= m_currentCellCoords
.GetRow();
7668 int col
= m_currentCellCoords
.GetCol();
7670 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7673 wxRect rect
= CellToRect(row
, col
);
7675 // hmmm... what could we do here to show that the cell is disabled?
7676 // for now, I just draw a thinner border than for the other ones, but
7677 // it doesn't look really good
7679 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7683 // The center of the drawn line is where the position/width/height of
7684 // the rectangle is actually at (on wxMSW at least), so the
7685 // size of the rectangle is reduced to compensate for the thickness of
7686 // the line. If this is too strange on non-wxMSW platforms then
7687 // please #ifdef this appropriately.
7688 rect
.x
+= penWidth
/ 2;
7689 rect
.y
+= penWidth
/ 2;
7690 rect
.width
-= penWidth
- 1;
7691 rect
.height
-= penWidth
- 1;
7693 // Now draw the rectangle
7694 // use the cellHighlightColour if the cell is inside a selection, this
7695 // will ensure the cell is always visible.
7696 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
: m_cellHighlightColour
, penWidth
, wxPENSTYLE_SOLID
));
7697 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7698 dc
.DrawRectangle(rect
);
7702 // VZ: my experiments with 3D borders...
7704 // how to properly set colours for arbitrary bg?
7705 wxCoord x1
= rect
.x
,
7707 x2
= rect
.x
+ rect
.width
- 1,
7708 y2
= rect
.y
+ rect
.height
- 1;
7710 dc
.SetPen(*wxWHITE_PEN
);
7711 dc
.DrawLine(x1
, y1
, x2
, y1
);
7712 dc
.DrawLine(x1
, y1
, x1
, y2
);
7714 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7715 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7717 dc
.SetPen(*wxBLACK_PEN
);
7718 dc
.DrawLine(x1
, y2
, x2
, y2
);
7719 dc
.DrawLine(x2
, y1
, x2
, y2
+ 1);
7723 wxPen
wxGrid::GetDefaultGridLinePen()
7725 return wxPen(GetGridLineColour(), 1, wxPENSTYLE_SOLID
);
7728 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
7730 return GetDefaultGridLinePen();
7733 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
7735 return GetDefaultGridLinePen();
7738 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7740 int row
= coords
.GetRow();
7741 int col
= coords
.GetCol();
7742 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7746 wxRect rect
= CellToRect( row
, col
);
7748 // right hand border
7749 dc
.SetPen( GetColGridLinePen(col
) );
7750 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7751 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7754 dc
.SetPen( GetRowGridLinePen(row
) );
7755 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7756 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7759 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7761 // This if block was previously in wxGrid::OnPaint but that doesn't
7762 // seem to get called under wxGTK - MB
7764 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7765 m_numRows
&& m_numCols
)
7767 m_currentCellCoords
.Set(0, 0);
7770 if ( IsCellEditControlShown() )
7772 // don't show highlight when the edit control is shown
7776 // if the active cell was repainted, repaint its highlight too because it
7777 // might have been damaged by the grid lines
7778 size_t count
= cells
.GetCount();
7779 for ( size_t n
= 0; n
< count
; n
++ )
7781 if ( cells
[n
] == m_currentCellCoords
)
7783 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7784 DrawCellHighlight(dc
, attr
);
7792 // TODO: remove this ???
7793 // This is used to redraw all grid lines e.g. when the grid line colour
7796 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7798 #if !WXGRID_DRAW_LINES
7802 if ( !m_gridLinesEnabled
|| !m_numRows
|| !m_numCols
)
7805 int top
, bottom
, left
, right
;
7807 #if 0 //#ifndef __WXGTK__
7811 m_gridWin
->GetClientSize(&cw
, &ch
);
7813 // virtual coords of visible area
7815 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7816 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7821 reg
.GetBox(x
, y
, w
, h
);
7822 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7823 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7827 m_gridWin
->GetClientSize(&cw
, &ch
);
7828 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7829 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7832 // avoid drawing grid lines past the last row and col
7834 right
= wxMin( right
, GetColRight(GetColAt( m_numCols
- 1 )) );
7835 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7837 // no gridlines inside multicells, clip them out
7838 int leftCol
= GetColPos( internalXToCol(left
) );
7839 int topRow
= internalYToRow(top
);
7840 int rightCol
= GetColPos( internalXToCol(right
) );
7841 int bottomRow
= internalYToRow(bottom
);
7843 wxRegion
clippedcells(0, 0, cw
, ch
);
7845 int i
, j
, cell_rows
, cell_cols
;
7848 for (j
=topRow
; j
<=bottomRow
; j
++)
7851 for (colPos
=leftCol
; colPos
<=rightCol
; colPos
++)
7853 i
= GetColAt( colPos
);
7855 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7856 if ((cell_rows
> 1) || (cell_cols
> 1))
7858 rect
= CellToRect(j
,i
);
7859 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7860 clippedcells
.Subtract(rect
);
7862 else if ((cell_rows
< 0) || (cell_cols
< 0))
7864 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7865 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7866 clippedcells
.Subtract(rect
);
7871 dc
.SetDeviceClippingRegion( clippedcells
);
7874 // horizontal grid lines
7876 // already declared above - int i;
7877 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7879 int bot
= GetRowBottom(i
) - 1;
7888 dc
.SetPen( GetRowGridLinePen(i
) );
7889 dc
.DrawLine( left
, bot
, right
, bot
);
7893 // vertical grid lines
7896 for ( colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
7898 i
= GetColAt( colPos
);
7900 int colRight
= GetColRight(i
);
7902 if (GetLayoutDirection() != wxLayout_RightToLeft
)
7906 if ( colRight
> right
)
7911 if ( colRight
>= left
)
7913 dc
.SetPen( GetColGridLinePen(i
) );
7914 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7918 dc
.DestroyClippingRegion();
7921 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
7927 size_t numLabels
= rows
.GetCount();
7929 for ( i
= 0; i
< numLabels
; i
++ )
7931 DrawRowLabel( dc
, rows
[i
] );
7935 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7937 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7942 int rowTop
= GetRowTop(row
),
7943 rowBottom
= GetRowBottom(row
) - 1;
7945 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxPENSTYLE_SOLID
) );
7946 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
7947 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7948 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7950 dc
.SetPen( *wxWHITE_PEN
);
7951 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7952 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
7954 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
7955 dc
.SetTextForeground( GetLabelTextColour() );
7956 dc
.SetFont( GetLabelFont() );
7959 GetRowLabelAlignment( &hAlign
, &vAlign
);
7962 rect
.SetY( GetRowTop(row
) + 2 );
7963 rect
.SetWidth( m_rowLabelWidth
- 4 );
7964 rect
.SetHeight( GetRowHeight(row
) - 4 );
7965 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7968 void wxGrid::SetUseNativeColLabels( bool native
)
7970 m_nativeColumnLabels
= native
;
7973 int height
= wxRendererNative::Get().GetHeaderButtonHeight( this );
7974 SetColLabelSize( height
);
7977 m_colLabelWin
->Refresh();
7980 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7986 size_t numLabels
= cols
.GetCount();
7988 for ( i
= 0; i
< numLabels
; i
++ )
7990 DrawColLabel( dc
, cols
[i
] );
7994 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7996 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7999 int colLeft
= GetColLeft(col
);
8003 if (m_nativeColumnLabels
)
8005 rect
.SetX( colLeft
);
8007 rect
.SetWidth( GetColWidth(col
));
8008 rect
.SetHeight( m_colLabelHeight
);
8010 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
8011 wxRendererNative::Get().DrawHeaderButton( win_dc
->GetWindow(), dc
, rect
, 0 );
8015 int colRight
= GetColRight(col
) - 1;
8017 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxPENSTYLE_SOLID
) );
8018 dc
.DrawLine( colRight
, 0, colRight
, m_colLabelHeight
- 1 );
8019 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
8020 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
8021 colRight
+ 1, m_colLabelHeight
- 1 );
8023 dc
.SetPen( *wxWHITE_PEN
);
8024 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
8025 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
8028 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
8029 dc
.SetTextForeground( GetLabelTextColour() );
8030 dc
.SetFont( GetLabelFont() );
8032 int hAlign
, vAlign
, orient
;
8033 GetColLabelAlignment( &hAlign
, &vAlign
);
8034 orient
= GetColLabelTextOrientation();
8036 rect
.SetX( colLeft
+ 2 );
8038 rect
.SetWidth( GetColWidth(col
) - 4 );
8039 rect
.SetHeight( m_colLabelHeight
- 4 );
8040 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
8043 void wxGrid::DrawTextRectangle( wxDC
& dc
,
8044 const wxString
& value
,
8048 int textOrientation
)
8050 wxArrayString lines
;
8052 StringToLines( value
, lines
);
8054 // Forward to new API.
8055 DrawTextRectangle( dc
,
8063 // VZ: this should be replaced with wxDC::DrawLabel() to which we just have to
8064 // add textOrientation support
8065 void wxGrid::DrawTextRectangle(wxDC
& dc
,
8066 const wxArrayString
& lines
,
8070 int textOrientation
)
8072 if ( lines
.empty() )
8075 wxDCClipper
clip(dc
, rect
);
8080 if ( textOrientation
== wxHORIZONTAL
)
8081 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
8083 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
8087 switch ( vertAlign
)
8089 case wxALIGN_BOTTOM
:
8090 if ( textOrientation
== wxHORIZONTAL
)
8091 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
8093 x
= rect
.x
+ rect
.width
- textWidth
;
8096 case wxALIGN_CENTRE
:
8097 if ( textOrientation
== wxHORIZONTAL
)
8098 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
8100 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
8105 if ( textOrientation
== wxHORIZONTAL
)
8112 // Align each line of a multi-line label
8113 size_t nLines
= lines
.GetCount();
8114 for ( size_t l
= 0; l
< nLines
; l
++ )
8116 const wxString
& line
= lines
[l
];
8120 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
8124 wxCoord lineWidth
= 0,
8126 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
8128 switch ( horizAlign
)
8131 if ( textOrientation
== wxHORIZONTAL
)
8132 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
8134 y
= rect
.y
+ lineWidth
+ 1;
8137 case wxALIGN_CENTRE
:
8138 if ( textOrientation
== wxHORIZONTAL
)
8139 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
8141 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
8146 if ( textOrientation
== wxHORIZONTAL
)
8149 y
= rect
.y
+ rect
.height
- 1;
8153 if ( textOrientation
== wxHORIZONTAL
)
8155 dc
.DrawText( line
, x
, y
);
8160 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
8166 // Split multi-line text up into an array of strings.
8167 // Any existing contents of the string array are preserved.
8169 // TODO: refactor wxTextFile::Read() and reuse the same code from here
8170 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
8174 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
8175 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
8177 while ( startPos
< (int)tVal
.length() )
8179 pos
= tVal
.Mid(startPos
).Find( eol
);
8184 else if ( pos
== 0 )
8186 lines
.Add( wxEmptyString
);
8190 lines
.Add( tVal
.Mid(startPos
, pos
) );
8193 startPos
+= pos
+ 1;
8196 if ( startPos
< (int)tVal
.length() )
8198 lines
.Add( tVal
.Mid( startPos
) );
8202 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
8203 const wxArrayString
& lines
,
8204 long *width
, long *height
) const
8208 wxCoord lineW
= 0, lineH
= 0;
8211 for ( i
= 0; i
< lines
.GetCount(); i
++ )
8213 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
8214 w
= wxMax( w
, lineW
);
8223 // ------ Batch processing.
8225 void wxGrid::EndBatch()
8227 if ( m_batchCount
> 0 )
8230 if ( !m_batchCount
)
8233 m_rowLabelWin
->Refresh();
8234 m_colLabelWin
->Refresh();
8235 m_cornerLabelWin
->Refresh();
8236 m_gridWin
->Refresh();
8241 // Use this, rather than wxWindow::Refresh(), to force an immediate
8242 // repainting of the grid. Has no effect if you are already inside a
8243 // BeginBatch / EndBatch block.
8245 void wxGrid::ForceRefresh()
8251 bool wxGrid::Enable(bool enable
)
8253 if ( !wxScrolledWindow::Enable(enable
) )
8256 // redraw in the new state
8257 m_gridWin
->Refresh();
8263 // ------ Edit control functions
8266 void wxGrid::EnableEditing( bool edit
)
8268 // TODO: improve this ?
8270 if ( edit
!= m_editable
)
8273 EnableCellEditControl(edit
);
8278 void wxGrid::EnableCellEditControl( bool enable
)
8283 if ( enable
!= m_cellEditCtrlEnabled
)
8287 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
8290 // this should be checked by the caller!
8291 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8293 // do it before ShowCellEditControl()
8294 m_cellEditCtrlEnabled
= enable
;
8296 ShowCellEditControl();
8300 //FIXME:add veto support
8301 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
8303 HideCellEditControl();
8304 SaveEditControlValue();
8306 // do it after HideCellEditControl()
8307 m_cellEditCtrlEnabled
= enable
;
8312 bool wxGrid::IsCurrentCellReadOnly() const
8315 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
8316 bool readonly
= attr
->IsReadOnly();
8322 bool wxGrid::CanEnableCellControl() const
8324 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
8325 !IsCurrentCellReadOnly();
8328 bool wxGrid::IsCellEditControlEnabled() const
8330 // the cell edit control might be disable for all cells or just for the
8331 // current one if it's read only
8332 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
8335 bool wxGrid::IsCellEditControlShown() const
8337 bool isShown
= false;
8339 if ( m_cellEditCtrlEnabled
)
8341 int row
= m_currentCellCoords
.GetRow();
8342 int col
= m_currentCellCoords
.GetCol();
8343 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8344 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
8349 if ( editor
->IsCreated() )
8351 isShown
= editor
->GetControl()->IsShown();
8361 void wxGrid::ShowCellEditControl()
8363 if ( IsCellEditControlEnabled() )
8365 if ( !IsVisible( m_currentCellCoords
, false ) )
8367 m_cellEditCtrlEnabled
= false;
8372 wxRect rect
= CellToRect( m_currentCellCoords
);
8373 int row
= m_currentCellCoords
.GetRow();
8374 int col
= m_currentCellCoords
.GetCol();
8376 // if this is part of a multicell, find owner (topleft)
8377 int cell_rows
, cell_cols
;
8378 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8379 if ( cell_rows
<= 0 || cell_cols
<= 0 )
8383 m_currentCellCoords
.SetRow( row
);
8384 m_currentCellCoords
.SetCol( col
);
8387 // erase the highlight and the cell contents because the editor
8388 // might not cover the entire cell
8389 wxClientDC
dc( m_gridWin
);
8391 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8392 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxBRUSHSTYLE_SOLID
));
8393 dc
.SetPen(*wxTRANSPARENT_PEN
);
8394 dc
.DrawRectangle(rect
);
8396 // convert to scrolled coords
8397 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8403 // cell is shifted by one pixel
8404 // However, don't allow x or y to become negative
8405 // since the SetSize() method interprets that as
8412 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8413 if ( !editor
->IsCreated() )
8415 editor
->Create(m_gridWin
, wxID_ANY
,
8416 new wxGridCellEditorEvtHandler(this, editor
));
8418 wxGridEditorCreatedEvent
evt(GetId(),
8419 wxEVT_GRID_EDITOR_CREATED
,
8423 editor
->GetControl());
8424 GetEventHandler()->ProcessEvent(evt
);
8427 // resize editor to overflow into righthand cells if allowed
8428 int maxWidth
= rect
.width
;
8429 wxString value
= GetCellValue(row
, col
);
8430 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
8433 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
8434 if (maxWidth
< rect
.width
)
8435 maxWidth
= rect
.width
;
8438 int client_right
= m_gridWin
->GetClientSize().GetWidth();
8439 if (rect
.x
+ maxWidth
> client_right
)
8440 maxWidth
= client_right
- rect
.x
;
8442 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
8444 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8445 // may have changed earlier
8446 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
8449 GetCellSize( row
, i
, &c_rows
, &c_cols
);
8451 // looks weird going over a multicell
8452 if (m_table
->IsEmptyCell( row
, i
) &&
8453 (rect
.width
< maxWidth
) && (c_rows
== 1))
8455 rect
.width
+= GetColWidth( i
);
8461 if (rect
.GetRight() > client_right
)
8462 rect
.SetRight( client_right
- 1 );
8465 editor
->SetCellAttr( attr
);
8466 editor
->SetSize( rect
);
8468 editor
->GetControl()->Move(
8469 editor
->GetControl()->GetPosition().x
+ nXMove
,
8470 editor
->GetControl()->GetPosition().y
);
8471 editor
->Show( true, attr
);
8473 // recalc dimensions in case we need to
8474 // expand the scrolled window to account for editor
8477 editor
->BeginEdit(row
, col
, this);
8478 editor
->SetCellAttr(NULL
);
8486 void wxGrid::HideCellEditControl()
8488 if ( IsCellEditControlEnabled() )
8490 int row
= m_currentCellCoords
.GetRow();
8491 int col
= m_currentCellCoords
.GetCol();
8493 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8494 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
8495 editor
->Show( false );
8499 m_gridWin
->SetFocus();
8501 // refresh whole row to the right
8502 wxRect
rect( CellToRect(row
, col
) );
8503 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8504 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8507 // ensure that the pixels under the focus ring get refreshed as well
8508 rect
.Inflate(10, 10);
8511 m_gridWin
->Refresh( false, &rect
);
8515 void wxGrid::SaveEditControlValue()
8517 if ( IsCellEditControlEnabled() )
8519 int row
= m_currentCellCoords
.GetRow();
8520 int col
= m_currentCellCoords
.GetCol();
8522 wxString oldval
= GetCellValue(row
, col
);
8524 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8525 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8526 bool changed
= editor
->EndEdit(row
, col
, this);
8533 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
8534 m_currentCellCoords
.GetRow(),
8535 m_currentCellCoords
.GetCol() ) < 0 )
8537 // Event has been vetoed, set the data back.
8538 SetCellValue(row
, col
, oldval
);
8545 // ------ Grid location functions
8546 // Note that all of these functions work with the logical coordinates of
8547 // grid cells and labels so you will need to convert from device
8548 // coordinates for mouse events etc.
8551 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
) const
8553 int row
= YToRow(y
);
8554 int col
= XToCol(x
);
8556 if ( row
== -1 || col
== -1 )
8558 coords
= wxGridNoCellCoords
;
8562 coords
.Set( row
, col
);
8566 // Internal Helper function for computing row or column from some
8567 // (unscrolled) coordinate value, using either
8568 // m_defaultRowHeight/m_defaultColWidth or binary search on array
8569 // of m_rowBottoms/m_ColRights to speed up the search!
8571 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
8572 const wxArrayInt
& BorderArray
, int nMax
,
8576 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
8581 size_t i_max
= coord
/ defaultDist
,
8584 if (BorderArray
.IsEmpty())
8586 if ((int) i_max
< nMax
)
8588 return clipToMinMax
? nMax
- 1 : -1;
8591 if ( i_max
>= BorderArray
.GetCount())
8593 i_max
= BorderArray
.GetCount() - 1;
8597 if ( coord
>= BorderArray
[i_max
])
8601 i_max
= coord
/ minDist
;
8603 i_max
= BorderArray
.GetCount() - 1;
8606 if ( i_max
>= BorderArray
.GetCount())
8607 i_max
= BorderArray
.GetCount() - 1;
8610 if ( coord
>= BorderArray
[i_max
])
8611 return clipToMinMax
? (int)i_max
: -1;
8612 if ( coord
< BorderArray
[0] )
8615 while ( i_max
- i_min
> 0 )
8617 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
8618 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8619 if (coord
>= BorderArray
[ i_max
- 1])
8623 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
8624 if (coord
< BorderArray
[median
])
8633 int wxGrid::YToRow( int y
) const
8635 return CoordToRowOrCol(y
, m_defaultRowHeight
,
8636 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8639 int wxGrid::XToCol( int x
, bool clipToMinMax
) const
8642 return clipToMinMax
&& (m_numCols
> 0) ? GetColAt( 0 ) : -1;
8644 wxASSERT_MSG(m_defaultColWidth
> 0, wxT("Default column width can not be zero"));
8646 int maxPos
= x
/ m_defaultColWidth
;
8649 if (m_colRights
.IsEmpty())
8651 if(maxPos
< m_numCols
)
8652 return GetColAt( maxPos
);
8653 return clipToMinMax
? GetColAt( m_numCols
- 1 ) : -1;
8656 if ( maxPos
>= m_numCols
)
8657 maxPos
= m_numCols
- 1;
8660 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8663 if (m_minAcceptableColWidth
)
8664 maxPos
= x
/ m_minAcceptableColWidth
;
8666 maxPos
= m_numCols
- 1;
8668 if ( maxPos
>= m_numCols
)
8669 maxPos
= m_numCols
- 1;
8672 //X is beyond the last column
8673 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8674 return clipToMinMax
? GetColAt( maxPos
) : -1;
8676 //X is before the first column
8677 if ( x
< m_colRights
[GetColAt( 0 )] )
8678 return GetColAt( 0 );
8680 //Perform a binary search
8681 while ( maxPos
- minPos
> 0 )
8683 wxCHECK_MSG(m_colRights
[GetColAt( minPos
)] <= x
&& x
< m_colRights
[GetColAt( maxPos
)],
8684 0, _T("wxGrid: internal error in XToCol"));
8686 if (x
>= m_colRights
[GetColAt( maxPos
- 1 )])
8687 return GetColAt( maxPos
);
8690 int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
8691 if (x
< m_colRights
[GetColAt( median
)])
8696 return GetColAt( maxPos
);
8699 // return the row number that that the y coord is near
8700 // the edge of, or -1 if not near an edge.
8701 // coords can only possibly be near an edge if
8702 // (a) the row/column is large enough to still allow for an "inner" area
8703 // that is _not_ nead the edge (i.e., if the height/width is smaller
8704 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8707 // (b) resizing rows/columns (the thing for which edge detection is
8708 // relevant at all) is enabled.
8710 int wxGrid::YToEdgeOfRow( int y
) const
8713 i
= internalYToRow(y
);
8715 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragRowSize() )
8717 // We know that we are in row i, test whether we are
8718 // close enough to lower or upper border, respectively.
8719 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8721 else if ( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8728 // return the col number that that the x coord is near the edge of, or
8729 // -1 if not near an edge
8730 // See comment at YToEdgeOfRow for conditions on edge detection.
8732 int wxGrid::XToEdgeOfCol( int x
) const
8735 i
= internalXToCol(x
);
8737 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragColSize() )
8739 // We know that we are in column i; test whether we are
8740 // close enough to right or left border, respectively.
8741 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8743 else if ( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8750 wxRect
wxGrid::CellToRect( int row
, int col
) const
8752 wxRect
rect( -1, -1, -1, -1 );
8754 if ( row
>= 0 && row
< m_numRows
&&
8755 col
>= 0 && col
< m_numCols
)
8757 int i
, cell_rows
, cell_cols
;
8758 rect
.width
= rect
.height
= 0;
8759 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8760 // if negative then find multicell owner
8765 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8767 rect
.x
= GetColLeft(col
);
8768 rect
.y
= GetRowTop(row
);
8769 for (i
=col
; i
< col
+ cell_cols
; i
++)
8770 rect
.width
+= GetColWidth(i
);
8771 for (i
=row
; i
< row
+ cell_rows
; i
++)
8772 rect
.height
+= GetRowHeight(i
);
8775 // if grid lines are enabled, then the area of the cell is a bit smaller
8776 if (m_gridLinesEnabled
)
8785 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
8787 // get the cell rectangle in logical coords
8789 wxRect
r( CellToRect( row
, col
) );
8791 // convert to device coords
8793 int left
, top
, right
, bottom
;
8794 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8795 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8797 // check against the client area of the grid window
8799 m_gridWin
->GetClientSize( &cw
, &ch
);
8801 if ( wholeCellVisible
)
8803 // is the cell wholly visible ?
8804 return ( left
>= 0 && right
<= cw
&&
8805 top
>= 0 && bottom
<= ch
);
8809 // is the cell partly visible ?
8811 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8812 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8816 // make the specified cell location visible by doing a minimal amount
8819 void wxGrid::MakeCellVisible( int row
, int col
)
8822 int xpos
= -1, ypos
= -1;
8824 if ( row
>= 0 && row
< m_numRows
&&
8825 col
>= 0 && col
< m_numCols
)
8827 // get the cell rectangle in logical coords
8828 wxRect
r( CellToRect( row
, col
) );
8830 // convert to device coords
8831 int left
, top
, right
, bottom
;
8832 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8833 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8836 m_gridWin
->GetClientSize( &cw
, &ch
);
8842 else if ( bottom
> ch
)
8844 int h
= r
.GetHeight();
8846 for ( i
= row
- 1; i
>= 0; i
-- )
8848 int rowHeight
= GetRowHeight(i
);
8849 if ( h
+ rowHeight
> ch
)
8856 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8857 // have rounding errors (this is important, because if we do,
8858 // we might not scroll at all and some cells won't be redrawn)
8860 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
8861 // so just add a full scroll unit...
8862 ypos
+= m_scrollLineY
;
8865 // special handling for wide cells - show always left part of the cell!
8866 // Otherwise, e.g. when stepping from row to row, it would jump between
8867 // left and right part of the cell on every step!
8869 if ( left
< 0 || (right
- left
) >= cw
)
8873 else if ( right
> cw
)
8875 // position the view so that the cell is on the right
8877 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8878 xpos
= x0
+ (right
- cw
);
8880 // see comment for ypos above
8881 xpos
+= m_scrollLineX
;
8884 if ( xpos
!= -1 || ypos
!= -1 )
8887 xpos
/= m_scrollLineX
;
8889 ypos
/= m_scrollLineY
;
8890 Scroll( xpos
, ypos
);
8897 // ------ Grid cursor movement functions
8900 bool wxGrid::MoveCursorUp( bool expandSelection
)
8902 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8903 m_currentCellCoords
.GetRow() >= 0 )
8905 if ( expandSelection
)
8907 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8908 m_selectingKeyboard
= m_currentCellCoords
;
8909 if ( m_selectingKeyboard
.GetRow() > 0 )
8911 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8912 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8913 m_selectingKeyboard
.GetCol() );
8914 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8917 else if ( m_currentCellCoords
.GetRow() > 0 )
8919 int row
= m_currentCellCoords
.GetRow() - 1;
8920 int col
= m_currentCellCoords
.GetCol();
8922 MakeCellVisible( row
, col
);
8923 SetCurrentCell( row
, col
);
8934 bool wxGrid::MoveCursorDown( bool expandSelection
)
8936 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8937 m_currentCellCoords
.GetRow() < m_numRows
)
8939 if ( expandSelection
)
8941 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8942 m_selectingKeyboard
= m_currentCellCoords
;
8943 if ( m_selectingKeyboard
.GetRow() < m_numRows
- 1 )
8945 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8946 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8947 m_selectingKeyboard
.GetCol() );
8948 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8951 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8953 int row
= m_currentCellCoords
.GetRow() + 1;
8954 int col
= m_currentCellCoords
.GetCol();
8956 MakeCellVisible( row
, col
);
8957 SetCurrentCell( row
, col
);
8968 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8970 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8971 m_currentCellCoords
.GetCol() >= 0 )
8973 if ( expandSelection
)
8975 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8976 m_selectingKeyboard
= m_currentCellCoords
;
8977 if ( m_selectingKeyboard
.GetCol() > 0 )
8979 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8980 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8981 m_selectingKeyboard
.GetCol() );
8982 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8985 else if ( GetColPos( m_currentCellCoords
.GetCol() ) > 0 )
8987 int row
= m_currentCellCoords
.GetRow();
8988 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) - 1 );
8991 MakeCellVisible( row
, col
);
8992 SetCurrentCell( row
, col
);
9003 bool wxGrid::MoveCursorRight( bool expandSelection
)
9005 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
9006 m_currentCellCoords
.GetCol() < m_numCols
)
9008 if ( expandSelection
)
9010 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
9011 m_selectingKeyboard
= m_currentCellCoords
;
9012 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
9014 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
9015 MakeCellVisible( m_selectingKeyboard
.GetRow(),
9016 m_selectingKeyboard
.GetCol() );
9017 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9020 else if ( GetColPos( m_currentCellCoords
.GetCol() ) < m_numCols
- 1 )
9022 int row
= m_currentCellCoords
.GetRow();
9023 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) + 1 );
9026 MakeCellVisible( row
, col
);
9027 SetCurrentCell( row
, col
);
9038 bool wxGrid::MovePageUp()
9040 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9043 int row
= m_currentCellCoords
.GetRow();
9047 m_gridWin
->GetClientSize( &cw
, &ch
);
9049 int y
= GetRowTop(row
);
9050 int newRow
= internalYToRow( y
- ch
+ 1 );
9052 if ( newRow
== row
)
9054 // row > 0, so newRow can never be less than 0 here.
9058 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
9059 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
9067 bool wxGrid::MovePageDown()
9069 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9072 int row
= m_currentCellCoords
.GetRow();
9073 if ( (row
+ 1) < m_numRows
)
9076 m_gridWin
->GetClientSize( &cw
, &ch
);
9078 int y
= GetRowTop(row
);
9079 int newRow
= internalYToRow( y
+ ch
);
9080 if ( newRow
== row
)
9082 // row < m_numRows, so newRow can't overflow here.
9086 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
9087 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
9095 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
9098 m_currentCellCoords
!= wxGridNoCellCoords
&&
9099 m_currentCellCoords
.GetRow() > 0 )
9101 int row
= m_currentCellCoords
.GetRow();
9102 int col
= m_currentCellCoords
.GetCol();
9104 if ( m_table
->IsEmptyCell(row
, col
) )
9106 // starting in an empty cell: find the next block of
9112 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9116 else if ( m_table
->IsEmptyCell(row
- 1, col
) )
9118 // starting at the top of a block: find the next block
9124 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9130 // starting within a block: find the top of the block
9135 if ( m_table
->IsEmptyCell(row
, col
) )
9143 MakeCellVisible( row
, col
);
9144 if ( expandSelection
)
9146 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9147 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9152 SetCurrentCell( row
, col
);
9161 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
9164 m_currentCellCoords
!= wxGridNoCellCoords
&&
9165 m_currentCellCoords
.GetRow() < m_numRows
- 1 )
9167 int row
= m_currentCellCoords
.GetRow();
9168 int col
= m_currentCellCoords
.GetCol();
9170 if ( m_table
->IsEmptyCell(row
, col
) )
9172 // starting in an empty cell: find the next block of
9175 while ( row
< m_numRows
- 1 )
9178 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9182 else if ( m_table
->IsEmptyCell(row
+ 1, col
) )
9184 // starting at the bottom of a block: find the next block
9187 while ( row
< m_numRows
- 1 )
9190 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9196 // starting within a block: find the bottom of the block
9198 while ( row
< m_numRows
- 1 )
9201 if ( m_table
->IsEmptyCell(row
, col
) )
9209 MakeCellVisible( row
, col
);
9210 if ( expandSelection
)
9212 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9213 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9218 SetCurrentCell( row
, col
);
9227 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
9230 m_currentCellCoords
!= wxGridNoCellCoords
&&
9231 m_currentCellCoords
.GetCol() > 0 )
9233 int row
= m_currentCellCoords
.GetRow();
9234 int col
= m_currentCellCoords
.GetCol();
9236 if ( m_table
->IsEmptyCell(row
, col
) )
9238 // starting in an empty cell: find the next block of
9244 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9248 else if ( m_table
->IsEmptyCell(row
, col
- 1) )
9250 // starting at the left of a block: find the next block
9256 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9262 // starting within a block: find the left of the block
9267 if ( m_table
->IsEmptyCell(row
, col
) )
9275 MakeCellVisible( row
, col
);
9276 if ( expandSelection
)
9278 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9279 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9284 SetCurrentCell( row
, col
);
9293 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
9296 m_currentCellCoords
!= wxGridNoCellCoords
&&
9297 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
9299 int row
= m_currentCellCoords
.GetRow();
9300 int col
= m_currentCellCoords
.GetCol();
9302 if ( m_table
->IsEmptyCell(row
, col
) )
9304 // starting in an empty cell: find the next block of
9307 while ( col
< m_numCols
- 1 )
9310 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9314 else if ( m_table
->IsEmptyCell(row
, col
+ 1) )
9316 // starting at the right of a block: find the next block
9319 while ( col
< m_numCols
- 1 )
9322 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9328 // starting within a block: find the right of the block
9330 while ( col
< m_numCols
- 1 )
9333 if ( m_table
->IsEmptyCell(row
, col
) )
9341 MakeCellVisible( row
, col
);
9342 if ( expandSelection
)
9344 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9345 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9350 SetCurrentCell( row
, col
);
9360 // ------ Label values and formatting
9363 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
9366 *horiz
= m_rowLabelHorizAlign
;
9368 *vert
= m_rowLabelVertAlign
;
9371 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
9374 *horiz
= m_colLabelHorizAlign
;
9376 *vert
= m_colLabelVertAlign
;
9379 int wxGrid::GetColLabelTextOrientation() const
9381 return m_colLabelTextOrientation
;
9384 wxString
wxGrid::GetRowLabelValue( int row
) const
9388 return m_table
->GetRowLabelValue( row
);
9398 wxString
wxGrid::GetColLabelValue( int col
) const
9402 return m_table
->GetColLabelValue( col
);
9412 void wxGrid::SetRowLabelSize( int width
)
9414 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
9416 if ( width
== wxGRID_AUTOSIZE
)
9418 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
9421 if ( width
!= m_rowLabelWidth
)
9425 m_rowLabelWin
->Show( false );
9426 m_cornerLabelWin
->Show( false );
9428 else if ( m_rowLabelWidth
== 0 )
9430 m_rowLabelWin
->Show( true );
9431 if ( m_colLabelHeight
> 0 )
9432 m_cornerLabelWin
->Show( true );
9435 m_rowLabelWidth
= width
;
9437 wxScrolledWindow::Refresh( true );
9441 void wxGrid::SetColLabelSize( int height
)
9443 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
9445 if ( height
== wxGRID_AUTOSIZE
)
9447 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
9450 if ( height
!= m_colLabelHeight
)
9454 m_colLabelWin
->Show( false );
9455 m_cornerLabelWin
->Show( false );
9457 else if ( m_colLabelHeight
== 0 )
9459 m_colLabelWin
->Show( true );
9460 if ( m_rowLabelWidth
> 0 )
9461 m_cornerLabelWin
->Show( true );
9464 m_colLabelHeight
= height
;
9466 wxScrolledWindow::Refresh( true );
9470 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
9472 if ( m_labelBackgroundColour
!= colour
)
9474 m_labelBackgroundColour
= colour
;
9475 m_rowLabelWin
->SetBackgroundColour( colour
);
9476 m_colLabelWin
->SetBackgroundColour( colour
);
9477 m_cornerLabelWin
->SetBackgroundColour( colour
);
9479 if ( !GetBatchCount() )
9481 m_rowLabelWin
->Refresh();
9482 m_colLabelWin
->Refresh();
9483 m_cornerLabelWin
->Refresh();
9488 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
9490 if ( m_labelTextColour
!= colour
)
9492 m_labelTextColour
= colour
;
9493 if ( !GetBatchCount() )
9495 m_rowLabelWin
->Refresh();
9496 m_colLabelWin
->Refresh();
9501 void wxGrid::SetLabelFont( const wxFont
& font
)
9504 if ( !GetBatchCount() )
9506 m_rowLabelWin
->Refresh();
9507 m_colLabelWin
->Refresh();
9511 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
9513 // allow old (incorrect) defs to be used
9516 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9517 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9518 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9523 case wxTOP
: vert
= wxALIGN_TOP
; break;
9524 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9525 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9528 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9530 m_rowLabelHorizAlign
= horiz
;
9533 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9535 m_rowLabelVertAlign
= vert
;
9538 if ( !GetBatchCount() )
9540 m_rowLabelWin
->Refresh();
9544 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
9546 // allow old (incorrect) defs to be used
9549 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9550 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9551 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9556 case wxTOP
: vert
= wxALIGN_TOP
; break;
9557 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9558 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9561 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9563 m_colLabelHorizAlign
= horiz
;
9566 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9568 m_colLabelVertAlign
= vert
;
9571 if ( !GetBatchCount() )
9573 m_colLabelWin
->Refresh();
9577 // Note: under MSW, the default column label font must be changed because it
9578 // does not support vertical printing
9580 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9581 // pGrid->SetLabelFont(font);
9582 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9584 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9586 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9587 m_colLabelTextOrientation
= textOrientation
;
9589 if ( !GetBatchCount() )
9590 m_colLabelWin
->Refresh();
9593 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9597 m_table
->SetRowLabelValue( row
, s
);
9598 if ( !GetBatchCount() )
9600 wxRect rect
= CellToRect( row
, 0 );
9601 if ( rect
.height
> 0 )
9603 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9605 rect
.width
= m_rowLabelWidth
;
9606 m_rowLabelWin
->Refresh( true, &rect
);
9612 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9616 m_table
->SetColLabelValue( col
, s
);
9617 if ( !GetBatchCount() )
9619 wxRect rect
= CellToRect( 0, col
);
9620 if ( rect
.width
> 0 )
9622 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9624 rect
.height
= m_colLabelHeight
;
9625 m_colLabelWin
->Refresh( true, &rect
);
9631 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9633 if ( m_gridLineColour
!= colour
)
9635 m_gridLineColour
= colour
;
9637 wxClientDC
dc( m_gridWin
);
9639 DrawAllGridLines( dc
, wxRegion() );
9643 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9645 if ( m_cellHighlightColour
!= colour
)
9647 m_cellHighlightColour
= colour
;
9649 wxClientDC
dc( m_gridWin
);
9651 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9652 DrawCellHighlight(dc
, attr
);
9657 void wxGrid::SetCellHighlightPenWidth(int width
)
9659 if (m_cellHighlightPenWidth
!= width
)
9661 m_cellHighlightPenWidth
= width
;
9663 // Just redrawing the cell highlight is not enough since that won't
9664 // make any visible change if the the thickness is getting smaller.
9665 int row
= m_currentCellCoords
.GetRow();
9666 int col
= m_currentCellCoords
.GetCol();
9667 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9670 wxRect rect
= CellToRect(row
, col
);
9671 m_gridWin
->Refresh(true, &rect
);
9675 void wxGrid::SetCellHighlightROPenWidth(int width
)
9677 if (m_cellHighlightROPenWidth
!= width
)
9679 m_cellHighlightROPenWidth
= width
;
9681 // Just redrawing the cell highlight is not enough since that won't
9682 // make any visible change if the the thickness is getting smaller.
9683 int row
= m_currentCellCoords
.GetRow();
9684 int col
= m_currentCellCoords
.GetCol();
9685 if ( row
== -1 || col
== -1 ||
9686 GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9689 wxRect rect
= CellToRect(row
, col
);
9690 m_gridWin
->Refresh(true, &rect
);
9694 void wxGrid::EnableGridLines( bool enable
)
9696 if ( enable
!= m_gridLinesEnabled
)
9698 m_gridLinesEnabled
= enable
;
9700 if ( !GetBatchCount() )
9704 wxClientDC
dc( m_gridWin
);
9706 DrawAllGridLines( dc
, wxRegion() );
9710 m_gridWin
->Refresh();
9716 int wxGrid::GetDefaultRowSize() const
9718 return m_defaultRowHeight
;
9721 int wxGrid::GetRowSize( int row
) const
9723 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9725 return GetRowHeight(row
);
9728 int wxGrid::GetDefaultColSize() const
9730 return m_defaultColWidth
;
9733 int wxGrid::GetColSize( int col
) const
9735 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9737 return GetColWidth(col
);
9740 // ============================================================================
9741 // access to the grid attributes: each of them has a default value in the grid
9742 // itself and may be overidden on a per-cell basis
9743 // ============================================================================
9745 // ----------------------------------------------------------------------------
9746 // setting default attributes
9747 // ----------------------------------------------------------------------------
9749 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9751 m_defaultCellAttr
->SetBackgroundColour(col
);
9753 m_gridWin
->SetBackgroundColour(col
);
9757 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9759 m_defaultCellAttr
->SetTextColour(col
);
9762 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9764 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9767 void wxGrid::SetDefaultCellOverflow( bool allow
)
9769 m_defaultCellAttr
->SetOverflow(allow
);
9772 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9774 m_defaultCellAttr
->SetFont(font
);
9777 // For editors and renderers the type registry takes precedence over the
9778 // default attr, so we need to register the new editor/renderer for the string
9779 // data type in order to make setting a default editor/renderer appear to
9782 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9784 RegisterDataType(wxGRID_VALUE_STRING
,
9786 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9789 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9791 RegisterDataType(wxGRID_VALUE_STRING
,
9792 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9796 // ----------------------------------------------------------------------------
9797 // access to the default attributes
9798 // ----------------------------------------------------------------------------
9800 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
9802 return m_defaultCellAttr
->GetBackgroundColour();
9805 wxColour
wxGrid::GetDefaultCellTextColour() const
9807 return m_defaultCellAttr
->GetTextColour();
9810 wxFont
wxGrid::GetDefaultCellFont() const
9812 return m_defaultCellAttr
->GetFont();
9815 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
9817 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9820 bool wxGrid::GetDefaultCellOverflow() const
9822 return m_defaultCellAttr
->GetOverflow();
9825 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9827 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9830 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9832 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9835 // ----------------------------------------------------------------------------
9836 // access to cell attributes
9837 // ----------------------------------------------------------------------------
9839 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
9841 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9842 wxColour colour
= attr
->GetBackgroundColour();
9848 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
9850 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9851 wxColour colour
= attr
->GetTextColour();
9857 wxFont
wxGrid::GetCellFont( int row
, int col
) const
9859 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9860 wxFont font
= attr
->GetFont();
9866 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
9868 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9869 attr
->GetAlignment(horiz
, vert
);
9873 bool wxGrid::GetCellOverflow( int row
, int col
) const
9875 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9876 bool allow
= attr
->GetOverflow();
9882 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
9884 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9885 attr
->GetSize( num_rows
, num_cols
);
9889 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
9891 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9892 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9898 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
9900 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9901 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9907 bool wxGrid::IsReadOnly(int row
, int col
) const
9909 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9910 bool isReadOnly
= attr
->IsReadOnly();
9916 // ----------------------------------------------------------------------------
9917 // attribute support: cache, automatic provider creation, ...
9918 // ----------------------------------------------------------------------------
9920 bool wxGrid::CanHaveAttributes() const
9927 return m_table
->CanHaveAttributes();
9930 void wxGrid::ClearAttrCache()
9932 if ( m_attrCache
.row
!= -1 )
9934 wxSafeDecRef(m_attrCache
.attr
);
9935 m_attrCache
.attr
= NULL
;
9936 m_attrCache
.row
= -1;
9940 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9944 wxGrid
*self
= (wxGrid
*)this; // const_cast
9946 self
->ClearAttrCache();
9947 self
->m_attrCache
.row
= row
;
9948 self
->m_attrCache
.col
= col
;
9949 self
->m_attrCache
.attr
= attr
;
9954 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9956 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9958 *attr
= m_attrCache
.attr
;
9959 wxSafeIncRef(m_attrCache
.attr
);
9961 #ifdef DEBUG_ATTR_CACHE
9962 gs_nAttrCacheHits
++;
9969 #ifdef DEBUG_ATTR_CACHE
9970 gs_nAttrCacheMisses
++;
9977 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9979 wxGridCellAttr
*attr
= NULL
;
9980 // Additional test to avoid looking at the cache e.g. for
9981 // wxNoCellCoords, as this will confuse memory management.
9984 if ( !LookupAttr(row
, col
, &attr
) )
9986 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9987 : (wxGridCellAttr
*)NULL
;
9988 CacheAttr(row
, col
, attr
);
9994 attr
->SetDefAttr(m_defaultCellAttr
);
9998 attr
= m_defaultCellAttr
;
10005 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
10007 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
10008 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
10010 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
10011 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
10013 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
10016 attr
= new wxGridCellAttr(m_defaultCellAttr
);
10018 // artificially inc the ref count to match DecRef() in caller
10020 m_table
->SetAttr(attr
, row
, col
);
10026 // ----------------------------------------------------------------------------
10027 // setting column attributes (wrappers around SetColAttr)
10028 // ----------------------------------------------------------------------------
10030 void wxGrid::SetColFormatBool(int col
)
10032 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
10035 void wxGrid::SetColFormatNumber(int col
)
10037 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
10040 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
10042 wxString typeName
= wxGRID_VALUE_FLOAT
;
10043 if ( (width
!= -1) || (precision
!= -1) )
10045 typeName
<< _T(':') << width
<< _T(',') << precision
;
10048 SetColFormatCustom(col
, typeName
);
10051 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
10053 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
10055 attr
= new wxGridCellAttr
;
10056 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
10057 attr
->SetRenderer(renderer
);
10058 wxGridCellEditor
*editor
= GetDefaultEditorForType(typeName
);
10059 attr
->SetEditor(editor
);
10061 SetColAttr(col
, attr
);
10065 // ----------------------------------------------------------------------------
10066 // setting cell attributes: this is forwarded to the table
10067 // ----------------------------------------------------------------------------
10069 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
10071 if ( CanHaveAttributes() )
10073 m_table
->SetAttr(attr
, row
, col
);
10078 wxSafeDecRef(attr
);
10082 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
10084 if ( CanHaveAttributes() )
10086 m_table
->SetRowAttr(attr
, row
);
10091 wxSafeDecRef(attr
);
10095 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
10097 if ( CanHaveAttributes() )
10099 m_table
->SetColAttr(attr
, col
);
10104 wxSafeDecRef(attr
);
10108 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
10110 if ( CanHaveAttributes() )
10112 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10113 attr
->SetBackgroundColour(colour
);
10118 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
10120 if ( CanHaveAttributes() )
10122 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10123 attr
->SetTextColour(colour
);
10128 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
10130 if ( CanHaveAttributes() )
10132 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10133 attr
->SetFont(font
);
10138 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
10140 if ( CanHaveAttributes() )
10142 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10143 attr
->SetAlignment(horiz
, vert
);
10148 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
10150 if ( CanHaveAttributes() )
10152 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10153 attr
->SetOverflow(allow
);
10158 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
10160 if ( CanHaveAttributes() )
10162 int cell_rows
, cell_cols
;
10164 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10165 attr
->GetSize(&cell_rows
, &cell_cols
);
10166 attr
->SetSize(num_rows
, num_cols
);
10169 // Cannot set the size of a cell to 0 or negative values
10170 // While it is perfectly legal to do that, this function cannot
10171 // handle all the possibilies, do it by hand by getting the CellAttr.
10172 // You can only set the size of a cell to 1,1 or greater with this fn
10173 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
10174 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10175 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
10176 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10178 // if this was already a multicell then "turn off" the other cells first
10179 if ((cell_rows
> 1) || (cell_rows
> 1))
10182 for (j
=row
; j
< row
+ cell_rows
; j
++)
10184 for (i
=col
; i
< col
+ cell_cols
; i
++)
10186 if ((i
!= col
) || (j
!= row
))
10188 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10189 attr_stub
->SetSize( 1, 1 );
10190 attr_stub
->DecRef();
10196 // mark the cells that will be covered by this cell to
10197 // negative or zero values to point back at this cell
10198 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
10201 for (j
=row
; j
< row
+ num_rows
; j
++)
10203 for (i
=col
; i
< col
+ num_cols
; i
++)
10205 if ((i
!= col
) || (j
!= row
))
10207 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10208 attr_stub
->SetSize( row
- j
, col
- i
);
10209 attr_stub
->DecRef();
10217 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
10219 if ( CanHaveAttributes() )
10221 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10222 attr
->SetRenderer(renderer
);
10227 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
10229 if ( CanHaveAttributes() )
10231 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10232 attr
->SetEditor(editor
);
10237 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
10239 if ( CanHaveAttributes() )
10241 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10242 attr
->SetReadOnly(isReadOnly
);
10247 // ----------------------------------------------------------------------------
10248 // Data type registration
10249 // ----------------------------------------------------------------------------
10251 void wxGrid::RegisterDataType(const wxString
& typeName
,
10252 wxGridCellRenderer
* renderer
,
10253 wxGridCellEditor
* editor
)
10255 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
10259 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
10261 wxString typeName
= m_table
->GetTypeName(row
, col
);
10262 return GetDefaultEditorForType(typeName
);
10265 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
10267 wxString typeName
= m_table
->GetTypeName(row
, col
);
10268 return GetDefaultRendererForType(typeName
);
10271 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
10273 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10274 if ( index
== wxNOT_FOUND
)
10276 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10281 return m_typeRegistry
->GetEditor(index
);
10284 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
10286 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10287 if ( index
== wxNOT_FOUND
)
10289 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10294 return m_typeRegistry
->GetRenderer(index
);
10297 // ----------------------------------------------------------------------------
10299 // ----------------------------------------------------------------------------
10301 void wxGrid::EnableDragRowSize( bool enable
)
10303 m_canDragRowSize
= enable
;
10306 void wxGrid::EnableDragColSize( bool enable
)
10308 m_canDragColSize
= enable
;
10311 void wxGrid::EnableDragGridSize( bool enable
)
10313 m_canDragGridSize
= enable
;
10316 void wxGrid::EnableDragCell( bool enable
)
10318 m_canDragCell
= enable
;
10321 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
10323 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
10325 if ( resizeExistingRows
)
10327 // since we are resizing all rows to the default row size,
10328 // we can simply clear the row heights and row bottoms
10329 // arrays (which also allows us to take advantage of
10330 // some speed optimisations)
10331 m_rowHeights
.Empty();
10332 m_rowBottoms
.Empty();
10333 if ( !GetBatchCount() )
10338 void wxGrid::SetRowSize( int row
, int height
)
10340 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
10342 // See comment in SetColSize
10343 if ( height
< GetRowMinimalAcceptableHeight())
10346 if ( m_rowHeights
.IsEmpty() )
10348 // need to really create the array
10352 int h
= wxMax( 0, height
);
10353 int diff
= h
- m_rowHeights
[row
];
10355 m_rowHeights
[row
] = h
;
10356 for ( int i
= row
; i
< m_numRows
; i
++ )
10358 m_rowBottoms
[i
] += diff
;
10361 if ( !GetBatchCount() )
10365 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
10367 // we dont allow zero default column width
10368 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
10370 if ( resizeExistingCols
)
10372 // since we are resizing all columns to the default column size,
10373 // we can simply clear the col widths and col rights
10374 // arrays (which also allows us to take advantage of
10375 // some speed optimisations)
10376 m_colWidths
.Empty();
10377 m_colRights
.Empty();
10378 if ( !GetBatchCount() )
10383 void wxGrid::SetColSize( int col
, int width
)
10385 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
10387 // should we check that it's bigger than GetColMinimalWidth(col) here?
10389 // No, because it is reasonable to assume the library user know's
10390 // what he is doing. However we should test against the weaker
10391 // constraint of minimalAcceptableWidth, as this breaks rendering
10393 // This test then fixes sf.net bug #645734
10395 if ( width
< GetColMinimalAcceptableWidth() )
10398 if ( m_colWidths
.IsEmpty() )
10400 // need to really create the array
10404 // if < 0 then calculate new width from label
10408 wxArrayString lines
;
10409 wxClientDC
dc(m_colLabelWin
);
10410 dc
.SetFont(GetLabelFont());
10411 StringToLines(GetColLabelValue(col
), lines
);
10412 GetTextBoxSize(dc
, lines
, &w
, &h
);
10416 int w
= wxMax( 0, width
);
10417 int diff
= w
- m_colWidths
[col
];
10418 m_colWidths
[col
] = w
;
10420 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
10422 m_colRights
[GetColAt(colPos
)] += diff
;
10425 if ( !GetBatchCount() )
10429 void wxGrid::SetColMinimalWidth( int col
, int width
)
10431 if (width
> GetColMinimalAcceptableWidth())
10433 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10434 m_colMinWidths
[key
] = width
;
10438 void wxGrid::SetRowMinimalHeight( int row
, int width
)
10440 if (width
> GetRowMinimalAcceptableHeight())
10442 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10443 m_rowMinHeights
[key
] = width
;
10447 int wxGrid::GetColMinimalWidth(int col
) const
10449 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10450 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
10452 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
10455 int wxGrid::GetRowMinimalHeight(int row
) const
10457 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10458 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
10460 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
10463 void wxGrid::SetColMinimalAcceptableWidth( int width
)
10465 // We do allow a width of 0 since this gives us
10466 // an easy way to temporarily hiding columns.
10468 m_minAcceptableColWidth
= width
;
10471 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
10473 // We do allow a height of 0 since this gives us
10474 // an easy way to temporarily hiding rows.
10476 m_minAcceptableRowHeight
= height
;
10479 int wxGrid::GetColMinimalAcceptableWidth() const
10481 return m_minAcceptableColWidth
;
10484 int wxGrid::GetRowMinimalAcceptableHeight() const
10486 return m_minAcceptableRowHeight
;
10489 // ----------------------------------------------------------------------------
10491 // ----------------------------------------------------------------------------
10494 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
10496 const bool column
= direction
== wxGRID_COLUMN
;
10498 wxClientDC
dc(m_gridWin
);
10500 // cancel editing of cell
10501 HideCellEditControl();
10502 SaveEditControlValue();
10504 // init both of them to avoid compiler warnings, even if we only need one
10512 wxCoord extent
, extentMax
= 0;
10513 int max
= column
? m_numRows
: m_numCols
;
10514 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
10521 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
10522 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
10525 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
10526 extent
= column
? size
.x
: size
.y
;
10527 if ( extent
> extentMax
)
10528 extentMax
= extent
;
10530 renderer
->DecRef();
10536 // now also compare with the column label extent
10538 dc
.SetFont( GetLabelFont() );
10542 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
10543 if ( GetColLabelTextOrientation() == wxVERTICAL
)
10547 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
10549 extent
= column
? w
: h
;
10550 if ( extent
> extentMax
)
10551 extentMax
= extent
;
10555 // empty column - give default extent (notice that if extentMax is less
10556 // than default extent but != 0, it's OK)
10557 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
10562 // leave some space around text
10570 // Ensure automatic width is not less than minimal width. See the
10571 // comment in SetColSize() for explanation of why this isn't done
10572 // in SetColSize().
10574 extentMax
= wxMax(extentMax
, GetColMinimalWidth(col
));
10576 SetColSize( col
, extentMax
);
10577 if ( !GetBatchCount() )
10580 m_gridWin
->GetClientSize( &cw
, &ch
);
10581 wxRect
rect ( CellToRect( 0, col
) );
10583 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10584 rect
.width
= cw
- rect
.x
;
10585 rect
.height
= m_colLabelHeight
;
10586 m_colLabelWin
->Refresh( true, &rect
);
10591 // Ensure automatic width is not less than minimal height. See the
10592 // comment in SetColSize() for explanation of why this isn't done
10593 // in SetRowSize().
10595 extentMax
= wxMax(extentMax
, GetRowMinimalHeight(row
));
10597 SetRowSize(row
, extentMax
);
10598 if ( !GetBatchCount() )
10601 m_gridWin
->GetClientSize( &cw
, &ch
);
10602 wxRect
rect( CellToRect( row
, 0 ) );
10604 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10605 rect
.width
= m_rowLabelWidth
;
10606 rect
.height
= ch
- rect
.y
;
10607 m_rowLabelWin
->Refresh( true, &rect
);
10614 SetColMinimalWidth(col
, extentMax
);
10616 SetRowMinimalHeight(row
, extentMax
);
10620 wxCoord
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
10622 // calculate size for the rows or columns?
10623 const bool calcRows
= direction
== wxGRID_ROW
;
10625 wxClientDC
dc(calcRows
? GetGridRowLabelWindow()
10626 : GetGridColLabelWindow());
10627 dc
.SetFont(GetLabelFont());
10629 // which dimension should we take into account for calculations?
10631 // for columns, the text can be only horizontal so it's easy but for rows
10632 // we also have to take into account the text orientation
10634 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
10636 wxArrayString lines
;
10637 wxCoord extentMax
= 0;
10639 const int numRowsOrCols
= calcRows
? m_numRows
: m_numCols
;
10640 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
10644 wxString label
= calcRows
? GetRowLabelValue(rowOrCol
)
10645 : GetColLabelValue(rowOrCol
);
10646 StringToLines(label
, lines
);
10649 GetTextBoxSize(dc
, lines
, &w
, &h
);
10651 const wxCoord extent
= useWidth
? w
: h
;
10652 if ( extent
> extentMax
)
10653 extentMax
= extent
;
10658 // empty column - give default extent (notice that if extentMax is less
10659 // than default extent but != 0, it's OK)
10660 extentMax
= calcRows
? GetDefaultRowLabelSize()
10661 : GetDefaultColLabelSize();
10664 // leave some space around text (taken from AutoSizeColOrRow)
10673 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10675 int width
= m_rowLabelWidth
;
10677 wxGridUpdateLocker locker
;
10679 locker
.Create(this);
10681 for ( int col
= 0; col
< m_numCols
; col
++ )
10684 AutoSizeColumn(col
, setAsMin
);
10686 width
+= GetColWidth(col
);
10692 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10694 int height
= m_colLabelHeight
;
10696 wxGridUpdateLocker locker
;
10698 locker
.Create(this);
10700 for ( int row
= 0; row
< m_numRows
; row
++ )
10703 AutoSizeRow(row
, setAsMin
);
10705 height
+= GetRowHeight(row
);
10711 void wxGrid::AutoSize()
10713 wxGridUpdateLocker
locker(this);
10715 // we need to round up the size of the scrollable area to a multiple of
10716 // scroll step to ensure that we don't get the scrollbars when we're sized
10717 // exactly to fit our contents
10718 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
10719 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
10720 wxSize
sizeFit(GetScrollX(size
.x
) * GetScrollLineX(),
10721 GetScrollY(size
.y
) * GetScrollLineY());
10723 // distribute the extra space between the columns/rows to avoid having
10724 // extra white space
10725 wxCoord diff
= sizeFit
.x
- size
.x
;
10726 if ( diff
&& m_numCols
)
10728 // try to resize the columns uniformly
10729 wxCoord diffPerCol
= diff
/ m_numCols
;
10732 for ( int col
= 0; col
< m_numCols
; col
++ )
10734 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
10738 // add remaining amount to the last columns
10739 diff
-= diffPerCol
* m_numCols
;
10742 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
10744 SetColSize(col
, GetColWidth(col
) + 1);
10750 diff
= sizeFit
.y
- size
.y
;
10751 if ( diff
&& m_numRows
)
10753 // try to resize the columns uniformly
10754 wxCoord diffPerRow
= diff
/ m_numRows
;
10757 for ( int row
= 0; row
< m_numRows
; row
++ )
10759 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
10763 // add remaining amount to the last rows
10764 diff
-= diffPerRow
* m_numRows
;
10767 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
10769 SetRowSize(row
, GetRowHeight(row
) + 1);
10774 // we know that we're not going to have scrollbars so disable them now to
10775 // avoid trouble in SetClientSize() which can otherwise set the correct
10776 // client size but also leave space for (not needed any more) scrollbars
10777 SetScrollbars(0, 0, 0, 0, 0, 0, true);
10778 SetClientSize(sizeFit
.x
+ m_rowLabelWidth
, sizeFit
.y
+ m_colLabelHeight
);
10781 void wxGrid::AutoSizeRowLabelSize( int row
)
10783 wxArrayString lines
;
10786 // Hide the edit control, so it
10787 // won't interfere with drag-shrinking.
10788 if ( IsCellEditControlShown() )
10790 HideCellEditControl();
10791 SaveEditControlValue();
10794 // autosize row height depending on label text
10795 StringToLines( GetRowLabelValue( row
), lines
);
10796 wxClientDC
dc( m_rowLabelWin
);
10797 GetTextBoxSize( dc
, lines
, &w
, &h
);
10798 if ( h
< m_defaultRowHeight
)
10799 h
= m_defaultRowHeight
;
10800 SetRowSize(row
, h
);
10804 void wxGrid::AutoSizeColLabelSize( int col
)
10806 wxArrayString lines
;
10809 // Hide the edit control, so it
10810 // won't interfere with drag-shrinking.
10811 if ( IsCellEditControlShown() )
10813 HideCellEditControl();
10814 SaveEditControlValue();
10817 // autosize column width depending on label text
10818 StringToLines( GetColLabelValue( col
), lines
);
10819 wxClientDC
dc( m_colLabelWin
);
10820 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10821 GetTextBoxSize( dc
, lines
, &w
, &h
);
10823 GetTextBoxSize( dc
, lines
, &h
, &w
);
10824 if ( w
< m_defaultColWidth
)
10825 w
= m_defaultColWidth
;
10826 SetColSize(col
, w
);
10830 wxSize
wxGrid::DoGetBestSize() const
10832 wxGrid
*self
= (wxGrid
*)this; // const_cast
10834 // we do the same as in AutoSize() here with the exception that we don't
10835 // change the column/row sizes, only calculate them
10836 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
10837 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
10838 wxSize
sizeFit(GetScrollX(size
.x
) * GetScrollLineX(),
10839 GetScrollY(size
.y
) * GetScrollLineY());
10841 // NOTE: This size should be cached, but first we need to add calls to
10842 // InvalidateBestSize everywhere that could change the results of this
10844 // CacheBestSize(size);
10846 return wxSize(sizeFit
.x
+ m_rowLabelWidth
, sizeFit
.y
+ m_colLabelHeight
)
10847 + GetWindowBorderSize();
10855 wxPen
& wxGrid::GetDividerPen() const
10860 // ----------------------------------------------------------------------------
10861 // cell value accessor functions
10862 // ----------------------------------------------------------------------------
10864 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10868 m_table
->SetValue( row
, col
, s
);
10869 if ( !GetBatchCount() )
10872 wxRect
rect( CellToRect( row
, col
) );
10874 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10875 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10876 m_gridWin
->Refresh( false, &rect
);
10879 if ( m_currentCellCoords
.GetRow() == row
&&
10880 m_currentCellCoords
.GetCol() == col
&&
10881 IsCellEditControlShown())
10882 // Note: If we are using IsCellEditControlEnabled,
10883 // this interacts badly with calling SetCellValue from
10884 // an EVT_GRID_CELL_CHANGE handler.
10886 HideCellEditControl();
10887 ShowCellEditControl(); // will reread data from table
10892 // ----------------------------------------------------------------------------
10893 // block, row and column selection
10894 // ----------------------------------------------------------------------------
10896 void wxGrid::SelectRow( int row
, bool addToSelected
)
10898 if ( IsSelection() && !addToSelected
)
10902 m_selection
->SelectRow( row
, false, addToSelected
);
10905 void wxGrid::SelectCol( int col
, bool addToSelected
)
10907 if ( IsSelection() && !addToSelected
)
10911 m_selection
->SelectCol( col
, false, addToSelected
);
10914 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10915 bool addToSelected
)
10917 if ( IsSelection() && !addToSelected
)
10921 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10922 false, addToSelected
);
10925 void wxGrid::SelectAll()
10927 if ( m_numRows
> 0 && m_numCols
> 0 )
10930 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10934 // ----------------------------------------------------------------------------
10935 // cell, row and col deselection
10936 // ----------------------------------------------------------------------------
10938 void wxGrid::DeselectRow( int row
)
10940 if ( !m_selection
)
10943 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10945 if ( m_selection
->IsInSelection(row
, 0 ) )
10946 m_selection
->ToggleCellSelection(row
, 0);
10950 int nCols
= GetNumberCols();
10951 for ( int i
= 0; i
< nCols
; i
++ )
10953 if ( m_selection
->IsInSelection(row
, i
) )
10954 m_selection
->ToggleCellSelection(row
, i
);
10959 void wxGrid::DeselectCol( int col
)
10961 if ( !m_selection
)
10964 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10966 if ( m_selection
->IsInSelection(0, col
) )
10967 m_selection
->ToggleCellSelection(0, col
);
10971 int nRows
= GetNumberRows();
10972 for ( int i
= 0; i
< nRows
; i
++ )
10974 if ( m_selection
->IsInSelection(i
, col
) )
10975 m_selection
->ToggleCellSelection(i
, col
);
10980 void wxGrid::DeselectCell( int row
, int col
)
10982 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10983 m_selection
->ToggleCellSelection(row
, col
);
10986 bool wxGrid::IsSelection() const
10988 return ( m_selection
&& (m_selection
->IsSelection() ||
10989 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10990 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10993 bool wxGrid::IsInSelection( int row
, int col
) const
10995 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10996 ( row
>= m_selectingTopLeft
.GetRow() &&
10997 col
>= m_selectingTopLeft
.GetCol() &&
10998 row
<= m_selectingBottomRight
.GetRow() &&
10999 col
<= m_selectingBottomRight
.GetCol() )) );
11002 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
11006 wxGridCellCoordsArray a
;
11010 return m_selection
->m_cellSelection
;
11013 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
11017 wxGridCellCoordsArray a
;
11021 return m_selection
->m_blockSelectionTopLeft
;
11024 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
11028 wxGridCellCoordsArray a
;
11032 return m_selection
->m_blockSelectionBottomRight
;
11035 wxArrayInt
wxGrid::GetSelectedRows() const
11043 return m_selection
->m_rowSelection
;
11046 wxArrayInt
wxGrid::GetSelectedCols() const
11054 return m_selection
->m_colSelection
;
11057 void wxGrid::ClearSelection()
11059 wxRect r1
= BlockToDeviceRect( m_selectingTopLeft
, m_selectingBottomRight
);
11060 wxRect r2
= BlockToDeviceRect( m_currentCellCoords
, m_selectingKeyboard
);
11061 m_selectingTopLeft
=
11062 m_selectingBottomRight
=
11063 m_selectingKeyboard
= wxGridNoCellCoords
;
11064 Refresh( false, &r1
);
11065 Refresh( false, &r2
);
11067 m_selection
->ClearSelection();
11070 // This function returns the rectangle that encloses the given block
11071 // in device coords clipped to the client size of the grid window.
11073 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
11074 const wxGridCellCoords
& bottomRight
) const
11077 wxRect tempCellRect
= CellToRect(topLeft
);
11078 if ( tempCellRect
!= wxGridNoCellRect
)
11080 resultRect
= tempCellRect
;
11084 resultRect
= wxRect(0, 0, 0, 0);
11087 tempCellRect
= CellToRect(bottomRight
);
11088 if ( tempCellRect
!= wxGridNoCellRect
)
11090 resultRect
+= tempCellRect
;
11094 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
11095 return wxGridNoCellRect
;
11098 // Ensure that left/right and top/bottom pairs are in order.
11099 int left
= resultRect
.GetLeft();
11100 int top
= resultRect
.GetTop();
11101 int right
= resultRect
.GetRight();
11102 int bottom
= resultRect
.GetBottom();
11104 int leftCol
= topLeft
.GetCol();
11105 int topRow
= topLeft
.GetRow();
11106 int rightCol
= bottomRight
.GetCol();
11107 int bottomRow
= bottomRight
.GetRow();
11116 leftCol
= rightCol
;
11127 topRow
= bottomRow
;
11131 // The following loop is ONLY necessary to detect and handle merged cells.
11133 m_gridWin
->GetClientSize( &cw
, &ch
);
11135 // Get the origin coordinates: notice that they will be negative if the
11136 // grid is scrolled downwards/to the right.
11137 int gridOriginX
= 0;
11138 int gridOriginY
= 0;
11139 CalcScrolledPosition(gridOriginX
, gridOriginY
, &gridOriginX
, &gridOriginY
);
11141 int onScreenLeftmostCol
= internalXToCol(-gridOriginX
);
11142 int onScreenUppermostRow
= internalYToRow(-gridOriginY
);
11144 int onScreenRightmostCol
= internalXToCol(-gridOriginX
+ cw
);
11145 int onScreenBottommostRow
= internalYToRow(-gridOriginY
+ ch
);
11147 // Bound our loop so that we only examine the portion of the selected block
11148 // that is shown on screen. Therefore, we compare the Top-Left block values
11149 // to the Top-Left screen values, and the Bottom-Right block values to the
11150 // Bottom-Right screen values, choosing appropriately.
11151 const int visibleTopRow
= wxMax(topRow
, onScreenUppermostRow
);
11152 const int visibleBottomRow
= wxMin(bottomRow
, onScreenBottommostRow
);
11153 const int visibleLeftCol
= wxMax(leftCol
, onScreenLeftmostCol
);
11154 const int visibleRightCol
= wxMin(rightCol
, onScreenRightmostCol
);
11156 for ( int j
= visibleTopRow
; j
<= visibleBottomRow
; j
++ )
11158 for ( int i
= visibleLeftCol
; i
<= visibleRightCol
; i
++ )
11160 if ( (j
== visibleTopRow
) || (j
== visibleBottomRow
) ||
11161 (i
== visibleLeftCol
) || (i
== visibleRightCol
) )
11163 tempCellRect
= CellToRect( j
, i
);
11165 if (tempCellRect
.x
< left
)
11166 left
= tempCellRect
.x
;
11167 if (tempCellRect
.y
< top
)
11168 top
= tempCellRect
.y
;
11169 if (tempCellRect
.x
+ tempCellRect
.width
> right
)
11170 right
= tempCellRect
.x
+ tempCellRect
.width
;
11171 if (tempCellRect
.y
+ tempCellRect
.height
> bottom
)
11172 bottom
= tempCellRect
.y
+ tempCellRect
.height
;
11176 i
= visibleRightCol
; // jump over inner cells.
11181 // Convert to scrolled coords
11182 CalcScrolledPosition( left
, top
, &left
, &top
);
11183 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
11185 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
11186 return wxRect(0,0,0,0);
11188 resultRect
.SetLeft( wxMax(0, left
) );
11189 resultRect
.SetTop( wxMax(0, top
) );
11190 resultRect
.SetRight( wxMin(cw
, right
) );
11191 resultRect
.SetBottom( wxMin(ch
, bottom
) );
11196 // ----------------------------------------------------------------------------
11198 // ----------------------------------------------------------------------------
11200 #if wxUSE_DRAG_AND_DROP
11202 // this allow setting drop target directly on wxGrid
11203 void wxGrid::SetDropTarget(wxDropTarget
*dropTarget
)
11205 GetGridWindow()->SetDropTarget(dropTarget
);
11208 #endif // wxUSE_DRAG_AND_DROP
11210 // ----------------------------------------------------------------------------
11211 // grid event classes
11212 // ----------------------------------------------------------------------------
11214 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
11216 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
11217 int row
, int col
, int x
, int y
, bool sel
,
11218 bool control
, bool shift
, bool alt
, bool meta
)
11219 : wxNotifyEvent( type
, id
)
11226 m_control
= control
;
11231 SetEventObject(obj
);
11235 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
11237 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
11238 int rowOrCol
, int x
, int y
,
11239 bool control
, bool shift
, bool alt
, bool meta
)
11240 : wxNotifyEvent( type
, id
)
11242 m_rowOrCol
= rowOrCol
;
11245 m_control
= control
;
11250 SetEventObject(obj
);
11254 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
11256 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
11257 const wxGridCellCoords
& topLeft
,
11258 const wxGridCellCoords
& bottomRight
,
11259 bool sel
, bool control
,
11260 bool shift
, bool alt
, bool meta
)
11261 : wxNotifyEvent( type
, id
)
11263 m_topLeft
= topLeft
;
11264 m_bottomRight
= bottomRight
;
11266 m_control
= control
;
11271 SetEventObject(obj
);
11275 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
11277 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
11278 wxObject
* obj
, int row
,
11279 int col
, wxControl
* ctrl
)
11280 : wxCommandEvent(type
, id
)
11282 SetEventObject(obj
);
11288 #endif // wxUSE_GRID