1 ///////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
5 // Modified by: Robin Dunn, Vadim Zeitlin, Santiago Palacios
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
25 #include "wx/dcclient.h"
26 #include "wx/settings.h"
28 #include "wx/textctrl.h"
29 #include "wx/checkbox.h"
30 #include "wx/combobox.h"
31 #include "wx/valtext.h"
34 #include "wx/listbox.h"
37 #include "wx/textfile.h"
38 #include "wx/spinctrl.h"
39 #include "wx/tokenzr.h"
40 #include "wx/renderer.h"
42 #include "wx/generic/gridsel.h"
44 const wxChar wxGridNameStr
[] = wxT("grid");
46 #if defined(__WXMOTIF__)
47 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
49 #define WXUNUSED_MOTIF(identifier) identifier
52 #if defined(__WXGTK__)
53 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
55 #define WXUNUSED_GTK(identifier) identifier
58 // Required for wxIs... functions
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr
*, wxArrayAttrs
,
66 class WXDLLIMPEXP_ADV
);
68 struct wxGridCellWithAttr
70 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
71 : coords(row
, col
), attr(attr_
)
80 wxGridCellCoords coords
;
84 // DECLARE_NO_COPY_CLASS(wxGridCellWithAttr)
85 // without rewriting the macros, which require a public copy constructor.
88 WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr
, wxGridCellWithAttrArray
,
89 class WXDLLIMPEXP_ADV
);
91 #include "wx/arrimpl.cpp"
93 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
94 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
100 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
101 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
102 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
103 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
104 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_BEGIN_DRAG
)
105 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
106 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
107 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
108 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
109 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
110 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
111 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_MOVE
)
112 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
113 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
114 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
115 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
116 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
117 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
119 // ----------------------------------------------------------------------------
121 // ----------------------------------------------------------------------------
123 // common base class for various grid subwindows
124 class WXDLLIMPEXP_ADV wxGridSubwindow
: public wxWindow
127 wxGridSubwindow() { m_owner
= NULL
; }
128 wxGridSubwindow(wxGrid
*owner
,
132 int additionalStyle
= 0,
133 const wxString
& name
= wxPanelNameStr
)
134 : wxWindow(owner
, id
, pos
, size
,
135 wxWANTS_CHARS
| wxBORDER_NONE
| additionalStyle
,
141 wxGrid
*GetOwner() { return m_owner
; }
144 void OnMouseCaptureLost(wxMouseCaptureLostEvent
& event
);
148 DECLARE_EVENT_TABLE()
149 DECLARE_NO_COPY_CLASS(wxGridSubwindow
)
152 class WXDLLIMPEXP_ADV wxGridRowLabelWindow
: public wxGridSubwindow
155 wxGridRowLabelWindow() { }
156 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
157 const wxPoint
&pos
, const wxSize
&size
);
160 void OnPaint( wxPaintEvent
& event
);
161 void OnMouseEvent( wxMouseEvent
& event
);
162 void OnMouseWheel( wxMouseEvent
& event
);
163 void OnKeyDown( wxKeyEvent
& event
);
164 void OnKeyUp( wxKeyEvent
& );
165 void OnChar( wxKeyEvent
& );
167 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
168 DECLARE_EVENT_TABLE()
169 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow
)
173 class WXDLLIMPEXP_ADV wxGridColLabelWindow
: public wxGridSubwindow
176 wxGridColLabelWindow() { }
177 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
178 const wxPoint
&pos
, const wxSize
&size
);
181 void OnPaint( wxPaintEvent
& event
);
182 void OnMouseEvent( wxMouseEvent
& event
);
183 void OnMouseWheel( wxMouseEvent
& event
);
184 void OnKeyDown( wxKeyEvent
& event
);
185 void OnKeyUp( wxKeyEvent
& );
186 void OnChar( wxKeyEvent
& );
188 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
189 DECLARE_EVENT_TABLE()
190 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow
)
194 class WXDLLIMPEXP_ADV wxGridCornerLabelWindow
: public wxGridSubwindow
197 wxGridCornerLabelWindow() { }
198 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
199 const wxPoint
&pos
, const wxSize
&size
);
202 void OnMouseEvent( wxMouseEvent
& event
);
203 void OnMouseWheel( wxMouseEvent
& event
);
204 void OnKeyDown( wxKeyEvent
& event
);
205 void OnKeyUp( wxKeyEvent
& );
206 void OnChar( wxKeyEvent
& );
207 void OnPaint( wxPaintEvent
& event
);
209 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
210 DECLARE_EVENT_TABLE()
211 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow
)
214 class WXDLLIMPEXP_ADV wxGridWindow
: public wxGridSubwindow
219 m_rowLabelWin
= NULL
;
220 m_colLabelWin
= NULL
;
223 wxGridWindow( wxGrid
*parent
,
224 wxGridRowLabelWindow
*rowLblWin
,
225 wxGridColLabelWindow
*colLblWin
,
226 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
228 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
231 wxGridRowLabelWindow
*m_rowLabelWin
;
232 wxGridColLabelWindow
*m_colLabelWin
;
234 void OnPaint( wxPaintEvent
&event
);
235 void OnMouseWheel( wxMouseEvent
& event
);
236 void OnMouseEvent( wxMouseEvent
& event
);
237 void OnKeyDown( wxKeyEvent
& );
238 void OnKeyUp( wxKeyEvent
& );
239 void OnChar( wxKeyEvent
& );
240 void OnEraseBackground( wxEraseEvent
& );
241 void OnFocus( wxFocusEvent
& );
243 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
244 DECLARE_EVENT_TABLE()
245 DECLARE_NO_COPY_CLASS(wxGridWindow
)
249 class wxGridCellEditorEvtHandler
: public wxEvtHandler
252 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
259 void OnKillFocus(wxFocusEvent
& event
);
260 void OnKeyDown(wxKeyEvent
& event
);
261 void OnChar(wxKeyEvent
& event
);
263 void SetInSetFocus(bool inSetFocus
) { m_inSetFocus
= inSetFocus
; }
267 wxGridCellEditor
*m_editor
;
269 // Work around the fact that a focus kill event can be sent to
270 // a combobox within a set focus event.
273 DECLARE_EVENT_TABLE()
274 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
275 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler
)
279 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler
, wxEvtHandler
)
281 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
282 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus
)
283 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
284 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
288 // ----------------------------------------------------------------------------
289 // the internal data representation used by wxGridCellAttrProvider
290 // ----------------------------------------------------------------------------
292 // this class stores attributes set for cells
293 class WXDLLIMPEXP_ADV wxGridCellAttrData
296 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
297 wxGridCellAttr
*GetAttr(int row
, int col
) const;
298 void UpdateAttrRows( size_t pos
, int numRows
);
299 void UpdateAttrCols( size_t pos
, int numCols
);
302 // searches for the attr for given cell, returns wxNOT_FOUND if not found
303 int FindIndex(int row
, int col
) const;
305 wxGridCellWithAttrArray m_attrs
;
308 // this class stores attributes set for rows or columns
309 class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
312 // empty ctor to suppress warnings
313 wxGridRowOrColAttrData() {}
314 ~wxGridRowOrColAttrData();
316 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
317 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
318 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
321 wxArrayInt m_rowsOrCols
;
322 wxArrayAttrs m_attrs
;
325 // NB: this is just a wrapper around 3 objects: one which stores cell
326 // attributes, and 2 others for row/col ones
327 class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
330 wxGridCellAttrData m_cellAttrs
;
331 wxGridRowOrColAttrData m_rowAttrs
,
336 // ----------------------------------------------------------------------------
337 // data structures used for the data type registry
338 // ----------------------------------------------------------------------------
340 struct wxGridDataTypeInfo
342 wxGridDataTypeInfo(const wxString
& typeName
,
343 wxGridCellRenderer
* renderer
,
344 wxGridCellEditor
* editor
)
345 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
348 ~wxGridDataTypeInfo()
350 wxSafeDecRef(m_renderer
);
351 wxSafeDecRef(m_editor
);
355 wxGridCellRenderer
* m_renderer
;
356 wxGridCellEditor
* m_editor
;
358 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo
)
362 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
,
363 class WXDLLIMPEXP_ADV
);
366 class WXDLLIMPEXP_ADV wxGridTypeRegistry
369 wxGridTypeRegistry() {}
370 ~wxGridTypeRegistry();
372 void RegisterDataType(const wxString
& typeName
,
373 wxGridCellRenderer
* renderer
,
374 wxGridCellEditor
* editor
);
376 // find one of already registered data types
377 int FindRegisteredDataType(const wxString
& typeName
);
379 // try to FindRegisteredDataType(), if this fails and typeName is one of
380 // standard typenames, register it and return its index
381 int FindDataType(const wxString
& typeName
);
383 // try to FindDataType(), if it fails see if it is not one of already
384 // registered data types with some params in which case clone the
385 // registered data type and set params for it
386 int FindOrCloneDataType(const wxString
& typeName
);
388 wxGridCellRenderer
* GetRenderer(int index
);
389 wxGridCellEditor
* GetEditor(int index
);
392 wxGridDataTypeInfoArray m_typeinfo
;
396 // ----------------------------------------------------------------------------
397 // conditional compilation
398 // ----------------------------------------------------------------------------
400 #ifndef WXGRID_DRAW_LINES
401 #define WXGRID_DRAW_LINES 1
404 // ----------------------------------------------------------------------------
406 // ----------------------------------------------------------------------------
408 //#define DEBUG_ATTR_CACHE
409 #ifdef DEBUG_ATTR_CACHE
410 static size_t gs_nAttrCacheHits
= 0;
411 static size_t gs_nAttrCacheMisses
= 0;
414 // ----------------------------------------------------------------------------
416 // ----------------------------------------------------------------------------
418 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
419 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
422 // TODO: this doesn't work at all, grid cells have different sizes and approx
423 // calculations don't work as because of the size mismatch scrollbars
424 // sometimes fail to be shown when they should be or vice versa
426 // The scroll bars may be a little flakey once in a while, but that is
427 // surely much less horrible than having scroll lines of only 1!!!
430 // Well, it's still seriously broken so it might be better but needs
433 static const size_t GRID_SCROLL_LINE_X
= 15; // 1;
434 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
436 // the size of hash tables used a bit everywhere (the max number of elements
437 // in these hash tables is the number of rows/columns)
438 static const int GRID_HASH_SIZE
= 100;
441 // ----------------------------------------------------------------------------
443 // ----------------------------------------------------------------------------
445 static inline int GetScrollX(int x
)
447 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
450 static inline int GetScrollY(int y
)
452 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
456 // ============================================================================
458 // ============================================================================
460 // ----------------------------------------------------------------------------
462 // ----------------------------------------------------------------------------
464 wxGridCellEditor::wxGridCellEditor()
470 wxGridCellEditor::~wxGridCellEditor()
475 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
476 wxWindowID
WXUNUSED(id
),
477 wxEvtHandler
* evtHandler
)
480 m_control
->PushEventHandler(evtHandler
);
483 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
484 wxGridCellAttr
*attr
)
486 // erase the background because we might not fill the cell
487 wxClientDC
dc(m_control
->GetParent());
488 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
490 gridWindow
->GetOwner()->PrepareDC(dc
);
492 dc
.SetPen(*wxTRANSPARENT_PEN
);
493 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
494 dc
.DrawRectangle(rectCell
);
496 // redraw the control we just painted over
497 m_control
->Refresh();
500 void wxGridCellEditor::Destroy()
504 m_control
->PopEventHandler( true /* delete it*/ );
506 m_control
->Destroy();
511 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
513 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
515 m_control
->Show(show
);
519 // set the colours/fonts if we have any
522 m_colFgOld
= m_control
->GetForegroundColour();
523 m_control
->SetForegroundColour(attr
->GetTextColour());
525 m_colBgOld
= m_control
->GetBackgroundColour();
526 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
528 // Workaround for GTK+1 font setting problem on some platforms
529 #if !defined(__WXGTK__) || defined(__WXGTK20__)
530 m_fontOld
= m_control
->GetFont();
531 m_control
->SetFont(attr
->GetFont());
534 // can't do anything more in the base class version, the other
535 // attributes may only be used by the derived classes
540 // restore the standard colours fonts
541 if ( m_colFgOld
.Ok() )
543 m_control
->SetForegroundColour(m_colFgOld
);
544 m_colFgOld
= wxNullColour
;
547 if ( m_colBgOld
.Ok() )
549 m_control
->SetBackgroundColour(m_colBgOld
);
550 m_colBgOld
= wxNullColour
;
553 // Workaround for GTK+1 font setting problem on some platforms
554 #if !defined(__WXGTK__) || defined(__WXGTK20__)
555 if ( m_fontOld
.Ok() )
557 m_control
->SetFont(m_fontOld
);
558 m_fontOld
= wxNullFont
;
564 void wxGridCellEditor::SetSize(const wxRect
& rect
)
566 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
568 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
571 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
576 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
578 bool ctrl
= event
.ControlDown();
579 bool alt
= event
.AltDown();
582 // On the Mac the Alt key is more like shift and is used for entry of
583 // valid characters, so check for Ctrl and Meta instead.
584 alt
= event
.MetaDown();
587 // Assume it's not a valid char if ctrl or alt is down, but if both are
588 // down then it may be because of an AltGr key combination, so let them
589 // through in that case.
590 if ((ctrl
|| alt
) && !(ctrl
&& alt
))
597 // If it's a F-Key or other special key then it shouldn't start the
599 if (event
.GetKeyCode() >= WXK_START
)
603 // if the unicode key code is not really a unicode character (it may
604 // be a function key or etc., the platforms appear to always give us a
605 // small value in this case) then fallback to the ASCII key code but
606 // don't do anything for function keys or etc.
607 key
= event
.GetUnicodeKey();
610 key
= event
.GetKeyCode();
611 keyOk
= (key
<= 127);
614 key
= event
.GetKeyCode();
615 keyOk
= (key
<= 255);
621 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
626 void wxGridCellEditor::StartingClick()
632 // ----------------------------------------------------------------------------
633 // wxGridCellTextEditor
634 // ----------------------------------------------------------------------------
636 wxGridCellTextEditor::wxGridCellTextEditor()
641 void wxGridCellTextEditor::Create(wxWindow
* parent
,
643 wxEvtHandler
* evtHandler
)
645 DoCreate(parent
, id
, evtHandler
);
648 void wxGridCellTextEditor::DoCreate(wxWindow
* parent
,
650 wxEvtHandler
* evtHandler
,
653 style
|= wxTE_PROCESS_ENTER
|
658 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
659 wxDefaultPosition
, wxDefaultSize
,
662 // set max length allowed in the textctrl, if the parameter was set
663 if ( m_maxChars
!= 0 )
665 Text()->SetMaxLength(m_maxChars
);
668 wxGridCellEditor::Create(parent
, id
, evtHandler
);
671 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
672 wxGridCellAttr
* WXUNUSED(attr
))
674 // as we fill the entire client area,
675 // don't do anything here to minimize flicker
678 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
680 wxRect
rect(rectOrig
);
682 // Make the edit control large enough to allow for internal margins
684 // TODO: remove this if the text ctrl sizing is improved esp. for unix
686 #if defined(__WXGTK__)
694 #elif defined(__WXMSW__)
708 int extra_x
= ( rect
.x
> 2 ) ? 2 : 1;
709 int extra_y
= ( rect
.y
> 2 ) ? 2 : 1;
711 #if defined(__WXMOTIF__)
716 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
717 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
718 rect
.SetRight( rect
.GetRight() + 2 * extra_x
);
719 rect
.SetBottom( rect
.GetBottom() + 2 * extra_y
);
722 wxGridCellEditor::SetSize(rect
);
725 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
727 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
729 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
731 DoBeginEdit(m_startValue
);
734 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
736 Text()->SetValue(startValue
);
737 Text()->SetInsertionPointEnd();
738 Text()->SetSelection(-1, -1);
742 bool wxGridCellTextEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
744 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
746 bool changed
= false;
747 wxString value
= Text()->GetValue();
748 if (value
!= m_startValue
)
752 grid
->GetTable()->SetValue(row
, col
, value
);
754 m_startValue
= wxEmptyString
;
756 // No point in setting the text of the hidden control
757 //Text()->SetValue(m_startValue);
762 void wxGridCellTextEditor::Reset()
764 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
766 DoReset(m_startValue
);
769 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
771 Text()->SetValue(startValue
);
772 Text()->SetInsertionPointEnd();
775 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
777 return wxGridCellEditor::IsAcceptedKey(event
);
780 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
782 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
783 // longer an appropriate way to get the character into the text control.
784 // Do it ourselves instead. We know that if we get this far that we have
785 // a valid character, so not a whole lot of testing needs to be done.
787 wxTextCtrl
* tc
= Text();
792 ch
= event
.GetUnicodeKey();
794 ch
= (wxChar
)event
.GetKeyCode();
796 ch
= (wxChar
)event
.GetKeyCode();
802 // delete the character at the cursor
803 pos
= tc
->GetInsertionPoint();
804 if (pos
< tc
->GetLastPosition())
805 tc
->Remove(pos
, pos
+ 1);
809 // delete the character before the cursor
810 pos
= tc
->GetInsertionPoint();
812 tc
->Remove(pos
- 1, pos
);
821 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
822 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
824 #if defined(__WXMOTIF__) || defined(__WXGTK__)
825 // wxMotif needs a little extra help...
826 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
827 wxString
s( Text()->GetValue() );
828 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
830 Text()->SetInsertionPoint( pos
);
832 // the other ports can handle a Return key press
838 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
848 if ( params
.ToLong(&tmp
) )
850 m_maxChars
= (size_t)tmp
;
854 wxLogDebug( _T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str() );
859 // return the value in the text control
860 wxString
wxGridCellTextEditor::GetValue() const
862 return Text()->GetValue();
865 // ----------------------------------------------------------------------------
866 // wxGridCellNumberEditor
867 // ----------------------------------------------------------------------------
869 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
875 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
877 wxEvtHandler
* evtHandler
)
882 // create a spin ctrl
883 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
884 wxDefaultPosition
, wxDefaultSize
,
888 wxGridCellEditor::Create(parent
, id
, evtHandler
);
893 // just a text control
894 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
897 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
902 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
904 // first get the value
905 wxGridTableBase
*table
= grid
->GetTable();
906 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
908 m_valueOld
= table
->GetValueAsLong(row
, col
);
913 wxString sValue
= table
->GetValue(row
, col
);
914 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty())
916 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
924 Spin()->SetValue((int)m_valueOld
);
930 DoBeginEdit(GetString());
934 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
944 value
= Spin()->GetValue();
945 changed
= value
!= m_valueOld
;
947 text
= wxString::Format(wxT("%ld"), value
);
952 text
= Text()->GetValue();
953 changed
= (text
.empty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
958 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
959 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
961 grid
->GetTable()->SetValue(row
, col
, text
);
967 void wxGridCellNumberEditor::Reset()
972 Spin()->SetValue((int)m_valueOld
);
977 DoReset(GetString());
981 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
983 if ( wxGridCellEditor::IsAcceptedKey(event
) )
985 int keycode
= event
.GetKeyCode();
986 if ( (keycode
< 128) &&
987 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
996 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
998 int keycode
= event
.GetKeyCode();
1001 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
1003 wxGridCellTextEditor::StartingKey(event
);
1005 // skip Skip() below
1012 if ( wxIsdigit(keycode
) )
1014 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
1015 spin
->SetValue(keycode
- '0');
1016 spin
->SetSelection(1,1);
1025 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
1036 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1040 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1044 // skip the error message below
1049 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1053 // return the value in the spin control if it is there (the text control otherwise)
1054 wxString
wxGridCellNumberEditor::GetValue() const
1061 long value
= Spin()->GetValue();
1062 s
.Printf(wxT("%ld"), value
);
1067 s
= Text()->GetValue();
1073 // ----------------------------------------------------------------------------
1074 // wxGridCellFloatEditor
1075 // ----------------------------------------------------------------------------
1077 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1080 m_precision
= precision
;
1083 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1085 wxEvtHandler
* evtHandler
)
1087 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1089 #if wxUSE_VALIDATORS
1090 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1094 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1096 // first get the value
1097 wxGridTableBase
*table
= grid
->GetTable();
1098 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1100 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1105 wxString sValue
= table
->GetValue(row
, col
);
1106 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.empty())
1108 wxFAIL_MSG( _T("this cell doesn't have float value") );
1113 DoBeginEdit(GetString());
1116 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1120 wxString
text(Text()->GetValue());
1122 if ( (text
.empty() || text
.ToDouble(&value
)) &&
1123 !wxIsSameDouble(value
, m_valueOld
) )
1125 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1126 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1128 grid
->GetTable()->SetValue(row
, col
, text
);
1136 void wxGridCellFloatEditor::Reset()
1138 DoReset(GetString());
1141 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1143 int keycode
= event
.GetKeyCode();
1145 tmpbuf
[0] = (char) keycode
;
1147 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1150 bool is_decimal_point
= ( strbuf
==
1151 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1153 bool is_decimal_point
= ( strbuf
== _T(".") );
1156 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1157 || is_decimal_point
)
1159 wxGridCellTextEditor::StartingKey(event
);
1161 // skip Skip() below
1168 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1179 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1183 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1185 m_precision
= (int)tmp
;
1187 // skip the error message below
1192 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1196 wxString
wxGridCellFloatEditor::GetString() const
1199 if ( m_precision
== -1 && m_width
!= -1)
1201 // default precision
1202 fmt
.Printf(_T("%%%d.f"), m_width
);
1204 else if ( m_precision
!= -1 && m_width
== -1)
1207 fmt
.Printf(_T("%%.%df"), m_precision
);
1209 else if ( m_precision
!= -1 && m_width
!= -1 )
1211 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1215 // default width/precision
1219 return wxString::Format(fmt
, m_valueOld
);
1222 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1224 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1226 const int keycode
= event
.GetKeyCode();
1227 if ( isascii(keycode
) )
1230 tmpbuf
[0] = (char) keycode
;
1232 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1235 const wxString decimalPoint
=
1236 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
1238 const wxString
decimalPoint(_T('.'));
1241 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1242 if ( wxIsdigit(keycode
) ||
1243 tolower(keycode
) == 'e' ||
1244 keycode
== decimalPoint
||
1256 #endif // wxUSE_TEXTCTRL
1260 // ----------------------------------------------------------------------------
1261 // wxGridCellBoolEditor
1262 // ----------------------------------------------------------------------------
1264 // the default values for GetValue()
1265 wxString
wxGridCellBoolEditor::ms_stringValues
[2] = { _T(""), _T("1") };
1267 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1269 wxEvtHandler
* evtHandler
)
1271 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1272 wxDefaultPosition
, wxDefaultSize
,
1275 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1278 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1280 bool resize
= false;
1281 wxSize size
= m_control
->GetSize();
1282 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1284 // check if the checkbox is not too big/small for this cell
1285 wxSize sizeBest
= m_control
->GetBestSize();
1286 if ( !(size
== sizeBest
) )
1288 // reset to default size if it had been made smaller
1294 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1296 // leave 1 pixel margin
1297 size
.x
= size
.y
= minSize
- 2;
1304 m_control
->SetSize(size
);
1307 // position it in the centre of the rectangle (TODO: support alignment?)
1309 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1310 // the checkbox without label still has some space to the right in wxGTK,
1311 // so shift it to the right
1313 #elif defined(__WXMSW__)
1314 // here too, but in other way
1319 int hAlign
= wxALIGN_CENTRE
;
1320 int vAlign
= wxALIGN_CENTRE
;
1322 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1325 if (hAlign
== wxALIGN_LEFT
)
1333 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1335 else if (hAlign
== wxALIGN_RIGHT
)
1337 x
= r
.x
+ r
.width
- size
.x
- 2;
1338 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1340 else if (hAlign
== wxALIGN_CENTRE
)
1342 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1343 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1346 m_control
->Move(x
, y
);
1349 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1351 m_control
->Show(show
);
1355 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1356 CBox()->SetBackgroundColour(colBg
);
1360 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1362 wxASSERT_MSG(m_control
,
1363 wxT("The wxGridCellEditor must be created first!"));
1365 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1367 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1371 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1373 if ( cellval
== ms_stringValues
[false] )
1374 m_startValue
= false;
1375 else if ( cellval
== ms_stringValues
[true] )
1376 m_startValue
= true;
1379 // do not try to be smart here and convert it to true or false
1380 // because we'll still overwrite it with something different and
1381 // this risks to be very surprising for the user code, let them
1383 wxFAIL_MSG( _T("invalid value for a cell with bool editor!") );
1387 CBox()->SetValue(m_startValue
);
1391 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1394 wxASSERT_MSG(m_control
,
1395 wxT("The wxGridCellEditor must be created first!"));
1397 bool changed
= false;
1398 bool value
= CBox()->GetValue();
1399 if ( value
!= m_startValue
)
1404 wxGridTableBase
* const table
= grid
->GetTable();
1405 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1406 table
->SetValueAsBool(row
, col
, value
);
1408 table
->SetValue(row
, col
, GetValue());
1414 void wxGridCellBoolEditor::Reset()
1416 wxASSERT_MSG(m_control
,
1417 wxT("The wxGridCellEditor must be created first!"));
1419 CBox()->SetValue(m_startValue
);
1422 void wxGridCellBoolEditor::StartingClick()
1424 CBox()->SetValue(!CBox()->GetValue());
1427 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1429 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1431 int keycode
= event
.GetKeyCode();
1444 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1446 int keycode
= event
.GetKeyCode();
1450 CBox()->SetValue(!CBox()->GetValue());
1454 CBox()->SetValue(true);
1458 CBox()->SetValue(false);
1463 wxString
wxGridCellBoolEditor::GetValue() const
1465 return ms_stringValues
[CBox()->GetValue()];
1469 wxGridCellBoolEditor::UseStringValues(const wxString
& valueTrue
,
1470 const wxString
& valueFalse
)
1472 ms_stringValues
[false] = valueFalse
;
1473 ms_stringValues
[true] = valueTrue
;
1477 wxGridCellBoolEditor::IsTrueValue(const wxString
& value
)
1479 return value
== ms_stringValues
[true];
1482 #endif // wxUSE_CHECKBOX
1486 // ----------------------------------------------------------------------------
1487 // wxGridCellChoiceEditor
1488 // ----------------------------------------------------------------------------
1490 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1492 : m_choices(choices
),
1493 m_allowOthers(allowOthers
) { }
1495 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1496 const wxString choices
[],
1498 : m_allowOthers(allowOthers
)
1502 m_choices
.Alloc(count
);
1503 for ( size_t n
= 0; n
< count
; n
++ )
1505 m_choices
.Add(choices
[n
]);
1510 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1512 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1513 editor
->m_allowOthers
= m_allowOthers
;
1514 editor
->m_choices
= m_choices
;
1519 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1521 wxEvtHandler
* evtHandler
)
1523 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1524 wxDefaultPosition
, wxDefaultSize
,
1526 m_allowOthers
? 0 : wxCB_READONLY
);
1528 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1531 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1532 wxGridCellAttr
* attr
)
1534 // as we fill the entire client area, don't do anything here to minimize
1537 // TODO: It doesn't actually fill the client area since the height of a
1538 // combo always defaults to the standard. Until someone has time to
1539 // figure out the right rectangle to paint, just do it the normal way.
1540 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1543 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1545 wxASSERT_MSG(m_control
,
1546 wxT("The wxGridCellEditor must be created first!"));
1548 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1550 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1552 // Don't immediately end if we get a kill focus event within BeginEdit
1554 evtHandler
->SetInSetFocus(true);
1556 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1560 Combo()->SetValue(m_startValue
);
1561 Combo()->SetInsertionPointEnd();
1563 else // the combobox is read-only
1565 // find the right position, or default to the first if not found
1566 int pos
= Combo()->FindString(m_startValue
);
1567 if (pos
== wxNOT_FOUND
)
1569 Combo()->SetSelection(pos
);
1572 Combo()->SetFocus();
1576 // When dropping down the menu, a kill focus event
1577 // happens after this point, so we can't reset the flag yet.
1578 #if !defined(__WXGTK20__)
1579 evtHandler
->SetInSetFocus(false);
1584 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1587 wxString value
= Combo()->GetValue();
1588 if ( value
== m_startValue
)
1591 grid
->GetTable()->SetValue(row
, col
, value
);
1596 void wxGridCellChoiceEditor::Reset()
1598 Combo()->SetValue(m_startValue
);
1599 Combo()->SetInsertionPointEnd();
1602 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1612 wxStringTokenizer
tk(params
, _T(','));
1613 while ( tk
.HasMoreTokens() )
1615 m_choices
.Add(tk
.GetNextToken());
1619 // return the value in the text control
1620 wxString
wxGridCellChoiceEditor::GetValue() const
1622 return Combo()->GetValue();
1625 #endif // wxUSE_COMBOBOX
1627 // ----------------------------------------------------------------------------
1628 // wxGridCellEditorEvtHandler
1629 // ----------------------------------------------------------------------------
1631 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
1633 // Don't disable the cell if we're just starting to edit it
1638 m_grid
->DisableCellEditControl();
1643 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1645 switch ( event
.GetKeyCode() )
1649 m_grid
->DisableCellEditControl();
1653 m_grid
->GetEventHandler()->ProcessEvent( event
);
1657 case WXK_NUMPAD_ENTER
:
1658 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1659 m_editor
->HandleReturn(event
);
1668 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1670 int row
= m_grid
->GetGridCursorRow();
1671 int col
= m_grid
->GetGridCursorCol();
1672 wxRect rect
= m_grid
->CellToRect( row
, col
);
1674 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
1676 // if cell width is smaller than grid client area, cell is wholly visible
1677 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
1679 switch ( event
.GetKeyCode() )
1684 case WXK_NUMPAD_ENTER
:
1689 if ( wholeCellVisible
)
1691 // no special processing needed...
1696 // do special processing for partly visible cell...
1698 // get the widths of all cells previous to this one
1700 for ( int i
= 0; i
< col
; i
++ )
1702 colXPos
+= m_grid
->GetColSize(i
);
1705 int xUnit
= 1, yUnit
= 1;
1706 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1709 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1713 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
1721 if ( wholeCellVisible
)
1723 // no special processing needed...
1728 // do special processing for partly visible cell...
1731 wxString value
= m_grid
->GetCellValue(row
, col
);
1732 if ( wxEmptyString
!= value
)
1734 // get width of cell CONTENTS (text)
1736 wxFont font
= m_grid
->GetCellFont(row
, col
);
1737 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
1739 // try to RIGHT align the text by scrolling
1740 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
1742 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
1743 // otherwise the last part of the cell content might be hidden below the scroll bar
1744 // FIXME: maybe there is a more suitable correction?
1745 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
1746 if ( textWidth
< 0 )
1752 // get the widths of all cells previous to this one
1754 for ( int i
= 0; i
< col
; i
++ )
1756 colXPos
+= m_grid
->GetColSize(i
);
1759 // and add the (modified) text width of the cell contents
1760 // as we'd like to see the last part of the cell contents
1761 colXPos
+= textWidth
;
1763 int xUnit
= 1, yUnit
= 1;
1764 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1765 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1776 // ----------------------------------------------------------------------------
1777 // wxGridCellWorker is an (almost) empty common base class for
1778 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1779 // ----------------------------------------------------------------------------
1781 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1786 wxGridCellWorker::~wxGridCellWorker()
1790 // ============================================================================
1792 // ============================================================================
1794 // ----------------------------------------------------------------------------
1795 // wxGridCellRenderer
1796 // ----------------------------------------------------------------------------
1798 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1799 wxGridCellAttr
& attr
,
1802 int WXUNUSED(row
), int WXUNUSED(col
),
1805 dc
.SetBackgroundMode( wxSOLID
);
1807 // grey out fields if the grid is disabled
1808 if ( grid
.IsEnabled() )
1812 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1816 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1821 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1824 dc
.SetPen( *wxTRANSPARENT_PEN
);
1825 dc
.DrawRectangle(rect
);
1828 // ----------------------------------------------------------------------------
1829 // wxGridCellStringRenderer
1830 // ----------------------------------------------------------------------------
1832 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
1833 const wxGridCellAttr
& attr
,
1837 dc
.SetBackgroundMode( wxTRANSPARENT
);
1839 // TODO some special colours for attr.IsReadOnly() case?
1841 // different coloured text when the grid is disabled
1842 if ( grid
.IsEnabled() )
1846 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1847 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1851 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1852 dc
.SetTextForeground( attr
.GetTextColour() );
1857 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1858 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
1861 dc
.SetFont( attr
.GetFont() );
1864 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
1866 const wxString
& text
)
1868 wxCoord x
= 0, y
= 0, max_x
= 0;
1869 dc
.SetFont(attr
.GetFont());
1870 wxStringTokenizer
tk(text
, _T('\n'));
1871 while ( tk
.HasMoreTokens() )
1873 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1874 max_x
= wxMax(max_x
, x
);
1877 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1879 return wxSize(max_x
, y
);
1882 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1883 wxGridCellAttr
& attr
,
1887 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1890 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1891 wxGridCellAttr
& attr
,
1893 const wxRect
& rectCell
,
1897 wxRect rect
= rectCell
;
1900 // erase only this cells background, overflow cells should have been erased
1901 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1904 attr
.GetAlignment(&hAlign
, &vAlign
);
1906 int overflowCols
= 0;
1908 if (attr
.GetOverflow())
1910 int cols
= grid
.GetNumberCols();
1911 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1912 int cell_rows
, cell_cols
;
1913 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <= 0
1914 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1916 int i
, c_cols
, c_rows
;
1917 for (i
= col
+cell_cols
; i
< cols
; i
++)
1919 bool is_empty
= true;
1920 for (int j
=row
; j
< row
+ cell_rows
; j
++)
1922 // check w/ anchor cell for multicell block
1923 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1926 if (!grid
.GetTable()->IsEmptyCell(j
+ c_rows
, i
))
1935 rect
.width
+= grid
.GetColSize(i
);
1943 if (rect
.width
>= best_width
)
1947 overflowCols
= i
- col
- cell_cols
+ 1;
1948 if (overflowCols
>= cols
)
1949 overflowCols
= cols
- 1;
1952 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1954 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1956 clip
.x
+= rectCell
.width
;
1957 // draw each overflow cell individually
1958 int col_end
= col
+ cell_cols
+ overflowCols
;
1959 if (col_end
>= grid
.GetNumberCols())
1960 col_end
= grid
.GetNumberCols() - 1;
1961 for (int i
= col
+ cell_cols
; i
<= col_end
; i
++)
1963 clip
.width
= grid
.GetColSize(i
) - 1;
1964 dc
.DestroyClippingRegion();
1965 dc
.SetClippingRegion(clip
);
1967 SetTextColoursAndFont(grid
, attr
, dc
,
1968 grid
.IsInSelection(row
,i
));
1970 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1971 rect
, hAlign
, vAlign
);
1972 clip
.x
+= grid
.GetColSize(i
) - 1;
1978 dc
.DestroyClippingRegion();
1982 // now we only have to draw the text
1983 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1985 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1986 rect
, hAlign
, vAlign
);
1989 // ----------------------------------------------------------------------------
1990 // wxGridCellNumberRenderer
1991 // ----------------------------------------------------------------------------
1993 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
1995 wxGridTableBase
*table
= grid
.GetTable();
1997 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1999 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
2003 text
= table
->GetValue(row
, col
);
2009 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
2010 wxGridCellAttr
& attr
,
2012 const wxRect
& rectCell
,
2016 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2018 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2020 // draw the text right aligned by default
2022 attr
.GetAlignment(&hAlign
, &vAlign
);
2023 hAlign
= wxALIGN_RIGHT
;
2025 wxRect rect
= rectCell
;
2028 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2031 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
2032 wxGridCellAttr
& attr
,
2036 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2039 // ----------------------------------------------------------------------------
2040 // wxGridCellFloatRenderer
2041 // ----------------------------------------------------------------------------
2043 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
2046 SetPrecision(precision
);
2049 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
2051 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
2052 renderer
->m_width
= m_width
;
2053 renderer
->m_precision
= m_precision
;
2054 renderer
->m_format
= m_format
;
2059 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2061 wxGridTableBase
*table
= grid
.GetTable();
2066 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
2068 val
= table
->GetValueAsDouble(row
, col
);
2073 text
= table
->GetValue(row
, col
);
2074 hasDouble
= text
.ToDouble(&val
);
2081 if ( m_width
== -1 )
2083 if ( m_precision
== -1 )
2085 // default width/precision
2086 m_format
= _T("%f");
2090 m_format
.Printf(_T("%%.%df"), m_precision
);
2093 else if ( m_precision
== -1 )
2095 // default precision
2096 m_format
.Printf(_T("%%%d.f"), m_width
);
2100 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
2104 text
.Printf(m_format
, val
);
2107 //else: text already contains the string
2112 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
2113 wxGridCellAttr
& attr
,
2115 const wxRect
& rectCell
,
2119 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2121 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2123 // draw the text right aligned by default
2125 attr
.GetAlignment(&hAlign
, &vAlign
);
2126 hAlign
= wxALIGN_RIGHT
;
2128 wxRect rect
= rectCell
;
2131 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2134 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
2135 wxGridCellAttr
& attr
,
2139 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2142 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
2146 // reset to defaults
2152 wxString tmp
= params
.BeforeFirst(_T(','));
2156 if ( tmp
.ToLong(&width
) )
2158 SetWidth((int)width
);
2162 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
2166 tmp
= params
.AfterFirst(_T(','));
2170 if ( tmp
.ToLong(&precision
) )
2172 SetPrecision((int)precision
);
2176 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
2182 // ----------------------------------------------------------------------------
2183 // wxGridCellBoolRenderer
2184 // ----------------------------------------------------------------------------
2186 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
2188 // FIXME these checkbox size calculations are really ugly...
2190 // between checkmark and box
2191 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2193 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2194 wxGridCellAttr
& WXUNUSED(attr
),
2199 // compute it only once (no locks for MT safeness in GUI thread...)
2200 if ( !ms_sizeCheckMark
.x
)
2202 // get checkbox size
2203 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2204 wxSize size
= checkbox
->GetBestSize();
2205 wxCoord checkSize
= size
.y
+ 2 * wxGRID_CHECKMARK_MARGIN
;
2207 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
2208 #if defined(__WXGTK__) || defined(__WXMOTIF__)
2209 checkSize
-= size
.y
/ 2;
2214 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2217 return ms_sizeCheckMark
;
2220 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2221 wxGridCellAttr
& attr
,
2227 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2229 // draw a check mark in the centre (ignoring alignment - TODO)
2230 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2232 // don't draw outside the cell
2233 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2234 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2236 // and even leave (at least) 1 pixel margin
2237 size
.x
= size
.y
= minSize
- 2;
2240 // draw a border around checkmark
2242 attr
.GetAlignment(&hAlign
, &vAlign
);
2245 if (hAlign
== wxALIGN_CENTRE
)
2247 rectBorder
.x
= rect
.x
+ rect
.width
/ 2 - size
.x
/ 2;
2248 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2249 rectBorder
.width
= size
.x
;
2250 rectBorder
.height
= size
.y
;
2252 else if (hAlign
== wxALIGN_LEFT
)
2254 rectBorder
.x
= rect
.x
+ 2;
2255 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2256 rectBorder
.width
= size
.x
;
2257 rectBorder
.height
= size
.y
;
2259 else if (hAlign
== wxALIGN_RIGHT
)
2261 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2262 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2263 rectBorder
.width
= size
.x
;
2264 rectBorder
.height
= size
.y
;
2268 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2270 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2274 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2275 value
= wxGridCellBoolEditor::IsTrueValue(cellval
);
2280 wxRect rectMark
= rectBorder
;
2283 // MSW DrawCheckMark() is weird (and should probably be changed...)
2284 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/ 2);
2288 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2291 dc
.SetTextForeground(attr
.GetTextColour());
2292 dc
.DrawCheckMark(rectMark
);
2295 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2296 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2297 dc
.DrawRectangle(rectBorder
);
2300 // ----------------------------------------------------------------------------
2302 // ----------------------------------------------------------------------------
2304 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2308 m_isReadOnly
= Unset
;
2313 m_attrkind
= wxGridCellAttr::Cell
;
2315 m_sizeRows
= m_sizeCols
= 1;
2316 m_overflow
= UnsetOverflow
;
2318 SetDefAttr(attrDefault
);
2321 wxGridCellAttr
*wxGridCellAttr::Clone() const
2323 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2325 if ( HasTextColour() )
2326 attr
->SetTextColour(GetTextColour());
2327 if ( HasBackgroundColour() )
2328 attr
->SetBackgroundColour(GetBackgroundColour());
2330 attr
->SetFont(GetFont());
2331 if ( HasAlignment() )
2332 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2334 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2338 attr
->SetRenderer(m_renderer
);
2339 m_renderer
->IncRef();
2343 attr
->SetEditor(m_editor
);
2348 attr
->SetReadOnly();
2350 attr
->SetOverflow( m_overflow
== Overflow
);
2351 attr
->SetKind( m_attrkind
);
2356 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2358 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2359 SetTextColour(mergefrom
->GetTextColour());
2360 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2361 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2362 if ( !HasFont() && mergefrom
->HasFont() )
2363 SetFont(mergefrom
->GetFont());
2364 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2367 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2368 SetAlignment(hAlign
, vAlign
);
2370 if ( !HasSize() && mergefrom
->HasSize() )
2371 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2373 // Directly access member functions as GetRender/Editor don't just return
2374 // m_renderer/m_editor
2376 // Maybe add support for merge of Render and Editor?
2377 if (!HasRenderer() && mergefrom
->HasRenderer() )
2379 m_renderer
= mergefrom
->m_renderer
;
2380 m_renderer
->IncRef();
2382 if ( !HasEditor() && mergefrom
->HasEditor() )
2384 m_editor
= mergefrom
->m_editor
;
2387 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2388 SetReadOnly(mergefrom
->IsReadOnly());
2390 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2391 SetOverflow(mergefrom
->GetOverflow());
2393 SetDefAttr(mergefrom
->m_defGridAttr
);
2396 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2398 // The size of a cell is normally 1,1
2400 // If this cell is larger (2,2) then this is the top left cell
2401 // the other cells that will be covered (lower right cells) must be
2402 // set to negative or zero values such that
2403 // row + num_rows of the covered cell points to the larger cell (this cell)
2404 // same goes for the col + num_cols.
2406 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2408 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
2409 !((num_rows
<= 0) && (num_cols
> 0)) ||
2410 !((num_rows
== 0) && (num_cols
== 0))),
2411 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2413 m_sizeRows
= num_rows
;
2414 m_sizeCols
= num_cols
;
2417 const wxColour
& wxGridCellAttr::GetTextColour() const
2419 if (HasTextColour())
2423 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2425 return m_defGridAttr
->GetTextColour();
2429 wxFAIL_MSG(wxT("Missing default cell attribute"));
2430 return wxNullColour
;
2434 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2436 if (HasBackgroundColour())
2440 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2442 return m_defGridAttr
->GetBackgroundColour();
2446 wxFAIL_MSG(wxT("Missing default cell attribute"));
2447 return wxNullColour
;
2451 const wxFont
& wxGridCellAttr::GetFont() const
2457 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2459 return m_defGridAttr
->GetFont();
2463 wxFAIL_MSG(wxT("Missing default cell attribute"));
2468 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2477 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2479 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2483 wxFAIL_MSG(wxT("Missing default cell attribute"));
2487 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2490 *num_rows
= m_sizeRows
;
2492 *num_cols
= m_sizeCols
;
2495 // GetRenderer and GetEditor use a slightly different decision path about
2496 // which attribute to use. If a non-default attr object has one then it is
2497 // used, otherwise the default editor or renderer is fetched from the grid and
2498 // used. It should be the default for the data type of the cell. If it is
2499 // NULL (because the table has a type that the grid does not have in its
2500 // registry), then the grid's default editor or renderer is used.
2502 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(const wxGrid
* grid
, int row
, int col
) const
2504 wxGridCellRenderer
*renderer
= NULL
;
2506 if ( m_renderer
&& this != m_defGridAttr
)
2508 // use the cells renderer if it has one
2509 renderer
= m_renderer
;
2512 else // no non-default cell renderer
2514 // get default renderer for the data type
2517 // GetDefaultRendererForCell() will do IncRef() for us
2518 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2521 if ( renderer
== NULL
)
2523 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2525 // if we still don't have one then use the grid default
2526 // (no need for IncRef() here neither)
2527 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2529 else // default grid attr
2531 // use m_renderer which we had decided not to use initially
2532 renderer
= m_renderer
;
2539 // we're supposed to always find something
2540 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2545 // same as above, except for s/renderer/editor/g
2546 wxGridCellEditor
* wxGridCellAttr::GetEditor(const wxGrid
* grid
, int row
, int col
) const
2548 wxGridCellEditor
*editor
= NULL
;
2550 if ( m_editor
&& this != m_defGridAttr
)
2552 // use the cells editor if it has one
2556 else // no non default cell editor
2558 // get default editor for the data type
2561 // GetDefaultEditorForCell() will do IncRef() for us
2562 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2565 if ( editor
== NULL
)
2567 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2569 // if we still don't have one then use the grid default
2570 // (no need for IncRef() here neither)
2571 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2573 else // default grid attr
2575 // use m_editor which we had decided not to use initially
2583 // we're supposed to always find something
2584 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2589 // ----------------------------------------------------------------------------
2590 // wxGridCellAttrData
2591 // ----------------------------------------------------------------------------
2593 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2595 int n
= FindIndex(row
, col
);
2596 if ( n
== wxNOT_FOUND
)
2598 // add the attribute
2599 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2603 // free the old attribute
2604 m_attrs
[(size_t)n
].attr
->DecRef();
2608 // change the attribute
2609 m_attrs
[(size_t)n
].attr
= attr
;
2613 // remove this attribute
2614 m_attrs
.RemoveAt((size_t)n
);
2619 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2621 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2623 int n
= FindIndex(row
, col
);
2624 if ( n
!= wxNOT_FOUND
)
2626 attr
= m_attrs
[(size_t)n
].attr
;
2633 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2635 size_t count
= m_attrs
.GetCount();
2636 for ( size_t n
= 0; n
< count
; n
++ )
2638 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2639 wxCoord row
= coords
.GetRow();
2640 if ((size_t)row
>= pos
)
2644 // If rows inserted, include row counter where necessary
2645 coords
.SetRow(row
+ numRows
);
2647 else if (numRows
< 0)
2649 // If rows deleted ...
2650 if ((size_t)row
>= pos
- numRows
)
2652 // ...either decrement row counter (if row still exists)...
2653 coords
.SetRow(row
+ numRows
);
2657 // ...or remove the attribute
2658 // No need to DecRef the attribute itself since this is
2659 // done be wxGridCellWithAttr's destructor!
2660 m_attrs
.RemoveAt(n
);
2669 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2671 size_t count
= m_attrs
.GetCount();
2672 for ( size_t n
= 0; n
< count
; n
++ )
2674 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2675 wxCoord col
= coords
.GetCol();
2676 if ( (size_t)col
>= pos
)
2680 // If rows inserted, include row counter where necessary
2681 coords
.SetCol(col
+ numCols
);
2683 else if (numCols
< 0)
2685 // If rows deleted ...
2686 if ((size_t)col
>= pos
- numCols
)
2688 // ...either decrement row counter (if row still exists)...
2689 coords
.SetCol(col
+ numCols
);
2693 // ...or remove the attribute
2694 // No need to DecRef the attribute itself since this is
2695 // done be wxGridCellWithAttr's destructor!
2696 m_attrs
.RemoveAt(n
);
2705 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2707 size_t count
= m_attrs
.GetCount();
2708 for ( size_t n
= 0; n
< count
; n
++ )
2710 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2711 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2720 // ----------------------------------------------------------------------------
2721 // wxGridRowOrColAttrData
2722 // ----------------------------------------------------------------------------
2724 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2726 size_t count
= m_attrs
.GetCount();
2727 for ( size_t n
= 0; n
< count
; n
++ )
2729 m_attrs
[n
]->DecRef();
2733 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2735 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2737 int n
= m_rowsOrCols
.Index(rowOrCol
);
2738 if ( n
!= wxNOT_FOUND
)
2740 attr
= m_attrs
[(size_t)n
];
2747 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2749 int i
= m_rowsOrCols
.Index(rowOrCol
);
2750 if ( i
== wxNOT_FOUND
)
2754 // add the attribute
2755 m_rowsOrCols
.Add(rowOrCol
);
2758 // nothing to remove
2762 size_t n
= (size_t)i
;
2765 // change the attribute
2766 m_attrs
[n
]->DecRef();
2771 // remove this attribute
2772 m_attrs
[n
]->DecRef();
2773 m_rowsOrCols
.RemoveAt(n
);
2774 m_attrs
.RemoveAt(n
);
2779 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2781 size_t count
= m_attrs
.GetCount();
2782 for ( size_t n
= 0; n
< count
; n
++ )
2784 int & rowOrCol
= m_rowsOrCols
[n
];
2785 if ( (size_t)rowOrCol
>= pos
)
2787 if ( numRowsOrCols
> 0 )
2789 // If rows inserted, include row counter where necessary
2790 rowOrCol
+= numRowsOrCols
;
2792 else if ( numRowsOrCols
< 0)
2794 // If rows deleted, either decrement row counter (if row still exists)
2795 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2796 rowOrCol
+= numRowsOrCols
;
2799 m_rowsOrCols
.RemoveAt(n
);
2800 m_attrs
[n
]->DecRef();
2801 m_attrs
.RemoveAt(n
);
2810 // ----------------------------------------------------------------------------
2811 // wxGridCellAttrProvider
2812 // ----------------------------------------------------------------------------
2814 wxGridCellAttrProvider::wxGridCellAttrProvider()
2816 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2819 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2824 void wxGridCellAttrProvider::InitData()
2826 m_data
= new wxGridCellAttrProviderData
;
2829 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2830 wxGridCellAttr::wxAttrKind kind
) const
2832 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2837 case (wxGridCellAttr::Any
):
2838 // Get cached merge attributes.
2839 // Currently not used as no cache implemented as not mutable
2840 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2843 // Basically implement old version.
2844 // Also check merge cache, so we don't have to re-merge every time..
2845 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2846 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2847 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2849 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
2851 // Two or more are non NULL
2852 attr
= new wxGridCellAttr
;
2853 attr
->SetKind(wxGridCellAttr::Merged
);
2855 // Order is important..
2858 attr
->MergeWith(attrcell
);
2863 attr
->MergeWith(attrcol
);
2868 attr
->MergeWith(attrrow
);
2872 // store merge attr if cache implemented
2874 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2878 // one or none is non null return it or null.
2897 case (wxGridCellAttr::Cell
):
2898 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2901 case (wxGridCellAttr::Col
):
2902 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2905 case (wxGridCellAttr::Row
):
2906 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2911 // (wxGridCellAttr::Default):
2912 // (wxGridCellAttr::Merged):
2920 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2926 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2929 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2934 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2937 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2942 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2945 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2949 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2951 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2955 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2959 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2961 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2965 // ----------------------------------------------------------------------------
2966 // wxGridTypeRegistry
2967 // ----------------------------------------------------------------------------
2969 wxGridTypeRegistry::~wxGridTypeRegistry()
2971 size_t count
= m_typeinfo
.GetCount();
2972 for ( size_t i
= 0; i
< count
; i
++ )
2973 delete m_typeinfo
[i
];
2976 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2977 wxGridCellRenderer
* renderer
,
2978 wxGridCellEditor
* editor
)
2980 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2982 // is it already registered?
2983 int loc
= FindRegisteredDataType(typeName
);
2984 if ( loc
!= wxNOT_FOUND
)
2986 delete m_typeinfo
[loc
];
2987 m_typeinfo
[loc
] = info
;
2991 m_typeinfo
.Add(info
);
2995 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2997 size_t count
= m_typeinfo
.GetCount();
2998 for ( size_t i
= 0; i
< count
; i
++ )
3000 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
3009 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
3011 int index
= FindRegisteredDataType(typeName
);
3012 if ( index
== wxNOT_FOUND
)
3014 // check whether this is one of the standard ones, in which case
3015 // register it "on the fly"
3017 if ( typeName
== wxGRID_VALUE_STRING
)
3019 RegisterDataType(wxGRID_VALUE_STRING
,
3020 new wxGridCellStringRenderer
,
3021 new wxGridCellTextEditor
);
3024 #endif // wxUSE_TEXTCTRL
3026 if ( typeName
== wxGRID_VALUE_BOOL
)
3028 RegisterDataType(wxGRID_VALUE_BOOL
,
3029 new wxGridCellBoolRenderer
,
3030 new wxGridCellBoolEditor
);
3033 #endif // wxUSE_CHECKBOX
3035 if ( typeName
== wxGRID_VALUE_NUMBER
)
3037 RegisterDataType(wxGRID_VALUE_NUMBER
,
3038 new wxGridCellNumberRenderer
,
3039 new wxGridCellNumberEditor
);
3041 else if ( typeName
== wxGRID_VALUE_FLOAT
)
3043 RegisterDataType(wxGRID_VALUE_FLOAT
,
3044 new wxGridCellFloatRenderer
,
3045 new wxGridCellFloatEditor
);
3048 #endif // wxUSE_TEXTCTRL
3050 if ( typeName
== wxGRID_VALUE_CHOICE
)
3052 RegisterDataType(wxGRID_VALUE_CHOICE
,
3053 new wxGridCellStringRenderer
,
3054 new wxGridCellChoiceEditor
);
3057 #endif // wxUSE_COMBOBOX
3062 // we get here only if just added the entry for this type, so return
3064 index
= m_typeinfo
.GetCount() - 1;
3070 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
3072 int index
= FindDataType(typeName
);
3073 if ( index
== wxNOT_FOUND
)
3075 // the first part of the typename is the "real" type, anything after ':'
3076 // are the parameters for the renderer
3077 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
3078 if ( index
== wxNOT_FOUND
)
3083 wxGridCellRenderer
*renderer
= GetRenderer(index
);
3084 wxGridCellRenderer
*rendererOld
= renderer
;
3085 renderer
= renderer
->Clone();
3086 rendererOld
->DecRef();
3088 wxGridCellEditor
*editor
= GetEditor(index
);
3089 wxGridCellEditor
*editorOld
= editor
;
3090 editor
= editor
->Clone();
3091 editorOld
->DecRef();
3093 // do it even if there are no parameters to reset them to defaults
3094 wxString params
= typeName
.AfterFirst(_T(':'));
3095 renderer
->SetParameters(params
);
3096 editor
->SetParameters(params
);
3098 // register the new typename
3099 RegisterDataType(typeName
, renderer
, editor
);
3101 // we just registered it, it's the last one
3102 index
= m_typeinfo
.GetCount() - 1;
3108 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
3110 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
3117 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
3119 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
3126 // ----------------------------------------------------------------------------
3128 // ----------------------------------------------------------------------------
3130 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
3132 wxGridTableBase::wxGridTableBase()
3134 m_view
= (wxGrid
*) NULL
;
3135 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
3138 wxGridTableBase::~wxGridTableBase()
3140 delete m_attrProvider
;
3143 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
3145 delete m_attrProvider
;
3146 m_attrProvider
= attrProvider
;
3149 bool wxGridTableBase::CanHaveAttributes()
3151 if ( ! GetAttrProvider() )
3153 // use the default attr provider by default
3154 SetAttrProvider(new wxGridCellAttrProvider
);
3160 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
3162 if ( m_attrProvider
)
3163 return m_attrProvider
->GetAttr(row
, col
, kind
);
3165 return (wxGridCellAttr
*)NULL
;
3168 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
3170 if ( m_attrProvider
)
3172 attr
->SetKind(wxGridCellAttr::Cell
);
3173 m_attrProvider
->SetAttr(attr
, row
, col
);
3177 // as we take ownership of the pointer and don't store it, we must
3183 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3185 if ( m_attrProvider
)
3187 attr
->SetKind(wxGridCellAttr::Row
);
3188 m_attrProvider
->SetRowAttr(attr
, row
);
3192 // as we take ownership of the pointer and don't store it, we must
3198 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3200 if ( m_attrProvider
)
3202 attr
->SetKind(wxGridCellAttr::Col
);
3203 m_attrProvider
->SetColAttr(attr
, col
);
3207 // as we take ownership of the pointer and don't store it, we must
3213 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3214 size_t WXUNUSED(numRows
) )
3216 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3221 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3223 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3228 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3229 size_t WXUNUSED(numRows
) )
3231 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3236 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3237 size_t WXUNUSED(numCols
) )
3239 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3244 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3246 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3251 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3252 size_t WXUNUSED(numCols
) )
3254 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3259 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3263 // RD: Starting the rows at zero confuses users,
3264 // no matter how much it makes sense to us geeks.
3270 wxString
wxGridTableBase::GetColLabelValue( int col
)
3272 // default col labels are:
3273 // cols 0 to 25 : A-Z
3274 // cols 26 to 675 : AA-ZZ
3279 for ( n
= 1; ; n
++ )
3281 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
3287 // reverse the string...
3289 for ( i
= 0; i
< n
; i
++ )
3297 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3299 return wxGRID_VALUE_STRING
;
3302 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3303 const wxString
& typeName
)
3305 return typeName
== wxGRID_VALUE_STRING
;
3308 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3310 return CanGetValueAs(row
, col
, typeName
);
3313 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3318 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3323 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3328 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3329 long WXUNUSED(value
) )
3333 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3334 double WXUNUSED(value
) )
3338 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3339 bool WXUNUSED(value
) )
3343 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3344 const wxString
& WXUNUSED(typeName
) )
3349 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3350 const wxString
& WXUNUSED(typeName
),
3351 void* WXUNUSED(value
) )
3355 //////////////////////////////////////////////////////////////////////
3357 // Message class for the grid table to send requests and notifications
3361 wxGridTableMessage::wxGridTableMessage()
3363 m_table
= (wxGridTableBase
*) NULL
;
3369 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3370 int commandInt1
, int commandInt2
)
3374 m_comInt1
= commandInt1
;
3375 m_comInt2
= commandInt2
;
3378 //////////////////////////////////////////////////////////////////////
3380 // A basic grid table for string data. An object of this class will
3381 // created by wxGrid if you don't specify an alternative table class.
3384 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3386 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3388 wxGridStringTable::wxGridStringTable()
3393 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3396 m_data
.Alloc( numRows
);
3399 sa
.Alloc( numCols
);
3400 sa
.Add( wxEmptyString
, numCols
);
3402 m_data
.Add( sa
, numRows
);
3405 wxGridStringTable::~wxGridStringTable()
3409 int wxGridStringTable::GetNumberRows()
3411 return m_data
.GetCount();
3414 int wxGridStringTable::GetNumberCols()
3416 if ( m_data
.GetCount() > 0 )
3417 return m_data
[0].GetCount();
3422 wxString
wxGridStringTable::GetValue( int row
, int col
)
3424 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3426 _T("invalid row or column index in wxGridStringTable") );
3428 return m_data
[row
][col
];
3431 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3433 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3434 _T("invalid row or column index in wxGridStringTable") );
3436 m_data
[row
][col
] = value
;
3439 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3441 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3443 _T("invalid row or column index in wxGridStringTable") );
3445 return (m_data
[row
][col
] == wxEmptyString
);
3448 void wxGridStringTable::Clear()
3451 int numRows
, numCols
;
3453 numRows
= m_data
.GetCount();
3456 numCols
= m_data
[0].GetCount();
3458 for ( row
= 0; row
< numRows
; row
++ )
3460 for ( col
= 0; col
< numCols
; col
++ )
3462 m_data
[row
][col
] = wxEmptyString
;
3468 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3470 size_t curNumRows
= m_data
.GetCount();
3471 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3472 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3474 if ( pos
>= curNumRows
)
3476 return AppendRows( numRows
);
3480 sa
.Alloc( curNumCols
);
3481 sa
.Add( wxEmptyString
, curNumCols
);
3482 m_data
.Insert( sa
, pos
, numRows
);
3486 wxGridTableMessage
msg( this,
3487 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3491 GetView()->ProcessTableMessage( msg
);
3497 bool wxGridStringTable::AppendRows( size_t numRows
)
3499 size_t curNumRows
= m_data
.GetCount();
3500 size_t curNumCols
= ( curNumRows
> 0
3501 ? m_data
[0].GetCount()
3502 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3505 if ( curNumCols
> 0 )
3507 sa
.Alloc( curNumCols
);
3508 sa
.Add( wxEmptyString
, curNumCols
);
3511 m_data
.Add( sa
, numRows
);
3515 wxGridTableMessage
msg( this,
3516 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3519 GetView()->ProcessTableMessage( msg
);
3525 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3527 size_t curNumRows
= m_data
.GetCount();
3529 if ( pos
>= curNumRows
)
3531 wxFAIL_MSG( wxString::Format
3533 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3535 (unsigned long)numRows
,
3536 (unsigned long)curNumRows
3542 if ( numRows
> curNumRows
- pos
)
3544 numRows
= curNumRows
- pos
;
3547 if ( numRows
>= curNumRows
)
3553 m_data
.RemoveAt( pos
, numRows
);
3558 wxGridTableMessage
msg( this,
3559 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3563 GetView()->ProcessTableMessage( msg
);
3569 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3573 size_t curNumRows
= m_data
.GetCount();
3574 size_t curNumCols
= ( curNumRows
> 0
3575 ? m_data
[0].GetCount()
3576 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3578 if ( pos
>= curNumCols
)
3580 return AppendCols( numCols
);
3583 if ( !m_colLabels
.IsEmpty() )
3585 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
3588 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
3589 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
3592 for ( row
= 0; row
< curNumRows
; row
++ )
3594 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3596 m_data
[row
].Insert( wxEmptyString
, col
);
3602 wxGridTableMessage
msg( this,
3603 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3607 GetView()->ProcessTableMessage( msg
);
3613 bool wxGridStringTable::AppendCols( size_t numCols
)
3617 size_t curNumRows
= m_data
.GetCount();
3622 // TODO: something better than this ?
3624 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3629 for ( row
= 0; row
< curNumRows
; row
++ )
3631 m_data
[row
].Add( wxEmptyString
, numCols
);
3636 wxGridTableMessage
msg( this,
3637 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3640 GetView()->ProcessTableMessage( msg
);
3646 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3650 size_t curNumRows
= m_data
.GetCount();
3651 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3652 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3654 if ( pos
>= curNumCols
)
3656 wxFAIL_MSG( wxString::Format
3658 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3660 (unsigned long)numCols
,
3661 (unsigned long)curNumCols
3668 colID
= GetView()->GetColAt( pos
);
3672 if ( numCols
> curNumCols
- colID
)
3674 numCols
= curNumCols
- colID
;
3677 if ( !m_colLabels
.IsEmpty() )
3679 // m_colLabels stores just as many elements as it needs, e.g. if only
3680 // the label of the first column had been set it would have only one
3681 // element and not numCols, so account for it
3682 int nToRm
= m_colLabels
.size() - colID
;
3684 m_colLabels
.RemoveAt( colID
, nToRm
);
3687 for ( row
= 0; row
< curNumRows
; row
++ )
3689 if ( numCols
>= curNumCols
)
3691 m_data
[row
].Clear();
3695 m_data
[row
].RemoveAt( colID
, numCols
);
3701 wxGridTableMessage
msg( this,
3702 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3706 GetView()->ProcessTableMessage( msg
);
3712 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3714 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3716 // using default label
3718 return wxGridTableBase::GetRowLabelValue( row
);
3722 return m_rowLabels
[row
];
3726 wxString
wxGridStringTable::GetColLabelValue( int col
)
3728 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3730 // using default label
3732 return wxGridTableBase::GetColLabelValue( col
);
3736 return m_colLabels
[col
];
3740 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3742 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3744 int n
= m_rowLabels
.GetCount();
3747 for ( i
= n
; i
<= row
; i
++ )
3749 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3753 m_rowLabels
[row
] = value
;
3756 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3758 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3760 int n
= m_colLabels
.GetCount();
3763 for ( i
= n
; i
<= col
; i
++ )
3765 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3769 m_colLabels
[col
] = value
;
3773 //////////////////////////////////////////////////////////////////////
3774 //////////////////////////////////////////////////////////////////////
3776 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
)
3777 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost
)
3780 void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
3782 m_owner
->CancelMouseCapture();
3785 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3787 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow
)
3788 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3789 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3790 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3791 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3792 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3793 EVT_CHAR( wxGridRowLabelWindow::OnChar
)
3796 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3798 const wxPoint
&pos
, const wxSize
&size
)
3799 : wxGridSubwindow(parent
, id
, pos
, size
)
3804 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3808 // NO - don't do this because it will set both the x and y origin
3809 // coords to match the parent scrolled window and we just want to
3810 // set the y coord - MB
3812 // m_owner->PrepareDC( dc );
3815 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3816 wxPoint pt
= dc
.GetDeviceOrigin();
3817 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
3819 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3820 m_owner
->DrawRowLabels( dc
, rows
);
3823 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3825 m_owner
->ProcessRowLabelMouseEvent( event
);
3828 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3830 m_owner
->GetEventHandler()->ProcessEvent( event
);
3833 // This seems to be required for wxMotif otherwise the mouse
3834 // cursor must be in the cell edit control to get key events
3836 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3838 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3842 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3844 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3848 void wxGridRowLabelWindow::OnChar( wxKeyEvent
& event
)
3850 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3854 //////////////////////////////////////////////////////////////////////
3856 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3858 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow
)
3859 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3860 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3861 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3862 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3863 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3864 EVT_CHAR( wxGridColLabelWindow::OnChar
)
3867 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3869 const wxPoint
&pos
, const wxSize
&size
)
3870 : wxGridSubwindow(parent
, id
, pos
, size
)
3875 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3879 // NO - don't do this because it will set both the x and y origin
3880 // coords to match the parent scrolled window and we just want to
3881 // set the x coord - MB
3883 // m_owner->PrepareDC( dc );
3886 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3887 wxPoint pt
= dc
.GetDeviceOrigin();
3888 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3889 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
3891 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
3893 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3894 m_owner
->DrawColLabels( dc
, cols
);
3897 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3899 m_owner
->ProcessColLabelMouseEvent( event
);
3902 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3904 m_owner
->GetEventHandler()->ProcessEvent( event
);
3907 // This seems to be required for wxMotif otherwise the mouse
3908 // cursor must be in the cell edit control to get key events
3910 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3912 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3916 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3918 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3922 void wxGridColLabelWindow::OnChar( wxKeyEvent
& event
)
3924 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3928 //////////////////////////////////////////////////////////////////////
3930 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3932 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow
)
3933 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3934 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3935 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3936 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3937 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3938 EVT_CHAR( wxGridCornerLabelWindow::OnChar
)
3941 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3943 const wxPoint
&pos
, const wxSize
&size
)
3944 : wxGridSubwindow(parent
, id
, pos
, size
)
3949 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3953 int client_height
= 0;
3954 int client_width
= 0;
3955 GetClientSize( &client_width
, &client_height
);
3957 // VZ: any reason for this ifdef? (FIXME)
3963 rect
.SetWidth( client_width
- 2 );
3964 rect
.SetHeight( client_height
- 2 );
3966 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3968 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
3969 dc
.DrawLine( client_width
- 1, client_height
- 1, client_width
- 1, 0 );
3970 dc
.DrawLine( client_width
- 1, client_height
- 1, 0, client_height
- 1 );
3971 dc
.DrawLine( 0, 0, client_width
, 0 );
3972 dc
.DrawLine( 0, 0, 0, client_height
);
3974 dc
.SetPen( *wxWHITE_PEN
);
3975 dc
.DrawLine( 1, 1, client_width
- 1, 1 );
3976 dc
.DrawLine( 1, 1, 1, client_height
- 1 );
3980 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3982 m_owner
->ProcessCornerLabelMouseEvent( event
);
3985 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3987 m_owner
->GetEventHandler()->ProcessEvent(event
);
3990 // This seems to be required for wxMotif otherwise the mouse
3991 // cursor must be in the cell edit control to get key events
3993 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3995 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3999 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
4001 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4005 void wxGridCornerLabelWindow::OnChar( wxKeyEvent
& event
)
4007 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4011 //////////////////////////////////////////////////////////////////////
4013 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
4015 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow
)
4016 EVT_PAINT( wxGridWindow::OnPaint
)
4017 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
4018 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
4019 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
4020 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
4021 EVT_CHAR( wxGridWindow::OnChar
)
4022 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
4023 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
4024 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
4027 wxGridWindow::wxGridWindow( wxGrid
*parent
,
4028 wxGridRowLabelWindow
*rowLblWin
,
4029 wxGridColLabelWindow
*colLblWin
,
4032 const wxSize
&size
)
4033 : wxGridSubwindow(parent
, id
, pos
, size
,
4034 wxCLIP_CHILDREN
, wxT("grid window") )
4037 m_rowLabelWin
= rowLblWin
;
4038 m_colLabelWin
= colLblWin
;
4041 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
4043 wxPaintDC
dc( this );
4044 m_owner
->PrepareDC( dc
);
4045 wxRegion reg
= GetUpdateRegion();
4046 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
4047 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
4049 #if WXGRID_DRAW_LINES
4050 m_owner
->DrawAllGridLines( dc
, reg
);
4053 m_owner
->DrawGridSpace( dc
);
4054 m_owner
->DrawHighlight( dc
, dirtyCells
);
4057 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
4059 wxWindow::ScrollWindow( dx
, dy
, rect
);
4060 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
4061 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
4064 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
4066 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
4069 m_owner
->ProcessGridCellMouseEvent( event
);
4072 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
4074 m_owner
->GetEventHandler()->ProcessEvent( event
);
4077 // This seems to be required for wxMotif/wxGTK otherwise the mouse
4078 // cursor must be in the cell edit control to get key events
4080 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
4082 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4086 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
4088 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4092 void wxGridWindow::OnChar( wxKeyEvent
& event
)
4094 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4098 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
4102 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
4104 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4108 //////////////////////////////////////////////////////////////////////
4110 // Internal Helper function for computing row or column from some
4111 // (unscrolled) coordinate value, using either
4112 // m_defaultRowHeight/m_defaultColWidth or binary search on array
4113 // of m_rowBottoms/m_ColRights to speed up the search!
4115 // Internal helper macros for simpler use of that function
4117 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
4118 const wxArrayInt
& BorderArray
, int nMax
,
4121 #define internalXToCol(x) XToCol(x, true)
4122 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
4123 m_minAcceptableRowHeight, \
4124 m_rowBottoms, m_numRows, true)
4126 /////////////////////////////////////////////////////////////////////
4128 #if wxUSE_EXTENDED_RTTI
4129 WX_DEFINE_FLAGS( wxGridStyle
)
4131 wxBEGIN_FLAGS( wxGridStyle
)
4132 // new style border flags, we put them first to
4133 // use them for streaming out
4134 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4135 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4136 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4137 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4138 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4139 wxFLAGS_MEMBER(wxBORDER_NONE
)
4141 // old style border flags
4142 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4143 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4144 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4145 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4146 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4147 wxFLAGS_MEMBER(wxBORDER
)
4149 // standard window styles
4150 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4151 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4152 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4153 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4154 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4155 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4156 wxFLAGS_MEMBER(wxVSCROLL
)
4157 wxFLAGS_MEMBER(wxHSCROLL
)
4159 wxEND_FLAGS( wxGridStyle
)
4161 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4163 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4164 wxHIDE_PROPERTY( Children
)
4165 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4166 wxEND_PROPERTIES_TABLE()
4168 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4169 wxEND_HANDLERS_TABLE()
4171 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4174 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
4177 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4180 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4181 EVT_PAINT( wxGrid::OnPaint
)
4182 EVT_SIZE( wxGrid::OnSize
)
4183 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
4184 EVT_KEY_UP( wxGrid::OnKeyUp
)
4185 EVT_CHAR ( wxGrid::OnChar
)
4186 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
4191 // in order to make sure that a size event is not
4192 // trigerred in a unfinished state
4193 m_cornerLabelWin
= NULL
;
4194 m_rowLabelWin
= NULL
;
4195 m_colLabelWin
= NULL
;
4199 wxGrid::wxGrid( wxWindow
*parent
,
4204 const wxString
& name
)
4205 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
4206 m_colMinWidths(GRID_HASH_SIZE
),
4207 m_rowMinHeights(GRID_HASH_SIZE
)
4210 SetInitialSize(size
);
4213 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
4214 const wxPoint
& pos
, const wxSize
& size
,
4215 long style
, const wxString
& name
)
4217 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
4218 style
| wxWANTS_CHARS
, name
))
4221 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4222 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4225 SetInitialSize(size
);
4233 // Must do this or ~wxScrollHelper will pop the wrong event handler
4234 SetTargetWindow(this);
4236 wxSafeDecRef(m_defaultCellAttr
);
4238 #ifdef DEBUG_ATTR_CACHE
4239 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4240 wxPrintf(_T("wxGrid attribute cache statistics: "
4241 "total: %u, hits: %u (%u%%)\n"),
4242 total
, gs_nAttrCacheHits
,
4243 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4246 // if we own the table, just delete it, otherwise at least don't leave it
4247 // with dangling view pointer
4250 else if ( m_table
&& m_table
->GetView() == this )
4251 m_table
->SetView(NULL
);
4253 delete m_typeRegistry
;
4258 // ----- internal init and update functions
4261 // NOTE: If using the default visual attributes works everywhere then this can
4262 // be removed as well as the #else cases below.
4263 #define _USE_VISATTR 0
4265 void wxGrid::Create()
4267 // set to true by CreateGrid
4270 // create the type registry
4271 m_typeRegistry
= new wxGridTypeRegistry
;
4274 m_table
= (wxGridTableBase
*) NULL
;
4277 m_cellEditCtrlEnabled
= false;
4279 m_defaultCellAttr
= new wxGridCellAttr();
4281 // Set default cell attributes
4282 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4283 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4284 m_defaultCellAttr
->SetFont(GetFont());
4285 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4286 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4287 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4290 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4291 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4293 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4294 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4297 m_defaultCellAttr
->SetTextColour(
4298 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4299 m_defaultCellAttr
->SetBackgroundColour(
4300 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4305 m_currentCellCoords
= wxGridNoCellCoords
;
4307 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4308 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4310 // subwindow components that make up the wxGrid
4311 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4316 m_colLabelWin
= new wxGridColLabelWindow( this,
4321 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4326 m_gridWin
= new wxGridWindow( this,
4333 SetTargetWindow( m_gridWin
);
4336 wxColour gfg
= gva
.colFg
;
4337 wxColour gbg
= gva
.colBg
;
4338 wxColour lfg
= lva
.colFg
;
4339 wxColour lbg
= lva
.colBg
;
4341 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4342 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4343 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4344 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4347 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4348 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4349 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4350 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4351 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4352 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4354 m_gridWin
->SetOwnForegroundColour(gfg
);
4355 m_gridWin
->SetOwnBackgroundColour(gbg
);
4360 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4361 wxGrid::wxGridSelectionModes selmode
)
4363 wxCHECK_MSG( !m_created
,
4365 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4367 m_numRows
= numRows
;
4368 m_numCols
= numCols
;
4370 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4371 m_table
->SetView( this );
4373 m_selection
= new wxGridSelection( this, selmode
);
4382 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4384 wxCHECK_RET( m_created
,
4385 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4387 m_selection
->SetSelectionMode( selmode
);
4390 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4392 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4393 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4395 return m_selection
->GetSelectionMode();
4398 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4399 wxGrid::wxGridSelectionModes selmode
)
4401 bool checkSelection
= false;
4404 // stop all processing
4409 m_table
->SetView(0);
4421 checkSelection
= true;
4423 // kill row and column size arrays
4424 m_colWidths
.Empty();
4425 m_colRights
.Empty();
4426 m_rowHeights
.Empty();
4427 m_rowBottoms
.Empty();
4432 m_numRows
= table
->GetNumberRows();
4433 m_numCols
= table
->GetNumberCols();
4436 m_table
->SetView( this );
4437 m_ownTable
= takeOwnership
;
4438 m_selection
= new wxGridSelection( this, selmode
);
4441 // If the newly set table is smaller than the
4442 // original one current cell and selection regions
4443 // might be invalid,
4444 m_selectingKeyboard
= wxGridNoCellCoords
;
4445 m_currentCellCoords
=
4446 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
4447 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
4448 if (m_selectingTopLeft
.GetRow() >= m_numRows
||
4449 m_selectingTopLeft
.GetCol() >= m_numCols
)
4451 m_selectingTopLeft
= wxGridNoCellCoords
;
4452 m_selectingBottomRight
= wxGridNoCellCoords
;
4455 m_selectingBottomRight
=
4456 wxGridCellCoords(wxMin(m_numRows
,
4457 m_selectingBottomRight
.GetRow()),
4459 m_selectingBottomRight
.GetCol()));
4471 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4472 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4474 if ( m_rowLabelWin
)
4476 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4480 m_labelBackgroundColour
= *wxWHITE
;
4483 m_labelTextColour
= *wxBLACK
;
4486 m_attrCache
.row
= -1;
4487 m_attrCache
.col
= -1;
4488 m_attrCache
.attr
= NULL
;
4490 // TODO: something better than this ?
4492 m_labelFont
= this->GetFont();
4493 m_labelFont
.SetWeight( wxBOLD
);
4495 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4496 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4498 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4499 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4500 m_colLabelTextOrientation
= wxHORIZONTAL
;
4502 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4503 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4505 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4506 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4508 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4509 m_defaultRowHeight
+= 8;
4511 m_defaultRowHeight
+= 4;
4514 m_gridLineColour
= wxColour( 192,192,192 );
4515 m_gridLinesEnabled
= true;
4516 m_cellHighlightColour
= *wxBLACK
;
4517 m_cellHighlightPenWidth
= 2;
4518 m_cellHighlightROPenWidth
= 1;
4520 m_canDragColMove
= false;
4522 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4523 m_winCapture
= (wxWindow
*)NULL
;
4524 m_canDragRowSize
= true;
4525 m_canDragColSize
= true;
4526 m_canDragGridSize
= true;
4527 m_canDragCell
= false;
4529 m_dragRowOrCol
= -1;
4530 m_isDragging
= false;
4531 m_startDragPos
= wxDefaultPosition
;
4533 m_waitForSlowClick
= false;
4535 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4536 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4538 m_currentCellCoords
= wxGridNoCellCoords
;
4542 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4543 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4545 m_editable
= true; // default for whole grid
4547 m_inOnKeyDown
= false;
4553 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4554 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4557 // ----------------------------------------------------------------------------
4558 // the idea is to call these functions only when necessary because they create
4559 // quite big arrays which eat memory mostly unnecessary - in particular, if
4560 // default widths/heights are used for all rows/columns, we may not use these
4563 // with some extra code, it should be possible to only store the widths/heights
4564 // different from default ones (resulting in space savings for huge grids) but
4565 // this is not done currently
4566 // ----------------------------------------------------------------------------
4568 void wxGrid::InitRowHeights()
4570 m_rowHeights
.Empty();
4571 m_rowBottoms
.Empty();
4573 m_rowHeights
.Alloc( m_numRows
);
4574 m_rowBottoms
.Alloc( m_numRows
);
4576 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4579 for ( int i
= 0; i
< m_numRows
; i
++ )
4581 rowBottom
+= m_defaultRowHeight
;
4582 m_rowBottoms
.Add( rowBottom
);
4586 void wxGrid::InitColWidths()
4588 m_colWidths
.Empty();
4589 m_colRights
.Empty();
4591 m_colWidths
.Alloc( m_numCols
);
4592 m_colRights
.Alloc( m_numCols
);
4594 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4597 for ( int i
= 0; i
< m_numCols
; i
++ )
4599 colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
4600 m_colRights
.Add( colRight
);
4604 int wxGrid::GetColWidth(int col
) const
4606 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4609 int wxGrid::GetColLeft(int col
) const
4611 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
4612 : m_colRights
[col
] - m_colWidths
[col
];
4615 int wxGrid::GetColRight(int col
) const
4617 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
4621 int wxGrid::GetRowHeight(int row
) const
4623 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4626 int wxGrid::GetRowTop(int row
) const
4628 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4629 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4632 int wxGrid::GetRowBottom(int row
) const
4634 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4635 : m_rowBottoms
[row
];
4638 void wxGrid::CalcDimensions()
4640 // compute the size of the scrollable area
4641 int w
= m_numCols
> 0 ? GetColRight(GetColAt(m_numCols
- 1)) : 0;
4642 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
4647 // take into account editor if shown
4648 if ( IsCellEditControlShown() )
4651 int r
= m_currentCellCoords
.GetRow();
4652 int c
= m_currentCellCoords
.GetCol();
4653 int x
= GetColLeft(c
);
4654 int y
= GetRowTop(r
);
4656 // how big is the editor
4657 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4658 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4659 editor
->GetControl()->GetSize(&w2
, &h2
);
4670 // preserve (more or less) the previous position
4672 GetViewStart( &x
, &y
);
4674 // ensure the position is valid for the new scroll ranges
4676 x
= wxMax( w
- 1, 0 );
4678 y
= wxMax( h
- 1, 0 );
4680 // do set scrollbar parameters
4681 SetScrollbars( m_scrollLineX
, m_scrollLineY
,
4682 GetScrollX(w
), GetScrollY(h
),
4684 GetBatchCount() != 0);
4686 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4687 // still must reposition the children
4691 void wxGrid::CalcWindowSizes()
4693 // escape if the window is has not been fully created yet
4695 if ( m_cornerLabelWin
== NULL
)
4699 GetClientSize( &cw
, &ch
);
4701 // this block of code tries to work around the following problem: the grid
4702 // could have been just resized to have enough space to show the full grid
4703 // window contents without the scrollbars, but its client size could be
4704 // not big enough because the grid has the scrollbars right now and so the
4705 // scrollbars would remain even though we don't need them any more
4707 // to prevent this from happening, check if we have enough space for
4708 // everything without the scrollbars and explicitly disable them then
4709 wxSize size
= GetSize() - GetWindowBorderSize();
4710 if ( size
!= wxSize(cw
, ch
) )
4712 // check if we have enough space for grid window after accounting for
4713 // the fixed size elements
4714 size
.x
-= m_rowLabelWidth
;
4715 size
.y
-= m_colLabelHeight
;
4717 const wxSize vsize
= m_gridWin
->GetVirtualSize();
4719 if ( size
.x
>= vsize
.x
&& size
.y
>= vsize
.y
)
4721 // yes, we do, so remove the scrollbars and use the new client size
4722 // (which should be the same as full window size - borders now)
4723 SetScrollbars(0, 0, 0, 0);
4724 GetClientSize(&cw
, &ch
);
4728 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4729 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4731 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4732 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
- m_rowLabelWidth
, m_colLabelHeight
);
4734 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4735 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
- m_colLabelHeight
);
4737 if ( m_gridWin
&& m_gridWin
->IsShown() )
4738 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
- m_rowLabelWidth
, ch
- m_colLabelHeight
);
4741 // this is called when the grid table sends a message
4742 // to indicate that it has been redimensioned
4744 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4747 bool result
= false;
4749 // Clear the attribute cache as the attribute might refer to a different
4750 // cell than stored in the cache after adding/removing rows/columns.
4753 // By the same reasoning, the editor should be dismissed if columns are
4754 // added or removed. And for consistency, it should IMHO always be
4755 // removed, not only if the cell "underneath" it actually changes.
4756 // For now, I intentionally do not save the editor's content as the
4757 // cell it might want to save that stuff to might no longer exist.
4758 HideCellEditControl();
4761 // if we were using the default widths/heights so far, we must change them
4763 if ( m_colWidths
.IsEmpty() )
4768 if ( m_rowHeights
.IsEmpty() )
4774 switch ( msg
.GetId() )
4776 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4778 size_t pos
= msg
.GetCommandInt();
4779 int numRows
= msg
.GetCommandInt2();
4781 m_numRows
+= numRows
;
4783 if ( !m_rowHeights
.IsEmpty() )
4785 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4786 m_rowBottoms
.Insert( 0, pos
, numRows
);
4790 bottom
= m_rowBottoms
[pos
- 1];
4792 for ( i
= pos
; i
< m_numRows
; i
++ )
4794 bottom
+= m_rowHeights
[i
];
4795 m_rowBottoms
[i
] = bottom
;
4799 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4801 // if we have just inserted cols into an empty grid the current
4802 // cell will be undefined...
4804 SetCurrentCell( 0, 0 );
4808 m_selection
->UpdateRows( pos
, numRows
);
4809 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4811 attrProvider
->UpdateAttrRows( pos
, numRows
);
4813 if ( !GetBatchCount() )
4816 m_rowLabelWin
->Refresh();
4822 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4824 int numRows
= msg
.GetCommandInt();
4825 int oldNumRows
= m_numRows
;
4826 m_numRows
+= numRows
;
4828 if ( !m_rowHeights
.IsEmpty() )
4830 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4831 m_rowBottoms
.Add( 0, numRows
);
4834 if ( oldNumRows
> 0 )
4835 bottom
= m_rowBottoms
[oldNumRows
- 1];
4837 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4839 bottom
+= m_rowHeights
[i
];
4840 m_rowBottoms
[i
] = bottom
;
4844 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4846 // if we have just inserted cols into an empty grid the current
4847 // cell will be undefined...
4849 SetCurrentCell( 0, 0 );
4852 if ( !GetBatchCount() )
4855 m_rowLabelWin
->Refresh();
4861 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4863 size_t pos
= msg
.GetCommandInt();
4864 int numRows
= msg
.GetCommandInt2();
4865 m_numRows
-= numRows
;
4867 if ( !m_rowHeights
.IsEmpty() )
4869 m_rowHeights
.RemoveAt( pos
, numRows
);
4870 m_rowBottoms
.RemoveAt( pos
, numRows
);
4873 for ( i
= 0; i
< m_numRows
; i
++ )
4875 h
+= m_rowHeights
[i
];
4876 m_rowBottoms
[i
] = h
;
4882 m_currentCellCoords
= wxGridNoCellCoords
;
4886 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4887 m_currentCellCoords
.Set( 0, 0 );
4891 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4892 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4895 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4897 // ifdef'd out following patch from Paul Gammans
4899 // No need to touch column attributes, unless we
4900 // removed _all_ rows, in this case, we remove
4901 // all column attributes.
4902 // I hate to do this here, but the
4903 // needed data is not available inside UpdateAttrRows.
4904 if ( !GetNumberRows() )
4905 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4909 if ( !GetBatchCount() )
4912 m_rowLabelWin
->Refresh();
4918 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4920 size_t pos
= msg
.GetCommandInt();
4921 int numCols
= msg
.GetCommandInt2();
4922 m_numCols
+= numCols
;
4924 if ( !m_colAt
.IsEmpty() )
4926 //Shift the column IDs
4928 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
4930 if ( m_colAt
[i
] >= (int)pos
)
4931 m_colAt
[i
] += numCols
;
4934 m_colAt
.Insert( pos
, pos
, numCols
);
4936 //Set the new columns' positions
4937 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
4943 if ( !m_colWidths
.IsEmpty() )
4945 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4946 m_colRights
.Insert( 0, pos
, numCols
);
4950 right
= m_colRights
[GetColAt( pos
- 1 )];
4953 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
4955 i
= GetColAt( colPos
);
4957 right
+= m_colWidths
[i
];
4958 m_colRights
[i
] = right
;
4962 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4964 // if we have just inserted cols into an empty grid the current
4965 // cell will be undefined...
4967 SetCurrentCell( 0, 0 );
4971 m_selection
->UpdateCols( pos
, numCols
);
4972 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4974 attrProvider
->UpdateAttrCols( pos
, numCols
);
4975 if ( !GetBatchCount() )
4978 m_colLabelWin
->Refresh();
4984 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4986 int numCols
= msg
.GetCommandInt();
4987 int oldNumCols
= m_numCols
;
4988 m_numCols
+= numCols
;
4990 if ( !m_colAt
.IsEmpty() )
4992 m_colAt
.Add( 0, numCols
);
4994 //Set the new columns' positions
4996 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
5002 if ( !m_colWidths
.IsEmpty() )
5004 m_colWidths
.Add( m_defaultColWidth
, numCols
);
5005 m_colRights
.Add( 0, numCols
);
5008 if ( oldNumCols
> 0 )
5009 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
5012 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
5014 i
= GetColAt( colPos
);
5016 right
+= m_colWidths
[i
];
5017 m_colRights
[i
] = right
;
5021 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5023 // if we have just inserted cols into an empty grid the current
5024 // cell will be undefined...
5026 SetCurrentCell( 0, 0 );
5028 if ( !GetBatchCount() )
5031 m_colLabelWin
->Refresh();
5037 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5039 size_t pos
= msg
.GetCommandInt();
5040 int numCols
= msg
.GetCommandInt2();
5041 m_numCols
-= numCols
;
5043 if ( !m_colAt
.IsEmpty() )
5045 int colID
= GetColAt( pos
);
5047 m_colAt
.RemoveAt( pos
, numCols
);
5049 //Shift the column IDs
5051 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5053 if ( m_colAt
[colPos
] > colID
)
5054 m_colAt
[colPos
] -= numCols
;
5058 if ( !m_colWidths
.IsEmpty() )
5060 m_colWidths
.RemoveAt( pos
, numCols
);
5061 m_colRights
.RemoveAt( pos
, numCols
);
5065 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5067 i
= GetColAt( colPos
);
5069 w
+= m_colWidths
[i
];
5076 m_currentCellCoords
= wxGridNoCellCoords
;
5080 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
5081 m_currentCellCoords
.Set( 0, 0 );
5085 m_selection
->UpdateCols( pos
, -((int)numCols
) );
5086 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5089 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
5091 // ifdef'd out following patch from Paul Gammans
5093 // No need to touch row attributes, unless we
5094 // removed _all_ columns, in this case, we remove
5095 // all row attributes.
5096 // I hate to do this here, but the
5097 // needed data is not available inside UpdateAttrCols.
5098 if ( !GetNumberCols() )
5099 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
5103 if ( !GetBatchCount() )
5106 m_colLabelWin
->Refresh();
5113 if (result
&& !GetBatchCount() )
5114 m_gridWin
->Refresh();
5119 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
) const
5121 wxRegionIterator
iter( reg
);
5124 wxArrayInt rowlabels
;
5131 // TODO: remove this when we can...
5132 // There is a bug in wxMotif that gives garbage update
5133 // rectangles if you jump-scroll a long way by clicking the
5134 // scrollbar with middle button. This is a work-around
5136 #if defined(__WXMOTIF__)
5138 m_gridWin
->GetClientSize( &cw
, &ch
);
5139 if ( r
.GetTop() > ch
)
5141 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5144 // logical bounds of update region
5147 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
5148 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
5150 // find the row labels within these bounds
5153 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5155 if ( GetRowBottom(row
) < top
)
5158 if ( GetRowTop(row
) > bottom
)
5161 rowlabels
.Add( row
);
5170 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
) const
5172 wxRegionIterator
iter( reg
);
5175 wxArrayInt colLabels
;
5182 // TODO: remove this when we can...
5183 // There is a bug in wxMotif that gives garbage update
5184 // rectangles if you jump-scroll a long way by clicking the
5185 // scrollbar with middle button. This is a work-around
5187 #if defined(__WXMOTIF__)
5189 m_gridWin
->GetClientSize( &cw
, &ch
);
5190 if ( r
.GetLeft() > cw
)
5192 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5195 // logical bounds of update region
5198 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
5199 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
5201 // find the cells within these bounds
5205 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5207 col
= GetColAt( colPos
);
5209 if ( GetColRight(col
) < left
)
5212 if ( GetColLeft(col
) > right
)
5215 colLabels
.Add( col
);
5224 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
) const
5226 wxRegionIterator
iter( reg
);
5229 wxGridCellCoordsArray cellsExposed
;
5231 int left
, top
, right
, bottom
;
5236 // TODO: remove this when we can...
5237 // There is a bug in wxMotif that gives garbage update
5238 // rectangles if you jump-scroll a long way by clicking the
5239 // scrollbar with middle button. This is a work-around
5241 #if defined(__WXMOTIF__)
5243 m_gridWin
->GetClientSize( &cw
, &ch
);
5244 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5245 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5246 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5247 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5250 // logical bounds of update region
5252 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5253 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5255 // find the cells within these bounds
5258 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5260 if ( GetRowBottom(row
) <= top
)
5263 if ( GetRowTop(row
) > bottom
)
5267 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5269 col
= GetColAt( colPos
);
5271 if ( GetColRight(col
) <= left
)
5274 if ( GetColLeft(col
) > right
)
5277 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
5284 return cellsExposed
;
5288 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5291 wxPoint
pos( event
.GetPosition() );
5292 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5294 if ( event
.Dragging() )
5298 m_isDragging
= true;
5299 m_rowLabelWin
->CaptureMouse();
5302 if ( event
.LeftIsDown() )
5304 switch ( m_cursorMode
)
5306 case WXGRID_CURSOR_RESIZE_ROW
:
5308 int cw
, ch
, left
, dummy
;
5309 m_gridWin
->GetClientSize( &cw
, &ch
);
5310 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5312 wxClientDC
dc( m_gridWin
);
5315 GetRowTop(m_dragRowOrCol
) +
5316 GetRowMinimalHeight(m_dragRowOrCol
) );
5317 dc
.SetLogicalFunction(wxINVERT
);
5318 if ( m_dragLastPos
>= 0 )
5320 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5322 dc
.DrawLine( left
, y
, left
+cw
, y
);
5327 case WXGRID_CURSOR_SELECT_ROW
:
5329 if ( (row
= YToRow( y
)) >= 0 )
5333 m_selection
->SelectRow( row
,
5334 event
.ControlDown(),
5343 // default label to suppress warnings about "enumeration value
5344 // 'xxx' not handled in switch
5352 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5357 if (m_rowLabelWin
->HasCapture())
5358 m_rowLabelWin
->ReleaseMouse();
5359 m_isDragging
= false;
5362 // ------------ Entering or leaving the window
5364 if ( event
.Entering() || event
.Leaving() )
5366 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5369 // ------------ Left button pressed
5371 else if ( event
.LeftDown() )
5373 // don't send a label click event for a hit on the
5374 // edge of the row label - this is probably the user
5375 // wanting to resize the row
5377 if ( YToEdgeOfRow(y
) < 0 )
5381 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5383 if ( !event
.ShiftDown() && !event
.CmdDown() )
5387 if ( event
.ShiftDown() )
5389 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5392 GetNumberCols() - 1,
5393 event
.ControlDown(),
5400 m_selection
->SelectRow( row
,
5401 event
.ControlDown(),
5408 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5413 // starting to drag-resize a row
5414 if ( CanDragRowSize() )
5415 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5419 // ------------ Left double click
5421 else if (event
.LeftDClick() )
5423 row
= YToEdgeOfRow(y
);
5428 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5430 // no default action at the moment
5435 // adjust row height depending on label text
5436 AutoSizeRowLabelSize( row
);
5438 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5443 // ------------ Left button released
5445 else if ( event
.LeftUp() )
5447 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5449 DoEndDragResizeRow();
5451 // Note: we are ending the event *after* doing
5452 // default processing in this case
5454 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5457 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5461 // ------------ Right button down
5463 else if ( event
.RightDown() )
5467 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5469 // no default action at the moment
5473 // ------------ Right double click
5475 else if ( event
.RightDClick() )
5479 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5481 // no default action at the moment
5485 // ------------ No buttons down and mouse moving
5487 else if ( event
.Moving() )
5489 m_dragRowOrCol
= YToEdgeOfRow( y
);
5490 if ( m_dragRowOrCol
>= 0 )
5492 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5494 // don't capture the mouse yet
5495 if ( CanDragRowSize() )
5496 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5499 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5501 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5506 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5509 wxPoint
pos( event
.GetPosition() );
5510 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5512 if ( event
.Dragging() )
5516 m_isDragging
= true;
5517 m_colLabelWin
->CaptureMouse();
5519 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
)
5520 m_dragRowOrCol
= XToCol( x
);
5523 if ( event
.LeftIsDown() )
5525 switch ( m_cursorMode
)
5527 case WXGRID_CURSOR_RESIZE_COL
:
5529 int cw
, ch
, dummy
, top
;
5530 m_gridWin
->GetClientSize( &cw
, &ch
);
5531 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5533 wxClientDC
dc( m_gridWin
);
5536 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5537 GetColMinimalWidth(m_dragRowOrCol
));
5538 dc
.SetLogicalFunction(wxINVERT
);
5539 if ( m_dragLastPos
>= 0 )
5541 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5543 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5548 case WXGRID_CURSOR_SELECT_COL
:
5550 if ( (col
= XToCol( x
)) >= 0 )
5554 m_selection
->SelectCol( col
,
5555 event
.ControlDown(),
5564 case WXGRID_CURSOR_MOVE_COL
:
5567 m_moveToCol
= GetColAt( 0 );
5569 m_moveToCol
= XToCol( x
);
5573 if ( m_moveToCol
< 0 )
5574 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5576 markerX
= GetColLeft( m_moveToCol
);
5578 if ( markerX
!= m_dragLastPos
)
5580 wxClientDC
dc( m_colLabelWin
);
5583 m_colLabelWin
->GetClientSize( &cw
, &ch
);
5587 //Clean up the last indicator
5588 if ( m_dragLastPos
>= 0 )
5590 wxPen
pen( m_colLabelWin
->GetBackgroundColour(), 2 );
5592 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
5593 dc
.SetPen(wxNullPen
);
5595 if ( XToCol( m_dragLastPos
) != -1 )
5596 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
5599 //Moving to the same place? Don't draw a marker
5600 if ( (m_moveToCol
== m_dragRowOrCol
)
5601 || (GetColPos( m_moveToCol
) == GetColPos( m_dragRowOrCol
) + 1)
5602 || (m_moveToCol
< 0 && m_dragRowOrCol
== GetColAt( m_numCols
- 1 )))
5609 wxPen
pen( *wxBLUE
, 2 );
5612 dc
.DrawLine( markerX
, 0, markerX
, ch
);
5614 dc
.SetPen(wxNullPen
);
5616 m_dragLastPos
= markerX
- 1;
5621 // default label to suppress warnings about "enumeration value
5622 // 'xxx' not handled in switch
5630 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5635 if (m_colLabelWin
->HasCapture())
5636 m_colLabelWin
->ReleaseMouse();
5637 m_isDragging
= false;
5640 // ------------ Entering or leaving the window
5642 if ( event
.Entering() || event
.Leaving() )
5644 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5647 // ------------ Left button pressed
5649 else if ( event
.LeftDown() )
5651 // don't send a label click event for a hit on the
5652 // edge of the col label - this is probably the user
5653 // wanting to resize the col
5655 if ( XToEdgeOfCol(x
) < 0 )
5659 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5661 if ( m_canDragColMove
)
5663 //Show button as pressed
5664 wxClientDC
dc( m_colLabelWin
);
5665 int colLeft
= GetColLeft( col
);
5666 int colRight
= GetColRight( col
) - 1;
5667 dc
.SetPen( wxPen( m_colLabelWin
->GetBackgroundColour(), 1 ) );
5668 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
5669 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
5671 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, m_colLabelWin
);
5675 if ( !event
.ShiftDown() && !event
.CmdDown() )
5679 if ( event
.ShiftDown() )
5681 m_selection
->SelectBlock( 0,
5682 m_currentCellCoords
.GetCol(),
5683 GetNumberRows() - 1, col
,
5684 event
.ControlDown(),
5691 m_selection
->SelectCol( col
,
5692 event
.ControlDown(),
5699 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5705 // starting to drag-resize a col
5707 if ( CanDragColSize() )
5708 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5712 // ------------ Left double click
5714 if ( event
.LeftDClick() )
5716 col
= XToEdgeOfCol(x
);
5721 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5723 // no default action at the moment
5728 // adjust column width depending on label text
5729 AutoSizeColLabelSize( col
);
5731 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5736 // ------------ Left button released
5738 else if ( event
.LeftUp() )
5740 switch ( m_cursorMode
)
5742 case WXGRID_CURSOR_RESIZE_COL
:
5743 DoEndDragResizeCol();
5745 // Note: we are ending the event *after* doing
5746 // default processing in this case
5748 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5751 case WXGRID_CURSOR_MOVE_COL
:
5754 SendEvent( wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
, event
);
5757 case WXGRID_CURSOR_SELECT_COL
:
5758 case WXGRID_CURSOR_SELECT_CELL
:
5759 case WXGRID_CURSOR_RESIZE_ROW
:
5760 case WXGRID_CURSOR_SELECT_ROW
:
5761 // nothing to do (?)
5765 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5769 // ------------ Right button down
5771 else if ( event
.RightDown() )
5775 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5777 // no default action at the moment
5781 // ------------ Right double click
5783 else if ( event
.RightDClick() )
5787 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5789 // no default action at the moment
5793 // ------------ No buttons down and mouse moving
5795 else if ( event
.Moving() )
5797 m_dragRowOrCol
= XToEdgeOfCol( x
);
5798 if ( m_dragRowOrCol
>= 0 )
5800 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5802 // don't capture the cursor yet
5803 if ( CanDragColSize() )
5804 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5807 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5809 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5814 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5816 if ( event
.LeftDown() )
5818 // indicate corner label by having both row and
5821 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5826 else if ( event
.LeftDClick() )
5828 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5830 else if ( event
.RightDown() )
5832 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5834 // no default action at the moment
5837 else if ( event
.RightDClick() )
5839 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5841 // no default action at the moment
5846 void wxGrid::CancelMouseCapture()
5848 // cancel operation currently in progress, whatever it is
5851 m_isDragging
= false;
5852 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
5853 m_winCapture
->SetCursor( *wxSTANDARD_CURSOR
);
5854 m_winCapture
= NULL
;
5856 // remove traces of whatever we drew on screen
5861 void wxGrid::ChangeCursorMode(CursorMode mode
,
5866 static const wxChar
*cursorModes
[] =
5876 wxLogTrace(_T("grid"),
5877 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5878 win
== m_colLabelWin
? _T("colLabelWin")
5879 : win
? _T("rowLabelWin")
5881 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5884 if ( mode
== m_cursorMode
&&
5885 win
== m_winCapture
&&
5886 captureMouse
== (m_winCapture
!= NULL
))
5891 // by default use the grid itself
5897 if (m_winCapture
->HasCapture())
5898 m_winCapture
->ReleaseMouse();
5899 m_winCapture
= (wxWindow
*)NULL
;
5902 m_cursorMode
= mode
;
5904 switch ( m_cursorMode
)
5906 case WXGRID_CURSOR_RESIZE_ROW
:
5907 win
->SetCursor( m_rowResizeCursor
);
5910 case WXGRID_CURSOR_RESIZE_COL
:
5911 win
->SetCursor( m_colResizeCursor
);
5914 case WXGRID_CURSOR_MOVE_COL
:
5915 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
5919 win
->SetCursor( *wxSTANDARD_CURSOR
);
5923 // we need to capture mouse when resizing
5924 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5925 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5927 if ( captureMouse
&& resize
)
5929 win
->CaptureMouse();
5934 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5937 wxPoint
pos( event
.GetPosition() );
5938 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5940 wxGridCellCoords coords
;
5941 XYToCell( x
, y
, coords
);
5943 int cell_rows
, cell_cols
;
5944 bool isFirstDrag
= !m_isDragging
;
5945 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5946 if ((cell_rows
< 0) || (cell_cols
< 0))
5948 coords
.SetRow(coords
.GetRow() + cell_rows
);
5949 coords
.SetCol(coords
.GetCol() + cell_cols
);
5952 if ( event
.Dragging() )
5954 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5956 // Don't start doing anything until the mouse has been dragged at
5957 // least 3 pixels in any direction...
5960 if (m_startDragPos
== wxDefaultPosition
)
5962 m_startDragPos
= pos
;
5965 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5969 m_isDragging
= true;
5970 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5972 // Hide the edit control, so it
5973 // won't interfere with drag-shrinking.
5974 if ( IsCellEditControlShown() )
5976 HideCellEditControl();
5977 SaveEditControlValue();
5980 if ( coords
!= wxGridNoCellCoords
)
5982 if ( event
.CmdDown() )
5984 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5985 m_selectingKeyboard
= coords
;
5986 HighlightBlock( m_selectingKeyboard
, coords
);
5988 else if ( CanDragCell() )
5992 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5993 m_selectingKeyboard
= coords
;
5995 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
6004 if ( !IsSelection() )
6006 HighlightBlock( coords
, coords
);
6010 HighlightBlock( m_currentCellCoords
, coords
);
6014 if (! IsVisible(coords
))
6016 MakeCellVisible(coords
);
6017 // TODO: need to introduce a delay or something here. The
6018 // scrolling is way to fast, at least on MSW - also on GTK.
6021 // Have we captured the mouse yet?
6024 m_winCapture
= m_gridWin
;
6025 m_winCapture
->CaptureMouse();
6030 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6032 int cw
, ch
, left
, dummy
;
6033 m_gridWin
->GetClientSize( &cw
, &ch
);
6034 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6036 wxClientDC
dc( m_gridWin
);
6038 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
6039 GetRowMinimalHeight(m_dragRowOrCol
) );
6040 dc
.SetLogicalFunction(wxINVERT
);
6041 if ( m_dragLastPos
>= 0 )
6043 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
6045 dc
.DrawLine( left
, y
, left
+cw
, y
);
6048 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6050 int cw
, ch
, dummy
, top
;
6051 m_gridWin
->GetClientSize( &cw
, &ch
);
6052 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6054 wxClientDC
dc( m_gridWin
);
6056 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
6057 GetColMinimalWidth(m_dragRowOrCol
) );
6058 dc
.SetLogicalFunction(wxINVERT
);
6059 if ( m_dragLastPos
>= 0 )
6061 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6063 dc
.DrawLine( x
, top
, x
, top
+ ch
);
6070 m_isDragging
= false;
6071 m_startDragPos
= wxDefaultPosition
;
6073 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
6074 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
6077 if ( event
.Entering() || event
.Leaving() )
6079 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6080 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
6085 // ------------ Left button pressed
6087 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
6089 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
6094 if ( !event
.CmdDown() )
6096 if ( event
.ShiftDown() )
6100 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
6101 m_currentCellCoords
.GetCol(),
6104 event
.ControlDown(),
6110 else if ( XToEdgeOfCol(x
) < 0 &&
6111 YToEdgeOfRow(y
) < 0 )
6113 DisableCellEditControl();
6114 MakeCellVisible( coords
);
6116 if ( event
.CmdDown() )
6120 m_selection
->ToggleCellSelection( coords
.GetRow(),
6122 event
.ControlDown(),
6127 m_selectingTopLeft
= wxGridNoCellCoords
;
6128 m_selectingBottomRight
= wxGridNoCellCoords
;
6129 m_selectingKeyboard
= coords
;
6133 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
6134 SetCurrentCell( coords
);
6137 if ( m_selection
->GetSelectionMode() !=
6138 wxGrid::wxGridSelectCells
)
6140 HighlightBlock( coords
, coords
);
6148 // ------------ Left double click
6150 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
6152 DisableCellEditControl();
6154 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
6156 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
6161 // we want double click to select a cell and start editing
6162 // (i.e. to behave in same way as sequence of two slow clicks):
6163 m_waitForSlowClick
= true;
6168 // ------------ Left button released
6170 else if ( event
.LeftUp() )
6172 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6176 if (m_winCapture
->HasCapture())
6177 m_winCapture
->ReleaseMouse();
6178 m_winCapture
= NULL
;
6181 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
6184 EnableCellEditControl();
6186 wxGridCellAttr
*attr
= GetCellAttr(coords
);
6187 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
6188 editor
->StartingClick();
6192 m_waitForSlowClick
= false;
6194 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6195 m_selectingBottomRight
!= wxGridNoCellCoords
)
6199 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6200 m_selectingTopLeft
.GetCol(),
6201 m_selectingBottomRight
.GetRow(),
6202 m_selectingBottomRight
.GetCol(),
6203 event
.ControlDown(),
6209 m_selectingTopLeft
= wxGridNoCellCoords
;
6210 m_selectingBottomRight
= wxGridNoCellCoords
;
6212 // Show the edit control, if it has been hidden for
6214 ShowCellEditControl();
6217 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6219 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6220 DoEndDragResizeRow();
6222 // Note: we are ending the event *after* doing
6223 // default processing in this case
6225 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
6227 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6229 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6230 DoEndDragResizeCol();
6232 // Note: we are ending the event *after* doing
6233 // default processing in this case
6235 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
6241 // ------------ Right button down
6243 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
6245 DisableCellEditControl();
6246 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
6251 // no default action at the moment
6255 // ------------ Right double click
6257 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
6259 DisableCellEditControl();
6260 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
6265 // no default action at the moment
6269 // ------------ Moving and no button action
6271 else if ( event
.Moving() && !event
.IsButton() )
6273 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
6275 // out of grid cell area
6276 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6280 int dragRow
= YToEdgeOfRow( y
);
6281 int dragCol
= XToEdgeOfCol( x
);
6283 // Dragging on the corner of a cell to resize in both
6284 // directions is not implemented yet...
6286 if ( dragRow
>= 0 && dragCol
>= 0 )
6288 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6294 m_dragRowOrCol
= dragRow
;
6296 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6298 if ( CanDragRowSize() && CanDragGridSize() )
6299 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false);
6302 else if ( dragCol
>= 0 )
6304 m_dragRowOrCol
= dragCol
;
6306 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6308 if ( CanDragColSize() && CanDragGridSize() )
6309 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false);
6312 else // Neither on a row or col edge
6314 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6316 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6322 void wxGrid::DoEndDragResizeRow()
6324 if ( m_dragLastPos
>= 0 )
6326 // erase the last line and resize the row
6328 int cw
, ch
, left
, dummy
;
6329 m_gridWin
->GetClientSize( &cw
, &ch
);
6330 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6332 wxClientDC
dc( m_gridWin
);
6334 dc
.SetLogicalFunction( wxINVERT
);
6335 dc
.DrawLine( left
, m_dragLastPos
, left
+ cw
, m_dragLastPos
);
6336 HideCellEditControl();
6337 SaveEditControlValue();
6339 int rowTop
= GetRowTop(m_dragRowOrCol
);
6340 SetRowSize( m_dragRowOrCol
,
6341 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
6343 if ( !GetBatchCount() )
6345 // Only needed to get the correct rect.y:
6346 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
6348 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6349 rect
.width
= m_rowLabelWidth
;
6350 rect
.height
= ch
- rect
.y
;
6351 m_rowLabelWin
->Refresh( true, &rect
);
6354 // if there is a multicell block, paint all of it
6357 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
6358 int leftCol
= XToCol(left
);
6359 int rightCol
= internalXToCol(left
+ cw
);
6362 for (i
=leftCol
; i
<rightCol
; i
++)
6364 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
6365 if (cell_rows
< subtract_rows
)
6366 subtract_rows
= cell_rows
;
6368 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
6369 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6370 rect
.height
= ch
- rect
.y
;
6373 m_gridWin
->Refresh( false, &rect
);
6376 ShowCellEditControl();
6381 void wxGrid::DoEndDragResizeCol()
6383 if ( m_dragLastPos
>= 0 )
6385 // erase the last line and resize the col
6387 int cw
, ch
, dummy
, top
;
6388 m_gridWin
->GetClientSize( &cw
, &ch
);
6389 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6391 wxClientDC
dc( m_gridWin
);
6393 dc
.SetLogicalFunction( wxINVERT
);
6394 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6395 HideCellEditControl();
6396 SaveEditControlValue();
6398 int colLeft
= GetColLeft(m_dragRowOrCol
);
6399 SetColSize( m_dragRowOrCol
,
6400 wxMax( m_dragLastPos
- colLeft
,
6401 GetColMinimalWidth(m_dragRowOrCol
) ) );
6403 if ( !GetBatchCount() )
6405 // Only needed to get the correct rect.x:
6406 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
6408 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6409 rect
.width
= cw
- rect
.x
;
6410 rect
.height
= m_colLabelHeight
;
6411 m_colLabelWin
->Refresh( true, &rect
);
6414 // if there is a multicell block, paint all of it
6417 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
6418 int topRow
= YToRow(top
);
6419 int bottomRow
= internalYToRow(top
+ cw
);
6422 for (i
=topRow
; i
<bottomRow
; i
++)
6424 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
6425 if (cell_cols
< subtract_cols
)
6426 subtract_cols
= cell_cols
;
6429 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
6430 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6431 rect
.width
= cw
- rect
.x
;
6435 m_gridWin
->Refresh( false, &rect
);
6438 ShowCellEditControl();
6442 void wxGrid::DoEndDragMoveCol()
6444 //The user clicked on the column but didn't actually drag
6445 if ( m_dragLastPos
< 0 )
6447 m_colLabelWin
->Refresh(); //Do this to "unpress" the column
6452 if ( m_moveToCol
== -1 )
6453 newPos
= m_numCols
- 1;
6456 newPos
= GetColPos( m_moveToCol
);
6457 if ( newPos
> GetColPos( m_dragRowOrCol
) )
6461 SetColPos( m_dragRowOrCol
, newPos
);
6464 void wxGrid::SetColPos( int colID
, int newPos
)
6466 if ( m_colAt
.IsEmpty() )
6468 m_colAt
.Alloc( m_numCols
);
6471 for ( i
= 0; i
< m_numCols
; i
++ )
6477 int oldPos
= GetColPos( colID
);
6479 //Reshuffle the m_colAt array
6480 if ( newPos
> oldPos
)
6483 for ( i
= oldPos
; i
< newPos
; i
++ )
6485 m_colAt
[i
] = m_colAt
[i
+1];
6491 for ( i
= oldPos
; i
> newPos
; i
-- )
6493 m_colAt
[i
] = m_colAt
[i
-1];
6497 m_colAt
[newPos
] = colID
;
6499 //Recalculate the column rights
6500 if ( !m_colWidths
.IsEmpty() )
6504 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6506 int colID
= GetColAt( colPos
);
6508 colRight
+= m_colWidths
[colID
];
6509 m_colRights
[colID
] = colRight
;
6513 m_colLabelWin
->Refresh();
6514 m_gridWin
->Refresh();
6519 void wxGrid::EnableDragColMove( bool enable
)
6521 if ( m_canDragColMove
== enable
)
6524 m_canDragColMove
= enable
;
6526 if ( !m_canDragColMove
)
6530 //Recalculate the column rights
6531 if ( !m_colWidths
.IsEmpty() )
6535 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6537 colRight
+= m_colWidths
[colPos
];
6538 m_colRights
[colPos
] = colRight
;
6542 m_colLabelWin
->Refresh();
6543 m_gridWin
->Refresh();
6549 // ------ interaction with data model
6551 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6553 switch ( msg
.GetId() )
6555 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6556 return GetModelValues();
6558 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6559 return SetModelValues();
6561 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6562 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6563 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6564 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6565 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6566 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6567 return Redimension( msg
);
6574 // The behaviour of this function depends on the grid table class
6575 // Clear() function. For the default wxGridStringTable class the
6576 // behavious is to replace all cell contents with wxEmptyString but
6577 // not to change the number of rows or cols.
6579 void wxGrid::ClearGrid()
6583 if (IsCellEditControlEnabled())
6584 DisableCellEditControl();
6587 if (!GetBatchCount())
6588 m_gridWin
->Refresh();
6592 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6594 // TODO: something with updateLabels flag
6598 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6604 if (IsCellEditControlEnabled())
6605 DisableCellEditControl();
6607 bool done
= m_table
->InsertRows( pos
, numRows
);
6610 // the table will have sent the results of the insert row
6611 // operation to this view object as a grid table message
6617 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6619 // TODO: something with updateLabels flag
6623 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6629 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6632 // the table will have sent the results of the append row
6633 // operation to this view object as a grid table message
6639 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6641 // TODO: something with updateLabels flag
6645 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6651 if (IsCellEditControlEnabled())
6652 DisableCellEditControl();
6654 bool done
= m_table
->DeleteRows( pos
, numRows
);
6656 // the table will have sent the results of the delete row
6657 // operation to this view object as a grid table message
6663 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6665 // TODO: something with updateLabels flag
6669 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6675 if (IsCellEditControlEnabled())
6676 DisableCellEditControl();
6678 bool done
= m_table
->InsertCols( pos
, numCols
);
6680 // the table will have sent the results of the insert col
6681 // operation to this view object as a grid table message
6687 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6689 // TODO: something with updateLabels flag
6693 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6699 bool done
= m_table
->AppendCols( numCols
);
6701 // the table will have sent the results of the append col
6702 // operation to this view object as a grid table message
6708 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6710 // TODO: something with updateLabels flag
6714 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6720 if (IsCellEditControlEnabled())
6721 DisableCellEditControl();
6723 bool done
= m_table
->DeleteCols( pos
, numCols
);
6725 // the table will have sent the results of the delete col
6726 // operation to this view object as a grid table message
6733 // ----- event handlers
6736 // Generate a grid event based on a mouse event and
6737 // return the result of ProcessEvent()
6739 int wxGrid::SendEvent( const wxEventType type
,
6741 wxMouseEvent
& mouseEv
)
6743 bool claimed
, vetoed
;
6745 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6747 int rowOrCol
= (row
== -1 ? col
: row
);
6749 wxGridSizeEvent
gridEvt( GetId(),
6753 mouseEv
.GetX() + GetRowLabelSize(),
6754 mouseEv
.GetY() + GetColLabelSize(),
6755 mouseEv
.ControlDown(),
6756 mouseEv
.ShiftDown(),
6758 mouseEv
.MetaDown() );
6760 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6761 vetoed
= !gridEvt
.IsAllowed();
6763 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6765 // Right now, it should _never_ end up here!
6766 wxGridRangeSelectEvent
gridEvt( GetId(),
6770 m_selectingBottomRight
,
6772 mouseEv
.ControlDown(),
6773 mouseEv
.ShiftDown(),
6775 mouseEv
.MetaDown() );
6777 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6778 vetoed
= !gridEvt
.IsAllowed();
6780 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
6781 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
6782 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
6783 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
6785 wxPoint pos
= mouseEv
.GetPosition();
6787 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
6788 pos
.y
+= GetColLabelSize();
6789 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
6790 pos
.x
+= GetRowLabelSize();
6792 wxGridEvent
gridEvt( GetId(),
6799 mouseEv
.ControlDown(),
6800 mouseEv
.ShiftDown(),
6802 mouseEv
.MetaDown() );
6803 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6804 vetoed
= !gridEvt
.IsAllowed();
6808 wxGridEvent
gridEvt( GetId(),
6812 mouseEv
.GetX() + GetRowLabelSize(),
6813 mouseEv
.GetY() + GetColLabelSize(),
6815 mouseEv
.ControlDown(),
6816 mouseEv
.ShiftDown(),
6818 mouseEv
.MetaDown() );
6819 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6820 vetoed
= !gridEvt
.IsAllowed();
6823 // A Veto'd event may not be `claimed' so test this first
6827 return claimed
? 1 : 0;
6830 // Generate a grid event of specified type and return the result
6831 // of ProcessEvent().
6833 int wxGrid::SendEvent( const wxEventType type
,
6836 bool claimed
, vetoed
;
6838 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6840 int rowOrCol
= (row
== -1 ? col
: row
);
6842 wxGridSizeEvent
gridEvt( GetId(), type
, this, rowOrCol
);
6844 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6845 vetoed
= !gridEvt
.IsAllowed();
6849 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
6851 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6852 vetoed
= !gridEvt
.IsAllowed();
6855 // A Veto'd event may not be `claimed' so test this first
6859 return claimed
? 1 : 0;
6862 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6864 // needed to prevent zillions of paint events on MSW
6868 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6870 // Don't do anything if between Begin/EndBatch...
6871 // EndBatch() will do all this on the last nested one anyway.
6872 if (! GetBatchCount())
6874 // Refresh to get correct scrolled position:
6875 wxScrolledWindow::Refresh(eraseb
, rect
);
6879 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6880 int width_label
, width_cell
, height_label
, height_cell
;
6883 // Copy rectangle can get scroll offsets..
6884 rect_x
= rect
->GetX();
6885 rect_y
= rect
->GetY();
6886 rectWidth
= rect
->GetWidth();
6887 rectHeight
= rect
->GetHeight();
6889 width_label
= m_rowLabelWidth
- rect_x
;
6890 if (width_label
> rectWidth
)
6891 width_label
= rectWidth
;
6893 height_label
= m_colLabelHeight
- rect_y
;
6894 if (height_label
> rectHeight
)
6895 height_label
= rectHeight
;
6897 if (rect_x
> m_rowLabelWidth
)
6899 x
= rect_x
- m_rowLabelWidth
;
6900 width_cell
= rectWidth
;
6905 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6908 if (rect_y
> m_colLabelHeight
)
6910 y
= rect_y
- m_colLabelHeight
;
6911 height_cell
= rectHeight
;
6916 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6919 // Paint corner label part intersecting rect.
6920 if ( width_label
> 0 && height_label
> 0 )
6922 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6923 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6926 // Paint col labels part intersecting rect.
6927 if ( width_cell
> 0 && height_label
> 0 )
6929 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6930 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6933 // Paint row labels part intersecting rect.
6934 if ( width_label
> 0 && height_cell
> 0 )
6936 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6937 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6940 // Paint cell area part intersecting rect.
6941 if ( width_cell
> 0 && height_cell
> 0 )
6943 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6944 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6949 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6950 m_colLabelWin
->Refresh(eraseb
, NULL
);
6951 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6952 m_gridWin
->Refresh(eraseb
, NULL
);
6957 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
))
6959 if (m_targetWindow
!= this) // check whether initialisation has been done
6961 // update our children window positions and scrollbars
6966 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6968 if ( m_inOnKeyDown
)
6970 // shouldn't be here - we are going round in circles...
6972 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6975 m_inOnKeyDown
= true;
6977 // propagate the event up and see if it gets processed
6978 wxWindow
*parent
= GetParent();
6979 wxKeyEvent
keyEvt( event
);
6980 keyEvt
.SetEventObject( parent
);
6982 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6984 if (GetLayoutDirection() == wxLayout_RightToLeft
)
6986 if (event
.GetKeyCode() == WXK_RIGHT
)
6987 event
.m_keyCode
= WXK_LEFT
;
6988 else if (event
.GetKeyCode() == WXK_LEFT
)
6989 event
.m_keyCode
= WXK_RIGHT
;
6992 // try local handlers
6993 switch ( event
.GetKeyCode() )
6996 if ( event
.ControlDown() )
6997 MoveCursorUpBlock( event
.ShiftDown() );
6999 MoveCursorUp( event
.ShiftDown() );
7003 if ( event
.ControlDown() )
7004 MoveCursorDownBlock( event
.ShiftDown() );
7006 MoveCursorDown( event
.ShiftDown() );
7010 if ( event
.ControlDown() )
7011 MoveCursorLeftBlock( event
.ShiftDown() );
7013 MoveCursorLeft( event
.ShiftDown() );
7017 if ( event
.ControlDown() )
7018 MoveCursorRightBlock( event
.ShiftDown() );
7020 MoveCursorRight( event
.ShiftDown() );
7024 case WXK_NUMPAD_ENTER
:
7025 if ( event
.ControlDown() )
7027 event
.Skip(); // to let the edit control have the return
7031 if ( GetGridCursorRow() < GetNumberRows()-1 )
7033 MoveCursorDown( event
.ShiftDown() );
7037 // at the bottom of a column
7038 DisableCellEditControl();
7048 if (event
.ShiftDown())
7050 if ( GetGridCursorCol() > 0 )
7052 MoveCursorLeft( false );
7057 DisableCellEditControl();
7062 if ( GetGridCursorCol() < GetNumberCols() - 1 )
7064 MoveCursorRight( false );
7069 DisableCellEditControl();
7075 if ( event
.ControlDown() )
7077 MakeCellVisible( 0, 0 );
7078 SetCurrentCell( 0, 0 );
7087 if ( event
.ControlDown() )
7089 MakeCellVisible( m_numRows
- 1, m_numCols
- 1 );
7090 SetCurrentCell( m_numRows
- 1, m_numCols
- 1 );
7107 if ( event
.ControlDown() )
7111 m_selection
->ToggleCellSelection(
7112 m_currentCellCoords
.GetRow(),
7113 m_currentCellCoords
.GetCol(),
7114 event
.ControlDown(),
7122 if ( !IsEditable() )
7123 MoveCursorRight( false );
7134 m_inOnKeyDown
= false;
7137 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
7139 // try local handlers
7141 if ( event
.GetKeyCode() == WXK_SHIFT
)
7143 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
7144 m_selectingBottomRight
!= wxGridNoCellCoords
)
7148 m_selection
->SelectBlock(
7149 m_selectingTopLeft
.GetRow(),
7150 m_selectingTopLeft
.GetCol(),
7151 m_selectingBottomRight
.GetRow(),
7152 m_selectingBottomRight
.GetCol(),
7153 event
.ControlDown(),
7160 m_selectingTopLeft
= wxGridNoCellCoords
;
7161 m_selectingBottomRight
= wxGridNoCellCoords
;
7162 m_selectingKeyboard
= wxGridNoCellCoords
;
7166 void wxGrid::OnChar( wxKeyEvent
& event
)
7168 // is it possible to edit the current cell at all?
7169 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7171 // yes, now check whether the cells editor accepts the key
7172 int row
= m_currentCellCoords
.GetRow();
7173 int col
= m_currentCellCoords
.GetCol();
7174 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7175 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7177 // <F2> is special and will always start editing, for
7178 // other keys - ask the editor itself
7179 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
7180 || editor
->IsAcceptedKey(event
) )
7182 // ensure cell is visble
7183 MakeCellVisible(row
, col
);
7184 EnableCellEditControl();
7186 // a problem can arise if the cell is not completely
7187 // visible (even after calling MakeCellVisible the
7188 // control is not created and calling StartingKey will
7190 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
7191 editor
->StartingKey(event
);
7207 void wxGrid::OnEraseBackground(wxEraseEvent
&)
7211 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
7213 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
7215 // the event has been intercepted - do nothing
7219 #if !(defined(__WXMAC__) && wxMAC_USE_CORE_GRAPHICS)
7220 wxClientDC
dc( m_gridWin
);
7224 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
7226 DisableCellEditControl();
7228 if ( IsVisible( m_currentCellCoords
, false ) )
7231 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
7232 if ( !m_gridLinesEnabled
)
7240 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
7242 // Otherwise refresh redraws the highlight!
7243 m_currentCellCoords
= coords
;
7245 #if defined(__WXMAC__) && wxMAC_USE_CORE_GRAPHICS
7246 m_gridWin
->Refresh(true /*, & r */);
7248 DrawGridCellArea( dc
, cells
);
7249 DrawAllGridLines( dc
, r
);
7254 m_currentCellCoords
= coords
;
7256 wxGridCellAttr
*attr
= GetCellAttr( coords
);
7257 #if !(defined(__WXMAC__) && wxMAC_USE_CORE_GRAPHICS)
7258 DrawCellHighlight( dc
, attr
);
7263 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7266 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7270 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
7273 rightCol
= GetNumberCols() - 1;
7275 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
7278 bottomRow
= GetNumberRows() - 1;
7282 if ( topRow
> bottomRow
)
7289 if ( leftCol
> rightCol
)
7296 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7297 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7299 // First the case that we selected a completely new area
7300 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
7301 m_selectingBottomRight
== wxGridNoCellCoords
)
7304 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
7305 wxGridCellCoords ( bottomRow
, rightCol
) );
7306 m_gridWin
->Refresh( false, &rect
);
7309 // Now handle changing an existing selection area.
7310 else if ( m_selectingTopLeft
!= updateTopLeft
||
7311 m_selectingBottomRight
!= updateBottomRight
)
7313 // Compute two optimal update rectangles:
7314 // Either one rectangle is a real subset of the
7315 // other, or they are (almost) disjoint!
7317 bool need_refresh
[4];
7321 need_refresh
[3] = false;
7324 // Store intermediate values
7325 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
7326 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
7327 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
7328 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
7330 // Determine the outer/inner coordinates.
7331 if (oldLeft
> leftCol
)
7337 if (oldTop
> topRow
)
7343 if (oldRight
< rightCol
)
7346 oldRight
= rightCol
;
7349 if (oldBottom
< bottomRow
)
7352 oldBottom
= bottomRow
;
7356 // Now, either the stuff marked old is the outer
7357 // rectangle or we don't have a situation where one
7358 // is contained in the other.
7360 if ( oldLeft
< leftCol
)
7362 // Refresh the newly selected or deselected
7363 // area to the left of the old or new selection.
7364 need_refresh
[0] = true;
7365 rect
[0] = BlockToDeviceRect(
7366 wxGridCellCoords( oldTop
, oldLeft
),
7367 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
7370 if ( oldTop
< topRow
)
7372 // Refresh the newly selected or deselected
7373 // area above the old or new selection.
7374 need_refresh
[1] = true;
7375 rect
[1] = BlockToDeviceRect(
7376 wxGridCellCoords( oldTop
, leftCol
),
7377 wxGridCellCoords( topRow
- 1, rightCol
) );
7380 if ( oldRight
> rightCol
)
7382 // Refresh the newly selected or deselected
7383 // area to the right of the old or new selection.
7384 need_refresh
[2] = true;
7385 rect
[2] = BlockToDeviceRect(
7386 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
7387 wxGridCellCoords( oldBottom
, oldRight
) );
7390 if ( oldBottom
> bottomRow
)
7392 // Refresh the newly selected or deselected
7393 // area below the old or new selection.
7394 need_refresh
[3] = true;
7395 rect
[3] = BlockToDeviceRect(
7396 wxGridCellCoords( bottomRow
+ 1, leftCol
),
7397 wxGridCellCoords( oldBottom
, rightCol
) );
7400 // various Refresh() calls
7401 for (i
= 0; i
< 4; i
++ )
7402 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7403 m_gridWin
->Refresh( false, &(rect
[i
]) );
7407 m_selectingTopLeft
= updateTopLeft
;
7408 m_selectingBottomRight
= updateBottomRight
;
7412 // ------ functions to get/send data (see also public functions)
7415 bool wxGrid::GetModelValues()
7417 // Hide the editor, so it won't hide a changed value.
7418 HideCellEditControl();
7422 // all we need to do is repaint the grid
7424 m_gridWin
->Refresh();
7431 bool wxGrid::SetModelValues()
7435 // Disable the editor, so it won't hide a changed value.
7436 // Do we also want to save the current value of the editor first?
7438 DisableCellEditControl();
7442 for ( row
= 0; row
< m_numRows
; row
++ )
7444 for ( col
= 0; col
< m_numCols
; col
++ )
7446 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
7456 // Note - this function only draws cells that are in the list of
7457 // exposed cells (usually set from the update region by
7458 // CalcExposedCells)
7460 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7462 if ( !m_numRows
|| !m_numCols
)
7465 int i
, numCells
= cells
.GetCount();
7466 int row
, col
, cell_rows
, cell_cols
;
7467 wxGridCellCoordsArray redrawCells
;
7469 for ( i
= numCells
- 1; i
>= 0; i
-- )
7471 row
= cells
[i
].GetRow();
7472 col
= cells
[i
].GetCol();
7473 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7475 // If this cell is part of a multicell block, find owner for repaint
7476 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7478 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
7479 bool marked
= false;
7480 for ( int j
= 0; j
< numCells
; j
++ )
7482 if ( cell
== cells
[j
] )
7491 int count
= redrawCells
.GetCount();
7492 for (int j
= 0; j
< count
; j
++)
7494 if ( cell
== redrawCells
[j
] )
7502 redrawCells
.Add( cell
);
7505 // don't bother drawing this cell
7509 // If this cell is empty, find cell to left that might want to overflow
7510 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7512 for ( int l
= 0; l
< cell_rows
; l
++ )
7514 // find a cell in this row to leave already marked for repaint
7516 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7517 if ((redrawCells
[k
].GetCol() < left
) &&
7518 (redrawCells
[k
].GetRow() == row
))
7520 left
= redrawCells
[k
].GetCol();
7524 left
= 0; // oh well
7526 for (int j
= col
- 1; j
>= left
; j
--)
7528 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7530 if (GetCellOverflow(row
+ l
, j
))
7532 wxGridCellCoords
cell(row
+ l
, j
);
7533 bool marked
= false;
7535 for (int k
= 0; k
< numCells
; k
++)
7537 if ( cell
== cells
[k
] )
7546 int count
= redrawCells
.GetCount();
7547 for (int k
= 0; k
< count
; k
++)
7549 if ( cell
== redrawCells
[k
] )
7556 redrawCells
.Add( cell
);
7565 DrawCell( dc
, cells
[i
] );
7568 numCells
= redrawCells
.GetCount();
7570 for ( i
= numCells
- 1; i
>= 0; i
-- )
7572 DrawCell( dc
, redrawCells
[i
] );
7576 void wxGrid::DrawGridSpace( wxDC
& dc
)
7579 m_gridWin
->GetClientSize( &cw
, &ch
);
7582 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7584 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
7585 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7587 if ( right
> rightCol
|| bottom
> bottomRow
)
7590 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7592 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
7593 dc
.SetPen( *wxTRANSPARENT_PEN
);
7595 if ( right
> rightCol
)
7597 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7600 if ( bottom
> bottomRow
)
7602 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7607 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7609 int row
= coords
.GetRow();
7610 int col
= coords
.GetCol();
7612 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7615 // we draw the cell border ourselves
7616 #if !WXGRID_DRAW_LINES
7617 if ( m_gridLinesEnabled
)
7618 DrawCellBorder( dc
, coords
);
7621 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7623 bool isCurrent
= coords
== m_currentCellCoords
;
7625 wxRect rect
= CellToRect( row
, col
);
7627 // if the editor is shown, we should use it and not the renderer
7628 // Note: However, only if it is really _shown_, i.e. not hidden!
7629 if ( isCurrent
&& IsCellEditControlShown() )
7631 // NB: this "#if..." is temporary and fixes a problem where the
7632 // edit control is erased by this code after being rendered.
7633 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7634 // implicitly, causing this out-of order render.
7635 #if !defined(__WXMAC__)
7636 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7637 editor
->PaintBackground(rect
, attr
);
7643 // but all the rest is drawn by the cell renderer and hence may be customized
7644 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7645 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7652 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7654 int row
= m_currentCellCoords
.GetRow();
7655 int col
= m_currentCellCoords
.GetCol();
7657 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7660 wxRect rect
= CellToRect(row
, col
);
7662 // hmmm... what could we do here to show that the cell is disabled?
7663 // for now, I just draw a thinner border than for the other ones, but
7664 // it doesn't look really good
7666 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7670 // The center of the drawn line is where the position/width/height of
7671 // the rectangle is actually at (on wxMSW at least), so the
7672 // size of the rectangle is reduced to compensate for the thickness of
7673 // the line. If this is too strange on non-wxMSW platforms then
7674 // please #ifdef this appropriately.
7675 rect
.x
+= penWidth
/ 2;
7676 rect
.y
+= penWidth
/ 2;
7677 rect
.width
-= penWidth
- 1;
7678 rect
.height
-= penWidth
- 1;
7680 // Now draw the rectangle
7681 // use the cellHighlightColour if the cell is inside a selection, this
7682 // will ensure the cell is always visible.
7683 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
: m_cellHighlightColour
, penWidth
, wxSOLID
));
7684 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7685 dc
.DrawRectangle(rect
);
7689 // VZ: my experiments with 3D borders...
7691 // how to properly set colours for arbitrary bg?
7692 wxCoord x1
= rect
.x
,
7694 x2
= rect
.x
+ rect
.width
- 1,
7695 y2
= rect
.y
+ rect
.height
- 1;
7697 dc
.SetPen(*wxWHITE_PEN
);
7698 dc
.DrawLine(x1
, y1
, x2
, y1
);
7699 dc
.DrawLine(x1
, y1
, x1
, y2
);
7701 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7702 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7704 dc
.SetPen(*wxBLACK_PEN
);
7705 dc
.DrawLine(x1
, y2
, x2
, y2
);
7706 dc
.DrawLine(x2
, y1
, x2
, y2
+ 1);
7710 wxPen
wxGrid::GetDefaultGridLinePen()
7712 return wxPen(GetGridLineColour(), 1, wxSOLID
);
7715 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
7717 return GetDefaultGridLinePen();
7720 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
7722 return GetDefaultGridLinePen();
7725 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7727 int row
= coords
.GetRow();
7728 int col
= coords
.GetCol();
7729 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7733 wxRect rect
= CellToRect( row
, col
);
7735 // right hand border
7736 dc
.SetPen( GetColGridLinePen(col
) );
7737 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7738 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7741 dc
.SetPen( GetRowGridLinePen(row
) );
7742 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7743 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7746 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7748 // This if block was previously in wxGrid::OnPaint but that doesn't
7749 // seem to get called under wxGTK - MB
7751 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7752 m_numRows
&& m_numCols
)
7754 m_currentCellCoords
.Set(0, 0);
7757 if ( IsCellEditControlShown() )
7759 // don't show highlight when the edit control is shown
7763 // if the active cell was repainted, repaint its highlight too because it
7764 // might have been damaged by the grid lines
7765 size_t count
= cells
.GetCount();
7766 for ( size_t n
= 0; n
< count
; n
++ )
7768 if ( cells
[n
] == m_currentCellCoords
)
7770 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7771 DrawCellHighlight(dc
, attr
);
7779 // TODO: remove this ???
7780 // This is used to redraw all grid lines e.g. when the grid line colour
7783 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7785 #if !WXGRID_DRAW_LINES
7789 if ( !m_gridLinesEnabled
|| !m_numRows
|| !m_numCols
)
7792 int top
, bottom
, left
, right
;
7794 #if 0 //#ifndef __WXGTK__
7798 m_gridWin
->GetClientSize(&cw
, &ch
);
7800 // virtual coords of visible area
7802 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7803 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7808 reg
.GetBox(x
, y
, w
, h
);
7809 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7810 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7814 m_gridWin
->GetClientSize(&cw
, &ch
);
7815 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7816 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7819 // avoid drawing grid lines past the last row and col
7821 right
= wxMin( right
, GetColRight(GetColAt( m_numCols
- 1 )) );
7822 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7824 // no gridlines inside multicells, clip them out
7825 int leftCol
= GetColPos( internalXToCol(left
) );
7826 int topRow
= internalYToRow(top
);
7827 int rightCol
= GetColPos( internalXToCol(right
) );
7828 int bottomRow
= internalYToRow(bottom
);
7830 #if !defined(__WXMAC__) || wxMAC_USE_CORE_GRAPHICS
7831 wxRegion
clippedcells(0, 0, cw
, ch
);
7833 int i
, j
, cell_rows
, cell_cols
;
7836 for (j
=topRow
; j
<=bottomRow
; j
++)
7839 for (colPos
=leftCol
; colPos
<=rightCol
; colPos
++)
7841 i
= GetColAt( colPos
);
7843 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7844 if ((cell_rows
> 1) || (cell_cols
> 1))
7846 rect
= CellToRect(j
,i
);
7847 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7848 clippedcells
.Subtract(rect
);
7850 else if ((cell_rows
< 0) || (cell_cols
< 0))
7852 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7853 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7854 clippedcells
.Subtract(rect
);
7859 wxRegion
clippedcells( left
, top
, right
- left
, bottom
- top
);
7861 int i
, j
, cell_rows
, cell_cols
;
7864 for (j
=topRow
; j
<=bottomRow
; j
++)
7866 for (i
=leftCol
; i
<=rightCol
; i
++)
7868 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7869 if ((cell_rows
> 1) || (cell_cols
> 1))
7871 rect
= CellToRect(j
, i
);
7872 clippedcells
.Subtract(rect
);
7874 else if ((cell_rows
< 0) || (cell_cols
< 0))
7876 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7877 clippedcells
.Subtract(rect
);
7883 dc
.SetClippingRegion( clippedcells
);
7886 // horizontal grid lines
7888 // already declared above - int i;
7889 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7891 int bot
= GetRowBottom(i
) - 1;
7900 dc
.SetPen( GetRowGridLinePen(i
) );
7901 dc
.DrawLine( left
, bot
, right
, bot
);
7905 // vertical grid lines
7908 for ( colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
7910 i
= GetColAt( colPos
);
7912 int colRight
= GetColRight(i
);
7914 if (GetLayoutDirection() != wxLayout_RightToLeft
)
7918 if ( colRight
> right
)
7923 if ( colRight
>= left
)
7925 dc
.SetPen( GetColGridLinePen(i
) );
7926 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7930 dc
.DestroyClippingRegion();
7933 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
7939 size_t numLabels
= rows
.GetCount();
7941 for ( i
= 0; i
< numLabels
; i
++ )
7943 DrawRowLabel( dc
, rows
[i
] );
7947 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7949 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7957 rect
.SetY( GetRowTop(row
) + 1 );
7958 rect
.SetWidth( m_rowLabelWidth
- 2 );
7959 rect
.SetHeight( GetRowHeight(row
) - 2 );
7961 CalcScrolledPosition( 0, rect
.y
, NULL
, &rect
.y
);
7963 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7965 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7967 int rowTop
= GetRowTop(row
),
7968 rowBottom
= GetRowBottom(row
) - 1;
7970 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
7971 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
7972 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7973 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7975 dc
.SetPen( *wxWHITE_PEN
);
7976 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7977 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
7980 dc
.SetBackgroundMode( wxTRANSPARENT
);
7981 dc
.SetTextForeground( GetLabelTextColour() );
7982 dc
.SetFont( GetLabelFont() );
7985 GetRowLabelAlignment( &hAlign
, &vAlign
);
7988 rect
.SetY( GetRowTop(row
) + 2 );
7989 rect
.SetWidth( m_rowLabelWidth
- 4 );
7990 rect
.SetHeight( GetRowHeight(row
) - 4 );
7991 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7994 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
8000 size_t numLabels
= cols
.GetCount();
8002 for ( i
= 0; i
< numLabels
; i
++ )
8004 DrawColLabel( dc
, cols
[i
] );
8008 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
8010 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
8013 int colLeft
= GetColLeft(col
);
8019 rect
.SetX( colLeft
+ 1 );
8021 rect
.SetWidth( GetColWidth(col
) - 2 );
8022 rect
.SetHeight( m_colLabelHeight
- 2 );
8024 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
8026 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
8028 int colRight
= GetColRight(col
) - 1;
8030 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
8031 dc
.DrawLine( colRight
, 0, colRight
, m_colLabelHeight
- 1 );
8032 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
8033 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
8034 colRight
+ 1, m_colLabelHeight
- 1 );
8036 dc
.SetPen( *wxWHITE_PEN
);
8037 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
8038 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
8041 dc
.SetBackgroundMode( wxTRANSPARENT
);
8042 dc
.SetTextForeground( GetLabelTextColour() );
8043 dc
.SetFont( GetLabelFont() );
8045 int hAlign
, vAlign
, orient
;
8046 GetColLabelAlignment( &hAlign
, &vAlign
);
8047 orient
= GetColLabelTextOrientation();
8049 rect
.SetX( colLeft
+ 2 );
8051 rect
.SetWidth( GetColWidth(col
) - 4 );
8052 rect
.SetHeight( m_colLabelHeight
- 4 );
8053 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
8056 void wxGrid::DrawTextRectangle( wxDC
& dc
,
8057 const wxString
& value
,
8061 int textOrientation
)
8063 wxArrayString lines
;
8065 StringToLines( value
, lines
);
8067 // Forward to new API.
8068 DrawTextRectangle( dc
,
8076 // VZ: this should be replaced with wxDC::DrawLabel() to which we just have to
8077 // add textOrientation support
8078 void wxGrid::DrawTextRectangle(wxDC
& dc
,
8079 const wxArrayString
& lines
,
8083 int textOrientation
)
8085 if ( lines
.empty() )
8088 wxDCClipper
clip(dc
, rect
);
8093 if ( textOrientation
== wxHORIZONTAL
)
8094 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
8096 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
8100 switch ( vertAlign
)
8102 case wxALIGN_BOTTOM
:
8103 if ( textOrientation
== wxHORIZONTAL
)
8104 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
8106 x
= rect
.x
+ rect
.width
- textWidth
;
8109 case wxALIGN_CENTRE
:
8110 if ( textOrientation
== wxHORIZONTAL
)
8111 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
8113 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
8118 if ( textOrientation
== wxHORIZONTAL
)
8125 // Align each line of a multi-line label
8126 size_t nLines
= lines
.GetCount();
8127 for ( size_t l
= 0; l
< nLines
; l
++ )
8129 const wxString
& line
= lines
[l
];
8133 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
8137 wxCoord lineWidth
= 0,
8139 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
8141 switch ( horizAlign
)
8144 if ( textOrientation
== wxHORIZONTAL
)
8145 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
8147 y
= rect
.y
+ lineWidth
+ 1;
8150 case wxALIGN_CENTRE
:
8151 if ( textOrientation
== wxHORIZONTAL
)
8152 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
8154 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
8159 if ( textOrientation
== wxHORIZONTAL
)
8162 y
= rect
.y
+ rect
.height
- 1;
8166 if ( textOrientation
== wxHORIZONTAL
)
8168 dc
.DrawText( line
, x
, y
);
8173 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
8179 // Split multi-line text up into an array of strings.
8180 // Any existing contents of the string array are preserved.
8182 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
8186 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
8187 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
8189 while ( startPos
< (int)tVal
.length() )
8191 pos
= tVal
.Mid(startPos
).Find( eol
);
8196 else if ( pos
== 0 )
8198 lines
.Add( wxEmptyString
);
8202 lines
.Add( value
.Mid(startPos
, pos
) );
8205 startPos
+= pos
+ 1;
8208 if ( startPos
< (int)value
.length() )
8210 lines
.Add( value
.Mid( startPos
) );
8214 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
8215 const wxArrayString
& lines
,
8216 long *width
, long *height
) const
8220 wxCoord lineW
= 0, lineH
= 0;
8223 for ( i
= 0; i
< lines
.GetCount(); i
++ )
8225 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
8226 w
= wxMax( w
, lineW
);
8235 // ------ Batch processing.
8237 void wxGrid::EndBatch()
8239 if ( m_batchCount
> 0 )
8242 if ( !m_batchCount
)
8245 m_rowLabelWin
->Refresh();
8246 m_colLabelWin
->Refresh();
8247 m_cornerLabelWin
->Refresh();
8248 m_gridWin
->Refresh();
8253 // Use this, rather than wxWindow::Refresh(), to force an immediate
8254 // repainting of the grid. Has no effect if you are already inside a
8255 // BeginBatch / EndBatch block.
8257 void wxGrid::ForceRefresh()
8263 bool wxGrid::Enable(bool enable
)
8265 if ( !wxScrolledWindow::Enable(enable
) )
8268 // redraw in the new state
8269 m_gridWin
->Refresh();
8275 // ------ Edit control functions
8278 void wxGrid::EnableEditing( bool edit
)
8280 // TODO: improve this ?
8282 if ( edit
!= m_editable
)
8285 EnableCellEditControl(edit
);
8290 void wxGrid::EnableCellEditControl( bool enable
)
8295 if ( enable
!= m_cellEditCtrlEnabled
)
8299 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
8302 // this should be checked by the caller!
8303 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8305 // do it before ShowCellEditControl()
8306 m_cellEditCtrlEnabled
= enable
;
8308 ShowCellEditControl();
8312 //FIXME:add veto support
8313 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
8315 HideCellEditControl();
8316 SaveEditControlValue();
8318 // do it after HideCellEditControl()
8319 m_cellEditCtrlEnabled
= enable
;
8324 bool wxGrid::IsCurrentCellReadOnly() const
8327 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
8328 bool readonly
= attr
->IsReadOnly();
8334 bool wxGrid::CanEnableCellControl() const
8336 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
8337 !IsCurrentCellReadOnly();
8340 bool wxGrid::IsCellEditControlEnabled() const
8342 // the cell edit control might be disable for all cells or just for the
8343 // current one if it's read only
8344 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
8347 bool wxGrid::IsCellEditControlShown() const
8349 bool isShown
= false;
8351 if ( m_cellEditCtrlEnabled
)
8353 int row
= m_currentCellCoords
.GetRow();
8354 int col
= m_currentCellCoords
.GetCol();
8355 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8356 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
8361 if ( editor
->IsCreated() )
8363 isShown
= editor
->GetControl()->IsShown();
8373 void wxGrid::ShowCellEditControl()
8375 if ( IsCellEditControlEnabled() )
8377 if ( !IsVisible( m_currentCellCoords
, false ) )
8379 m_cellEditCtrlEnabled
= false;
8384 wxRect rect
= CellToRect( m_currentCellCoords
);
8385 int row
= m_currentCellCoords
.GetRow();
8386 int col
= m_currentCellCoords
.GetCol();
8388 // if this is part of a multicell, find owner (topleft)
8389 int cell_rows
, cell_cols
;
8390 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8391 if ( cell_rows
<= 0 || cell_cols
<= 0 )
8395 m_currentCellCoords
.SetRow( row
);
8396 m_currentCellCoords
.SetCol( col
);
8399 // erase the highlight and the cell contents because the editor
8400 // might not cover the entire cell
8401 wxClientDC
dc( m_gridWin
);
8403 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8404 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
8405 dc
.SetPen(*wxTRANSPARENT_PEN
);
8406 dc
.DrawRectangle(rect
);
8408 // convert to scrolled coords
8409 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8415 // cell is shifted by one pixel
8416 // However, don't allow x or y to become negative
8417 // since the SetSize() method interprets that as
8424 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8425 if ( !editor
->IsCreated() )
8427 editor
->Create(m_gridWin
, wxID_ANY
,
8428 new wxGridCellEditorEvtHandler(this, editor
));
8430 wxGridEditorCreatedEvent
evt(GetId(),
8431 wxEVT_GRID_EDITOR_CREATED
,
8435 editor
->GetControl());
8436 GetEventHandler()->ProcessEvent(evt
);
8439 // resize editor to overflow into righthand cells if allowed
8440 int maxWidth
= rect
.width
;
8441 wxString value
= GetCellValue(row
, col
);
8442 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
8445 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
8446 if (maxWidth
< rect
.width
)
8447 maxWidth
= rect
.width
;
8450 int client_right
= m_gridWin
->GetClientSize().GetWidth();
8451 if (rect
.x
+ maxWidth
> client_right
)
8452 maxWidth
= client_right
- rect
.x
;
8454 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
8456 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8457 // may have changed earlier
8458 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
8461 GetCellSize( row
, i
, &c_rows
, &c_cols
);
8463 // looks weird going over a multicell
8464 if (m_table
->IsEmptyCell( row
, i
) &&
8465 (rect
.width
< maxWidth
) && (c_rows
== 1))
8467 rect
.width
+= GetColWidth( i
);
8473 if (rect
.GetRight() > client_right
)
8474 rect
.SetRight( client_right
- 1 );
8477 editor
->SetCellAttr( attr
);
8478 editor
->SetSize( rect
);
8480 editor
->GetControl()->Move(
8481 editor
->GetControl()->GetPosition().x
+ nXMove
,
8482 editor
->GetControl()->GetPosition().y
);
8483 editor
->Show( true, attr
);
8485 // recalc dimensions in case we need to
8486 // expand the scrolled window to account for editor
8489 editor
->BeginEdit(row
, col
, this);
8490 editor
->SetCellAttr(NULL
);
8498 void wxGrid::HideCellEditControl()
8500 if ( IsCellEditControlEnabled() )
8502 int row
= m_currentCellCoords
.GetRow();
8503 int col
= m_currentCellCoords
.GetCol();
8505 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8506 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
8507 editor
->Show( false );
8511 m_gridWin
->SetFocus();
8513 // refresh whole row to the right
8514 wxRect
rect( CellToRect(row
, col
) );
8515 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8516 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8519 // ensure that the pixels under the focus ring get refreshed as well
8520 rect
.Inflate(10, 10);
8523 m_gridWin
->Refresh( false, &rect
);
8527 void wxGrid::SaveEditControlValue()
8529 if ( IsCellEditControlEnabled() )
8531 int row
= m_currentCellCoords
.GetRow();
8532 int col
= m_currentCellCoords
.GetCol();
8534 wxString oldval
= GetCellValue(row
, col
);
8536 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8537 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8538 bool changed
= editor
->EndEdit(row
, col
, this);
8545 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
8546 m_currentCellCoords
.GetRow(),
8547 m_currentCellCoords
.GetCol() ) < 0 )
8549 // Event has been vetoed, set the data back.
8550 SetCellValue(row
, col
, oldval
);
8557 // ------ Grid location functions
8558 // Note that all of these functions work with the logical coordinates of
8559 // grid cells and labels so you will need to convert from device
8560 // coordinates for mouse events etc.
8563 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
) const
8565 int row
= YToRow(y
);
8566 int col
= XToCol(x
);
8568 if ( row
== -1 || col
== -1 )
8570 coords
= wxGridNoCellCoords
;
8574 coords
.Set( row
, col
);
8578 // Internal Helper function for computing row or column from some
8579 // (unscrolled) coordinate value, using either
8580 // m_defaultRowHeight/m_defaultColWidth or binary search on array
8581 // of m_rowBottoms/m_ColRights to speed up the search!
8583 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
8584 const wxArrayInt
& BorderArray
, int nMax
,
8588 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
8593 size_t i_max
= coord
/ defaultDist
,
8596 if (BorderArray
.IsEmpty())
8598 if ((int) i_max
< nMax
)
8600 return clipToMinMax
? nMax
- 1 : -1;
8603 if ( i_max
>= BorderArray
.GetCount())
8605 i_max
= BorderArray
.GetCount() - 1;
8609 if ( coord
>= BorderArray
[i_max
])
8613 i_max
= coord
/ minDist
;
8615 i_max
= BorderArray
.GetCount() - 1;
8618 if ( i_max
>= BorderArray
.GetCount())
8619 i_max
= BorderArray
.GetCount() - 1;
8622 if ( coord
>= BorderArray
[i_max
])
8623 return clipToMinMax
? (int)i_max
: -1;
8624 if ( coord
< BorderArray
[0] )
8627 while ( i_max
- i_min
> 0 )
8629 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
8630 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8631 if (coord
>= BorderArray
[ i_max
- 1])
8635 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
8636 if (coord
< BorderArray
[median
])
8645 int wxGrid::YToRow( int y
) const
8647 return CoordToRowOrCol(y
, m_defaultRowHeight
,
8648 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8651 int wxGrid::XToCol( int x
, bool clipToMinMax
) const
8654 return clipToMinMax
&& (m_numCols
> 0) ? GetColAt( 0 ) : -1;
8656 wxASSERT_MSG(m_defaultColWidth
> 0, wxT("Default column width can not be zero"));
8658 int maxPos
= x
/ m_defaultColWidth
;
8661 if (m_colRights
.IsEmpty())
8663 if(maxPos
< m_numCols
)
8664 return GetColAt( maxPos
);
8665 return clipToMinMax
? GetColAt( m_numCols
- 1 ) : -1;
8668 if ( maxPos
>= m_numCols
)
8669 maxPos
= m_numCols
- 1;
8672 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8675 if (m_minAcceptableColWidth
)
8676 maxPos
= x
/ m_minAcceptableColWidth
;
8678 maxPos
= m_numCols
- 1;
8680 if ( maxPos
>= m_numCols
)
8681 maxPos
= m_numCols
- 1;
8684 //X is beyond the last column
8685 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8686 return clipToMinMax
? GetColAt( maxPos
) : -1;
8688 //X is before the first column
8689 if ( x
< m_colRights
[GetColAt( 0 )] )
8690 return GetColAt( 0 );
8692 //Perform a binary search
8693 while ( maxPos
- minPos
> 0 )
8695 wxCHECK_MSG(m_colRights
[GetColAt( minPos
)] <= x
&& x
< m_colRights
[GetColAt( maxPos
)],
8696 0, _T("wxGrid: internal error in XToCol"));
8698 if (x
>= m_colRights
[GetColAt( maxPos
- 1 )])
8699 return GetColAt( maxPos
);
8702 int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
8703 if (x
< m_colRights
[GetColAt( median
)])
8708 return GetColAt( maxPos
);
8711 // return the row number that that the y coord is near
8712 // the edge of, or -1 if not near an edge.
8713 // coords can only possibly be near an edge if
8714 // (a) the row/column is large enough to still allow for an "inner" area
8715 // that is _not_ nead the edge (i.e., if the height/width is smaller
8716 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8719 // (b) resizing rows/columns (the thing for which edge detection is
8720 // relevant at all) is enabled.
8722 int wxGrid::YToEdgeOfRow( int y
) const
8725 i
= internalYToRow(y
);
8727 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragRowSize() )
8729 // We know that we are in row i, test whether we are
8730 // close enough to lower or upper border, respectively.
8731 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8733 else if ( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8740 // return the col number that that the x coord is near the edge of, or
8741 // -1 if not near an edge
8742 // See comment at YToEdgeOfRow for conditions on edge detection.
8744 int wxGrid::XToEdgeOfCol( int x
) const
8747 i
= internalXToCol(x
);
8749 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragColSize() )
8751 // We know that we are in column i; test whether we are
8752 // close enough to right or left border, respectively.
8753 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8755 else if ( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8762 wxRect
wxGrid::CellToRect( int row
, int col
) const
8764 wxRect
rect( -1, -1, -1, -1 );
8766 if ( row
>= 0 && row
< m_numRows
&&
8767 col
>= 0 && col
< m_numCols
)
8769 int i
, cell_rows
, cell_cols
;
8770 rect
.width
= rect
.height
= 0;
8771 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8772 // if negative then find multicell owner
8777 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8779 rect
.x
= GetColLeft(col
);
8780 rect
.y
= GetRowTop(row
);
8781 for (i
=col
; i
< col
+ cell_cols
; i
++)
8782 rect
.width
+= GetColWidth(i
);
8783 for (i
=row
; i
< row
+ cell_rows
; i
++)
8784 rect
.height
+= GetRowHeight(i
);
8787 // if grid lines are enabled, then the area of the cell is a bit smaller
8788 if (m_gridLinesEnabled
)
8797 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
8799 // get the cell rectangle in logical coords
8801 wxRect
r( CellToRect( row
, col
) );
8803 // convert to device coords
8805 int left
, top
, right
, bottom
;
8806 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8807 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8809 // check against the client area of the grid window
8811 m_gridWin
->GetClientSize( &cw
, &ch
);
8813 if ( wholeCellVisible
)
8815 // is the cell wholly visible ?
8816 return ( left
>= 0 && right
<= cw
&&
8817 top
>= 0 && bottom
<= ch
);
8821 // is the cell partly visible ?
8823 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8824 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8828 // make the specified cell location visible by doing a minimal amount
8831 void wxGrid::MakeCellVisible( int row
, int col
)
8834 int xpos
= -1, ypos
= -1;
8836 if ( row
>= 0 && row
< m_numRows
&&
8837 col
>= 0 && col
< m_numCols
)
8839 // get the cell rectangle in logical coords
8840 wxRect
r( CellToRect( row
, col
) );
8842 // convert to device coords
8843 int left
, top
, right
, bottom
;
8844 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8845 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8848 m_gridWin
->GetClientSize( &cw
, &ch
);
8854 else if ( bottom
> ch
)
8856 int h
= r
.GetHeight();
8858 for ( i
= row
- 1; i
>= 0; i
-- )
8860 int rowHeight
= GetRowHeight(i
);
8861 if ( h
+ rowHeight
> ch
)
8868 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8869 // have rounding errors (this is important, because if we do,
8870 // we might not scroll at all and some cells won't be redrawn)
8872 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
8873 // so just add a full scroll unit...
8874 ypos
+= m_scrollLineY
;
8877 // special handling for wide cells - show always left part of the cell!
8878 // Otherwise, e.g. when stepping from row to row, it would jump between
8879 // left and right part of the cell on every step!
8881 if ( left
< 0 || (right
- left
) >= cw
)
8885 else if ( right
> cw
)
8887 // position the view so that the cell is on the right
8889 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8890 xpos
= x0
+ (right
- cw
);
8892 // see comment for ypos above
8893 xpos
+= m_scrollLineX
;
8896 if ( xpos
!= -1 || ypos
!= -1 )
8899 xpos
/= m_scrollLineX
;
8901 ypos
/= m_scrollLineY
;
8902 Scroll( xpos
, ypos
);
8909 // ------ Grid cursor movement functions
8912 bool wxGrid::MoveCursorUp( bool expandSelection
)
8914 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8915 m_currentCellCoords
.GetRow() >= 0 )
8917 if ( expandSelection
)
8919 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8920 m_selectingKeyboard
= m_currentCellCoords
;
8921 if ( m_selectingKeyboard
.GetRow() > 0 )
8923 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8924 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8925 m_selectingKeyboard
.GetCol() );
8926 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8929 else if ( m_currentCellCoords
.GetRow() > 0 )
8931 int row
= m_currentCellCoords
.GetRow() - 1;
8932 int col
= m_currentCellCoords
.GetCol();
8934 MakeCellVisible( row
, col
);
8935 SetCurrentCell( row
, col
);
8946 bool wxGrid::MoveCursorDown( bool expandSelection
)
8948 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8949 m_currentCellCoords
.GetRow() < m_numRows
)
8951 if ( expandSelection
)
8953 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8954 m_selectingKeyboard
= m_currentCellCoords
;
8955 if ( m_selectingKeyboard
.GetRow() < m_numRows
- 1 )
8957 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8958 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8959 m_selectingKeyboard
.GetCol() );
8960 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8963 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8965 int row
= m_currentCellCoords
.GetRow() + 1;
8966 int col
= m_currentCellCoords
.GetCol();
8968 MakeCellVisible( row
, col
);
8969 SetCurrentCell( row
, col
);
8980 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8982 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8983 m_currentCellCoords
.GetCol() >= 0 )
8985 if ( expandSelection
)
8987 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8988 m_selectingKeyboard
= m_currentCellCoords
;
8989 if ( m_selectingKeyboard
.GetCol() > 0 )
8991 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8992 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8993 m_selectingKeyboard
.GetCol() );
8994 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8997 else if ( GetColPos( m_currentCellCoords
.GetCol() ) > 0 )
8999 int row
= m_currentCellCoords
.GetRow();
9000 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) - 1 );
9003 MakeCellVisible( row
, col
);
9004 SetCurrentCell( row
, col
);
9015 bool wxGrid::MoveCursorRight( bool expandSelection
)
9017 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
9018 m_currentCellCoords
.GetCol() < m_numCols
)
9020 if ( expandSelection
)
9022 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
9023 m_selectingKeyboard
= m_currentCellCoords
;
9024 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
9026 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
9027 MakeCellVisible( m_selectingKeyboard
.GetRow(),
9028 m_selectingKeyboard
.GetCol() );
9029 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9032 else if ( GetColPos( m_currentCellCoords
.GetCol() ) < m_numCols
- 1 )
9034 int row
= m_currentCellCoords
.GetRow();
9035 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) + 1 );
9038 MakeCellVisible( row
, col
);
9039 SetCurrentCell( row
, col
);
9050 bool wxGrid::MovePageUp()
9052 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9055 int row
= m_currentCellCoords
.GetRow();
9059 m_gridWin
->GetClientSize( &cw
, &ch
);
9061 int y
= GetRowTop(row
);
9062 int newRow
= internalYToRow( y
- ch
+ 1 );
9064 if ( newRow
== row
)
9066 // row > 0, so newRow can never be less than 0 here.
9070 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
9071 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
9079 bool wxGrid::MovePageDown()
9081 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9084 int row
= m_currentCellCoords
.GetRow();
9085 if ( (row
+ 1) < m_numRows
)
9088 m_gridWin
->GetClientSize( &cw
, &ch
);
9090 int y
= GetRowTop(row
);
9091 int newRow
= internalYToRow( y
+ ch
);
9092 if ( newRow
== row
)
9094 // row < m_numRows, so newRow can't overflow here.
9098 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
9099 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
9107 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
9110 m_currentCellCoords
!= wxGridNoCellCoords
&&
9111 m_currentCellCoords
.GetRow() > 0 )
9113 int row
= m_currentCellCoords
.GetRow();
9114 int col
= m_currentCellCoords
.GetCol();
9116 if ( m_table
->IsEmptyCell(row
, col
) )
9118 // starting in an empty cell: find the next block of
9124 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9128 else if ( m_table
->IsEmptyCell(row
- 1, col
) )
9130 // starting at the top of a block: find the next block
9136 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9142 // starting within a block: find the top of the block
9147 if ( m_table
->IsEmptyCell(row
, col
) )
9155 MakeCellVisible( row
, col
);
9156 if ( expandSelection
)
9158 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9159 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9164 SetCurrentCell( row
, col
);
9173 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
9176 m_currentCellCoords
!= wxGridNoCellCoords
&&
9177 m_currentCellCoords
.GetRow() < m_numRows
- 1 )
9179 int row
= m_currentCellCoords
.GetRow();
9180 int col
= m_currentCellCoords
.GetCol();
9182 if ( m_table
->IsEmptyCell(row
, col
) )
9184 // starting in an empty cell: find the next block of
9187 while ( row
< m_numRows
- 1 )
9190 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9194 else if ( m_table
->IsEmptyCell(row
+ 1, col
) )
9196 // starting at the bottom of a block: find the next block
9199 while ( row
< m_numRows
- 1 )
9202 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9208 // starting within a block: find the bottom of the block
9210 while ( row
< m_numRows
- 1 )
9213 if ( m_table
->IsEmptyCell(row
, col
) )
9221 MakeCellVisible( row
, col
);
9222 if ( expandSelection
)
9224 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9225 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9230 SetCurrentCell( row
, col
);
9239 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
9242 m_currentCellCoords
!= wxGridNoCellCoords
&&
9243 m_currentCellCoords
.GetCol() > 0 )
9245 int row
= m_currentCellCoords
.GetRow();
9246 int col
= m_currentCellCoords
.GetCol();
9248 if ( m_table
->IsEmptyCell(row
, col
) )
9250 // starting in an empty cell: find the next block of
9256 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9260 else if ( m_table
->IsEmptyCell(row
, col
- 1) )
9262 // starting at the left of a block: find the next block
9268 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9274 // starting within a block: find the left of the block
9279 if ( m_table
->IsEmptyCell(row
, col
) )
9287 MakeCellVisible( row
, col
);
9288 if ( expandSelection
)
9290 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9291 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9296 SetCurrentCell( row
, col
);
9305 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
9308 m_currentCellCoords
!= wxGridNoCellCoords
&&
9309 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
9311 int row
= m_currentCellCoords
.GetRow();
9312 int col
= m_currentCellCoords
.GetCol();
9314 if ( m_table
->IsEmptyCell(row
, col
) )
9316 // starting in an empty cell: find the next block of
9319 while ( col
< m_numCols
- 1 )
9322 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9326 else if ( m_table
->IsEmptyCell(row
, col
+ 1) )
9328 // starting at the right of a block: find the next block
9331 while ( col
< m_numCols
- 1 )
9334 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9340 // starting within a block: find the right of the block
9342 while ( col
< m_numCols
- 1 )
9345 if ( m_table
->IsEmptyCell(row
, col
) )
9353 MakeCellVisible( row
, col
);
9354 if ( expandSelection
)
9356 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9357 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9362 SetCurrentCell( row
, col
);
9372 // ------ Label values and formatting
9375 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
9378 *horiz
= m_rowLabelHorizAlign
;
9380 *vert
= m_rowLabelVertAlign
;
9383 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
9386 *horiz
= m_colLabelHorizAlign
;
9388 *vert
= m_colLabelVertAlign
;
9391 int wxGrid::GetColLabelTextOrientation() const
9393 return m_colLabelTextOrientation
;
9396 wxString
wxGrid::GetRowLabelValue( int row
) const
9400 return m_table
->GetRowLabelValue( row
);
9410 wxString
wxGrid::GetColLabelValue( int col
) const
9414 return m_table
->GetColLabelValue( col
);
9424 void wxGrid::SetRowLabelSize( int width
)
9426 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
9428 if ( width
== wxGRID_AUTOSIZE
)
9430 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
9433 if ( width
!= m_rowLabelWidth
)
9437 m_rowLabelWin
->Show( false );
9438 m_cornerLabelWin
->Show( false );
9440 else if ( m_rowLabelWidth
== 0 )
9442 m_rowLabelWin
->Show( true );
9443 if ( m_colLabelHeight
> 0 )
9444 m_cornerLabelWin
->Show( true );
9447 m_rowLabelWidth
= width
;
9449 wxScrolledWindow::Refresh( true );
9453 void wxGrid::SetColLabelSize( int height
)
9455 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
9457 if ( height
== wxGRID_AUTOSIZE
)
9459 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
9462 if ( height
!= m_colLabelHeight
)
9466 m_colLabelWin
->Show( false );
9467 m_cornerLabelWin
->Show( false );
9469 else if ( m_colLabelHeight
== 0 )
9471 m_colLabelWin
->Show( true );
9472 if ( m_rowLabelWidth
> 0 )
9473 m_cornerLabelWin
->Show( true );
9476 m_colLabelHeight
= height
;
9478 wxScrolledWindow::Refresh( true );
9482 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
9484 if ( m_labelBackgroundColour
!= colour
)
9486 m_labelBackgroundColour
= colour
;
9487 m_rowLabelWin
->SetBackgroundColour( colour
);
9488 m_colLabelWin
->SetBackgroundColour( colour
);
9489 m_cornerLabelWin
->SetBackgroundColour( colour
);
9491 if ( !GetBatchCount() )
9493 m_rowLabelWin
->Refresh();
9494 m_colLabelWin
->Refresh();
9495 m_cornerLabelWin
->Refresh();
9500 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
9502 if ( m_labelTextColour
!= colour
)
9504 m_labelTextColour
= colour
;
9505 if ( !GetBatchCount() )
9507 m_rowLabelWin
->Refresh();
9508 m_colLabelWin
->Refresh();
9513 void wxGrid::SetLabelFont( const wxFont
& font
)
9516 if ( !GetBatchCount() )
9518 m_rowLabelWin
->Refresh();
9519 m_colLabelWin
->Refresh();
9523 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
9525 // allow old (incorrect) defs to be used
9528 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9529 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9530 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9535 case wxTOP
: vert
= wxALIGN_TOP
; break;
9536 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9537 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9540 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9542 m_rowLabelHorizAlign
= horiz
;
9545 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9547 m_rowLabelVertAlign
= vert
;
9550 if ( !GetBatchCount() )
9552 m_rowLabelWin
->Refresh();
9556 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
9558 // allow old (incorrect) defs to be used
9561 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9562 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9563 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9568 case wxTOP
: vert
= wxALIGN_TOP
; break;
9569 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9570 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9573 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9575 m_colLabelHorizAlign
= horiz
;
9578 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9580 m_colLabelVertAlign
= vert
;
9583 if ( !GetBatchCount() )
9585 m_colLabelWin
->Refresh();
9589 // Note: under MSW, the default column label font must be changed because it
9590 // does not support vertical printing
9592 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9593 // pGrid->SetLabelFont(font);
9594 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9596 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9598 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9599 m_colLabelTextOrientation
= textOrientation
;
9601 if ( !GetBatchCount() )
9602 m_colLabelWin
->Refresh();
9605 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9609 m_table
->SetRowLabelValue( row
, s
);
9610 if ( !GetBatchCount() )
9612 wxRect rect
= CellToRect( row
, 0 );
9613 if ( rect
.height
> 0 )
9615 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9617 rect
.width
= m_rowLabelWidth
;
9618 m_rowLabelWin
->Refresh( true, &rect
);
9624 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9628 m_table
->SetColLabelValue( col
, s
);
9629 if ( !GetBatchCount() )
9631 wxRect rect
= CellToRect( 0, col
);
9632 if ( rect
.width
> 0 )
9634 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9636 rect
.height
= m_colLabelHeight
;
9637 m_colLabelWin
->Refresh( true, &rect
);
9643 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9645 if ( m_gridLineColour
!= colour
)
9647 m_gridLineColour
= colour
;
9649 wxClientDC
dc( m_gridWin
);
9651 DrawAllGridLines( dc
, wxRegion() );
9655 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9657 if ( m_cellHighlightColour
!= colour
)
9659 m_cellHighlightColour
= colour
;
9661 wxClientDC
dc( m_gridWin
);
9663 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9664 DrawCellHighlight(dc
, attr
);
9669 void wxGrid::SetCellHighlightPenWidth(int width
)
9671 if (m_cellHighlightPenWidth
!= width
)
9673 m_cellHighlightPenWidth
= width
;
9675 // Just redrawing the cell highlight is not enough since that won't
9676 // make any visible change if the the thickness is getting smaller.
9677 int row
= m_currentCellCoords
.GetRow();
9678 int col
= m_currentCellCoords
.GetCol();
9679 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9682 wxRect rect
= CellToRect(row
, col
);
9683 m_gridWin
->Refresh(true, &rect
);
9687 void wxGrid::SetCellHighlightROPenWidth(int width
)
9689 if (m_cellHighlightROPenWidth
!= width
)
9691 m_cellHighlightROPenWidth
= width
;
9693 // Just redrawing the cell highlight is not enough since that won't
9694 // make any visible change if the the thickness is getting smaller.
9695 int row
= m_currentCellCoords
.GetRow();
9696 int col
= m_currentCellCoords
.GetCol();
9697 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9700 wxRect rect
= CellToRect(row
, col
);
9701 m_gridWin
->Refresh(true, &rect
);
9705 void wxGrid::EnableGridLines( bool enable
)
9707 if ( enable
!= m_gridLinesEnabled
)
9709 m_gridLinesEnabled
= enable
;
9711 if ( !GetBatchCount() )
9715 wxClientDC
dc( m_gridWin
);
9717 DrawAllGridLines( dc
, wxRegion() );
9721 m_gridWin
->Refresh();
9727 int wxGrid::GetDefaultRowSize() const
9729 return m_defaultRowHeight
;
9732 int wxGrid::GetRowSize( int row
) const
9734 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9736 return GetRowHeight(row
);
9739 int wxGrid::GetDefaultColSize() const
9741 return m_defaultColWidth
;
9744 int wxGrid::GetColSize( int col
) const
9746 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9748 return GetColWidth(col
);
9751 // ============================================================================
9752 // access to the grid attributes: each of them has a default value in the grid
9753 // itself and may be overidden on a per-cell basis
9754 // ============================================================================
9756 // ----------------------------------------------------------------------------
9757 // setting default attributes
9758 // ----------------------------------------------------------------------------
9760 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9762 m_defaultCellAttr
->SetBackgroundColour(col
);
9764 m_gridWin
->SetBackgroundColour(col
);
9768 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9770 m_defaultCellAttr
->SetTextColour(col
);
9773 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9775 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9778 void wxGrid::SetDefaultCellOverflow( bool allow
)
9780 m_defaultCellAttr
->SetOverflow(allow
);
9783 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9785 m_defaultCellAttr
->SetFont(font
);
9788 // For editors and renderers the type registry takes precedence over the
9789 // default attr, so we need to register the new editor/renderer for the string
9790 // data type in order to make setting a default editor/renderer appear to
9793 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9795 RegisterDataType(wxGRID_VALUE_STRING
,
9797 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9800 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9802 RegisterDataType(wxGRID_VALUE_STRING
,
9803 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9807 // ----------------------------------------------------------------------------
9808 // access to the default attributes
9809 // ----------------------------------------------------------------------------
9811 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
9813 return m_defaultCellAttr
->GetBackgroundColour();
9816 wxColour
wxGrid::GetDefaultCellTextColour() const
9818 return m_defaultCellAttr
->GetTextColour();
9821 wxFont
wxGrid::GetDefaultCellFont() const
9823 return m_defaultCellAttr
->GetFont();
9826 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
9828 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9831 bool wxGrid::GetDefaultCellOverflow() const
9833 return m_defaultCellAttr
->GetOverflow();
9836 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9838 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9841 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9843 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9846 // ----------------------------------------------------------------------------
9847 // access to cell attributes
9848 // ----------------------------------------------------------------------------
9850 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
9852 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9853 wxColour colour
= attr
->GetBackgroundColour();
9859 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
9861 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9862 wxColour colour
= attr
->GetTextColour();
9868 wxFont
wxGrid::GetCellFont( int row
, int col
) const
9870 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9871 wxFont font
= attr
->GetFont();
9877 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
9879 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9880 attr
->GetAlignment(horiz
, vert
);
9884 bool wxGrid::GetCellOverflow( int row
, int col
) const
9886 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9887 bool allow
= attr
->GetOverflow();
9893 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
9895 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9896 attr
->GetSize( num_rows
, num_cols
);
9900 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
9902 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9903 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9909 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
9911 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9912 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9918 bool wxGrid::IsReadOnly(int row
, int col
) const
9920 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9921 bool isReadOnly
= attr
->IsReadOnly();
9927 // ----------------------------------------------------------------------------
9928 // attribute support: cache, automatic provider creation, ...
9929 // ----------------------------------------------------------------------------
9931 bool wxGrid::CanHaveAttributes() const
9938 return m_table
->CanHaveAttributes();
9941 void wxGrid::ClearAttrCache()
9943 if ( m_attrCache
.row
!= -1 )
9945 wxSafeDecRef(m_attrCache
.attr
);
9946 m_attrCache
.attr
= NULL
;
9947 m_attrCache
.row
= -1;
9951 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9955 wxGrid
*self
= (wxGrid
*)this; // const_cast
9957 self
->ClearAttrCache();
9958 self
->m_attrCache
.row
= row
;
9959 self
->m_attrCache
.col
= col
;
9960 self
->m_attrCache
.attr
= attr
;
9965 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9967 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9969 *attr
= m_attrCache
.attr
;
9970 wxSafeIncRef(m_attrCache
.attr
);
9972 #ifdef DEBUG_ATTR_CACHE
9973 gs_nAttrCacheHits
++;
9980 #ifdef DEBUG_ATTR_CACHE
9981 gs_nAttrCacheMisses
++;
9988 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9990 wxGridCellAttr
*attr
= NULL
;
9991 // Additional test to avoid looking at the cache e.g. for
9992 // wxNoCellCoords, as this will confuse memory management.
9995 if ( !LookupAttr(row
, col
, &attr
) )
9997 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9998 : (wxGridCellAttr
*)NULL
;
9999 CacheAttr(row
, col
, attr
);
10005 attr
->SetDefAttr(m_defaultCellAttr
);
10009 attr
= m_defaultCellAttr
;
10016 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
10018 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
10019 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
10021 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
10022 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
10024 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
10027 attr
= new wxGridCellAttr(m_defaultCellAttr
);
10029 // artificially inc the ref count to match DecRef() in caller
10031 m_table
->SetAttr(attr
, row
, col
);
10037 // ----------------------------------------------------------------------------
10038 // setting column attributes (wrappers around SetColAttr)
10039 // ----------------------------------------------------------------------------
10041 void wxGrid::SetColFormatBool(int col
)
10043 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
10046 void wxGrid::SetColFormatNumber(int col
)
10048 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
10051 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
10053 wxString typeName
= wxGRID_VALUE_FLOAT
;
10054 if ( (width
!= -1) || (precision
!= -1) )
10056 typeName
<< _T(':') << width
<< _T(',') << precision
;
10059 SetColFormatCustom(col
, typeName
);
10062 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
10064 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
10066 attr
= new wxGridCellAttr
;
10067 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
10068 attr
->SetRenderer(renderer
);
10070 SetColAttr(col
, attr
);
10074 // ----------------------------------------------------------------------------
10075 // setting cell attributes: this is forwarded to the table
10076 // ----------------------------------------------------------------------------
10078 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
10080 if ( CanHaveAttributes() )
10082 m_table
->SetAttr(attr
, row
, col
);
10087 wxSafeDecRef(attr
);
10091 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
10093 if ( CanHaveAttributes() )
10095 m_table
->SetRowAttr(attr
, row
);
10100 wxSafeDecRef(attr
);
10104 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
10106 if ( CanHaveAttributes() )
10108 m_table
->SetColAttr(attr
, col
);
10113 wxSafeDecRef(attr
);
10117 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
10119 if ( CanHaveAttributes() )
10121 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10122 attr
->SetBackgroundColour(colour
);
10127 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
10129 if ( CanHaveAttributes() )
10131 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10132 attr
->SetTextColour(colour
);
10137 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
10139 if ( CanHaveAttributes() )
10141 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10142 attr
->SetFont(font
);
10147 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
10149 if ( CanHaveAttributes() )
10151 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10152 attr
->SetAlignment(horiz
, vert
);
10157 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
10159 if ( CanHaveAttributes() )
10161 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10162 attr
->SetOverflow(allow
);
10167 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
10169 if ( CanHaveAttributes() )
10171 int cell_rows
, cell_cols
;
10173 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10174 attr
->GetSize(&cell_rows
, &cell_cols
);
10175 attr
->SetSize(num_rows
, num_cols
);
10178 // Cannot set the size of a cell to 0 or negative values
10179 // While it is perfectly legal to do that, this function cannot
10180 // handle all the possibilies, do it by hand by getting the CellAttr.
10181 // You can only set the size of a cell to 1,1 or greater with this fn
10182 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
10183 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10184 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
10185 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10187 // if this was already a multicell then "turn off" the other cells first
10188 if ((cell_rows
> 1) || (cell_rows
> 1))
10191 for (j
=row
; j
< row
+ cell_rows
; j
++)
10193 for (i
=col
; i
< col
+ cell_cols
; i
++)
10195 if ((i
!= col
) || (j
!= row
))
10197 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10198 attr_stub
->SetSize( 1, 1 );
10199 attr_stub
->DecRef();
10205 // mark the cells that will be covered by this cell to
10206 // negative or zero values to point back at this cell
10207 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
10210 for (j
=row
; j
< row
+ num_rows
; j
++)
10212 for (i
=col
; i
< col
+ num_cols
; i
++)
10214 if ((i
!= col
) || (j
!= row
))
10216 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10217 attr_stub
->SetSize( row
- j
, col
- i
);
10218 attr_stub
->DecRef();
10226 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
10228 if ( CanHaveAttributes() )
10230 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10231 attr
->SetRenderer(renderer
);
10236 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
10238 if ( CanHaveAttributes() )
10240 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10241 attr
->SetEditor(editor
);
10246 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
10248 if ( CanHaveAttributes() )
10250 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10251 attr
->SetReadOnly(isReadOnly
);
10256 // ----------------------------------------------------------------------------
10257 // Data type registration
10258 // ----------------------------------------------------------------------------
10260 void wxGrid::RegisterDataType(const wxString
& typeName
,
10261 wxGridCellRenderer
* renderer
,
10262 wxGridCellEditor
* editor
)
10264 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
10268 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
10270 wxString typeName
= m_table
->GetTypeName(row
, col
);
10271 return GetDefaultEditorForType(typeName
);
10274 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
10276 wxString typeName
= m_table
->GetTypeName(row
, col
);
10277 return GetDefaultRendererForType(typeName
);
10280 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
10282 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10283 if ( index
== wxNOT_FOUND
)
10285 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10290 return m_typeRegistry
->GetEditor(index
);
10293 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
10295 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10296 if ( index
== wxNOT_FOUND
)
10298 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10303 return m_typeRegistry
->GetRenderer(index
);
10306 // ----------------------------------------------------------------------------
10308 // ----------------------------------------------------------------------------
10310 void wxGrid::EnableDragRowSize( bool enable
)
10312 m_canDragRowSize
= enable
;
10315 void wxGrid::EnableDragColSize( bool enable
)
10317 m_canDragColSize
= enable
;
10320 void wxGrid::EnableDragGridSize( bool enable
)
10322 m_canDragGridSize
= enable
;
10325 void wxGrid::EnableDragCell( bool enable
)
10327 m_canDragCell
= enable
;
10330 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
10332 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
10334 if ( resizeExistingRows
)
10336 // since we are resizing all rows to the default row size,
10337 // we can simply clear the row heights and row bottoms
10338 // arrays (which also allows us to take advantage of
10339 // some speed optimisations)
10340 m_rowHeights
.Empty();
10341 m_rowBottoms
.Empty();
10342 if ( !GetBatchCount() )
10347 void wxGrid::SetRowSize( int row
, int height
)
10349 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
10351 // See comment in SetColSize
10352 if ( height
< GetRowMinimalAcceptableHeight())
10355 if ( m_rowHeights
.IsEmpty() )
10357 // need to really create the array
10361 int h
= wxMax( 0, height
);
10362 int diff
= h
- m_rowHeights
[row
];
10364 m_rowHeights
[row
] = h
;
10365 for ( int i
= row
; i
< m_numRows
; i
++ )
10367 m_rowBottoms
[i
] += diff
;
10370 if ( !GetBatchCount() )
10374 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
10376 // we dont allow zero default column width
10377 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
10379 if ( resizeExistingCols
)
10381 // since we are resizing all columns to the default column size,
10382 // we can simply clear the col widths and col rights
10383 // arrays (which also allows us to take advantage of
10384 // some speed optimisations)
10385 m_colWidths
.Empty();
10386 m_colRights
.Empty();
10387 if ( !GetBatchCount() )
10392 void wxGrid::SetColSize( int col
, int width
)
10394 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
10396 // should we check that it's bigger than GetColMinimalWidth(col) here?
10398 // No, because it is reasonable to assume the library user know's
10399 // what he is doing. However we should test against the weaker
10400 // constraint of minimalAcceptableWidth, as this breaks rendering
10402 // This test then fixes sf.net bug #645734
10404 if ( width
< GetColMinimalAcceptableWidth() )
10407 if ( m_colWidths
.IsEmpty() )
10409 // need to really create the array
10413 // if < 0 then calculate new width from label
10417 wxArrayString lines
;
10418 wxClientDC
dc(m_colLabelWin
);
10419 dc
.SetFont(GetLabelFont());
10420 StringToLines(GetColLabelValue(col
), lines
);
10421 GetTextBoxSize(dc
, lines
, &w
, &h
);
10425 int w
= wxMax( 0, width
);
10426 int diff
= w
- m_colWidths
[col
];
10427 m_colWidths
[col
] = w
;
10429 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
10431 m_colRights
[GetColAt(colPos
)] += diff
;
10434 if ( !GetBatchCount() )
10438 void wxGrid::SetColMinimalWidth( int col
, int width
)
10440 if (width
> GetColMinimalAcceptableWidth())
10442 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10443 m_colMinWidths
[key
] = width
;
10447 void wxGrid::SetRowMinimalHeight( int row
, int width
)
10449 if (width
> GetRowMinimalAcceptableHeight())
10451 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10452 m_rowMinHeights
[key
] = width
;
10456 int wxGrid::GetColMinimalWidth(int col
) const
10458 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10459 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
10461 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
10464 int wxGrid::GetRowMinimalHeight(int row
) const
10466 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10467 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
10469 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
10472 void wxGrid::SetColMinimalAcceptableWidth( int width
)
10474 // We do allow a width of 0 since this gives us
10475 // an easy way to temporarily hiding columns.
10477 m_minAcceptableColWidth
= width
;
10480 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
10482 // We do allow a height of 0 since this gives us
10483 // an easy way to temporarily hiding rows.
10485 m_minAcceptableRowHeight
= height
;
10488 int wxGrid::GetColMinimalAcceptableWidth() const
10490 return m_minAcceptableColWidth
;
10493 int wxGrid::GetRowMinimalAcceptableHeight() const
10495 return m_minAcceptableRowHeight
;
10498 // ----------------------------------------------------------------------------
10500 // ----------------------------------------------------------------------------
10503 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
10505 const bool column
= direction
== wxGRID_COLUMN
;
10507 wxClientDC
dc(m_gridWin
);
10509 // cancel editing of cell
10510 HideCellEditControl();
10511 SaveEditControlValue();
10513 // init both of them to avoid compiler warnings, even if we only need one
10521 wxCoord extent
, extentMax
= 0;
10522 int max
= column
? m_numRows
: m_numCols
;
10523 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
10530 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
10531 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
10534 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
10535 extent
= column
? size
.x
: size
.y
;
10536 if ( extent
> extentMax
)
10537 extentMax
= extent
;
10539 renderer
->DecRef();
10545 // now also compare with the column label extent
10547 dc
.SetFont( GetLabelFont() );
10551 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
10552 if ( GetColLabelTextOrientation() == wxVERTICAL
)
10556 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
10558 extent
= column
? w
: h
;
10559 if ( extent
> extentMax
)
10560 extentMax
= extent
;
10564 // empty column - give default extent (notice that if extentMax is less
10565 // than default extent but != 0, it's OK)
10566 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
10571 // leave some space around text
10579 SetColSize( col
, extentMax
);
10580 if ( !GetBatchCount() )
10583 m_gridWin
->GetClientSize( &cw
, &ch
);
10584 wxRect
rect ( CellToRect( 0, col
) );
10586 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10587 rect
.width
= cw
- rect
.x
;
10588 rect
.height
= m_colLabelHeight
;
10589 m_colLabelWin
->Refresh( true, &rect
);
10594 SetRowSize(row
, extentMax
);
10595 if ( !GetBatchCount() )
10598 m_gridWin
->GetClientSize( &cw
, &ch
);
10599 wxRect
rect( CellToRect( row
, 0 ) );
10601 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10602 rect
.width
= m_rowLabelWidth
;
10603 rect
.height
= ch
- rect
.y
;
10604 m_rowLabelWin
->Refresh( true, &rect
);
10611 SetColMinimalWidth(col
, extentMax
);
10613 SetRowMinimalHeight(row
, extentMax
);
10617 wxCoord
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
10619 // calculate size for the rows or columns?
10620 const bool calcRows
= direction
== wxGRID_ROW
;
10622 wxClientDC
dc(calcRows
? GetGridRowLabelWindow()
10623 : GetGridColLabelWindow());
10624 dc
.SetFont(GetLabelFont());
10626 // which dimension should we take into account for calculations?
10628 // for columns, the text can be only horizontal so it's easy but for rows
10629 // we also have to take into account the text orientation
10631 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
10633 wxArrayString lines
;
10634 wxCoord extentMax
= 0;
10636 const int numRowsOrCols
= calcRows
? m_numRows
: m_numCols
;
10637 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
10641 wxString label
= calcRows
? GetRowLabelValue(rowOrCol
)
10642 : GetColLabelValue(rowOrCol
);
10643 StringToLines(label
, lines
);
10646 GetTextBoxSize(dc
, lines
, &w
, &h
);
10648 const wxCoord extent
= useWidth
? w
: h
;
10649 if ( extent
> extentMax
)
10650 extentMax
= extent
;
10655 // empty column - give default extent (notice that if extentMax is less
10656 // than default extent but != 0, it's OK)
10657 extentMax
= calcRows
? GetDefaultRowLabelSize()
10658 : GetDefaultColLabelSize();
10661 // leave some space around text (taken from AutoSizeColOrRow)
10670 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10672 int width
= m_rowLabelWidth
;
10674 wxGridUpdateLocker locker
;
10676 locker
.Create(this);
10678 for ( int col
= 0; col
< m_numCols
; col
++ )
10681 AutoSizeColumn(col
, setAsMin
);
10683 width
+= GetColWidth(col
);
10689 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10691 int height
= m_colLabelHeight
;
10693 wxGridUpdateLocker locker
;
10695 locker
.Create(this);
10697 for ( int row
= 0; row
< m_numRows
; row
++ )
10700 AutoSizeRow(row
, setAsMin
);
10702 height
+= GetRowHeight(row
);
10708 void wxGrid::AutoSize()
10710 wxGridUpdateLocker
locker(this);
10712 // we need to round up the size of the scrollable area to a multiple of
10713 // scroll step to ensure that we don't get the scrollbars when we're sized
10714 // exactly to fit our contents
10715 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
10716 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
10717 wxSize
sizeFit(GetScrollX(size
.x
) * GetScrollLineX(),
10718 GetScrollY(size
.y
) * GetScrollLineY());
10720 // distribute the extra space between the columns/rows to avoid having
10721 // extra white space
10722 wxCoord diff
= sizeFit
.x
- size
.x
;
10723 if ( diff
&& m_numCols
)
10725 // try to resize the columns uniformly
10726 wxCoord diffPerCol
= diff
/ m_numCols
;
10729 for ( int col
= 0; col
< m_numCols
; col
++ )
10731 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
10735 // add remaining amount to the last columns
10736 diff
-= diffPerCol
* m_numCols
;
10739 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
10741 SetColSize(col
, GetColWidth(col
) + 1);
10747 diff
= sizeFit
.y
- size
.y
;
10748 if ( diff
&& m_numRows
)
10750 // try to resize the columns uniformly
10751 wxCoord diffPerRow
= diff
/ m_numRows
;
10754 for ( int row
= 0; row
< m_numRows
; row
++ )
10756 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
10760 // add remaining amount to the last rows
10761 diff
-= diffPerRow
* m_numRows
;
10764 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
10766 SetRowSize(row
, GetRowHeight(row
) + 1);
10771 // we know that we're not going to have scrollbars so disable them now to
10772 // avoid trouble in SetClientSize() which can otherwise set the correct
10773 // client size but also leave space for (not needed any more) scrollbars
10774 SetScrollbars(0, 0, 0, 0, 0, 0, true);
10775 SetClientSize(sizeFit
.x
+ m_rowLabelWidth
, sizeFit
.y
+ m_colLabelHeight
);
10778 void wxGrid::AutoSizeRowLabelSize( int row
)
10780 wxArrayString lines
;
10783 // Hide the edit control, so it
10784 // won't interfere with drag-shrinking.
10785 if ( IsCellEditControlShown() )
10787 HideCellEditControl();
10788 SaveEditControlValue();
10791 // autosize row height depending on label text
10792 StringToLines( GetRowLabelValue( row
), lines
);
10793 wxClientDC
dc( m_rowLabelWin
);
10794 GetTextBoxSize( dc
, lines
, &w
, &h
);
10795 if ( h
< m_defaultRowHeight
)
10796 h
= m_defaultRowHeight
;
10797 SetRowSize(row
, h
);
10801 void wxGrid::AutoSizeColLabelSize( int col
)
10803 wxArrayString lines
;
10806 // Hide the edit control, so it
10807 // won't interfere with drag-shrinking.
10808 if ( IsCellEditControlShown() )
10810 HideCellEditControl();
10811 SaveEditControlValue();
10814 // autosize column width depending on label text
10815 StringToLines( GetColLabelValue( col
), lines
);
10816 wxClientDC
dc( m_colLabelWin
);
10817 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10818 GetTextBoxSize( dc
, lines
, &w
, &h
);
10820 GetTextBoxSize( dc
, lines
, &h
, &w
);
10821 if ( w
< m_defaultColWidth
)
10822 w
= m_defaultColWidth
;
10823 SetColSize(col
, w
);
10827 wxSize
wxGrid::DoGetBestSize() const
10829 wxGrid
*self
= (wxGrid
*)this; // const_cast
10831 // we do the same as in AutoSize() here with the exception that we don't
10832 // change the column/row sizes, only calculate them
10833 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
10834 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
10835 wxSize
sizeFit(GetScrollX(size
.x
) * GetScrollLineX(),
10836 GetScrollY(size
.y
) * GetScrollLineY());
10838 // NOTE: This size should be cached, but first we need to add calls to
10839 // InvalidateBestSize everywhere that could change the results of this
10841 // CacheBestSize(size);
10843 return wxSize(sizeFit
.x
+ m_rowLabelWidth
, sizeFit
.y
+ m_colLabelHeight
)
10844 + GetWindowBorderSize();
10852 wxPen
& wxGrid::GetDividerPen() const
10857 // ----------------------------------------------------------------------------
10858 // cell value accessor functions
10859 // ----------------------------------------------------------------------------
10861 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10865 m_table
->SetValue( row
, col
, s
);
10866 if ( !GetBatchCount() )
10869 wxRect
rect( CellToRect( row
, col
) );
10871 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10872 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10873 m_gridWin
->Refresh( false, &rect
);
10876 if ( m_currentCellCoords
.GetRow() == row
&&
10877 m_currentCellCoords
.GetCol() == col
&&
10878 IsCellEditControlShown())
10879 // Note: If we are using IsCellEditControlEnabled,
10880 // this interacts badly with calling SetCellValue from
10881 // an EVT_GRID_CELL_CHANGE handler.
10883 HideCellEditControl();
10884 ShowCellEditControl(); // will reread data from table
10889 // ----------------------------------------------------------------------------
10890 // block, row and column selection
10891 // ----------------------------------------------------------------------------
10893 void wxGrid::SelectRow( int row
, bool addToSelected
)
10895 if ( IsSelection() && !addToSelected
)
10899 m_selection
->SelectRow( row
, false, addToSelected
);
10902 void wxGrid::SelectCol( int col
, bool addToSelected
)
10904 if ( IsSelection() && !addToSelected
)
10908 m_selection
->SelectCol( col
, false, addToSelected
);
10911 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10912 bool addToSelected
)
10914 if ( IsSelection() && !addToSelected
)
10918 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10919 false, addToSelected
);
10922 void wxGrid::SelectAll()
10924 if ( m_numRows
> 0 && m_numCols
> 0 )
10927 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10931 // ----------------------------------------------------------------------------
10932 // cell, row and col deselection
10933 // ----------------------------------------------------------------------------
10935 void wxGrid::DeselectRow( int row
)
10937 if ( !m_selection
)
10940 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10942 if ( m_selection
->IsInSelection(row
, 0 ) )
10943 m_selection
->ToggleCellSelection(row
, 0);
10947 int nCols
= GetNumberCols();
10948 for ( int i
= 0; i
< nCols
; i
++ )
10950 if ( m_selection
->IsInSelection(row
, i
) )
10951 m_selection
->ToggleCellSelection(row
, i
);
10956 void wxGrid::DeselectCol( int col
)
10958 if ( !m_selection
)
10961 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10963 if ( m_selection
->IsInSelection(0, col
) )
10964 m_selection
->ToggleCellSelection(0, col
);
10968 int nRows
= GetNumberRows();
10969 for ( int i
= 0; i
< nRows
; i
++ )
10971 if ( m_selection
->IsInSelection(i
, col
) )
10972 m_selection
->ToggleCellSelection(i
, col
);
10977 void wxGrid::DeselectCell( int row
, int col
)
10979 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10980 m_selection
->ToggleCellSelection(row
, col
);
10983 bool wxGrid::IsSelection() const
10985 return ( m_selection
&& (m_selection
->IsSelection() ||
10986 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10987 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10990 bool wxGrid::IsInSelection( int row
, int col
) const
10992 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10993 ( row
>= m_selectingTopLeft
.GetRow() &&
10994 col
>= m_selectingTopLeft
.GetCol() &&
10995 row
<= m_selectingBottomRight
.GetRow() &&
10996 col
<= m_selectingBottomRight
.GetCol() )) );
10999 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
11003 wxGridCellCoordsArray a
;
11007 return m_selection
->m_cellSelection
;
11010 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
11014 wxGridCellCoordsArray a
;
11018 return m_selection
->m_blockSelectionTopLeft
;
11021 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
11025 wxGridCellCoordsArray a
;
11029 return m_selection
->m_blockSelectionBottomRight
;
11032 wxArrayInt
wxGrid::GetSelectedRows() const
11040 return m_selection
->m_rowSelection
;
11043 wxArrayInt
wxGrid::GetSelectedCols() const
11051 return m_selection
->m_colSelection
;
11054 void wxGrid::ClearSelection()
11056 m_selectingTopLeft
=
11057 m_selectingBottomRight
=
11058 m_selectingKeyboard
= wxGridNoCellCoords
;
11060 m_selection
->ClearSelection();
11063 // This function returns the rectangle that encloses the given block
11064 // in device coords clipped to the client size of the grid window.
11066 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
11067 const wxGridCellCoords
&bottomRight
) const
11069 wxRect
rect( wxGridNoCellRect
);
11072 cellRect
= CellToRect( topLeft
);
11073 if ( cellRect
!= wxGridNoCellRect
)
11079 rect
= wxRect(0, 0, 0, 0);
11082 cellRect
= CellToRect( bottomRight
);
11083 if ( cellRect
!= wxGridNoCellRect
)
11089 return wxGridNoCellRect
;
11093 int left
= rect
.GetLeft();
11094 int top
= rect
.GetTop();
11095 int right
= rect
.GetRight();
11096 int bottom
= rect
.GetBottom();
11098 int leftCol
= topLeft
.GetCol();
11099 int topRow
= topLeft
.GetRow();
11100 int rightCol
= bottomRight
.GetCol();
11101 int bottomRow
= bottomRight
.GetRow();
11109 leftCol
= rightCol
;
11119 topRow
= bottomRow
;
11123 for ( j
= topRow
; j
<= bottomRow
; j
++ )
11125 for ( i
= leftCol
; i
<= rightCol
; i
++ )
11127 if ((j
== topRow
) || (j
== bottomRow
) || (i
== leftCol
) || (i
== rightCol
))
11129 cellRect
= CellToRect( j
, i
);
11131 if (cellRect
.x
< left
)
11133 if (cellRect
.y
< top
)
11135 if (cellRect
.x
+ cellRect
.width
> right
)
11136 right
= cellRect
.x
+ cellRect
.width
;
11137 if (cellRect
.y
+ cellRect
.height
> bottom
)
11138 bottom
= cellRect
.y
+ cellRect
.height
;
11142 i
= rightCol
; // jump over inner cells.
11147 // convert to scrolled coords
11149 CalcScrolledPosition( left
, top
, &left
, &top
);
11150 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
11153 m_gridWin
->GetClientSize( &cw
, &ch
);
11155 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
11156 return wxRect(0,0,0,0);
11158 rect
.SetLeft( wxMax(0, left
) );
11159 rect
.SetTop( wxMax(0, top
) );
11160 rect
.SetRight( wxMin(cw
, right
) );
11161 rect
.SetBottom( wxMin(ch
, bottom
) );
11166 // ----------------------------------------------------------------------------
11167 // grid event classes
11168 // ----------------------------------------------------------------------------
11170 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
11172 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
11173 int row
, int col
, int x
, int y
, bool sel
,
11174 bool control
, bool shift
, bool alt
, bool meta
)
11175 : wxNotifyEvent( type
, id
)
11182 m_control
= control
;
11187 SetEventObject(obj
);
11191 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
11193 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
11194 int rowOrCol
, int x
, int y
,
11195 bool control
, bool shift
, bool alt
, bool meta
)
11196 : wxNotifyEvent( type
, id
)
11198 m_rowOrCol
= rowOrCol
;
11201 m_control
= control
;
11206 SetEventObject(obj
);
11210 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
11212 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
11213 const wxGridCellCoords
& topLeft
,
11214 const wxGridCellCoords
& bottomRight
,
11215 bool sel
, bool control
,
11216 bool shift
, bool alt
, bool meta
)
11217 : wxNotifyEvent( type
, id
)
11219 m_topLeft
= topLeft
;
11220 m_bottomRight
= bottomRight
;
11222 m_control
= control
;
11227 SetEventObject(obj
);
11231 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
11233 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
11234 wxObject
* obj
, int row
,
11235 int col
, wxControl
* ctrl
)
11236 : wxCommandEvent(type
, id
)
11238 SetEventObject(obj
);
11244 #endif // wxUSE_GRID