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 int style
= wxBORDER_NONE
;
1525 style
|= wxCB_READONLY
;
1526 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1527 wxDefaultPosition
, wxDefaultSize
,
1530 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1533 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1534 wxGridCellAttr
* attr
)
1536 // as we fill the entire client area, don't do anything here to minimize
1539 // TODO: It doesn't actually fill the client area since the height of a
1540 // combo always defaults to the standard. Until someone has time to
1541 // figure out the right rectangle to paint, just do it the normal way.
1542 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1545 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1547 wxASSERT_MSG(m_control
,
1548 wxT("The wxGridCellEditor must be created first!"));
1550 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1552 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1554 // Don't immediately end if we get a kill focus event within BeginEdit
1556 evtHandler
->SetInSetFocus(true);
1558 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1562 Combo()->SetValue(m_startValue
);
1563 Combo()->SetInsertionPointEnd();
1565 else // the combobox is read-only
1567 // find the right position, or default to the first if not found
1568 int pos
= Combo()->FindString(m_startValue
);
1569 if (pos
== wxNOT_FOUND
)
1571 Combo()->SetSelection(pos
);
1574 Combo()->SetFocus();
1578 // When dropping down the menu, a kill focus event
1579 // happens after this point, so we can't reset the flag yet.
1580 #if !defined(__WXGTK20__)
1581 evtHandler
->SetInSetFocus(false);
1586 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1589 wxString value
= Combo()->GetValue();
1590 if ( value
== m_startValue
)
1593 grid
->GetTable()->SetValue(row
, col
, value
);
1598 void wxGridCellChoiceEditor::Reset()
1600 Combo()->SetValue(m_startValue
);
1601 Combo()->SetInsertionPointEnd();
1604 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1614 wxStringTokenizer
tk(params
, _T(','));
1615 while ( tk
.HasMoreTokens() )
1617 m_choices
.Add(tk
.GetNextToken());
1621 // return the value in the text control
1622 wxString
wxGridCellChoiceEditor::GetValue() const
1624 return Combo()->GetValue();
1627 #endif // wxUSE_COMBOBOX
1629 // ----------------------------------------------------------------------------
1630 // wxGridCellEditorEvtHandler
1631 // ----------------------------------------------------------------------------
1633 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
1635 // Don't disable the cell if we're just starting to edit it
1640 m_grid
->DisableCellEditControl();
1645 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1647 switch ( event
.GetKeyCode() )
1651 m_grid
->DisableCellEditControl();
1655 m_grid
->GetEventHandler()->ProcessEvent( event
);
1659 case WXK_NUMPAD_ENTER
:
1660 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1661 m_editor
->HandleReturn(event
);
1670 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1672 int row
= m_grid
->GetGridCursorRow();
1673 int col
= m_grid
->GetGridCursorCol();
1674 wxRect rect
= m_grid
->CellToRect( row
, col
);
1676 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
1678 // if cell width is smaller than grid client area, cell is wholly visible
1679 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
1681 switch ( event
.GetKeyCode() )
1686 case WXK_NUMPAD_ENTER
:
1691 if ( wholeCellVisible
)
1693 // no special processing needed...
1698 // do special processing for partly visible cell...
1700 // get the widths of all cells previous to this one
1702 for ( int i
= 0; i
< col
; i
++ )
1704 colXPos
+= m_grid
->GetColSize(i
);
1707 int xUnit
= 1, yUnit
= 1;
1708 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1711 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1715 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
1723 if ( wholeCellVisible
)
1725 // no special processing needed...
1730 // do special processing for partly visible cell...
1733 wxString value
= m_grid
->GetCellValue(row
, col
);
1734 if ( wxEmptyString
!= value
)
1736 // get width of cell CONTENTS (text)
1738 wxFont font
= m_grid
->GetCellFont(row
, col
);
1739 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
1741 // try to RIGHT align the text by scrolling
1742 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
1744 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
1745 // otherwise the last part of the cell content might be hidden below the scroll bar
1746 // FIXME: maybe there is a more suitable correction?
1747 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
1748 if ( textWidth
< 0 )
1754 // get the widths of all cells previous to this one
1756 for ( int i
= 0; i
< col
; i
++ )
1758 colXPos
+= m_grid
->GetColSize(i
);
1761 // and add the (modified) text width of the cell contents
1762 // as we'd like to see the last part of the cell contents
1763 colXPos
+= textWidth
;
1765 int xUnit
= 1, yUnit
= 1;
1766 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1767 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1778 // ----------------------------------------------------------------------------
1779 // wxGridCellWorker is an (almost) empty common base class for
1780 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1781 // ----------------------------------------------------------------------------
1783 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1788 wxGridCellWorker::~wxGridCellWorker()
1792 // ============================================================================
1794 // ============================================================================
1796 // ----------------------------------------------------------------------------
1797 // wxGridCellRenderer
1798 // ----------------------------------------------------------------------------
1800 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1801 wxGridCellAttr
& attr
,
1804 int WXUNUSED(row
), int WXUNUSED(col
),
1807 dc
.SetBackgroundMode( wxSOLID
);
1809 // grey out fields if the grid is disabled
1810 if ( grid
.IsEnabled() )
1814 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1818 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1823 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1826 dc
.SetPen( *wxTRANSPARENT_PEN
);
1827 dc
.DrawRectangle(rect
);
1830 // ----------------------------------------------------------------------------
1831 // wxGridCellStringRenderer
1832 // ----------------------------------------------------------------------------
1834 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
1835 const wxGridCellAttr
& attr
,
1839 dc
.SetBackgroundMode( wxTRANSPARENT
);
1841 // TODO some special colours for attr.IsReadOnly() case?
1843 // different coloured text when the grid is disabled
1844 if ( grid
.IsEnabled() )
1848 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1849 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1853 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1854 dc
.SetTextForeground( attr
.GetTextColour() );
1859 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1860 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
1863 dc
.SetFont( attr
.GetFont() );
1866 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
1868 const wxString
& text
)
1870 wxCoord x
= 0, y
= 0, max_x
= 0;
1871 dc
.SetFont(attr
.GetFont());
1872 wxStringTokenizer
tk(text
, _T('\n'));
1873 while ( tk
.HasMoreTokens() )
1875 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1876 max_x
= wxMax(max_x
, x
);
1879 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1881 return wxSize(max_x
, y
);
1884 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1885 wxGridCellAttr
& attr
,
1889 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1892 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1893 wxGridCellAttr
& attr
,
1895 const wxRect
& rectCell
,
1899 wxRect rect
= rectCell
;
1902 // erase only this cells background, overflow cells should have been erased
1903 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1906 attr
.GetAlignment(&hAlign
, &vAlign
);
1908 int overflowCols
= 0;
1910 if (attr
.GetOverflow())
1912 int cols
= grid
.GetNumberCols();
1913 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1914 int cell_rows
, cell_cols
;
1915 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <= 0
1916 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1918 int i
, c_cols
, c_rows
;
1919 for (i
= col
+cell_cols
; i
< cols
; i
++)
1921 bool is_empty
= true;
1922 for (int j
=row
; j
< row
+ cell_rows
; j
++)
1924 // check w/ anchor cell for multicell block
1925 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1928 if (!grid
.GetTable()->IsEmptyCell(j
+ c_rows
, i
))
1937 rect
.width
+= grid
.GetColSize(i
);
1945 if (rect
.width
>= best_width
)
1949 overflowCols
= i
- col
- cell_cols
+ 1;
1950 if (overflowCols
>= cols
)
1951 overflowCols
= cols
- 1;
1954 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1956 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1958 clip
.x
+= rectCell
.width
;
1959 // draw each overflow cell individually
1960 int col_end
= col
+ cell_cols
+ overflowCols
;
1961 if (col_end
>= grid
.GetNumberCols())
1962 col_end
= grid
.GetNumberCols() - 1;
1963 for (int i
= col
+ cell_cols
; i
<= col_end
; i
++)
1965 clip
.width
= grid
.GetColSize(i
) - 1;
1966 dc
.DestroyClippingRegion();
1967 dc
.SetClippingRegion(clip
);
1969 SetTextColoursAndFont(grid
, attr
, dc
,
1970 grid
.IsInSelection(row
,i
));
1972 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1973 rect
, hAlign
, vAlign
);
1974 clip
.x
+= grid
.GetColSize(i
) - 1;
1980 dc
.DestroyClippingRegion();
1984 // now we only have to draw the text
1985 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1987 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1988 rect
, hAlign
, vAlign
);
1991 // ----------------------------------------------------------------------------
1992 // wxGridCellNumberRenderer
1993 // ----------------------------------------------------------------------------
1995 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
1997 wxGridTableBase
*table
= grid
.GetTable();
1999 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
2001 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
2005 text
= table
->GetValue(row
, col
);
2011 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
2012 wxGridCellAttr
& attr
,
2014 const wxRect
& rectCell
,
2018 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2020 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2022 // draw the text right aligned by default
2024 attr
.GetAlignment(&hAlign
, &vAlign
);
2025 hAlign
= wxALIGN_RIGHT
;
2027 wxRect rect
= rectCell
;
2030 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2033 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
2034 wxGridCellAttr
& attr
,
2038 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2041 // ----------------------------------------------------------------------------
2042 // wxGridCellFloatRenderer
2043 // ----------------------------------------------------------------------------
2045 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
2048 SetPrecision(precision
);
2051 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
2053 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
2054 renderer
->m_width
= m_width
;
2055 renderer
->m_precision
= m_precision
;
2056 renderer
->m_format
= m_format
;
2061 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2063 wxGridTableBase
*table
= grid
.GetTable();
2068 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
2070 val
= table
->GetValueAsDouble(row
, col
);
2075 text
= table
->GetValue(row
, col
);
2076 hasDouble
= text
.ToDouble(&val
);
2083 if ( m_width
== -1 )
2085 if ( m_precision
== -1 )
2087 // default width/precision
2088 m_format
= _T("%f");
2092 m_format
.Printf(_T("%%.%df"), m_precision
);
2095 else if ( m_precision
== -1 )
2097 // default precision
2098 m_format
.Printf(_T("%%%d.f"), m_width
);
2102 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
2106 text
.Printf(m_format
, val
);
2109 //else: text already contains the string
2114 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
2115 wxGridCellAttr
& attr
,
2117 const wxRect
& rectCell
,
2121 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2123 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2125 // draw the text right aligned by default
2127 attr
.GetAlignment(&hAlign
, &vAlign
);
2128 hAlign
= wxALIGN_RIGHT
;
2130 wxRect rect
= rectCell
;
2133 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2136 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
2137 wxGridCellAttr
& attr
,
2141 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2144 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
2148 // reset to defaults
2154 wxString tmp
= params
.BeforeFirst(_T(','));
2158 if ( tmp
.ToLong(&width
) )
2160 SetWidth((int)width
);
2164 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
2168 tmp
= params
.AfterFirst(_T(','));
2172 if ( tmp
.ToLong(&precision
) )
2174 SetPrecision((int)precision
);
2178 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
2184 // ----------------------------------------------------------------------------
2185 // wxGridCellBoolRenderer
2186 // ----------------------------------------------------------------------------
2188 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
2190 // FIXME these checkbox size calculations are really ugly...
2192 // between checkmark and box
2193 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2195 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2196 wxGridCellAttr
& WXUNUSED(attr
),
2201 // compute it only once (no locks for MT safeness in GUI thread...)
2202 if ( !ms_sizeCheckMark
.x
)
2204 // get checkbox size
2205 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2206 wxSize size
= checkbox
->GetBestSize();
2207 wxCoord checkSize
= size
.y
+ 2 * wxGRID_CHECKMARK_MARGIN
;
2209 #if defined(__WXMOTIF__)
2210 checkSize
-= size
.y
/ 2;
2215 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2218 return ms_sizeCheckMark
;
2221 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2222 wxGridCellAttr
& attr
,
2228 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2230 // draw a check mark in the centre (ignoring alignment - TODO)
2231 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2233 // don't draw outside the cell
2234 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2235 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2237 // and even leave (at least) 1 pixel margin
2238 size
.x
= size
.y
= minSize
;
2241 // draw a border around checkmark
2243 attr
.GetAlignment(&hAlign
, &vAlign
);
2246 if (hAlign
== wxALIGN_CENTRE
)
2248 rectBorder
.x
= rect
.x
+ rect
.width
/ 2 - size
.x
/ 2;
2249 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2250 rectBorder
.width
= size
.x
;
2251 rectBorder
.height
= size
.y
;
2253 else if (hAlign
== wxALIGN_LEFT
)
2255 rectBorder
.x
= rect
.x
+ 2;
2256 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2257 rectBorder
.width
= size
.x
;
2258 rectBorder
.height
= size
.y
;
2260 else if (hAlign
== wxALIGN_RIGHT
)
2262 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2263 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2264 rectBorder
.width
= size
.x
;
2265 rectBorder
.height
= size
.y
;
2269 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2271 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2275 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2276 value
= wxGridCellBoolEditor::IsTrueValue(cellval
);
2281 flags
|= wxCONTROL_CHECKED
;
2283 wxRendererNative::Get().DrawCheckBox( &grid
, dc
, rectBorder
, flags
);
2286 // ----------------------------------------------------------------------------
2288 // ----------------------------------------------------------------------------
2290 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2294 m_isReadOnly
= Unset
;
2299 m_attrkind
= wxGridCellAttr::Cell
;
2301 m_sizeRows
= m_sizeCols
= 1;
2302 m_overflow
= UnsetOverflow
;
2304 SetDefAttr(attrDefault
);
2307 wxGridCellAttr
*wxGridCellAttr::Clone() const
2309 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2311 if ( HasTextColour() )
2312 attr
->SetTextColour(GetTextColour());
2313 if ( HasBackgroundColour() )
2314 attr
->SetBackgroundColour(GetBackgroundColour());
2316 attr
->SetFont(GetFont());
2317 if ( HasAlignment() )
2318 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2320 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2324 attr
->SetRenderer(m_renderer
);
2325 m_renderer
->IncRef();
2329 attr
->SetEditor(m_editor
);
2334 attr
->SetReadOnly();
2336 attr
->SetOverflow( m_overflow
== Overflow
);
2337 attr
->SetKind( m_attrkind
);
2342 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2344 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2345 SetTextColour(mergefrom
->GetTextColour());
2346 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2347 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2348 if ( !HasFont() && mergefrom
->HasFont() )
2349 SetFont(mergefrom
->GetFont());
2350 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2353 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2354 SetAlignment(hAlign
, vAlign
);
2356 if ( !HasSize() && mergefrom
->HasSize() )
2357 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2359 // Directly access member functions as GetRender/Editor don't just return
2360 // m_renderer/m_editor
2362 // Maybe add support for merge of Render and Editor?
2363 if (!HasRenderer() && mergefrom
->HasRenderer() )
2365 m_renderer
= mergefrom
->m_renderer
;
2366 m_renderer
->IncRef();
2368 if ( !HasEditor() && mergefrom
->HasEditor() )
2370 m_editor
= mergefrom
->m_editor
;
2373 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2374 SetReadOnly(mergefrom
->IsReadOnly());
2376 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2377 SetOverflow(mergefrom
->GetOverflow());
2379 SetDefAttr(mergefrom
->m_defGridAttr
);
2382 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2384 // The size of a cell is normally 1,1
2386 // If this cell is larger (2,2) then this is the top left cell
2387 // the other cells that will be covered (lower right cells) must be
2388 // set to negative or zero values such that
2389 // row + num_rows of the covered cell points to the larger cell (this cell)
2390 // same goes for the col + num_cols.
2392 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2394 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
2395 !((num_rows
<= 0) && (num_cols
> 0)) ||
2396 !((num_rows
== 0) && (num_cols
== 0))),
2397 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2399 m_sizeRows
= num_rows
;
2400 m_sizeCols
= num_cols
;
2403 const wxColour
& wxGridCellAttr::GetTextColour() const
2405 if (HasTextColour())
2409 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2411 return m_defGridAttr
->GetTextColour();
2415 wxFAIL_MSG(wxT("Missing default cell attribute"));
2416 return wxNullColour
;
2420 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2422 if (HasBackgroundColour())
2426 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2428 return m_defGridAttr
->GetBackgroundColour();
2432 wxFAIL_MSG(wxT("Missing default cell attribute"));
2433 return wxNullColour
;
2437 const wxFont
& wxGridCellAttr::GetFont() const
2443 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2445 return m_defGridAttr
->GetFont();
2449 wxFAIL_MSG(wxT("Missing default cell attribute"));
2454 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2463 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2465 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2469 wxFAIL_MSG(wxT("Missing default cell attribute"));
2473 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2476 *num_rows
= m_sizeRows
;
2478 *num_cols
= m_sizeCols
;
2481 // GetRenderer and GetEditor use a slightly different decision path about
2482 // which attribute to use. If a non-default attr object has one then it is
2483 // used, otherwise the default editor or renderer is fetched from the grid and
2484 // used. It should be the default for the data type of the cell. If it is
2485 // NULL (because the table has a type that the grid does not have in its
2486 // registry), then the grid's default editor or renderer is used.
2488 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(const wxGrid
* grid
, int row
, int col
) const
2490 wxGridCellRenderer
*renderer
= NULL
;
2492 if ( m_renderer
&& this != m_defGridAttr
)
2494 // use the cells renderer if it has one
2495 renderer
= m_renderer
;
2498 else // no non-default cell renderer
2500 // get default renderer for the data type
2503 // GetDefaultRendererForCell() will do IncRef() for us
2504 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2507 if ( renderer
== NULL
)
2509 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2511 // if we still don't have one then use the grid default
2512 // (no need for IncRef() here neither)
2513 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2515 else // default grid attr
2517 // use m_renderer which we had decided not to use initially
2518 renderer
= m_renderer
;
2525 // we're supposed to always find something
2526 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2531 // same as above, except for s/renderer/editor/g
2532 wxGridCellEditor
* wxGridCellAttr::GetEditor(const wxGrid
* grid
, int row
, int col
) const
2534 wxGridCellEditor
*editor
= NULL
;
2536 if ( m_editor
&& this != m_defGridAttr
)
2538 // use the cells editor if it has one
2542 else // no non default cell editor
2544 // get default editor for the data type
2547 // GetDefaultEditorForCell() will do IncRef() for us
2548 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2551 if ( editor
== NULL
)
2553 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2555 // if we still don't have one then use the grid default
2556 // (no need for IncRef() here neither)
2557 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2559 else // default grid attr
2561 // use m_editor which we had decided not to use initially
2569 // we're supposed to always find something
2570 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2575 // ----------------------------------------------------------------------------
2576 // wxGridCellAttrData
2577 // ----------------------------------------------------------------------------
2579 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2581 int n
= FindIndex(row
, col
);
2582 if ( n
== wxNOT_FOUND
)
2584 // add the attribute
2585 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2589 // free the old attribute
2590 m_attrs
[(size_t)n
].attr
->DecRef();
2594 // change the attribute
2595 m_attrs
[(size_t)n
].attr
= attr
;
2599 // remove this attribute
2600 m_attrs
.RemoveAt((size_t)n
);
2605 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2607 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2609 int n
= FindIndex(row
, col
);
2610 if ( n
!= wxNOT_FOUND
)
2612 attr
= m_attrs
[(size_t)n
].attr
;
2619 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2621 size_t count
= m_attrs
.GetCount();
2622 for ( size_t n
= 0; n
< count
; n
++ )
2624 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2625 wxCoord row
= coords
.GetRow();
2626 if ((size_t)row
>= pos
)
2630 // If rows inserted, include row counter where necessary
2631 coords
.SetRow(row
+ numRows
);
2633 else if (numRows
< 0)
2635 // If rows deleted ...
2636 if ((size_t)row
>= pos
- numRows
)
2638 // ...either decrement row counter (if row still exists)...
2639 coords
.SetRow(row
+ numRows
);
2643 // ...or remove the attribute
2644 // No need to DecRef the attribute itself since this is
2645 // done be wxGridCellWithAttr's destructor!
2646 m_attrs
.RemoveAt(n
);
2655 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2657 size_t count
= m_attrs
.GetCount();
2658 for ( size_t n
= 0; n
< count
; n
++ )
2660 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2661 wxCoord col
= coords
.GetCol();
2662 if ( (size_t)col
>= pos
)
2666 // If rows inserted, include row counter where necessary
2667 coords
.SetCol(col
+ numCols
);
2669 else if (numCols
< 0)
2671 // If rows deleted ...
2672 if ((size_t)col
>= pos
- numCols
)
2674 // ...either decrement row counter (if row still exists)...
2675 coords
.SetCol(col
+ numCols
);
2679 // ...or remove the attribute
2680 // No need to DecRef the attribute itself since this is
2681 // done be wxGridCellWithAttr's destructor!
2682 m_attrs
.RemoveAt(n
);
2691 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2693 size_t count
= m_attrs
.GetCount();
2694 for ( size_t n
= 0; n
< count
; n
++ )
2696 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2697 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2706 // ----------------------------------------------------------------------------
2707 // wxGridRowOrColAttrData
2708 // ----------------------------------------------------------------------------
2710 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2712 size_t count
= m_attrs
.GetCount();
2713 for ( size_t n
= 0; n
< count
; n
++ )
2715 m_attrs
[n
]->DecRef();
2719 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2721 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2723 int n
= m_rowsOrCols
.Index(rowOrCol
);
2724 if ( n
!= wxNOT_FOUND
)
2726 attr
= m_attrs
[(size_t)n
];
2733 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2735 int i
= m_rowsOrCols
.Index(rowOrCol
);
2736 if ( i
== wxNOT_FOUND
)
2740 // add the attribute
2741 m_rowsOrCols
.Add(rowOrCol
);
2744 // nothing to remove
2748 size_t n
= (size_t)i
;
2751 // change the attribute
2752 m_attrs
[n
]->DecRef();
2757 // remove this attribute
2758 m_attrs
[n
]->DecRef();
2759 m_rowsOrCols
.RemoveAt(n
);
2760 m_attrs
.RemoveAt(n
);
2765 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2767 size_t count
= m_attrs
.GetCount();
2768 for ( size_t n
= 0; n
< count
; n
++ )
2770 int & rowOrCol
= m_rowsOrCols
[n
];
2771 if ( (size_t)rowOrCol
>= pos
)
2773 if ( numRowsOrCols
> 0 )
2775 // If rows inserted, include row counter where necessary
2776 rowOrCol
+= numRowsOrCols
;
2778 else if ( numRowsOrCols
< 0)
2780 // If rows deleted, either decrement row counter (if row still exists)
2781 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2782 rowOrCol
+= numRowsOrCols
;
2785 m_rowsOrCols
.RemoveAt(n
);
2786 m_attrs
[n
]->DecRef();
2787 m_attrs
.RemoveAt(n
);
2796 // ----------------------------------------------------------------------------
2797 // wxGridCellAttrProvider
2798 // ----------------------------------------------------------------------------
2800 wxGridCellAttrProvider::wxGridCellAttrProvider()
2802 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2805 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2810 void wxGridCellAttrProvider::InitData()
2812 m_data
= new wxGridCellAttrProviderData
;
2815 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2816 wxGridCellAttr::wxAttrKind kind
) const
2818 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2823 case (wxGridCellAttr::Any
):
2824 // Get cached merge attributes.
2825 // Currently not used as no cache implemented as not mutable
2826 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2829 // Basically implement old version.
2830 // Also check merge cache, so we don't have to re-merge every time..
2831 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2832 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2833 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2835 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
2837 // Two or more are non NULL
2838 attr
= new wxGridCellAttr
;
2839 attr
->SetKind(wxGridCellAttr::Merged
);
2841 // Order is important..
2844 attr
->MergeWith(attrcell
);
2849 attr
->MergeWith(attrcol
);
2854 attr
->MergeWith(attrrow
);
2858 // store merge attr if cache implemented
2860 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2864 // one or none is non null return it or null.
2883 case (wxGridCellAttr::Cell
):
2884 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2887 case (wxGridCellAttr::Col
):
2888 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2891 case (wxGridCellAttr::Row
):
2892 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2897 // (wxGridCellAttr::Default):
2898 // (wxGridCellAttr::Merged):
2906 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2912 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2915 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2920 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2923 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2928 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2931 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2935 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2937 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2941 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2945 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2947 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2951 // ----------------------------------------------------------------------------
2952 // wxGridTypeRegistry
2953 // ----------------------------------------------------------------------------
2955 wxGridTypeRegistry::~wxGridTypeRegistry()
2957 size_t count
= m_typeinfo
.GetCount();
2958 for ( size_t i
= 0; i
< count
; i
++ )
2959 delete m_typeinfo
[i
];
2962 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2963 wxGridCellRenderer
* renderer
,
2964 wxGridCellEditor
* editor
)
2966 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2968 // is it already registered?
2969 int loc
= FindRegisteredDataType(typeName
);
2970 if ( loc
!= wxNOT_FOUND
)
2972 delete m_typeinfo
[loc
];
2973 m_typeinfo
[loc
] = info
;
2977 m_typeinfo
.Add(info
);
2981 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2983 size_t count
= m_typeinfo
.GetCount();
2984 for ( size_t i
= 0; i
< count
; i
++ )
2986 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2995 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2997 int index
= FindRegisteredDataType(typeName
);
2998 if ( index
== wxNOT_FOUND
)
3000 // check whether this is one of the standard ones, in which case
3001 // register it "on the fly"
3003 if ( typeName
== wxGRID_VALUE_STRING
)
3005 RegisterDataType(wxGRID_VALUE_STRING
,
3006 new wxGridCellStringRenderer
,
3007 new wxGridCellTextEditor
);
3010 #endif // wxUSE_TEXTCTRL
3012 if ( typeName
== wxGRID_VALUE_BOOL
)
3014 RegisterDataType(wxGRID_VALUE_BOOL
,
3015 new wxGridCellBoolRenderer
,
3016 new wxGridCellBoolEditor
);
3019 #endif // wxUSE_CHECKBOX
3021 if ( typeName
== wxGRID_VALUE_NUMBER
)
3023 RegisterDataType(wxGRID_VALUE_NUMBER
,
3024 new wxGridCellNumberRenderer
,
3025 new wxGridCellNumberEditor
);
3027 else if ( typeName
== wxGRID_VALUE_FLOAT
)
3029 RegisterDataType(wxGRID_VALUE_FLOAT
,
3030 new wxGridCellFloatRenderer
,
3031 new wxGridCellFloatEditor
);
3034 #endif // wxUSE_TEXTCTRL
3036 if ( typeName
== wxGRID_VALUE_CHOICE
)
3038 RegisterDataType(wxGRID_VALUE_CHOICE
,
3039 new wxGridCellStringRenderer
,
3040 new wxGridCellChoiceEditor
);
3043 #endif // wxUSE_COMBOBOX
3048 // we get here only if just added the entry for this type, so return
3050 index
= m_typeinfo
.GetCount() - 1;
3056 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
3058 int index
= FindDataType(typeName
);
3059 if ( index
== wxNOT_FOUND
)
3061 // the first part of the typename is the "real" type, anything after ':'
3062 // are the parameters for the renderer
3063 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
3064 if ( index
== wxNOT_FOUND
)
3069 wxGridCellRenderer
*renderer
= GetRenderer(index
);
3070 wxGridCellRenderer
*rendererOld
= renderer
;
3071 renderer
= renderer
->Clone();
3072 rendererOld
->DecRef();
3074 wxGridCellEditor
*editor
= GetEditor(index
);
3075 wxGridCellEditor
*editorOld
= editor
;
3076 editor
= editor
->Clone();
3077 editorOld
->DecRef();
3079 // do it even if there are no parameters to reset them to defaults
3080 wxString params
= typeName
.AfterFirst(_T(':'));
3081 renderer
->SetParameters(params
);
3082 editor
->SetParameters(params
);
3084 // register the new typename
3085 RegisterDataType(typeName
, renderer
, editor
);
3087 // we just registered it, it's the last one
3088 index
= m_typeinfo
.GetCount() - 1;
3094 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
3096 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
3103 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
3105 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
3112 // ----------------------------------------------------------------------------
3114 // ----------------------------------------------------------------------------
3116 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
3118 wxGridTableBase::wxGridTableBase()
3120 m_view
= (wxGrid
*) NULL
;
3121 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
3124 wxGridTableBase::~wxGridTableBase()
3126 delete m_attrProvider
;
3129 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
3131 delete m_attrProvider
;
3132 m_attrProvider
= attrProvider
;
3135 bool wxGridTableBase::CanHaveAttributes()
3137 if ( ! GetAttrProvider() )
3139 // use the default attr provider by default
3140 SetAttrProvider(new wxGridCellAttrProvider
);
3146 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
3148 if ( m_attrProvider
)
3149 return m_attrProvider
->GetAttr(row
, col
, kind
);
3151 return (wxGridCellAttr
*)NULL
;
3154 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
3156 if ( m_attrProvider
)
3158 attr
->SetKind(wxGridCellAttr::Cell
);
3159 m_attrProvider
->SetAttr(attr
, row
, col
);
3163 // as we take ownership of the pointer and don't store it, we must
3169 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3171 if ( m_attrProvider
)
3173 attr
->SetKind(wxGridCellAttr::Row
);
3174 m_attrProvider
->SetRowAttr(attr
, row
);
3178 // as we take ownership of the pointer and don't store it, we must
3184 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3186 if ( m_attrProvider
)
3188 attr
->SetKind(wxGridCellAttr::Col
);
3189 m_attrProvider
->SetColAttr(attr
, col
);
3193 // as we take ownership of the pointer and don't store it, we must
3199 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3200 size_t WXUNUSED(numRows
) )
3202 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3207 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3209 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3214 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3215 size_t WXUNUSED(numRows
) )
3217 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3222 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3223 size_t WXUNUSED(numCols
) )
3225 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3230 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3232 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3237 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3238 size_t WXUNUSED(numCols
) )
3240 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3245 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3249 // RD: Starting the rows at zero confuses users,
3250 // no matter how much it makes sense to us geeks.
3256 wxString
wxGridTableBase::GetColLabelValue( int col
)
3258 // default col labels are:
3259 // cols 0 to 25 : A-Z
3260 // cols 26 to 675 : AA-ZZ
3265 for ( n
= 1; ; n
++ )
3267 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
3273 // reverse the string...
3275 for ( i
= 0; i
< n
; i
++ )
3283 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3285 return wxGRID_VALUE_STRING
;
3288 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3289 const wxString
& typeName
)
3291 return typeName
== wxGRID_VALUE_STRING
;
3294 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3296 return CanGetValueAs(row
, col
, typeName
);
3299 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3304 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3309 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3314 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3315 long WXUNUSED(value
) )
3319 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3320 double WXUNUSED(value
) )
3324 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3325 bool WXUNUSED(value
) )
3329 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3330 const wxString
& WXUNUSED(typeName
) )
3335 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3336 const wxString
& WXUNUSED(typeName
),
3337 void* WXUNUSED(value
) )
3341 //////////////////////////////////////////////////////////////////////
3343 // Message class for the grid table to send requests and notifications
3347 wxGridTableMessage::wxGridTableMessage()
3349 m_table
= (wxGridTableBase
*) NULL
;
3355 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3356 int commandInt1
, int commandInt2
)
3360 m_comInt1
= commandInt1
;
3361 m_comInt2
= commandInt2
;
3364 //////////////////////////////////////////////////////////////////////
3366 // A basic grid table for string data. An object of this class will
3367 // created by wxGrid if you don't specify an alternative table class.
3370 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3372 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3374 wxGridStringTable::wxGridStringTable()
3379 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3382 m_data
.Alloc( numRows
);
3385 sa
.Alloc( numCols
);
3386 sa
.Add( wxEmptyString
, numCols
);
3388 m_data
.Add( sa
, numRows
);
3391 wxGridStringTable::~wxGridStringTable()
3395 int wxGridStringTable::GetNumberRows()
3397 return m_data
.GetCount();
3400 int wxGridStringTable::GetNumberCols()
3402 if ( m_data
.GetCount() > 0 )
3403 return m_data
[0].GetCount();
3408 wxString
wxGridStringTable::GetValue( int row
, int col
)
3410 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3412 _T("invalid row or column index in wxGridStringTable") );
3414 return m_data
[row
][col
];
3417 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3419 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3420 _T("invalid row or column index in wxGridStringTable") );
3422 m_data
[row
][col
] = value
;
3425 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3427 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3429 _T("invalid row or column index in wxGridStringTable") );
3431 return (m_data
[row
][col
] == wxEmptyString
);
3434 void wxGridStringTable::Clear()
3437 int numRows
, numCols
;
3439 numRows
= m_data
.GetCount();
3442 numCols
= m_data
[0].GetCount();
3444 for ( row
= 0; row
< numRows
; row
++ )
3446 for ( col
= 0; col
< numCols
; col
++ )
3448 m_data
[row
][col
] = wxEmptyString
;
3454 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3456 size_t curNumRows
= m_data
.GetCount();
3457 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3458 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3460 if ( pos
>= curNumRows
)
3462 return AppendRows( numRows
);
3466 sa
.Alloc( curNumCols
);
3467 sa
.Add( wxEmptyString
, curNumCols
);
3468 m_data
.Insert( sa
, pos
, numRows
);
3472 wxGridTableMessage
msg( this,
3473 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3477 GetView()->ProcessTableMessage( msg
);
3483 bool wxGridStringTable::AppendRows( size_t numRows
)
3485 size_t curNumRows
= m_data
.GetCount();
3486 size_t curNumCols
= ( curNumRows
> 0
3487 ? m_data
[0].GetCount()
3488 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3491 if ( curNumCols
> 0 )
3493 sa
.Alloc( curNumCols
);
3494 sa
.Add( wxEmptyString
, curNumCols
);
3497 m_data
.Add( sa
, numRows
);
3501 wxGridTableMessage
msg( this,
3502 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3505 GetView()->ProcessTableMessage( msg
);
3511 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3513 size_t curNumRows
= m_data
.GetCount();
3515 if ( pos
>= curNumRows
)
3517 wxFAIL_MSG( wxString::Format
3519 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3521 (unsigned long)numRows
,
3522 (unsigned long)curNumRows
3528 if ( numRows
> curNumRows
- pos
)
3530 numRows
= curNumRows
- pos
;
3533 if ( numRows
>= curNumRows
)
3539 m_data
.RemoveAt( pos
, numRows
);
3544 wxGridTableMessage
msg( this,
3545 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3549 GetView()->ProcessTableMessage( msg
);
3555 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3559 size_t curNumRows
= m_data
.GetCount();
3560 size_t curNumCols
= ( curNumRows
> 0
3561 ? m_data
[0].GetCount()
3562 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3564 if ( pos
>= curNumCols
)
3566 return AppendCols( numCols
);
3569 if ( !m_colLabels
.IsEmpty() )
3571 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
3574 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
3575 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
3578 for ( row
= 0; row
< curNumRows
; row
++ )
3580 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3582 m_data
[row
].Insert( wxEmptyString
, col
);
3588 wxGridTableMessage
msg( this,
3589 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3593 GetView()->ProcessTableMessage( msg
);
3599 bool wxGridStringTable::AppendCols( size_t numCols
)
3603 size_t curNumRows
= m_data
.GetCount();
3608 // TODO: something better than this ?
3610 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3615 for ( row
= 0; row
< curNumRows
; row
++ )
3617 m_data
[row
].Add( wxEmptyString
, numCols
);
3622 wxGridTableMessage
msg( this,
3623 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3626 GetView()->ProcessTableMessage( msg
);
3632 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3636 size_t curNumRows
= m_data
.GetCount();
3637 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3638 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3640 if ( pos
>= curNumCols
)
3642 wxFAIL_MSG( wxString::Format
3644 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3646 (unsigned long)numCols
,
3647 (unsigned long)curNumCols
3654 colID
= GetView()->GetColAt( pos
);
3658 if ( numCols
> curNumCols
- colID
)
3660 numCols
= curNumCols
- colID
;
3663 if ( !m_colLabels
.IsEmpty() )
3665 // m_colLabels stores just as many elements as it needs, e.g. if only
3666 // the label of the first column had been set it would have only one
3667 // element and not numCols, so account for it
3668 int nToRm
= m_colLabels
.size() - colID
;
3670 m_colLabels
.RemoveAt( colID
, nToRm
);
3673 for ( row
= 0; row
< curNumRows
; row
++ )
3675 if ( numCols
>= curNumCols
)
3677 m_data
[row
].Clear();
3681 m_data
[row
].RemoveAt( colID
, numCols
);
3687 wxGridTableMessage
msg( this,
3688 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3692 GetView()->ProcessTableMessage( msg
);
3698 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3700 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3702 // using default label
3704 return wxGridTableBase::GetRowLabelValue( row
);
3708 return m_rowLabels
[row
];
3712 wxString
wxGridStringTable::GetColLabelValue( int col
)
3714 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3716 // using default label
3718 return wxGridTableBase::GetColLabelValue( col
);
3722 return m_colLabels
[col
];
3726 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3728 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3730 int n
= m_rowLabels
.GetCount();
3733 for ( i
= n
; i
<= row
; i
++ )
3735 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3739 m_rowLabels
[row
] = value
;
3742 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3744 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3746 int n
= m_colLabels
.GetCount();
3749 for ( i
= n
; i
<= col
; i
++ )
3751 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3755 m_colLabels
[col
] = value
;
3759 //////////////////////////////////////////////////////////////////////
3760 //////////////////////////////////////////////////////////////////////
3762 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
)
3763 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost
)
3766 void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
3768 m_owner
->CancelMouseCapture();
3771 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3773 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow
)
3774 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3775 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3776 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3777 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3778 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3779 EVT_CHAR( wxGridRowLabelWindow::OnChar
)
3782 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3784 const wxPoint
&pos
, const wxSize
&size
)
3785 : wxGridSubwindow(parent
, id
, pos
, size
)
3790 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3794 // NO - don't do this because it will set both the x and y origin
3795 // coords to match the parent scrolled window and we just want to
3796 // set the y coord - MB
3798 // m_owner->PrepareDC( dc );
3801 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3802 wxPoint pt
= dc
.GetDeviceOrigin();
3803 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
3805 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3806 m_owner
->DrawRowLabels( dc
, rows
);
3809 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3811 m_owner
->ProcessRowLabelMouseEvent( event
);
3814 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3816 m_owner
->GetEventHandler()->ProcessEvent( event
);
3819 // This seems to be required for wxMotif otherwise the mouse
3820 // cursor must be in the cell edit control to get key events
3822 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3824 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3828 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3830 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3834 void wxGridRowLabelWindow::OnChar( wxKeyEvent
& event
)
3836 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3840 //////////////////////////////////////////////////////////////////////
3842 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3844 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow
)
3845 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3846 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3847 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3848 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3849 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3850 EVT_CHAR( wxGridColLabelWindow::OnChar
)
3853 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3855 const wxPoint
&pos
, const wxSize
&size
)
3856 : wxGridSubwindow(parent
, id
, pos
, size
)
3861 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3865 // NO - don't do this because it will set both the x and y origin
3866 // coords to match the parent scrolled window and we just want to
3867 // set the x coord - MB
3869 // m_owner->PrepareDC( dc );
3872 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3873 wxPoint pt
= dc
.GetDeviceOrigin();
3874 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3875 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
3877 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
3879 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3880 m_owner
->DrawColLabels( dc
, cols
);
3883 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3885 m_owner
->ProcessColLabelMouseEvent( event
);
3888 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3890 m_owner
->GetEventHandler()->ProcessEvent( event
);
3893 // This seems to be required for wxMotif otherwise the mouse
3894 // cursor must be in the cell edit control to get key events
3896 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3898 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3902 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3904 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3908 void wxGridColLabelWindow::OnChar( wxKeyEvent
& event
)
3910 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3914 //////////////////////////////////////////////////////////////////////
3916 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3918 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow
)
3919 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3920 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3921 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3922 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3923 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3924 EVT_CHAR( wxGridCornerLabelWindow::OnChar
)
3927 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3929 const wxPoint
&pos
, const wxSize
&size
)
3930 : wxGridSubwindow(parent
, id
, pos
, size
)
3935 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3939 int client_height
= 0;
3940 int client_width
= 0;
3941 GetClientSize( &client_width
, &client_height
);
3943 // VZ: any reason for this ifdef? (FIXME)
3949 rect
.SetWidth( client_width
- 2 );
3950 rect
.SetHeight( client_height
- 2 );
3952 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3954 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
3955 dc
.DrawLine( client_width
- 1, client_height
- 1, client_width
- 1, 0 );
3956 dc
.DrawLine( client_width
- 1, client_height
- 1, 0, client_height
- 1 );
3957 dc
.DrawLine( 0, 0, client_width
, 0 );
3958 dc
.DrawLine( 0, 0, 0, client_height
);
3960 dc
.SetPen( *wxWHITE_PEN
);
3961 dc
.DrawLine( 1, 1, client_width
- 1, 1 );
3962 dc
.DrawLine( 1, 1, 1, client_height
- 1 );
3966 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3968 m_owner
->ProcessCornerLabelMouseEvent( event
);
3971 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3973 m_owner
->GetEventHandler()->ProcessEvent(event
);
3976 // This seems to be required for wxMotif otherwise the mouse
3977 // cursor must be in the cell edit control to get key events
3979 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3981 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3985 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3987 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3991 void wxGridCornerLabelWindow::OnChar( wxKeyEvent
& event
)
3993 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3997 //////////////////////////////////////////////////////////////////////
3999 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
4001 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow
)
4002 EVT_PAINT( wxGridWindow::OnPaint
)
4003 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
4004 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
4005 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
4006 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
4007 EVT_CHAR( wxGridWindow::OnChar
)
4008 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
4009 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
4010 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
4013 wxGridWindow::wxGridWindow( wxGrid
*parent
,
4014 wxGridRowLabelWindow
*rowLblWin
,
4015 wxGridColLabelWindow
*colLblWin
,
4018 const wxSize
&size
)
4019 : wxGridSubwindow(parent
, id
, pos
, size
,
4020 wxCLIP_CHILDREN
, wxT("grid window") )
4023 m_rowLabelWin
= rowLblWin
;
4024 m_colLabelWin
= colLblWin
;
4027 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
4029 wxPaintDC
dc( this );
4030 m_owner
->PrepareDC( dc
);
4031 wxRegion reg
= GetUpdateRegion();
4032 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
4033 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
4035 #if WXGRID_DRAW_LINES
4036 m_owner
->DrawAllGridLines( dc
, reg
);
4039 m_owner
->DrawGridSpace( dc
);
4040 m_owner
->DrawHighlight( dc
, dirtyCells
);
4043 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
4045 wxWindow::ScrollWindow( dx
, dy
, rect
);
4046 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
4047 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
4050 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
4052 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
4055 m_owner
->ProcessGridCellMouseEvent( event
);
4058 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
4060 m_owner
->GetEventHandler()->ProcessEvent( event
);
4063 // This seems to be required for wxMotif/wxGTK otherwise the mouse
4064 // cursor must be in the cell edit control to get key events
4066 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
4068 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4072 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
4074 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4078 void wxGridWindow::OnChar( wxKeyEvent
& event
)
4080 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4084 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
4088 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
4090 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4094 //////////////////////////////////////////////////////////////////////
4096 // Internal Helper function for computing row or column from some
4097 // (unscrolled) coordinate value, using either
4098 // m_defaultRowHeight/m_defaultColWidth or binary search on array
4099 // of m_rowBottoms/m_ColRights to speed up the search!
4101 // Internal helper macros for simpler use of that function
4103 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
4104 const wxArrayInt
& BorderArray
, int nMax
,
4107 #define internalXToCol(x) XToCol(x, true)
4108 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
4109 m_minAcceptableRowHeight, \
4110 m_rowBottoms, m_numRows, true)
4112 /////////////////////////////////////////////////////////////////////
4114 #if wxUSE_EXTENDED_RTTI
4115 WX_DEFINE_FLAGS( wxGridStyle
)
4117 wxBEGIN_FLAGS( wxGridStyle
)
4118 // new style border flags, we put them first to
4119 // use them for streaming out
4120 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4121 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4122 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4123 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4124 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4125 wxFLAGS_MEMBER(wxBORDER_NONE
)
4127 // old style border flags
4128 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4129 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4130 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4131 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4132 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4133 wxFLAGS_MEMBER(wxBORDER
)
4135 // standard window styles
4136 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4137 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4138 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4139 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4140 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4141 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4142 wxFLAGS_MEMBER(wxVSCROLL
)
4143 wxFLAGS_MEMBER(wxHSCROLL
)
4145 wxEND_FLAGS( wxGridStyle
)
4147 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4149 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4150 wxHIDE_PROPERTY( Children
)
4151 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4152 wxEND_PROPERTIES_TABLE()
4154 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4155 wxEND_HANDLERS_TABLE()
4157 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4160 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
4163 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4166 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4167 EVT_PAINT( wxGrid::OnPaint
)
4168 EVT_SIZE( wxGrid::OnSize
)
4169 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
4170 EVT_KEY_UP( wxGrid::OnKeyUp
)
4171 EVT_CHAR ( wxGrid::OnChar
)
4172 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
4177 // in order to make sure that a size event is not
4178 // trigerred in a unfinished state
4179 m_cornerLabelWin
= NULL
;
4180 m_rowLabelWin
= NULL
;
4181 m_colLabelWin
= NULL
;
4185 wxGrid::wxGrid( wxWindow
*parent
,
4190 const wxString
& name
)
4191 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
4192 m_colMinWidths(GRID_HASH_SIZE
),
4193 m_rowMinHeights(GRID_HASH_SIZE
)
4196 SetInitialSize(size
);
4199 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
4200 const wxPoint
& pos
, const wxSize
& size
,
4201 long style
, const wxString
& name
)
4203 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
4204 style
| wxWANTS_CHARS
, name
))
4207 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4208 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4211 SetInitialSize(size
);
4219 // Must do this or ~wxScrollHelper will pop the wrong event handler
4220 SetTargetWindow(this);
4222 wxSafeDecRef(m_defaultCellAttr
);
4224 #ifdef DEBUG_ATTR_CACHE
4225 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4226 wxPrintf(_T("wxGrid attribute cache statistics: "
4227 "total: %u, hits: %u (%u%%)\n"),
4228 total
, gs_nAttrCacheHits
,
4229 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4232 // if we own the table, just delete it, otherwise at least don't leave it
4233 // with dangling view pointer
4236 else if ( m_table
&& m_table
->GetView() == this )
4237 m_table
->SetView(NULL
);
4239 delete m_typeRegistry
;
4244 // ----- internal init and update functions
4247 // NOTE: If using the default visual attributes works everywhere then this can
4248 // be removed as well as the #else cases below.
4249 #define _USE_VISATTR 0
4251 void wxGrid::Create()
4253 // set to true by CreateGrid
4256 // create the type registry
4257 m_typeRegistry
= new wxGridTypeRegistry
;
4260 m_table
= (wxGridTableBase
*) NULL
;
4263 m_cellEditCtrlEnabled
= false;
4265 m_defaultCellAttr
= new wxGridCellAttr();
4267 // Set default cell attributes
4268 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4269 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4270 m_defaultCellAttr
->SetFont(GetFont());
4271 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4272 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4273 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4276 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4277 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4279 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4280 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4283 m_defaultCellAttr
->SetTextColour(
4284 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4285 m_defaultCellAttr
->SetBackgroundColour(
4286 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4291 m_currentCellCoords
= wxGridNoCellCoords
;
4293 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4294 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4296 // subwindow components that make up the wxGrid
4297 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4302 m_colLabelWin
= new wxGridColLabelWindow( this,
4307 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4312 m_gridWin
= new wxGridWindow( this,
4319 SetTargetWindow( m_gridWin
);
4322 wxColour gfg
= gva
.colFg
;
4323 wxColour gbg
= gva
.colBg
;
4324 wxColour lfg
= lva
.colFg
;
4325 wxColour lbg
= lva
.colBg
;
4327 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4328 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4329 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4330 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4333 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4334 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4335 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4336 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4337 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4338 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4340 m_gridWin
->SetOwnForegroundColour(gfg
);
4341 m_gridWin
->SetOwnBackgroundColour(gbg
);
4346 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4347 wxGrid::wxGridSelectionModes selmode
)
4349 wxCHECK_MSG( !m_created
,
4351 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4353 m_numRows
= numRows
;
4354 m_numCols
= numCols
;
4356 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4357 m_table
->SetView( this );
4359 m_selection
= new wxGridSelection( this, selmode
);
4368 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4370 wxCHECK_RET( m_created
,
4371 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4373 m_selection
->SetSelectionMode( selmode
);
4376 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4378 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4379 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4381 return m_selection
->GetSelectionMode();
4384 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4385 wxGrid::wxGridSelectionModes selmode
)
4387 bool checkSelection
= false;
4390 // stop all processing
4395 m_table
->SetView(0);
4407 checkSelection
= true;
4409 // kill row and column size arrays
4410 m_colWidths
.Empty();
4411 m_colRights
.Empty();
4412 m_rowHeights
.Empty();
4413 m_rowBottoms
.Empty();
4418 m_numRows
= table
->GetNumberRows();
4419 m_numCols
= table
->GetNumberCols();
4422 m_table
->SetView( this );
4423 m_ownTable
= takeOwnership
;
4424 m_selection
= new wxGridSelection( this, selmode
);
4427 // If the newly set table is smaller than the
4428 // original one current cell and selection regions
4429 // might be invalid,
4430 m_selectingKeyboard
= wxGridNoCellCoords
;
4431 m_currentCellCoords
=
4432 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
4433 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
4434 if (m_selectingTopLeft
.GetRow() >= m_numRows
||
4435 m_selectingTopLeft
.GetCol() >= m_numCols
)
4437 m_selectingTopLeft
= wxGridNoCellCoords
;
4438 m_selectingBottomRight
= wxGridNoCellCoords
;
4441 m_selectingBottomRight
=
4442 wxGridCellCoords(wxMin(m_numRows
,
4443 m_selectingBottomRight
.GetRow()),
4445 m_selectingBottomRight
.GetCol()));
4457 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4458 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4460 if ( m_rowLabelWin
)
4462 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4466 m_labelBackgroundColour
= *wxWHITE
;
4469 m_labelTextColour
= *wxBLACK
;
4472 m_attrCache
.row
= -1;
4473 m_attrCache
.col
= -1;
4474 m_attrCache
.attr
= NULL
;
4476 // TODO: something better than this ?
4478 m_labelFont
= this->GetFont();
4479 m_labelFont
.SetWeight( wxBOLD
);
4481 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4482 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4484 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4485 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4486 m_colLabelTextOrientation
= wxHORIZONTAL
;
4488 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4489 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4491 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4492 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4494 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4495 m_defaultRowHeight
+= 8;
4497 m_defaultRowHeight
+= 4;
4500 m_gridLineColour
= wxColour( 192,192,192 );
4501 m_gridLinesEnabled
= true;
4502 m_cellHighlightColour
= *wxBLACK
;
4503 m_cellHighlightPenWidth
= 2;
4504 m_cellHighlightROPenWidth
= 1;
4506 m_canDragColMove
= false;
4508 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4509 m_winCapture
= (wxWindow
*)NULL
;
4510 m_canDragRowSize
= true;
4511 m_canDragColSize
= true;
4512 m_canDragGridSize
= true;
4513 m_canDragCell
= false;
4515 m_dragRowOrCol
= -1;
4516 m_isDragging
= false;
4517 m_startDragPos
= wxDefaultPosition
;
4519 m_waitForSlowClick
= false;
4521 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4522 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4524 m_currentCellCoords
= wxGridNoCellCoords
;
4528 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4529 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4531 m_editable
= true; // default for whole grid
4533 m_inOnKeyDown
= false;
4539 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4540 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4543 // ----------------------------------------------------------------------------
4544 // the idea is to call these functions only when necessary because they create
4545 // quite big arrays which eat memory mostly unnecessary - in particular, if
4546 // default widths/heights are used for all rows/columns, we may not use these
4549 // with some extra code, it should be possible to only store the widths/heights
4550 // different from default ones (resulting in space savings for huge grids) but
4551 // this is not done currently
4552 // ----------------------------------------------------------------------------
4554 void wxGrid::InitRowHeights()
4556 m_rowHeights
.Empty();
4557 m_rowBottoms
.Empty();
4559 m_rowHeights
.Alloc( m_numRows
);
4560 m_rowBottoms
.Alloc( m_numRows
);
4562 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4565 for ( int i
= 0; i
< m_numRows
; i
++ )
4567 rowBottom
+= m_defaultRowHeight
;
4568 m_rowBottoms
.Add( rowBottom
);
4572 void wxGrid::InitColWidths()
4574 m_colWidths
.Empty();
4575 m_colRights
.Empty();
4577 m_colWidths
.Alloc( m_numCols
);
4578 m_colRights
.Alloc( m_numCols
);
4580 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4583 for ( int i
= 0; i
< m_numCols
; i
++ )
4585 colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
4586 m_colRights
.Add( colRight
);
4590 int wxGrid::GetColWidth(int col
) const
4592 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4595 int wxGrid::GetColLeft(int col
) const
4597 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
4598 : m_colRights
[col
] - m_colWidths
[col
];
4601 int wxGrid::GetColRight(int col
) const
4603 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
4607 int wxGrid::GetRowHeight(int row
) const
4609 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4612 int wxGrid::GetRowTop(int row
) const
4614 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4615 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4618 int wxGrid::GetRowBottom(int row
) const
4620 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4621 : m_rowBottoms
[row
];
4624 void wxGrid::CalcDimensions()
4626 // compute the size of the scrollable area
4627 int w
= m_numCols
> 0 ? GetColRight(GetColAt(m_numCols
- 1)) : 0;
4628 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
4633 // take into account editor if shown
4634 if ( IsCellEditControlShown() )
4637 int r
= m_currentCellCoords
.GetRow();
4638 int c
= m_currentCellCoords
.GetCol();
4639 int x
= GetColLeft(c
);
4640 int y
= GetRowTop(r
);
4642 // how big is the editor
4643 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4644 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4645 editor
->GetControl()->GetSize(&w2
, &h2
);
4656 // preserve (more or less) the previous position
4658 GetViewStart( &x
, &y
);
4660 // ensure the position is valid for the new scroll ranges
4662 x
= wxMax( w
- 1, 0 );
4664 y
= wxMax( h
- 1, 0 );
4666 // do set scrollbar parameters
4667 SetScrollbars( m_scrollLineX
, m_scrollLineY
,
4668 GetScrollX(w
), GetScrollY(h
),
4670 GetBatchCount() != 0);
4672 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4673 // still must reposition the children
4677 void wxGrid::CalcWindowSizes()
4679 // escape if the window is has not been fully created yet
4681 if ( m_cornerLabelWin
== NULL
)
4685 GetClientSize( &cw
, &ch
);
4687 // this block of code tries to work around the following problem: the grid
4688 // could have been just resized to have enough space to show the full grid
4689 // window contents without the scrollbars, but its client size could be
4690 // not big enough because the grid has the scrollbars right now and so the
4691 // scrollbars would remain even though we don't need them any more
4693 // to prevent this from happening, check if we have enough space for
4694 // everything without the scrollbars and explicitly disable them then
4695 wxSize size
= GetSize() - GetWindowBorderSize();
4696 if ( size
!= wxSize(cw
, ch
) )
4698 // check if we have enough space for grid window after accounting for
4699 // the fixed size elements
4700 size
.x
-= m_rowLabelWidth
;
4701 size
.y
-= m_colLabelHeight
;
4703 const wxSize vsize
= m_gridWin
->GetVirtualSize();
4705 if ( size
.x
>= vsize
.x
&& size
.y
>= vsize
.y
)
4707 // yes, we do, so remove the scrollbars and use the new client size
4708 // (which should be the same as full window size - borders now)
4709 SetScrollbars(0, 0, 0, 0);
4710 GetClientSize(&cw
, &ch
);
4714 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4715 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4717 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4718 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
- m_rowLabelWidth
, m_colLabelHeight
);
4720 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4721 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
- m_colLabelHeight
);
4723 if ( m_gridWin
&& m_gridWin
->IsShown() )
4724 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
- m_rowLabelWidth
, ch
- m_colLabelHeight
);
4727 // this is called when the grid table sends a message
4728 // to indicate that it has been redimensioned
4730 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4733 bool result
= false;
4735 // Clear the attribute cache as the attribute might refer to a different
4736 // cell than stored in the cache after adding/removing rows/columns.
4739 // By the same reasoning, the editor should be dismissed if columns are
4740 // added or removed. And for consistency, it should IMHO always be
4741 // removed, not only if the cell "underneath" it actually changes.
4742 // For now, I intentionally do not save the editor's content as the
4743 // cell it might want to save that stuff to might no longer exist.
4744 HideCellEditControl();
4747 // if we were using the default widths/heights so far, we must change them
4749 if ( m_colWidths
.IsEmpty() )
4754 if ( m_rowHeights
.IsEmpty() )
4760 switch ( msg
.GetId() )
4762 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4764 size_t pos
= msg
.GetCommandInt();
4765 int numRows
= msg
.GetCommandInt2();
4767 m_numRows
+= numRows
;
4769 if ( !m_rowHeights
.IsEmpty() )
4771 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4772 m_rowBottoms
.Insert( 0, pos
, numRows
);
4776 bottom
= m_rowBottoms
[pos
- 1];
4778 for ( i
= pos
; i
< m_numRows
; i
++ )
4780 bottom
+= m_rowHeights
[i
];
4781 m_rowBottoms
[i
] = bottom
;
4785 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4787 // if we have just inserted cols into an empty grid the current
4788 // cell will be undefined...
4790 SetCurrentCell( 0, 0 );
4794 m_selection
->UpdateRows( pos
, numRows
);
4795 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4797 attrProvider
->UpdateAttrRows( pos
, numRows
);
4799 if ( !GetBatchCount() )
4802 m_rowLabelWin
->Refresh();
4808 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4810 int numRows
= msg
.GetCommandInt();
4811 int oldNumRows
= m_numRows
;
4812 m_numRows
+= numRows
;
4814 if ( !m_rowHeights
.IsEmpty() )
4816 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4817 m_rowBottoms
.Add( 0, numRows
);
4820 if ( oldNumRows
> 0 )
4821 bottom
= m_rowBottoms
[oldNumRows
- 1];
4823 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4825 bottom
+= m_rowHeights
[i
];
4826 m_rowBottoms
[i
] = bottom
;
4830 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4832 // if we have just inserted cols into an empty grid the current
4833 // cell will be undefined...
4835 SetCurrentCell( 0, 0 );
4838 if ( !GetBatchCount() )
4841 m_rowLabelWin
->Refresh();
4847 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4849 size_t pos
= msg
.GetCommandInt();
4850 int numRows
= msg
.GetCommandInt2();
4851 m_numRows
-= numRows
;
4853 if ( !m_rowHeights
.IsEmpty() )
4855 m_rowHeights
.RemoveAt( pos
, numRows
);
4856 m_rowBottoms
.RemoveAt( pos
, numRows
);
4859 for ( i
= 0; i
< m_numRows
; i
++ )
4861 h
+= m_rowHeights
[i
];
4862 m_rowBottoms
[i
] = h
;
4868 m_currentCellCoords
= wxGridNoCellCoords
;
4872 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4873 m_currentCellCoords
.Set( 0, 0 );
4877 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4878 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4881 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4883 // ifdef'd out following patch from Paul Gammans
4885 // No need to touch column attributes, unless we
4886 // removed _all_ rows, in this case, we remove
4887 // all column attributes.
4888 // I hate to do this here, but the
4889 // needed data is not available inside UpdateAttrRows.
4890 if ( !GetNumberRows() )
4891 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4895 if ( !GetBatchCount() )
4898 m_rowLabelWin
->Refresh();
4904 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4906 size_t pos
= msg
.GetCommandInt();
4907 int numCols
= msg
.GetCommandInt2();
4908 m_numCols
+= numCols
;
4910 if ( !m_colAt
.IsEmpty() )
4912 //Shift the column IDs
4914 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
4916 if ( m_colAt
[i
] >= (int)pos
)
4917 m_colAt
[i
] += numCols
;
4920 m_colAt
.Insert( pos
, pos
, numCols
);
4922 //Set the new columns' positions
4923 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
4929 if ( !m_colWidths
.IsEmpty() )
4931 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4932 m_colRights
.Insert( 0, pos
, numCols
);
4936 right
= m_colRights
[GetColAt( pos
- 1 )];
4939 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
4941 i
= GetColAt( colPos
);
4943 right
+= m_colWidths
[i
];
4944 m_colRights
[i
] = right
;
4948 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4950 // if we have just inserted cols into an empty grid the current
4951 // cell will be undefined...
4953 SetCurrentCell( 0, 0 );
4957 m_selection
->UpdateCols( pos
, numCols
);
4958 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4960 attrProvider
->UpdateAttrCols( pos
, numCols
);
4961 if ( !GetBatchCount() )
4964 m_colLabelWin
->Refresh();
4970 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4972 int numCols
= msg
.GetCommandInt();
4973 int oldNumCols
= m_numCols
;
4974 m_numCols
+= numCols
;
4976 if ( !m_colAt
.IsEmpty() )
4978 m_colAt
.Add( 0, numCols
);
4980 //Set the new columns' positions
4982 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4988 if ( !m_colWidths
.IsEmpty() )
4990 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4991 m_colRights
.Add( 0, numCols
);
4994 if ( oldNumCols
> 0 )
4995 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
4998 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
5000 i
= GetColAt( colPos
);
5002 right
+= m_colWidths
[i
];
5003 m_colRights
[i
] = right
;
5007 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5009 // if we have just inserted cols into an empty grid the current
5010 // cell will be undefined...
5012 SetCurrentCell( 0, 0 );
5014 if ( !GetBatchCount() )
5017 m_colLabelWin
->Refresh();
5023 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5025 size_t pos
= msg
.GetCommandInt();
5026 int numCols
= msg
.GetCommandInt2();
5027 m_numCols
-= numCols
;
5029 if ( !m_colAt
.IsEmpty() )
5031 int colID
= GetColAt( pos
);
5033 m_colAt
.RemoveAt( pos
, numCols
);
5035 //Shift the column IDs
5037 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5039 if ( m_colAt
[colPos
] > colID
)
5040 m_colAt
[colPos
] -= numCols
;
5044 if ( !m_colWidths
.IsEmpty() )
5046 m_colWidths
.RemoveAt( pos
, numCols
);
5047 m_colRights
.RemoveAt( pos
, numCols
);
5051 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5053 i
= GetColAt( colPos
);
5055 w
+= m_colWidths
[i
];
5062 m_currentCellCoords
= wxGridNoCellCoords
;
5066 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
5067 m_currentCellCoords
.Set( 0, 0 );
5071 m_selection
->UpdateCols( pos
, -((int)numCols
) );
5072 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5075 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
5077 // ifdef'd out following patch from Paul Gammans
5079 // No need to touch row attributes, unless we
5080 // removed _all_ columns, in this case, we remove
5081 // all row attributes.
5082 // I hate to do this here, but the
5083 // needed data is not available inside UpdateAttrCols.
5084 if ( !GetNumberCols() )
5085 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
5089 if ( !GetBatchCount() )
5092 m_colLabelWin
->Refresh();
5099 if (result
&& !GetBatchCount() )
5100 m_gridWin
->Refresh();
5105 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
) const
5107 wxRegionIterator
iter( reg
);
5110 wxArrayInt rowlabels
;
5117 // TODO: remove this when we can...
5118 // There is a bug in wxMotif that gives garbage update
5119 // rectangles if you jump-scroll a long way by clicking the
5120 // scrollbar with middle button. This is a work-around
5122 #if defined(__WXMOTIF__)
5124 m_gridWin
->GetClientSize( &cw
, &ch
);
5125 if ( r
.GetTop() > ch
)
5127 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5130 // logical bounds of update region
5133 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
5134 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
5136 // find the row labels within these bounds
5139 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5141 if ( GetRowBottom(row
) < top
)
5144 if ( GetRowTop(row
) > bottom
)
5147 rowlabels
.Add( row
);
5156 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
) const
5158 wxRegionIterator
iter( reg
);
5161 wxArrayInt colLabels
;
5168 // TODO: remove this when we can...
5169 // There is a bug in wxMotif that gives garbage update
5170 // rectangles if you jump-scroll a long way by clicking the
5171 // scrollbar with middle button. This is a work-around
5173 #if defined(__WXMOTIF__)
5175 m_gridWin
->GetClientSize( &cw
, &ch
);
5176 if ( r
.GetLeft() > cw
)
5178 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5181 // logical bounds of update region
5184 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
5185 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
5187 // find the cells within these bounds
5191 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5193 col
= GetColAt( colPos
);
5195 if ( GetColRight(col
) < left
)
5198 if ( GetColLeft(col
) > right
)
5201 colLabels
.Add( col
);
5210 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
) const
5212 wxRegionIterator
iter( reg
);
5215 wxGridCellCoordsArray cellsExposed
;
5217 int left
, top
, right
, bottom
;
5222 // TODO: remove this when we can...
5223 // There is a bug in wxMotif that gives garbage update
5224 // rectangles if you jump-scroll a long way by clicking the
5225 // scrollbar with middle button. This is a work-around
5227 #if defined(__WXMOTIF__)
5229 m_gridWin
->GetClientSize( &cw
, &ch
);
5230 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5231 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5232 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5233 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5236 // logical bounds of update region
5238 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5239 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5241 // find the cells within these bounds
5244 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5246 if ( GetRowBottom(row
) <= top
)
5249 if ( GetRowTop(row
) > bottom
)
5253 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5255 col
= GetColAt( colPos
);
5257 if ( GetColRight(col
) <= left
)
5260 if ( GetColLeft(col
) > right
)
5263 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
5270 return cellsExposed
;
5274 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5277 wxPoint
pos( event
.GetPosition() );
5278 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5280 if ( event
.Dragging() )
5284 m_isDragging
= true;
5285 m_rowLabelWin
->CaptureMouse();
5288 if ( event
.LeftIsDown() )
5290 switch ( m_cursorMode
)
5292 case WXGRID_CURSOR_RESIZE_ROW
:
5294 int cw
, ch
, left
, dummy
;
5295 m_gridWin
->GetClientSize( &cw
, &ch
);
5296 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5298 wxClientDC
dc( m_gridWin
);
5301 GetRowTop(m_dragRowOrCol
) +
5302 GetRowMinimalHeight(m_dragRowOrCol
) );
5303 dc
.SetLogicalFunction(wxINVERT
);
5304 if ( m_dragLastPos
>= 0 )
5306 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5308 dc
.DrawLine( left
, y
, left
+cw
, y
);
5313 case WXGRID_CURSOR_SELECT_ROW
:
5315 if ( (row
= YToRow( y
)) >= 0 )
5319 m_selection
->SelectRow( row
,
5320 event
.ControlDown(),
5329 // default label to suppress warnings about "enumeration value
5330 // 'xxx' not handled in switch
5338 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5343 if (m_rowLabelWin
->HasCapture())
5344 m_rowLabelWin
->ReleaseMouse();
5345 m_isDragging
= false;
5348 // ------------ Entering or leaving the window
5350 if ( event
.Entering() || event
.Leaving() )
5352 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5355 // ------------ Left button pressed
5357 else if ( event
.LeftDown() )
5359 // don't send a label click event for a hit on the
5360 // edge of the row label - this is probably the user
5361 // wanting to resize the row
5363 if ( YToEdgeOfRow(y
) < 0 )
5367 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5369 if ( !event
.ShiftDown() && !event
.CmdDown() )
5373 if ( event
.ShiftDown() )
5375 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5378 GetNumberCols() - 1,
5379 event
.ControlDown(),
5386 m_selection
->SelectRow( row
,
5387 event
.ControlDown(),
5394 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5399 // starting to drag-resize a row
5400 if ( CanDragRowSize() )
5401 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5405 // ------------ Left double click
5407 else if (event
.LeftDClick() )
5409 row
= YToEdgeOfRow(y
);
5414 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5416 // no default action at the moment
5421 // adjust row height depending on label text
5422 AutoSizeRowLabelSize( row
);
5424 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5429 // ------------ Left button released
5431 else if ( event
.LeftUp() )
5433 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5435 DoEndDragResizeRow();
5437 // Note: we are ending the event *after* doing
5438 // default processing in this case
5440 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5443 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5447 // ------------ Right button down
5449 else if ( event
.RightDown() )
5453 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5455 // no default action at the moment
5459 // ------------ Right double click
5461 else if ( event
.RightDClick() )
5465 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5467 // no default action at the moment
5471 // ------------ No buttons down and mouse moving
5473 else if ( event
.Moving() )
5475 m_dragRowOrCol
= YToEdgeOfRow( y
);
5476 if ( m_dragRowOrCol
>= 0 )
5478 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5480 // don't capture the mouse yet
5481 if ( CanDragRowSize() )
5482 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5485 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5487 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5492 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5495 wxPoint
pos( event
.GetPosition() );
5496 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5498 if ( event
.Dragging() )
5502 m_isDragging
= true;
5503 m_colLabelWin
->CaptureMouse();
5505 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
)
5506 m_dragRowOrCol
= XToCol( x
);
5509 if ( event
.LeftIsDown() )
5511 switch ( m_cursorMode
)
5513 case WXGRID_CURSOR_RESIZE_COL
:
5515 int cw
, ch
, dummy
, top
;
5516 m_gridWin
->GetClientSize( &cw
, &ch
);
5517 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5519 wxClientDC
dc( m_gridWin
);
5522 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5523 GetColMinimalWidth(m_dragRowOrCol
));
5524 dc
.SetLogicalFunction(wxINVERT
);
5525 if ( m_dragLastPos
>= 0 )
5527 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5529 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5534 case WXGRID_CURSOR_SELECT_COL
:
5536 if ( (col
= XToCol( x
)) >= 0 )
5540 m_selection
->SelectCol( col
,
5541 event
.ControlDown(),
5550 case WXGRID_CURSOR_MOVE_COL
:
5553 m_moveToCol
= GetColAt( 0 );
5555 m_moveToCol
= XToCol( x
);
5559 if ( m_moveToCol
< 0 )
5560 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5562 markerX
= GetColLeft( m_moveToCol
);
5564 if ( markerX
!= m_dragLastPos
)
5566 wxClientDC
dc( m_colLabelWin
);
5569 m_colLabelWin
->GetClientSize( &cw
, &ch
);
5573 //Clean up the last indicator
5574 if ( m_dragLastPos
>= 0 )
5576 wxPen
pen( m_colLabelWin
->GetBackgroundColour(), 2 );
5578 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
5579 dc
.SetPen(wxNullPen
);
5581 if ( XToCol( m_dragLastPos
) != -1 )
5582 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
5585 //Moving to the same place? Don't draw a marker
5586 if ( (m_moveToCol
== m_dragRowOrCol
)
5587 || (GetColPos( m_moveToCol
) == GetColPos( m_dragRowOrCol
) + 1)
5588 || (m_moveToCol
< 0 && m_dragRowOrCol
== GetColAt( m_numCols
- 1 )))
5595 wxPen
pen( *wxBLUE
, 2 );
5598 dc
.DrawLine( markerX
, 0, markerX
, ch
);
5600 dc
.SetPen(wxNullPen
);
5602 m_dragLastPos
= markerX
- 1;
5607 // default label to suppress warnings about "enumeration value
5608 // 'xxx' not handled in switch
5616 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5621 if (m_colLabelWin
->HasCapture())
5622 m_colLabelWin
->ReleaseMouse();
5623 m_isDragging
= false;
5626 // ------------ Entering or leaving the window
5628 if ( event
.Entering() || event
.Leaving() )
5630 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5633 // ------------ Left button pressed
5635 else if ( event
.LeftDown() )
5637 // don't send a label click event for a hit on the
5638 // edge of the col label - this is probably the user
5639 // wanting to resize the col
5641 if ( XToEdgeOfCol(x
) < 0 )
5645 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5647 if ( m_canDragColMove
)
5649 //Show button as pressed
5650 wxClientDC
dc( m_colLabelWin
);
5651 int colLeft
= GetColLeft( col
);
5652 int colRight
= GetColRight( col
) - 1;
5653 dc
.SetPen( wxPen( m_colLabelWin
->GetBackgroundColour(), 1 ) );
5654 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
5655 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
5657 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, m_colLabelWin
);
5661 if ( !event
.ShiftDown() && !event
.CmdDown() )
5665 if ( event
.ShiftDown() )
5667 m_selection
->SelectBlock( 0,
5668 m_currentCellCoords
.GetCol(),
5669 GetNumberRows() - 1, col
,
5670 event
.ControlDown(),
5677 m_selection
->SelectCol( col
,
5678 event
.ControlDown(),
5685 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5691 // starting to drag-resize a col
5693 if ( CanDragColSize() )
5694 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5698 // ------------ Left double click
5700 if ( event
.LeftDClick() )
5702 col
= XToEdgeOfCol(x
);
5707 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5709 // no default action at the moment
5714 // adjust column width depending on label text
5715 AutoSizeColLabelSize( col
);
5717 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5722 // ------------ Left button released
5724 else if ( event
.LeftUp() )
5726 switch ( m_cursorMode
)
5728 case WXGRID_CURSOR_RESIZE_COL
:
5729 DoEndDragResizeCol();
5731 // Note: we are ending the event *after* doing
5732 // default processing in this case
5734 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5737 case WXGRID_CURSOR_MOVE_COL
:
5740 SendEvent( wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
, event
);
5743 case WXGRID_CURSOR_SELECT_COL
:
5744 case WXGRID_CURSOR_SELECT_CELL
:
5745 case WXGRID_CURSOR_RESIZE_ROW
:
5746 case WXGRID_CURSOR_SELECT_ROW
:
5747 // nothing to do (?)
5751 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5755 // ------------ Right button down
5757 else if ( event
.RightDown() )
5761 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5763 // no default action at the moment
5767 // ------------ Right double click
5769 else if ( event
.RightDClick() )
5773 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5775 // no default action at the moment
5779 // ------------ No buttons down and mouse moving
5781 else if ( event
.Moving() )
5783 m_dragRowOrCol
= XToEdgeOfCol( x
);
5784 if ( m_dragRowOrCol
>= 0 )
5786 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5788 // don't capture the cursor yet
5789 if ( CanDragColSize() )
5790 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5793 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5795 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5800 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5802 if ( event
.LeftDown() )
5804 // indicate corner label by having both row and
5807 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5812 else if ( event
.LeftDClick() )
5814 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5816 else if ( event
.RightDown() )
5818 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5820 // no default action at the moment
5823 else if ( event
.RightDClick() )
5825 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5827 // no default action at the moment
5832 void wxGrid::CancelMouseCapture()
5834 // cancel operation currently in progress, whatever it is
5837 m_isDragging
= false;
5838 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
5839 m_winCapture
->SetCursor( *wxSTANDARD_CURSOR
);
5840 m_winCapture
= NULL
;
5842 // remove traces of whatever we drew on screen
5847 void wxGrid::ChangeCursorMode(CursorMode mode
,
5852 static const wxChar
*cursorModes
[] =
5862 wxLogTrace(_T("grid"),
5863 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5864 win
== m_colLabelWin
? _T("colLabelWin")
5865 : win
? _T("rowLabelWin")
5867 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5870 if ( mode
== m_cursorMode
&&
5871 win
== m_winCapture
&&
5872 captureMouse
== (m_winCapture
!= NULL
))
5877 // by default use the grid itself
5883 if (m_winCapture
->HasCapture())
5884 m_winCapture
->ReleaseMouse();
5885 m_winCapture
= (wxWindow
*)NULL
;
5888 m_cursorMode
= mode
;
5890 switch ( m_cursorMode
)
5892 case WXGRID_CURSOR_RESIZE_ROW
:
5893 win
->SetCursor( m_rowResizeCursor
);
5896 case WXGRID_CURSOR_RESIZE_COL
:
5897 win
->SetCursor( m_colResizeCursor
);
5900 case WXGRID_CURSOR_MOVE_COL
:
5901 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
5905 win
->SetCursor( *wxSTANDARD_CURSOR
);
5909 // we need to capture mouse when resizing
5910 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5911 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5913 if ( captureMouse
&& resize
)
5915 win
->CaptureMouse();
5920 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5923 wxPoint
pos( event
.GetPosition() );
5924 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5926 wxGridCellCoords coords
;
5927 XYToCell( x
, y
, coords
);
5929 int cell_rows
, cell_cols
;
5930 bool isFirstDrag
= !m_isDragging
;
5931 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5932 if ((cell_rows
< 0) || (cell_cols
< 0))
5934 coords
.SetRow(coords
.GetRow() + cell_rows
);
5935 coords
.SetCol(coords
.GetCol() + cell_cols
);
5938 if ( event
.Dragging() )
5940 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5942 // Don't start doing anything until the mouse has been dragged at
5943 // least 3 pixels in any direction...
5946 if (m_startDragPos
== wxDefaultPosition
)
5948 m_startDragPos
= pos
;
5951 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5955 m_isDragging
= true;
5956 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5958 // Hide the edit control, so it
5959 // won't interfere with drag-shrinking.
5960 if ( IsCellEditControlShown() )
5962 HideCellEditControl();
5963 SaveEditControlValue();
5966 if ( coords
!= wxGridNoCellCoords
)
5968 if ( event
.CmdDown() )
5970 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5971 m_selectingKeyboard
= coords
;
5972 HighlightBlock( m_selectingKeyboard
, coords
);
5974 else if ( CanDragCell() )
5978 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5979 m_selectingKeyboard
= coords
;
5981 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5990 if ( !IsSelection() )
5992 HighlightBlock( coords
, coords
);
5996 HighlightBlock( m_currentCellCoords
, coords
);
6000 if (! IsVisible(coords
))
6002 MakeCellVisible(coords
);
6003 // TODO: need to introduce a delay or something here. The
6004 // scrolling is way to fast, at least on MSW - also on GTK.
6007 // Have we captured the mouse yet?
6010 m_winCapture
= m_gridWin
;
6011 m_winCapture
->CaptureMouse();
6016 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6018 int cw
, ch
, left
, dummy
;
6019 m_gridWin
->GetClientSize( &cw
, &ch
);
6020 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6022 wxClientDC
dc( m_gridWin
);
6024 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
6025 GetRowMinimalHeight(m_dragRowOrCol
) );
6026 dc
.SetLogicalFunction(wxINVERT
);
6027 if ( m_dragLastPos
>= 0 )
6029 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
6031 dc
.DrawLine( left
, y
, left
+cw
, y
);
6034 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6036 int cw
, ch
, dummy
, top
;
6037 m_gridWin
->GetClientSize( &cw
, &ch
);
6038 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6040 wxClientDC
dc( m_gridWin
);
6042 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
6043 GetColMinimalWidth(m_dragRowOrCol
) );
6044 dc
.SetLogicalFunction(wxINVERT
);
6045 if ( m_dragLastPos
>= 0 )
6047 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6049 dc
.DrawLine( x
, top
, x
, top
+ ch
);
6056 m_isDragging
= false;
6057 m_startDragPos
= wxDefaultPosition
;
6059 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
6060 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
6063 if ( event
.Entering() || event
.Leaving() )
6065 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6066 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
6071 // ------------ Left button pressed
6073 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
6075 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
6080 if ( !event
.CmdDown() )
6082 if ( event
.ShiftDown() )
6086 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
6087 m_currentCellCoords
.GetCol(),
6090 event
.ControlDown(),
6096 else if ( XToEdgeOfCol(x
) < 0 &&
6097 YToEdgeOfRow(y
) < 0 )
6099 DisableCellEditControl();
6100 MakeCellVisible( coords
);
6102 if ( event
.CmdDown() )
6106 m_selection
->ToggleCellSelection( coords
.GetRow(),
6108 event
.ControlDown(),
6113 m_selectingTopLeft
= wxGridNoCellCoords
;
6114 m_selectingBottomRight
= wxGridNoCellCoords
;
6115 m_selectingKeyboard
= coords
;
6119 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
6120 SetCurrentCell( coords
);
6123 if ( m_selection
->GetSelectionMode() !=
6124 wxGrid::wxGridSelectCells
)
6126 HighlightBlock( coords
, coords
);
6134 // ------------ Left double click
6136 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
6138 DisableCellEditControl();
6140 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
6142 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
6147 // we want double click to select a cell and start editing
6148 // (i.e. to behave in same way as sequence of two slow clicks):
6149 m_waitForSlowClick
= true;
6154 // ------------ Left button released
6156 else if ( event
.LeftUp() )
6158 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6162 if (m_winCapture
->HasCapture())
6163 m_winCapture
->ReleaseMouse();
6164 m_winCapture
= NULL
;
6167 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
6170 EnableCellEditControl();
6172 wxGridCellAttr
*attr
= GetCellAttr(coords
);
6173 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
6174 editor
->StartingClick();
6178 m_waitForSlowClick
= false;
6180 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6181 m_selectingBottomRight
!= wxGridNoCellCoords
)
6185 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6186 m_selectingTopLeft
.GetCol(),
6187 m_selectingBottomRight
.GetRow(),
6188 m_selectingBottomRight
.GetCol(),
6189 event
.ControlDown(),
6195 m_selectingTopLeft
= wxGridNoCellCoords
;
6196 m_selectingBottomRight
= wxGridNoCellCoords
;
6198 // Show the edit control, if it has been hidden for
6200 ShowCellEditControl();
6203 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6205 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6206 DoEndDragResizeRow();
6208 // Note: we are ending the event *after* doing
6209 // default processing in this case
6211 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
6213 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6215 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6216 DoEndDragResizeCol();
6218 // Note: we are ending the event *after* doing
6219 // default processing in this case
6221 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
6227 // ------------ Right button down
6229 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
6231 DisableCellEditControl();
6232 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
6237 // no default action at the moment
6241 // ------------ Right double click
6243 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
6245 DisableCellEditControl();
6246 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
6251 // no default action at the moment
6255 // ------------ Moving and no button action
6257 else if ( event
.Moving() && !event
.IsButton() )
6259 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
6261 // out of grid cell area
6262 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6266 int dragRow
= YToEdgeOfRow( y
);
6267 int dragCol
= XToEdgeOfCol( x
);
6269 // Dragging on the corner of a cell to resize in both
6270 // directions is not implemented yet...
6272 if ( dragRow
>= 0 && dragCol
>= 0 )
6274 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6280 m_dragRowOrCol
= dragRow
;
6282 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6284 if ( CanDragRowSize() && CanDragGridSize() )
6285 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false);
6288 else if ( dragCol
>= 0 )
6290 m_dragRowOrCol
= dragCol
;
6292 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6294 if ( CanDragColSize() && CanDragGridSize() )
6295 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false);
6298 else // Neither on a row or col edge
6300 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6302 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6308 void wxGrid::DoEndDragResizeRow()
6310 if ( m_dragLastPos
>= 0 )
6312 // erase the last line and resize the row
6314 int cw
, ch
, left
, dummy
;
6315 m_gridWin
->GetClientSize( &cw
, &ch
);
6316 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6318 wxClientDC
dc( m_gridWin
);
6320 dc
.SetLogicalFunction( wxINVERT
);
6321 dc
.DrawLine( left
, m_dragLastPos
, left
+ cw
, m_dragLastPos
);
6322 HideCellEditControl();
6323 SaveEditControlValue();
6325 int rowTop
= GetRowTop(m_dragRowOrCol
);
6326 SetRowSize( m_dragRowOrCol
,
6327 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
6329 if ( !GetBatchCount() )
6331 // Only needed to get the correct rect.y:
6332 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
6334 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6335 rect
.width
= m_rowLabelWidth
;
6336 rect
.height
= ch
- rect
.y
;
6337 m_rowLabelWin
->Refresh( true, &rect
);
6340 // if there is a multicell block, paint all of it
6343 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
6344 int leftCol
= XToCol(left
);
6345 int rightCol
= internalXToCol(left
+ cw
);
6348 for (i
=leftCol
; i
<rightCol
; i
++)
6350 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
6351 if (cell_rows
< subtract_rows
)
6352 subtract_rows
= cell_rows
;
6354 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
6355 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6356 rect
.height
= ch
- rect
.y
;
6359 m_gridWin
->Refresh( false, &rect
);
6362 ShowCellEditControl();
6367 void wxGrid::DoEndDragResizeCol()
6369 if ( m_dragLastPos
>= 0 )
6371 // erase the last line and resize the col
6373 int cw
, ch
, dummy
, top
;
6374 m_gridWin
->GetClientSize( &cw
, &ch
);
6375 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6377 wxClientDC
dc( m_gridWin
);
6379 dc
.SetLogicalFunction( wxINVERT
);
6380 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6381 HideCellEditControl();
6382 SaveEditControlValue();
6384 int colLeft
= GetColLeft(m_dragRowOrCol
);
6385 SetColSize( m_dragRowOrCol
,
6386 wxMax( m_dragLastPos
- colLeft
,
6387 GetColMinimalWidth(m_dragRowOrCol
) ) );
6389 if ( !GetBatchCount() )
6391 // Only needed to get the correct rect.x:
6392 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
6394 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6395 rect
.width
= cw
- rect
.x
;
6396 rect
.height
= m_colLabelHeight
;
6397 m_colLabelWin
->Refresh( true, &rect
);
6400 // if there is a multicell block, paint all of it
6403 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
6404 int topRow
= YToRow(top
);
6405 int bottomRow
= internalYToRow(top
+ cw
);
6408 for (i
=topRow
; i
<bottomRow
; i
++)
6410 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
6411 if (cell_cols
< subtract_cols
)
6412 subtract_cols
= cell_cols
;
6415 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
6416 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6417 rect
.width
= cw
- rect
.x
;
6421 m_gridWin
->Refresh( false, &rect
);
6424 ShowCellEditControl();
6428 void wxGrid::DoEndDragMoveCol()
6430 //The user clicked on the column but didn't actually drag
6431 if ( m_dragLastPos
< 0 )
6433 m_colLabelWin
->Refresh(); //Do this to "unpress" the column
6438 if ( m_moveToCol
== -1 )
6439 newPos
= m_numCols
- 1;
6442 newPos
= GetColPos( m_moveToCol
);
6443 if ( newPos
> GetColPos( m_dragRowOrCol
) )
6447 SetColPos( m_dragRowOrCol
, newPos
);
6450 void wxGrid::SetColPos( int colID
, int newPos
)
6452 if ( m_colAt
.IsEmpty() )
6454 m_colAt
.Alloc( m_numCols
);
6457 for ( i
= 0; i
< m_numCols
; i
++ )
6463 int oldPos
= GetColPos( colID
);
6465 //Reshuffle the m_colAt array
6466 if ( newPos
> oldPos
)
6469 for ( i
= oldPos
; i
< newPos
; i
++ )
6471 m_colAt
[i
] = m_colAt
[i
+1];
6477 for ( i
= oldPos
; i
> newPos
; i
-- )
6479 m_colAt
[i
] = m_colAt
[i
-1];
6483 m_colAt
[newPos
] = colID
;
6485 //Recalculate the column rights
6486 if ( !m_colWidths
.IsEmpty() )
6490 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6492 int colID
= GetColAt( colPos
);
6494 colRight
+= m_colWidths
[colID
];
6495 m_colRights
[colID
] = colRight
;
6499 m_colLabelWin
->Refresh();
6500 m_gridWin
->Refresh();
6505 void wxGrid::EnableDragColMove( bool enable
)
6507 if ( m_canDragColMove
== enable
)
6510 m_canDragColMove
= enable
;
6512 if ( !m_canDragColMove
)
6516 //Recalculate the column rights
6517 if ( !m_colWidths
.IsEmpty() )
6521 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6523 colRight
+= m_colWidths
[colPos
];
6524 m_colRights
[colPos
] = colRight
;
6528 m_colLabelWin
->Refresh();
6529 m_gridWin
->Refresh();
6535 // ------ interaction with data model
6537 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6539 switch ( msg
.GetId() )
6541 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6542 return GetModelValues();
6544 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6545 return SetModelValues();
6547 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6548 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6549 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6550 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6551 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6552 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6553 return Redimension( msg
);
6560 // The behaviour of this function depends on the grid table class
6561 // Clear() function. For the default wxGridStringTable class the
6562 // behavious is to replace all cell contents with wxEmptyString but
6563 // not to change the number of rows or cols.
6565 void wxGrid::ClearGrid()
6569 if (IsCellEditControlEnabled())
6570 DisableCellEditControl();
6573 if (!GetBatchCount())
6574 m_gridWin
->Refresh();
6578 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6580 // TODO: something with updateLabels flag
6584 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6590 if (IsCellEditControlEnabled())
6591 DisableCellEditControl();
6593 bool done
= m_table
->InsertRows( pos
, numRows
);
6596 // the table will have sent the results of the insert row
6597 // operation to this view object as a grid table message
6603 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6605 // TODO: something with updateLabels flag
6609 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6615 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6618 // the table will have sent the results of the append row
6619 // operation to this view object as a grid table message
6625 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6627 // TODO: something with updateLabels flag
6631 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6637 if (IsCellEditControlEnabled())
6638 DisableCellEditControl();
6640 bool done
= m_table
->DeleteRows( pos
, numRows
);
6642 // the table will have sent the results of the delete row
6643 // operation to this view object as a grid table message
6649 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6651 // TODO: something with updateLabels flag
6655 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6661 if (IsCellEditControlEnabled())
6662 DisableCellEditControl();
6664 bool done
= m_table
->InsertCols( pos
, numCols
);
6666 // the table will have sent the results of the insert col
6667 // operation to this view object as a grid table message
6673 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6675 // TODO: something with updateLabels flag
6679 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6685 bool done
= m_table
->AppendCols( numCols
);
6687 // the table will have sent the results of the append col
6688 // operation to this view object as a grid table message
6694 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6696 // TODO: something with updateLabels flag
6700 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6706 if (IsCellEditControlEnabled())
6707 DisableCellEditControl();
6709 bool done
= m_table
->DeleteCols( pos
, numCols
);
6711 // the table will have sent the results of the delete col
6712 // operation to this view object as a grid table message
6719 // ----- event handlers
6722 // Generate a grid event based on a mouse event and
6723 // return the result of ProcessEvent()
6725 int wxGrid::SendEvent( const wxEventType type
,
6727 wxMouseEvent
& mouseEv
)
6729 bool claimed
, vetoed
;
6731 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6733 int rowOrCol
= (row
== -1 ? col
: row
);
6735 wxGridSizeEvent
gridEvt( GetId(),
6739 mouseEv
.GetX() + GetRowLabelSize(),
6740 mouseEv
.GetY() + GetColLabelSize(),
6741 mouseEv
.ControlDown(),
6742 mouseEv
.ShiftDown(),
6744 mouseEv
.MetaDown() );
6746 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6747 vetoed
= !gridEvt
.IsAllowed();
6749 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6751 // Right now, it should _never_ end up here!
6752 wxGridRangeSelectEvent
gridEvt( GetId(),
6756 m_selectingBottomRight
,
6758 mouseEv
.ControlDown(),
6759 mouseEv
.ShiftDown(),
6761 mouseEv
.MetaDown() );
6763 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6764 vetoed
= !gridEvt
.IsAllowed();
6766 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
6767 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
6768 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
6769 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
6771 wxPoint pos
= mouseEv
.GetPosition();
6773 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
6774 pos
.y
+= GetColLabelSize();
6775 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
6776 pos
.x
+= GetRowLabelSize();
6778 wxGridEvent
gridEvt( GetId(),
6785 mouseEv
.ControlDown(),
6786 mouseEv
.ShiftDown(),
6788 mouseEv
.MetaDown() );
6789 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6790 vetoed
= !gridEvt
.IsAllowed();
6794 wxGridEvent
gridEvt( GetId(),
6798 mouseEv
.GetX() + GetRowLabelSize(),
6799 mouseEv
.GetY() + GetColLabelSize(),
6801 mouseEv
.ControlDown(),
6802 mouseEv
.ShiftDown(),
6804 mouseEv
.MetaDown() );
6805 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6806 vetoed
= !gridEvt
.IsAllowed();
6809 // A Veto'd event may not be `claimed' so test this first
6813 return claimed
? 1 : 0;
6816 // Generate a grid event of specified type and return the result
6817 // of ProcessEvent().
6819 int wxGrid::SendEvent( const wxEventType type
,
6822 bool claimed
, vetoed
;
6824 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6826 int rowOrCol
= (row
== -1 ? col
: row
);
6828 wxGridSizeEvent
gridEvt( GetId(), type
, this, rowOrCol
);
6830 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6831 vetoed
= !gridEvt
.IsAllowed();
6835 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
6837 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6838 vetoed
= !gridEvt
.IsAllowed();
6841 // A Veto'd event may not be `claimed' so test this first
6845 return claimed
? 1 : 0;
6848 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6850 // needed to prevent zillions of paint events on MSW
6854 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6856 // Don't do anything if between Begin/EndBatch...
6857 // EndBatch() will do all this on the last nested one anyway.
6858 if (! GetBatchCount())
6860 // Refresh to get correct scrolled position:
6861 wxScrolledWindow::Refresh(eraseb
, rect
);
6865 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6866 int width_label
, width_cell
, height_label
, height_cell
;
6869 // Copy rectangle can get scroll offsets..
6870 rect_x
= rect
->GetX();
6871 rect_y
= rect
->GetY();
6872 rectWidth
= rect
->GetWidth();
6873 rectHeight
= rect
->GetHeight();
6875 width_label
= m_rowLabelWidth
- rect_x
;
6876 if (width_label
> rectWidth
)
6877 width_label
= rectWidth
;
6879 height_label
= m_colLabelHeight
- rect_y
;
6880 if (height_label
> rectHeight
)
6881 height_label
= rectHeight
;
6883 if (rect_x
> m_rowLabelWidth
)
6885 x
= rect_x
- m_rowLabelWidth
;
6886 width_cell
= rectWidth
;
6891 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6894 if (rect_y
> m_colLabelHeight
)
6896 y
= rect_y
- m_colLabelHeight
;
6897 height_cell
= rectHeight
;
6902 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6905 // Paint corner label part intersecting rect.
6906 if ( width_label
> 0 && height_label
> 0 )
6908 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6909 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6912 // Paint col labels part intersecting rect.
6913 if ( width_cell
> 0 && height_label
> 0 )
6915 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6916 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6919 // Paint row labels part intersecting rect.
6920 if ( width_label
> 0 && height_cell
> 0 )
6922 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6923 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6926 // Paint cell area part intersecting rect.
6927 if ( width_cell
> 0 && height_cell
> 0 )
6929 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6930 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6935 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6936 m_colLabelWin
->Refresh(eraseb
, NULL
);
6937 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6938 m_gridWin
->Refresh(eraseb
, NULL
);
6943 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
))
6945 if (m_targetWindow
!= this) // check whether initialisation has been done
6947 // update our children window positions and scrollbars
6952 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6954 if ( m_inOnKeyDown
)
6956 // shouldn't be here - we are going round in circles...
6958 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6961 m_inOnKeyDown
= true;
6963 // propagate the event up and see if it gets processed
6964 wxWindow
*parent
= GetParent();
6965 wxKeyEvent
keyEvt( event
);
6966 keyEvt
.SetEventObject( parent
);
6968 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6970 if (GetLayoutDirection() == wxLayout_RightToLeft
)
6972 if (event
.GetKeyCode() == WXK_RIGHT
)
6973 event
.m_keyCode
= WXK_LEFT
;
6974 else if (event
.GetKeyCode() == WXK_LEFT
)
6975 event
.m_keyCode
= WXK_RIGHT
;
6978 // try local handlers
6979 switch ( event
.GetKeyCode() )
6982 if ( event
.ControlDown() )
6983 MoveCursorUpBlock( event
.ShiftDown() );
6985 MoveCursorUp( event
.ShiftDown() );
6989 if ( event
.ControlDown() )
6990 MoveCursorDownBlock( event
.ShiftDown() );
6992 MoveCursorDown( event
.ShiftDown() );
6996 if ( event
.ControlDown() )
6997 MoveCursorLeftBlock( event
.ShiftDown() );
6999 MoveCursorLeft( event
.ShiftDown() );
7003 if ( event
.ControlDown() )
7004 MoveCursorRightBlock( event
.ShiftDown() );
7006 MoveCursorRight( event
.ShiftDown() );
7010 case WXK_NUMPAD_ENTER
:
7011 if ( event
.ControlDown() )
7013 event
.Skip(); // to let the edit control have the return
7017 if ( GetGridCursorRow() < GetNumberRows()-1 )
7019 MoveCursorDown( event
.ShiftDown() );
7023 // at the bottom of a column
7024 DisableCellEditControl();
7034 if (event
.ShiftDown())
7036 if ( GetGridCursorCol() > 0 )
7038 MoveCursorLeft( false );
7043 DisableCellEditControl();
7048 if ( GetGridCursorCol() < GetNumberCols() - 1 )
7050 MoveCursorRight( false );
7055 DisableCellEditControl();
7061 if ( event
.ControlDown() )
7063 MakeCellVisible( 0, 0 );
7064 SetCurrentCell( 0, 0 );
7073 if ( event
.ControlDown() )
7075 MakeCellVisible( m_numRows
- 1, m_numCols
- 1 );
7076 SetCurrentCell( m_numRows
- 1, m_numCols
- 1 );
7093 if ( event
.ControlDown() )
7097 m_selection
->ToggleCellSelection(
7098 m_currentCellCoords
.GetRow(),
7099 m_currentCellCoords
.GetCol(),
7100 event
.ControlDown(),
7108 if ( !IsEditable() )
7109 MoveCursorRight( false );
7120 m_inOnKeyDown
= false;
7123 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
7125 // try local handlers
7127 if ( event
.GetKeyCode() == WXK_SHIFT
)
7129 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
7130 m_selectingBottomRight
!= wxGridNoCellCoords
)
7134 m_selection
->SelectBlock(
7135 m_selectingTopLeft
.GetRow(),
7136 m_selectingTopLeft
.GetCol(),
7137 m_selectingBottomRight
.GetRow(),
7138 m_selectingBottomRight
.GetCol(),
7139 event
.ControlDown(),
7146 m_selectingTopLeft
= wxGridNoCellCoords
;
7147 m_selectingBottomRight
= wxGridNoCellCoords
;
7148 m_selectingKeyboard
= wxGridNoCellCoords
;
7152 void wxGrid::OnChar( wxKeyEvent
& event
)
7154 // is it possible to edit the current cell at all?
7155 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7157 // yes, now check whether the cells editor accepts the key
7158 int row
= m_currentCellCoords
.GetRow();
7159 int col
= m_currentCellCoords
.GetCol();
7160 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7161 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7163 // <F2> is special and will always start editing, for
7164 // other keys - ask the editor itself
7165 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
7166 || editor
->IsAcceptedKey(event
) )
7168 // ensure cell is visble
7169 MakeCellVisible(row
, col
);
7170 EnableCellEditControl();
7172 // a problem can arise if the cell is not completely
7173 // visible (even after calling MakeCellVisible the
7174 // control is not created and calling StartingKey will
7176 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
7177 editor
->StartingKey(event
);
7193 void wxGrid::OnEraseBackground(wxEraseEvent
&)
7197 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
7199 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
7201 // the event has been intercepted - do nothing
7205 #if !defined(__WXMAC__)
7206 wxClientDC
dc( m_gridWin
);
7210 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
7212 DisableCellEditControl();
7214 if ( IsVisible( m_currentCellCoords
, false ) )
7217 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
7218 if ( !m_gridLinesEnabled
)
7226 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
7228 // Otherwise refresh redraws the highlight!
7229 m_currentCellCoords
= coords
;
7231 #if defined(__WXMAC__)
7232 m_gridWin
->Refresh(true /*, & r */);
7234 DrawGridCellArea( dc
, cells
);
7235 DrawAllGridLines( dc
, r
);
7240 m_currentCellCoords
= coords
;
7242 wxGridCellAttr
*attr
= GetCellAttr( coords
);
7243 #if !defined(__WXMAC__)
7244 DrawCellHighlight( dc
, attr
);
7249 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7252 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7256 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
7259 rightCol
= GetNumberCols() - 1;
7261 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
7264 bottomRow
= GetNumberRows() - 1;
7268 if ( topRow
> bottomRow
)
7275 if ( leftCol
> rightCol
)
7282 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7283 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7285 // First the case that we selected a completely new area
7286 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
7287 m_selectingBottomRight
== wxGridNoCellCoords
)
7290 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
7291 wxGridCellCoords ( bottomRow
, rightCol
) );
7292 m_gridWin
->Refresh( false, &rect
);
7295 // Now handle changing an existing selection area.
7296 else if ( m_selectingTopLeft
!= updateTopLeft
||
7297 m_selectingBottomRight
!= updateBottomRight
)
7299 // Compute two optimal update rectangles:
7300 // Either one rectangle is a real subset of the
7301 // other, or they are (almost) disjoint!
7303 bool need_refresh
[4];
7307 need_refresh
[3] = false;
7310 // Store intermediate values
7311 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
7312 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
7313 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
7314 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
7316 // Determine the outer/inner coordinates.
7317 if (oldLeft
> leftCol
)
7323 if (oldTop
> topRow
)
7329 if (oldRight
< rightCol
)
7332 oldRight
= rightCol
;
7335 if (oldBottom
< bottomRow
)
7338 oldBottom
= bottomRow
;
7342 // Now, either the stuff marked old is the outer
7343 // rectangle or we don't have a situation where one
7344 // is contained in the other.
7346 if ( oldLeft
< leftCol
)
7348 // Refresh the newly selected or deselected
7349 // area to the left of the old or new selection.
7350 need_refresh
[0] = true;
7351 rect
[0] = BlockToDeviceRect(
7352 wxGridCellCoords( oldTop
, oldLeft
),
7353 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
7356 if ( oldTop
< topRow
)
7358 // Refresh the newly selected or deselected
7359 // area above the old or new selection.
7360 need_refresh
[1] = true;
7361 rect
[1] = BlockToDeviceRect(
7362 wxGridCellCoords( oldTop
, leftCol
),
7363 wxGridCellCoords( topRow
- 1, rightCol
) );
7366 if ( oldRight
> rightCol
)
7368 // Refresh the newly selected or deselected
7369 // area to the right of the old or new selection.
7370 need_refresh
[2] = true;
7371 rect
[2] = BlockToDeviceRect(
7372 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
7373 wxGridCellCoords( oldBottom
, oldRight
) );
7376 if ( oldBottom
> bottomRow
)
7378 // Refresh the newly selected or deselected
7379 // area below the old or new selection.
7380 need_refresh
[3] = true;
7381 rect
[3] = BlockToDeviceRect(
7382 wxGridCellCoords( bottomRow
+ 1, leftCol
),
7383 wxGridCellCoords( oldBottom
, rightCol
) );
7386 // various Refresh() calls
7387 for (i
= 0; i
< 4; i
++ )
7388 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7389 m_gridWin
->Refresh( false, &(rect
[i
]) );
7393 m_selectingTopLeft
= updateTopLeft
;
7394 m_selectingBottomRight
= updateBottomRight
;
7398 // ------ functions to get/send data (see also public functions)
7401 bool wxGrid::GetModelValues()
7403 // Hide the editor, so it won't hide a changed value.
7404 HideCellEditControl();
7408 // all we need to do is repaint the grid
7410 m_gridWin
->Refresh();
7417 bool wxGrid::SetModelValues()
7421 // Disable the editor, so it won't hide a changed value.
7422 // Do we also want to save the current value of the editor first?
7424 DisableCellEditControl();
7428 for ( row
= 0; row
< m_numRows
; row
++ )
7430 for ( col
= 0; col
< m_numCols
; col
++ )
7432 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
7442 // Note - this function only draws cells that are in the list of
7443 // exposed cells (usually set from the update region by
7444 // CalcExposedCells)
7446 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7448 if ( !m_numRows
|| !m_numCols
)
7451 int i
, numCells
= cells
.GetCount();
7452 int row
, col
, cell_rows
, cell_cols
;
7453 wxGridCellCoordsArray redrawCells
;
7455 for ( i
= numCells
- 1; i
>= 0; i
-- )
7457 row
= cells
[i
].GetRow();
7458 col
= cells
[i
].GetCol();
7459 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7461 // If this cell is part of a multicell block, find owner for repaint
7462 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7464 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
7465 bool marked
= false;
7466 for ( int j
= 0; j
< numCells
; j
++ )
7468 if ( cell
== cells
[j
] )
7477 int count
= redrawCells
.GetCount();
7478 for (int j
= 0; j
< count
; j
++)
7480 if ( cell
== redrawCells
[j
] )
7488 redrawCells
.Add( cell
);
7491 // don't bother drawing this cell
7495 // If this cell is empty, find cell to left that might want to overflow
7496 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7498 for ( int l
= 0; l
< cell_rows
; l
++ )
7500 // find a cell in this row to leave already marked for repaint
7502 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7503 if ((redrawCells
[k
].GetCol() < left
) &&
7504 (redrawCells
[k
].GetRow() == row
))
7506 left
= redrawCells
[k
].GetCol();
7510 left
= 0; // oh well
7512 for (int j
= col
- 1; j
>= left
; j
--)
7514 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7516 if (GetCellOverflow(row
+ l
, j
))
7518 wxGridCellCoords
cell(row
+ l
, j
);
7519 bool marked
= false;
7521 for (int k
= 0; k
< numCells
; k
++)
7523 if ( cell
== cells
[k
] )
7532 int count
= redrawCells
.GetCount();
7533 for (int k
= 0; k
< count
; k
++)
7535 if ( cell
== redrawCells
[k
] )
7542 redrawCells
.Add( cell
);
7551 DrawCell( dc
, cells
[i
] );
7554 numCells
= redrawCells
.GetCount();
7556 for ( i
= numCells
- 1; i
>= 0; i
-- )
7558 DrawCell( dc
, redrawCells
[i
] );
7562 void wxGrid::DrawGridSpace( wxDC
& dc
)
7565 m_gridWin
->GetClientSize( &cw
, &ch
);
7568 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7570 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
7571 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7573 if ( right
> rightCol
|| bottom
> bottomRow
)
7576 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7578 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
7579 dc
.SetPen( *wxTRANSPARENT_PEN
);
7581 if ( right
> rightCol
)
7583 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7586 if ( bottom
> bottomRow
)
7588 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7593 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7595 int row
= coords
.GetRow();
7596 int col
= coords
.GetCol();
7598 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7601 // we draw the cell border ourselves
7602 #if !WXGRID_DRAW_LINES
7603 if ( m_gridLinesEnabled
)
7604 DrawCellBorder( dc
, coords
);
7607 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7609 bool isCurrent
= coords
== m_currentCellCoords
;
7611 wxRect rect
= CellToRect( row
, col
);
7613 // if the editor is shown, we should use it and not the renderer
7614 // Note: However, only if it is really _shown_, i.e. not hidden!
7615 if ( isCurrent
&& IsCellEditControlShown() )
7617 // NB: this "#if..." is temporary and fixes a problem where the
7618 // edit control is erased by this code after being rendered.
7619 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7620 // implicitly, causing this out-of order render.
7621 #if !defined(__WXMAC__)
7622 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7623 editor
->PaintBackground(rect
, attr
);
7629 // but all the rest is drawn by the cell renderer and hence may be customized
7630 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7631 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7638 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7640 int row
= m_currentCellCoords
.GetRow();
7641 int col
= m_currentCellCoords
.GetCol();
7643 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7646 wxRect rect
= CellToRect(row
, col
);
7648 // hmmm... what could we do here to show that the cell is disabled?
7649 // for now, I just draw a thinner border than for the other ones, but
7650 // it doesn't look really good
7652 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7656 // The center of the drawn line is where the position/width/height of
7657 // the rectangle is actually at (on wxMSW at least), so the
7658 // size of the rectangle is reduced to compensate for the thickness of
7659 // the line. If this is too strange on non-wxMSW platforms then
7660 // please #ifdef this appropriately.
7661 rect
.x
+= penWidth
/ 2;
7662 rect
.y
+= penWidth
/ 2;
7663 rect
.width
-= penWidth
- 1;
7664 rect
.height
-= penWidth
- 1;
7666 // Now draw the rectangle
7667 // use the cellHighlightColour if the cell is inside a selection, this
7668 // will ensure the cell is always visible.
7669 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
: m_cellHighlightColour
, penWidth
, wxSOLID
));
7670 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7671 dc
.DrawRectangle(rect
);
7675 // VZ: my experiments with 3D borders...
7677 // how to properly set colours for arbitrary bg?
7678 wxCoord x1
= rect
.x
,
7680 x2
= rect
.x
+ rect
.width
- 1,
7681 y2
= rect
.y
+ rect
.height
- 1;
7683 dc
.SetPen(*wxWHITE_PEN
);
7684 dc
.DrawLine(x1
, y1
, x2
, y1
);
7685 dc
.DrawLine(x1
, y1
, x1
, y2
);
7687 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7688 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7690 dc
.SetPen(*wxBLACK_PEN
);
7691 dc
.DrawLine(x1
, y2
, x2
, y2
);
7692 dc
.DrawLine(x2
, y1
, x2
, y2
+ 1);
7696 wxPen
wxGrid::GetDefaultGridLinePen()
7698 return wxPen(GetGridLineColour(), 1, wxSOLID
);
7701 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
7703 return GetDefaultGridLinePen();
7706 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
7708 return GetDefaultGridLinePen();
7711 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7713 int row
= coords
.GetRow();
7714 int col
= coords
.GetCol();
7715 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7719 wxRect rect
= CellToRect( row
, col
);
7721 // right hand border
7722 dc
.SetPen( GetColGridLinePen(col
) );
7723 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7724 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7727 dc
.SetPen( GetRowGridLinePen(row
) );
7728 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7729 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7732 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7734 // This if block was previously in wxGrid::OnPaint but that doesn't
7735 // seem to get called under wxGTK - MB
7737 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7738 m_numRows
&& m_numCols
)
7740 m_currentCellCoords
.Set(0, 0);
7743 if ( IsCellEditControlShown() )
7745 // don't show highlight when the edit control is shown
7749 // if the active cell was repainted, repaint its highlight too because it
7750 // might have been damaged by the grid lines
7751 size_t count
= cells
.GetCount();
7752 for ( size_t n
= 0; n
< count
; n
++ )
7754 if ( cells
[n
] == m_currentCellCoords
)
7756 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7757 DrawCellHighlight(dc
, attr
);
7765 // TODO: remove this ???
7766 // This is used to redraw all grid lines e.g. when the grid line colour
7769 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7771 #if !WXGRID_DRAW_LINES
7775 if ( !m_gridLinesEnabled
|| !m_numRows
|| !m_numCols
)
7778 int top
, bottom
, left
, right
;
7780 #if 0 //#ifndef __WXGTK__
7784 m_gridWin
->GetClientSize(&cw
, &ch
);
7786 // virtual coords of visible area
7788 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7789 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7794 reg
.GetBox(x
, y
, w
, h
);
7795 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7796 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7800 m_gridWin
->GetClientSize(&cw
, &ch
);
7801 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7802 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7805 // avoid drawing grid lines past the last row and col
7807 right
= wxMin( right
, GetColRight(GetColAt( m_numCols
- 1 )) );
7808 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7810 // no gridlines inside multicells, clip them out
7811 int leftCol
= GetColPos( internalXToCol(left
) );
7812 int topRow
= internalYToRow(top
);
7813 int rightCol
= GetColPos( internalXToCol(right
) );
7814 int bottomRow
= internalYToRow(bottom
);
7816 wxRegion
clippedcells(0, 0, cw
, ch
);
7818 int i
, j
, cell_rows
, cell_cols
;
7821 for (j
=topRow
; j
<=bottomRow
; j
++)
7824 for (colPos
=leftCol
; colPos
<=rightCol
; colPos
++)
7826 i
= GetColAt( colPos
);
7828 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7829 if ((cell_rows
> 1) || (cell_cols
> 1))
7831 rect
= CellToRect(j
,i
);
7832 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7833 clippedcells
.Subtract(rect
);
7835 else if ((cell_rows
< 0) || (cell_cols
< 0))
7837 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7838 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7839 clippedcells
.Subtract(rect
);
7844 dc
.SetClippingRegion( clippedcells
);
7847 // horizontal grid lines
7849 // already declared above - int i;
7850 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7852 int bot
= GetRowBottom(i
) - 1;
7861 dc
.SetPen( GetRowGridLinePen(i
) );
7862 dc
.DrawLine( left
, bot
, right
, bot
);
7866 // vertical grid lines
7869 for ( colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
7871 i
= GetColAt( colPos
);
7873 int colRight
= GetColRight(i
);
7875 if (GetLayoutDirection() != wxLayout_RightToLeft
)
7879 if ( colRight
> right
)
7884 if ( colRight
>= left
)
7886 dc
.SetPen( GetColGridLinePen(i
) );
7887 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7891 dc
.DestroyClippingRegion();
7894 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
7900 size_t numLabels
= rows
.GetCount();
7902 for ( i
= 0; i
< numLabels
; i
++ )
7904 DrawRowLabel( dc
, rows
[i
] );
7908 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7910 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7918 rect
.SetY( GetRowTop(row
) + 1 );
7919 rect
.SetWidth( m_rowLabelWidth
- 2 );
7920 rect
.SetHeight( GetRowHeight(row
) - 2 );
7922 CalcScrolledPosition( 0, rect
.y
, NULL
, &rect
.y
);
7924 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7926 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7928 int rowTop
= GetRowTop(row
),
7929 rowBottom
= GetRowBottom(row
) - 1;
7931 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
7932 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
7933 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7934 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7936 dc
.SetPen( *wxWHITE_PEN
);
7937 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7938 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
7941 dc
.SetBackgroundMode( wxTRANSPARENT
);
7942 dc
.SetTextForeground( GetLabelTextColour() );
7943 dc
.SetFont( GetLabelFont() );
7946 GetRowLabelAlignment( &hAlign
, &vAlign
);
7949 rect
.SetY( GetRowTop(row
) + 2 );
7950 rect
.SetWidth( m_rowLabelWidth
- 4 );
7951 rect
.SetHeight( GetRowHeight(row
) - 4 );
7952 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7955 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7961 size_t numLabels
= cols
.GetCount();
7963 for ( i
= 0; i
< numLabels
; i
++ )
7965 DrawColLabel( dc
, cols
[i
] );
7969 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7971 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7974 int colLeft
= GetColLeft(col
);
7980 rect
.SetX( colLeft
+ 1 );
7982 rect
.SetWidth( GetColWidth(col
) - 2 );
7983 rect
.SetHeight( m_colLabelHeight
- 2 );
7985 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7987 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7989 int colRight
= GetColRight(col
) - 1;
7991 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
7992 dc
.DrawLine( colRight
, 0, colRight
, m_colLabelHeight
- 1 );
7993 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7994 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
7995 colRight
+ 1, m_colLabelHeight
- 1 );
7997 dc
.SetPen( *wxWHITE_PEN
);
7998 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
7999 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
8002 dc
.SetBackgroundMode( wxTRANSPARENT
);
8003 dc
.SetTextForeground( GetLabelTextColour() );
8004 dc
.SetFont( GetLabelFont() );
8006 int hAlign
, vAlign
, orient
;
8007 GetColLabelAlignment( &hAlign
, &vAlign
);
8008 orient
= GetColLabelTextOrientation();
8010 rect
.SetX( colLeft
+ 2 );
8012 rect
.SetWidth( GetColWidth(col
) - 4 );
8013 rect
.SetHeight( m_colLabelHeight
- 4 );
8014 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
8017 void wxGrid::DrawTextRectangle( wxDC
& dc
,
8018 const wxString
& value
,
8022 int textOrientation
)
8024 wxArrayString lines
;
8026 StringToLines( value
, lines
);
8028 // Forward to new API.
8029 DrawTextRectangle( dc
,
8037 // VZ: this should be replaced with wxDC::DrawLabel() to which we just have to
8038 // add textOrientation support
8039 void wxGrid::DrawTextRectangle(wxDC
& dc
,
8040 const wxArrayString
& lines
,
8044 int textOrientation
)
8046 if ( lines
.empty() )
8049 wxDCClipper
clip(dc
, rect
);
8054 if ( textOrientation
== wxHORIZONTAL
)
8055 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
8057 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
8061 switch ( vertAlign
)
8063 case wxALIGN_BOTTOM
:
8064 if ( textOrientation
== wxHORIZONTAL
)
8065 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
8067 x
= rect
.x
+ rect
.width
- textWidth
;
8070 case wxALIGN_CENTRE
:
8071 if ( textOrientation
== wxHORIZONTAL
)
8072 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
8074 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
8079 if ( textOrientation
== wxHORIZONTAL
)
8086 // Align each line of a multi-line label
8087 size_t nLines
= lines
.GetCount();
8088 for ( size_t l
= 0; l
< nLines
; l
++ )
8090 const wxString
& line
= lines
[l
];
8094 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
8098 wxCoord lineWidth
= 0,
8100 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
8102 switch ( horizAlign
)
8105 if ( textOrientation
== wxHORIZONTAL
)
8106 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
8108 y
= rect
.y
+ lineWidth
+ 1;
8111 case wxALIGN_CENTRE
:
8112 if ( textOrientation
== wxHORIZONTAL
)
8113 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
8115 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
8120 if ( textOrientation
== wxHORIZONTAL
)
8123 y
= rect
.y
+ rect
.height
- 1;
8127 if ( textOrientation
== wxHORIZONTAL
)
8129 dc
.DrawText( line
, x
, y
);
8134 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
8140 // Split multi-line text up into an array of strings.
8141 // Any existing contents of the string array are preserved.
8143 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
8147 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
8148 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
8150 while ( startPos
< (int)tVal
.length() )
8152 pos
= tVal
.Mid(startPos
).Find( eol
);
8157 else if ( pos
== 0 )
8159 lines
.Add( wxEmptyString
);
8163 lines
.Add( value
.Mid(startPos
, pos
) );
8166 startPos
+= pos
+ 1;
8169 if ( startPos
< (int)value
.length() )
8171 lines
.Add( value
.Mid( startPos
) );
8175 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
8176 const wxArrayString
& lines
,
8177 long *width
, long *height
) const
8181 wxCoord lineW
= 0, lineH
= 0;
8184 for ( i
= 0; i
< lines
.GetCount(); i
++ )
8186 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
8187 w
= wxMax( w
, lineW
);
8196 // ------ Batch processing.
8198 void wxGrid::EndBatch()
8200 if ( m_batchCount
> 0 )
8203 if ( !m_batchCount
)
8206 m_rowLabelWin
->Refresh();
8207 m_colLabelWin
->Refresh();
8208 m_cornerLabelWin
->Refresh();
8209 m_gridWin
->Refresh();
8214 // Use this, rather than wxWindow::Refresh(), to force an immediate
8215 // repainting of the grid. Has no effect if you are already inside a
8216 // BeginBatch / EndBatch block.
8218 void wxGrid::ForceRefresh()
8224 bool wxGrid::Enable(bool enable
)
8226 if ( !wxScrolledWindow::Enable(enable
) )
8229 // redraw in the new state
8230 m_gridWin
->Refresh();
8236 // ------ Edit control functions
8239 void wxGrid::EnableEditing( bool edit
)
8241 // TODO: improve this ?
8243 if ( edit
!= m_editable
)
8246 EnableCellEditControl(edit
);
8251 void wxGrid::EnableCellEditControl( bool enable
)
8256 if ( enable
!= m_cellEditCtrlEnabled
)
8260 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
8263 // this should be checked by the caller!
8264 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8266 // do it before ShowCellEditControl()
8267 m_cellEditCtrlEnabled
= enable
;
8269 ShowCellEditControl();
8273 //FIXME:add veto support
8274 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
8276 HideCellEditControl();
8277 SaveEditControlValue();
8279 // do it after HideCellEditControl()
8280 m_cellEditCtrlEnabled
= enable
;
8285 bool wxGrid::IsCurrentCellReadOnly() const
8288 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
8289 bool readonly
= attr
->IsReadOnly();
8295 bool wxGrid::CanEnableCellControl() const
8297 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
8298 !IsCurrentCellReadOnly();
8301 bool wxGrid::IsCellEditControlEnabled() const
8303 // the cell edit control might be disable for all cells or just for the
8304 // current one if it's read only
8305 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
8308 bool wxGrid::IsCellEditControlShown() const
8310 bool isShown
= false;
8312 if ( m_cellEditCtrlEnabled
)
8314 int row
= m_currentCellCoords
.GetRow();
8315 int col
= m_currentCellCoords
.GetCol();
8316 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8317 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
8322 if ( editor
->IsCreated() )
8324 isShown
= editor
->GetControl()->IsShown();
8334 void wxGrid::ShowCellEditControl()
8336 if ( IsCellEditControlEnabled() )
8338 if ( !IsVisible( m_currentCellCoords
, false ) )
8340 m_cellEditCtrlEnabled
= false;
8345 wxRect rect
= CellToRect( m_currentCellCoords
);
8346 int row
= m_currentCellCoords
.GetRow();
8347 int col
= m_currentCellCoords
.GetCol();
8349 // if this is part of a multicell, find owner (topleft)
8350 int cell_rows
, cell_cols
;
8351 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8352 if ( cell_rows
<= 0 || cell_cols
<= 0 )
8356 m_currentCellCoords
.SetRow( row
);
8357 m_currentCellCoords
.SetCol( col
);
8360 // erase the highlight and the cell contents because the editor
8361 // might not cover the entire cell
8362 wxClientDC
dc( m_gridWin
);
8364 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8365 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
8366 dc
.SetPen(*wxTRANSPARENT_PEN
);
8367 dc
.DrawRectangle(rect
);
8369 // convert to scrolled coords
8370 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8376 // cell is shifted by one pixel
8377 // However, don't allow x or y to become negative
8378 // since the SetSize() method interprets that as
8385 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8386 if ( !editor
->IsCreated() )
8388 editor
->Create(m_gridWin
, wxID_ANY
,
8389 new wxGridCellEditorEvtHandler(this, editor
));
8391 wxGridEditorCreatedEvent
evt(GetId(),
8392 wxEVT_GRID_EDITOR_CREATED
,
8396 editor
->GetControl());
8397 GetEventHandler()->ProcessEvent(evt
);
8400 // resize editor to overflow into righthand cells if allowed
8401 int maxWidth
= rect
.width
;
8402 wxString value
= GetCellValue(row
, col
);
8403 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
8406 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
8407 if (maxWidth
< rect
.width
)
8408 maxWidth
= rect
.width
;
8411 int client_right
= m_gridWin
->GetClientSize().GetWidth();
8412 if (rect
.x
+ maxWidth
> client_right
)
8413 maxWidth
= client_right
- rect
.x
;
8415 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
8417 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8418 // may have changed earlier
8419 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
8422 GetCellSize( row
, i
, &c_rows
, &c_cols
);
8424 // looks weird going over a multicell
8425 if (m_table
->IsEmptyCell( row
, i
) &&
8426 (rect
.width
< maxWidth
) && (c_rows
== 1))
8428 rect
.width
+= GetColWidth( i
);
8434 if (rect
.GetRight() > client_right
)
8435 rect
.SetRight( client_right
- 1 );
8438 editor
->SetCellAttr( attr
);
8439 editor
->SetSize( rect
);
8441 editor
->GetControl()->Move(
8442 editor
->GetControl()->GetPosition().x
+ nXMove
,
8443 editor
->GetControl()->GetPosition().y
);
8444 editor
->Show( true, attr
);
8446 // recalc dimensions in case we need to
8447 // expand the scrolled window to account for editor
8450 editor
->BeginEdit(row
, col
, this);
8451 editor
->SetCellAttr(NULL
);
8459 void wxGrid::HideCellEditControl()
8461 if ( IsCellEditControlEnabled() )
8463 int row
= m_currentCellCoords
.GetRow();
8464 int col
= m_currentCellCoords
.GetCol();
8466 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8467 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
8468 editor
->Show( false );
8472 m_gridWin
->SetFocus();
8474 // refresh whole row to the right
8475 wxRect
rect( CellToRect(row
, col
) );
8476 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8477 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8480 // ensure that the pixels under the focus ring get refreshed as well
8481 rect
.Inflate(10, 10);
8484 m_gridWin
->Refresh( false, &rect
);
8488 void wxGrid::SaveEditControlValue()
8490 if ( IsCellEditControlEnabled() )
8492 int row
= m_currentCellCoords
.GetRow();
8493 int col
= m_currentCellCoords
.GetCol();
8495 wxString oldval
= GetCellValue(row
, col
);
8497 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8498 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8499 bool changed
= editor
->EndEdit(row
, col
, this);
8506 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
8507 m_currentCellCoords
.GetRow(),
8508 m_currentCellCoords
.GetCol() ) < 0 )
8510 // Event has been vetoed, set the data back.
8511 SetCellValue(row
, col
, oldval
);
8518 // ------ Grid location functions
8519 // Note that all of these functions work with the logical coordinates of
8520 // grid cells and labels so you will need to convert from device
8521 // coordinates for mouse events etc.
8524 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
) const
8526 int row
= YToRow(y
);
8527 int col
= XToCol(x
);
8529 if ( row
== -1 || col
== -1 )
8531 coords
= wxGridNoCellCoords
;
8535 coords
.Set( row
, col
);
8539 // Internal Helper function for computing row or column from some
8540 // (unscrolled) coordinate value, using either
8541 // m_defaultRowHeight/m_defaultColWidth or binary search on array
8542 // of m_rowBottoms/m_ColRights to speed up the search!
8544 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
8545 const wxArrayInt
& BorderArray
, int nMax
,
8549 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
8554 size_t i_max
= coord
/ defaultDist
,
8557 if (BorderArray
.IsEmpty())
8559 if ((int) i_max
< nMax
)
8561 return clipToMinMax
? nMax
- 1 : -1;
8564 if ( i_max
>= BorderArray
.GetCount())
8566 i_max
= BorderArray
.GetCount() - 1;
8570 if ( coord
>= BorderArray
[i_max
])
8574 i_max
= coord
/ minDist
;
8576 i_max
= BorderArray
.GetCount() - 1;
8579 if ( i_max
>= BorderArray
.GetCount())
8580 i_max
= BorderArray
.GetCount() - 1;
8583 if ( coord
>= BorderArray
[i_max
])
8584 return clipToMinMax
? (int)i_max
: -1;
8585 if ( coord
< BorderArray
[0] )
8588 while ( i_max
- i_min
> 0 )
8590 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
8591 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8592 if (coord
>= BorderArray
[ i_max
- 1])
8596 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
8597 if (coord
< BorderArray
[median
])
8606 int wxGrid::YToRow( int y
) const
8608 return CoordToRowOrCol(y
, m_defaultRowHeight
,
8609 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8612 int wxGrid::XToCol( int x
, bool clipToMinMax
) const
8615 return clipToMinMax
&& (m_numCols
> 0) ? GetColAt( 0 ) : -1;
8617 wxASSERT_MSG(m_defaultColWidth
> 0, wxT("Default column width can not be zero"));
8619 int maxPos
= x
/ m_defaultColWidth
;
8622 if (m_colRights
.IsEmpty())
8624 if(maxPos
< m_numCols
)
8625 return GetColAt( maxPos
);
8626 return clipToMinMax
? GetColAt( m_numCols
- 1 ) : -1;
8629 if ( maxPos
>= m_numCols
)
8630 maxPos
= m_numCols
- 1;
8633 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8636 if (m_minAcceptableColWidth
)
8637 maxPos
= x
/ m_minAcceptableColWidth
;
8639 maxPos
= m_numCols
- 1;
8641 if ( maxPos
>= m_numCols
)
8642 maxPos
= m_numCols
- 1;
8645 //X is beyond the last column
8646 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8647 return clipToMinMax
? GetColAt( maxPos
) : -1;
8649 //X is before the first column
8650 if ( x
< m_colRights
[GetColAt( 0 )] )
8651 return GetColAt( 0 );
8653 //Perform a binary search
8654 while ( maxPos
- minPos
> 0 )
8656 wxCHECK_MSG(m_colRights
[GetColAt( minPos
)] <= x
&& x
< m_colRights
[GetColAt( maxPos
)],
8657 0, _T("wxGrid: internal error in XToCol"));
8659 if (x
>= m_colRights
[GetColAt( maxPos
- 1 )])
8660 return GetColAt( maxPos
);
8663 int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
8664 if (x
< m_colRights
[GetColAt( median
)])
8669 return GetColAt( maxPos
);
8672 // return the row number that that the y coord is near
8673 // the edge of, or -1 if not near an edge.
8674 // coords can only possibly be near an edge if
8675 // (a) the row/column is large enough to still allow for an "inner" area
8676 // that is _not_ nead the edge (i.e., if the height/width is smaller
8677 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8680 // (b) resizing rows/columns (the thing for which edge detection is
8681 // relevant at all) is enabled.
8683 int wxGrid::YToEdgeOfRow( int y
) const
8686 i
= internalYToRow(y
);
8688 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragRowSize() )
8690 // We know that we are in row i, test whether we are
8691 // close enough to lower or upper border, respectively.
8692 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8694 else if ( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8701 // return the col number that that the x coord is near the edge of, or
8702 // -1 if not near an edge
8703 // See comment at YToEdgeOfRow for conditions on edge detection.
8705 int wxGrid::XToEdgeOfCol( int x
) const
8708 i
= internalXToCol(x
);
8710 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragColSize() )
8712 // We know that we are in column i; test whether we are
8713 // close enough to right or left border, respectively.
8714 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8716 else if ( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8723 wxRect
wxGrid::CellToRect( int row
, int col
) const
8725 wxRect
rect( -1, -1, -1, -1 );
8727 if ( row
>= 0 && row
< m_numRows
&&
8728 col
>= 0 && col
< m_numCols
)
8730 int i
, cell_rows
, cell_cols
;
8731 rect
.width
= rect
.height
= 0;
8732 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8733 // if negative then find multicell owner
8738 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8740 rect
.x
= GetColLeft(col
);
8741 rect
.y
= GetRowTop(row
);
8742 for (i
=col
; i
< col
+ cell_cols
; i
++)
8743 rect
.width
+= GetColWidth(i
);
8744 for (i
=row
; i
< row
+ cell_rows
; i
++)
8745 rect
.height
+= GetRowHeight(i
);
8748 // if grid lines are enabled, then the area of the cell is a bit smaller
8749 if (m_gridLinesEnabled
)
8758 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
8760 // get the cell rectangle in logical coords
8762 wxRect
r( CellToRect( row
, col
) );
8764 // convert to device coords
8766 int left
, top
, right
, bottom
;
8767 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8768 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8770 // check against the client area of the grid window
8772 m_gridWin
->GetClientSize( &cw
, &ch
);
8774 if ( wholeCellVisible
)
8776 // is the cell wholly visible ?
8777 return ( left
>= 0 && right
<= cw
&&
8778 top
>= 0 && bottom
<= ch
);
8782 // is the cell partly visible ?
8784 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8785 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8789 // make the specified cell location visible by doing a minimal amount
8792 void wxGrid::MakeCellVisible( int row
, int col
)
8795 int xpos
= -1, ypos
= -1;
8797 if ( row
>= 0 && row
< m_numRows
&&
8798 col
>= 0 && col
< m_numCols
)
8800 // get the cell rectangle in logical coords
8801 wxRect
r( CellToRect( row
, col
) );
8803 // convert to device coords
8804 int left
, top
, right
, bottom
;
8805 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8806 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8809 m_gridWin
->GetClientSize( &cw
, &ch
);
8815 else if ( bottom
> ch
)
8817 int h
= r
.GetHeight();
8819 for ( i
= row
- 1; i
>= 0; i
-- )
8821 int rowHeight
= GetRowHeight(i
);
8822 if ( h
+ rowHeight
> ch
)
8829 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8830 // have rounding errors (this is important, because if we do,
8831 // we might not scroll at all and some cells won't be redrawn)
8833 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
8834 // so just add a full scroll unit...
8835 ypos
+= m_scrollLineY
;
8838 // special handling for wide cells - show always left part of the cell!
8839 // Otherwise, e.g. when stepping from row to row, it would jump between
8840 // left and right part of the cell on every step!
8842 if ( left
< 0 || (right
- left
) >= cw
)
8846 else if ( right
> cw
)
8848 // position the view so that the cell is on the right
8850 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8851 xpos
= x0
+ (right
- cw
);
8853 // see comment for ypos above
8854 xpos
+= m_scrollLineX
;
8857 if ( xpos
!= -1 || ypos
!= -1 )
8860 xpos
/= m_scrollLineX
;
8862 ypos
/= m_scrollLineY
;
8863 Scroll( xpos
, ypos
);
8870 // ------ Grid cursor movement functions
8873 bool wxGrid::MoveCursorUp( bool expandSelection
)
8875 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8876 m_currentCellCoords
.GetRow() >= 0 )
8878 if ( expandSelection
)
8880 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8881 m_selectingKeyboard
= m_currentCellCoords
;
8882 if ( m_selectingKeyboard
.GetRow() > 0 )
8884 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8885 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8886 m_selectingKeyboard
.GetCol() );
8887 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8890 else if ( m_currentCellCoords
.GetRow() > 0 )
8892 int row
= m_currentCellCoords
.GetRow() - 1;
8893 int col
= m_currentCellCoords
.GetCol();
8895 MakeCellVisible( row
, col
);
8896 SetCurrentCell( row
, col
);
8907 bool wxGrid::MoveCursorDown( bool expandSelection
)
8909 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8910 m_currentCellCoords
.GetRow() < m_numRows
)
8912 if ( expandSelection
)
8914 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8915 m_selectingKeyboard
= m_currentCellCoords
;
8916 if ( m_selectingKeyboard
.GetRow() < m_numRows
- 1 )
8918 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8919 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8920 m_selectingKeyboard
.GetCol() );
8921 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8924 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8926 int row
= m_currentCellCoords
.GetRow() + 1;
8927 int col
= m_currentCellCoords
.GetCol();
8929 MakeCellVisible( row
, col
);
8930 SetCurrentCell( row
, col
);
8941 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8943 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8944 m_currentCellCoords
.GetCol() >= 0 )
8946 if ( expandSelection
)
8948 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8949 m_selectingKeyboard
= m_currentCellCoords
;
8950 if ( m_selectingKeyboard
.GetCol() > 0 )
8952 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8953 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8954 m_selectingKeyboard
.GetCol() );
8955 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8958 else if ( GetColPos( m_currentCellCoords
.GetCol() ) > 0 )
8960 int row
= m_currentCellCoords
.GetRow();
8961 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) - 1 );
8964 MakeCellVisible( row
, col
);
8965 SetCurrentCell( row
, col
);
8976 bool wxGrid::MoveCursorRight( bool expandSelection
)
8978 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8979 m_currentCellCoords
.GetCol() < m_numCols
)
8981 if ( expandSelection
)
8983 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8984 m_selectingKeyboard
= m_currentCellCoords
;
8985 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8987 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8988 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8989 m_selectingKeyboard
.GetCol() );
8990 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8993 else if ( GetColPos( m_currentCellCoords
.GetCol() ) < m_numCols
- 1 )
8995 int row
= m_currentCellCoords
.GetRow();
8996 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) + 1 );
8999 MakeCellVisible( row
, col
);
9000 SetCurrentCell( row
, col
);
9011 bool wxGrid::MovePageUp()
9013 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9016 int row
= m_currentCellCoords
.GetRow();
9020 m_gridWin
->GetClientSize( &cw
, &ch
);
9022 int y
= GetRowTop(row
);
9023 int newRow
= internalYToRow( y
- ch
+ 1 );
9025 if ( newRow
== row
)
9027 // row > 0, so newRow can never be less than 0 here.
9031 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
9032 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
9040 bool wxGrid::MovePageDown()
9042 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9045 int row
= m_currentCellCoords
.GetRow();
9046 if ( (row
+ 1) < m_numRows
)
9049 m_gridWin
->GetClientSize( &cw
, &ch
);
9051 int y
= GetRowTop(row
);
9052 int newRow
= internalYToRow( y
+ ch
);
9053 if ( newRow
== row
)
9055 // row < m_numRows, so newRow can't overflow here.
9059 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
9060 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
9068 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
9071 m_currentCellCoords
!= wxGridNoCellCoords
&&
9072 m_currentCellCoords
.GetRow() > 0 )
9074 int row
= m_currentCellCoords
.GetRow();
9075 int col
= m_currentCellCoords
.GetCol();
9077 if ( m_table
->IsEmptyCell(row
, col
) )
9079 // starting in an empty cell: find the next block of
9085 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9089 else if ( m_table
->IsEmptyCell(row
- 1, col
) )
9091 // starting at the top of a block: find the next block
9097 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9103 // starting within a block: find the top of the block
9108 if ( m_table
->IsEmptyCell(row
, col
) )
9116 MakeCellVisible( row
, col
);
9117 if ( expandSelection
)
9119 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9120 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9125 SetCurrentCell( row
, col
);
9134 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
9137 m_currentCellCoords
!= wxGridNoCellCoords
&&
9138 m_currentCellCoords
.GetRow() < m_numRows
- 1 )
9140 int row
= m_currentCellCoords
.GetRow();
9141 int col
= m_currentCellCoords
.GetCol();
9143 if ( m_table
->IsEmptyCell(row
, col
) )
9145 // starting in an empty cell: find the next block of
9148 while ( row
< m_numRows
- 1 )
9151 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9155 else if ( m_table
->IsEmptyCell(row
+ 1, col
) )
9157 // starting at the bottom of a block: find the next block
9160 while ( row
< m_numRows
- 1 )
9163 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9169 // starting within a block: find the bottom of the block
9171 while ( row
< m_numRows
- 1 )
9174 if ( m_table
->IsEmptyCell(row
, col
) )
9182 MakeCellVisible( row
, col
);
9183 if ( expandSelection
)
9185 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9186 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9191 SetCurrentCell( row
, col
);
9200 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
9203 m_currentCellCoords
!= wxGridNoCellCoords
&&
9204 m_currentCellCoords
.GetCol() > 0 )
9206 int row
= m_currentCellCoords
.GetRow();
9207 int col
= m_currentCellCoords
.GetCol();
9209 if ( m_table
->IsEmptyCell(row
, col
) )
9211 // starting in an empty cell: find the next block of
9217 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9221 else if ( m_table
->IsEmptyCell(row
, col
- 1) )
9223 // starting at the left of a block: find the next block
9229 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9235 // starting within a block: find the left of the block
9240 if ( m_table
->IsEmptyCell(row
, col
) )
9248 MakeCellVisible( row
, col
);
9249 if ( expandSelection
)
9251 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9252 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9257 SetCurrentCell( row
, col
);
9266 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
9269 m_currentCellCoords
!= wxGridNoCellCoords
&&
9270 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
9272 int row
= m_currentCellCoords
.GetRow();
9273 int col
= m_currentCellCoords
.GetCol();
9275 if ( m_table
->IsEmptyCell(row
, col
) )
9277 // starting in an empty cell: find the next block of
9280 while ( col
< m_numCols
- 1 )
9283 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9287 else if ( m_table
->IsEmptyCell(row
, col
+ 1) )
9289 // starting at the right of a block: find the next block
9292 while ( col
< m_numCols
- 1 )
9295 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9301 // starting within a block: find the right of the block
9303 while ( col
< m_numCols
- 1 )
9306 if ( m_table
->IsEmptyCell(row
, col
) )
9314 MakeCellVisible( row
, col
);
9315 if ( expandSelection
)
9317 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9318 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9323 SetCurrentCell( row
, col
);
9333 // ------ Label values and formatting
9336 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
9339 *horiz
= m_rowLabelHorizAlign
;
9341 *vert
= m_rowLabelVertAlign
;
9344 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
9347 *horiz
= m_colLabelHorizAlign
;
9349 *vert
= m_colLabelVertAlign
;
9352 int wxGrid::GetColLabelTextOrientation() const
9354 return m_colLabelTextOrientation
;
9357 wxString
wxGrid::GetRowLabelValue( int row
) const
9361 return m_table
->GetRowLabelValue( row
);
9371 wxString
wxGrid::GetColLabelValue( int col
) const
9375 return m_table
->GetColLabelValue( col
);
9385 void wxGrid::SetRowLabelSize( int width
)
9387 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
9389 if ( width
== wxGRID_AUTOSIZE
)
9391 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
9394 if ( width
!= m_rowLabelWidth
)
9398 m_rowLabelWin
->Show( false );
9399 m_cornerLabelWin
->Show( false );
9401 else if ( m_rowLabelWidth
== 0 )
9403 m_rowLabelWin
->Show( true );
9404 if ( m_colLabelHeight
> 0 )
9405 m_cornerLabelWin
->Show( true );
9408 m_rowLabelWidth
= width
;
9410 wxScrolledWindow::Refresh( true );
9414 void wxGrid::SetColLabelSize( int height
)
9416 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
9418 if ( height
== wxGRID_AUTOSIZE
)
9420 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
9423 if ( height
!= m_colLabelHeight
)
9427 m_colLabelWin
->Show( false );
9428 m_cornerLabelWin
->Show( false );
9430 else if ( m_colLabelHeight
== 0 )
9432 m_colLabelWin
->Show( true );
9433 if ( m_rowLabelWidth
> 0 )
9434 m_cornerLabelWin
->Show( true );
9437 m_colLabelHeight
= height
;
9439 wxScrolledWindow::Refresh( true );
9443 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
9445 if ( m_labelBackgroundColour
!= colour
)
9447 m_labelBackgroundColour
= colour
;
9448 m_rowLabelWin
->SetBackgroundColour( colour
);
9449 m_colLabelWin
->SetBackgroundColour( colour
);
9450 m_cornerLabelWin
->SetBackgroundColour( colour
);
9452 if ( !GetBatchCount() )
9454 m_rowLabelWin
->Refresh();
9455 m_colLabelWin
->Refresh();
9456 m_cornerLabelWin
->Refresh();
9461 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
9463 if ( m_labelTextColour
!= colour
)
9465 m_labelTextColour
= colour
;
9466 if ( !GetBatchCount() )
9468 m_rowLabelWin
->Refresh();
9469 m_colLabelWin
->Refresh();
9474 void wxGrid::SetLabelFont( const wxFont
& font
)
9477 if ( !GetBatchCount() )
9479 m_rowLabelWin
->Refresh();
9480 m_colLabelWin
->Refresh();
9484 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
9486 // allow old (incorrect) defs to be used
9489 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9490 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9491 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9496 case wxTOP
: vert
= wxALIGN_TOP
; break;
9497 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9498 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9501 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9503 m_rowLabelHorizAlign
= horiz
;
9506 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9508 m_rowLabelVertAlign
= vert
;
9511 if ( !GetBatchCount() )
9513 m_rowLabelWin
->Refresh();
9517 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
9519 // allow old (incorrect) defs to be used
9522 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9523 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9524 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9529 case wxTOP
: vert
= wxALIGN_TOP
; break;
9530 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9531 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9534 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9536 m_colLabelHorizAlign
= horiz
;
9539 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9541 m_colLabelVertAlign
= vert
;
9544 if ( !GetBatchCount() )
9546 m_colLabelWin
->Refresh();
9550 // Note: under MSW, the default column label font must be changed because it
9551 // does not support vertical printing
9553 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9554 // pGrid->SetLabelFont(font);
9555 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9557 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9559 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9560 m_colLabelTextOrientation
= textOrientation
;
9562 if ( !GetBatchCount() )
9563 m_colLabelWin
->Refresh();
9566 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9570 m_table
->SetRowLabelValue( row
, s
);
9571 if ( !GetBatchCount() )
9573 wxRect rect
= CellToRect( row
, 0 );
9574 if ( rect
.height
> 0 )
9576 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9578 rect
.width
= m_rowLabelWidth
;
9579 m_rowLabelWin
->Refresh( true, &rect
);
9585 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9589 m_table
->SetColLabelValue( col
, s
);
9590 if ( !GetBatchCount() )
9592 wxRect rect
= CellToRect( 0, col
);
9593 if ( rect
.width
> 0 )
9595 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9597 rect
.height
= m_colLabelHeight
;
9598 m_colLabelWin
->Refresh( true, &rect
);
9604 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9606 if ( m_gridLineColour
!= colour
)
9608 m_gridLineColour
= colour
;
9610 wxClientDC
dc( m_gridWin
);
9612 DrawAllGridLines( dc
, wxRegion() );
9616 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9618 if ( m_cellHighlightColour
!= colour
)
9620 m_cellHighlightColour
= colour
;
9622 wxClientDC
dc( m_gridWin
);
9624 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9625 DrawCellHighlight(dc
, attr
);
9630 void wxGrid::SetCellHighlightPenWidth(int width
)
9632 if (m_cellHighlightPenWidth
!= width
)
9634 m_cellHighlightPenWidth
= width
;
9636 // Just redrawing the cell highlight is not enough since that won't
9637 // make any visible change if the the thickness is getting smaller.
9638 int row
= m_currentCellCoords
.GetRow();
9639 int col
= m_currentCellCoords
.GetCol();
9640 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9643 wxRect rect
= CellToRect(row
, col
);
9644 m_gridWin
->Refresh(true, &rect
);
9648 void wxGrid::SetCellHighlightROPenWidth(int width
)
9650 if (m_cellHighlightROPenWidth
!= width
)
9652 m_cellHighlightROPenWidth
= width
;
9654 // Just redrawing the cell highlight is not enough since that won't
9655 // make any visible change if the the thickness is getting smaller.
9656 int row
= m_currentCellCoords
.GetRow();
9657 int col
= m_currentCellCoords
.GetCol();
9658 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9661 wxRect rect
= CellToRect(row
, col
);
9662 m_gridWin
->Refresh(true, &rect
);
9666 void wxGrid::EnableGridLines( bool enable
)
9668 if ( enable
!= m_gridLinesEnabled
)
9670 m_gridLinesEnabled
= enable
;
9672 if ( !GetBatchCount() )
9676 wxClientDC
dc( m_gridWin
);
9678 DrawAllGridLines( dc
, wxRegion() );
9682 m_gridWin
->Refresh();
9688 int wxGrid::GetDefaultRowSize() const
9690 return m_defaultRowHeight
;
9693 int wxGrid::GetRowSize( int row
) const
9695 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9697 return GetRowHeight(row
);
9700 int wxGrid::GetDefaultColSize() const
9702 return m_defaultColWidth
;
9705 int wxGrid::GetColSize( int col
) const
9707 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9709 return GetColWidth(col
);
9712 // ============================================================================
9713 // access to the grid attributes: each of them has a default value in the grid
9714 // itself and may be overidden on a per-cell basis
9715 // ============================================================================
9717 // ----------------------------------------------------------------------------
9718 // setting default attributes
9719 // ----------------------------------------------------------------------------
9721 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9723 m_defaultCellAttr
->SetBackgroundColour(col
);
9725 m_gridWin
->SetBackgroundColour(col
);
9729 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9731 m_defaultCellAttr
->SetTextColour(col
);
9734 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9736 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9739 void wxGrid::SetDefaultCellOverflow( bool allow
)
9741 m_defaultCellAttr
->SetOverflow(allow
);
9744 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9746 m_defaultCellAttr
->SetFont(font
);
9749 // For editors and renderers the type registry takes precedence over the
9750 // default attr, so we need to register the new editor/renderer for the string
9751 // data type in order to make setting a default editor/renderer appear to
9754 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9756 RegisterDataType(wxGRID_VALUE_STRING
,
9758 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9761 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9763 RegisterDataType(wxGRID_VALUE_STRING
,
9764 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9768 // ----------------------------------------------------------------------------
9769 // access to the default attributes
9770 // ----------------------------------------------------------------------------
9772 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
9774 return m_defaultCellAttr
->GetBackgroundColour();
9777 wxColour
wxGrid::GetDefaultCellTextColour() const
9779 return m_defaultCellAttr
->GetTextColour();
9782 wxFont
wxGrid::GetDefaultCellFont() const
9784 return m_defaultCellAttr
->GetFont();
9787 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
9789 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9792 bool wxGrid::GetDefaultCellOverflow() const
9794 return m_defaultCellAttr
->GetOverflow();
9797 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9799 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9802 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9804 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9807 // ----------------------------------------------------------------------------
9808 // access to cell attributes
9809 // ----------------------------------------------------------------------------
9811 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
9813 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9814 wxColour colour
= attr
->GetBackgroundColour();
9820 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
9822 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9823 wxColour colour
= attr
->GetTextColour();
9829 wxFont
wxGrid::GetCellFont( int row
, int col
) const
9831 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9832 wxFont font
= attr
->GetFont();
9838 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
9840 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9841 attr
->GetAlignment(horiz
, vert
);
9845 bool wxGrid::GetCellOverflow( int row
, int col
) const
9847 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9848 bool allow
= attr
->GetOverflow();
9854 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
9856 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9857 attr
->GetSize( num_rows
, num_cols
);
9861 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
9863 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9864 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9870 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
9872 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9873 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9879 bool wxGrid::IsReadOnly(int row
, int col
) const
9881 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9882 bool isReadOnly
= attr
->IsReadOnly();
9888 // ----------------------------------------------------------------------------
9889 // attribute support: cache, automatic provider creation, ...
9890 // ----------------------------------------------------------------------------
9892 bool wxGrid::CanHaveAttributes() const
9899 return m_table
->CanHaveAttributes();
9902 void wxGrid::ClearAttrCache()
9904 if ( m_attrCache
.row
!= -1 )
9906 wxSafeDecRef(m_attrCache
.attr
);
9907 m_attrCache
.attr
= NULL
;
9908 m_attrCache
.row
= -1;
9912 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9916 wxGrid
*self
= (wxGrid
*)this; // const_cast
9918 self
->ClearAttrCache();
9919 self
->m_attrCache
.row
= row
;
9920 self
->m_attrCache
.col
= col
;
9921 self
->m_attrCache
.attr
= attr
;
9926 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9928 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9930 *attr
= m_attrCache
.attr
;
9931 wxSafeIncRef(m_attrCache
.attr
);
9933 #ifdef DEBUG_ATTR_CACHE
9934 gs_nAttrCacheHits
++;
9941 #ifdef DEBUG_ATTR_CACHE
9942 gs_nAttrCacheMisses
++;
9949 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9951 wxGridCellAttr
*attr
= NULL
;
9952 // Additional test to avoid looking at the cache e.g. for
9953 // wxNoCellCoords, as this will confuse memory management.
9956 if ( !LookupAttr(row
, col
, &attr
) )
9958 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9959 : (wxGridCellAttr
*)NULL
;
9960 CacheAttr(row
, col
, attr
);
9966 attr
->SetDefAttr(m_defaultCellAttr
);
9970 attr
= m_defaultCellAttr
;
9977 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9979 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9980 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9982 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9983 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9985 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9988 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9990 // artificially inc the ref count to match DecRef() in caller
9992 m_table
->SetAttr(attr
, row
, col
);
9998 // ----------------------------------------------------------------------------
9999 // setting column attributes (wrappers around SetColAttr)
10000 // ----------------------------------------------------------------------------
10002 void wxGrid::SetColFormatBool(int col
)
10004 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
10007 void wxGrid::SetColFormatNumber(int col
)
10009 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
10012 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
10014 wxString typeName
= wxGRID_VALUE_FLOAT
;
10015 if ( (width
!= -1) || (precision
!= -1) )
10017 typeName
<< _T(':') << width
<< _T(',') << precision
;
10020 SetColFormatCustom(col
, typeName
);
10023 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
10025 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
10027 attr
= new wxGridCellAttr
;
10028 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
10029 attr
->SetRenderer(renderer
);
10031 SetColAttr(col
, attr
);
10035 // ----------------------------------------------------------------------------
10036 // setting cell attributes: this is forwarded to the table
10037 // ----------------------------------------------------------------------------
10039 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
10041 if ( CanHaveAttributes() )
10043 m_table
->SetAttr(attr
, row
, col
);
10048 wxSafeDecRef(attr
);
10052 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
10054 if ( CanHaveAttributes() )
10056 m_table
->SetRowAttr(attr
, row
);
10061 wxSafeDecRef(attr
);
10065 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
10067 if ( CanHaveAttributes() )
10069 m_table
->SetColAttr(attr
, col
);
10074 wxSafeDecRef(attr
);
10078 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
10080 if ( CanHaveAttributes() )
10082 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10083 attr
->SetBackgroundColour(colour
);
10088 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
10090 if ( CanHaveAttributes() )
10092 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10093 attr
->SetTextColour(colour
);
10098 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
10100 if ( CanHaveAttributes() )
10102 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10103 attr
->SetFont(font
);
10108 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
10110 if ( CanHaveAttributes() )
10112 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10113 attr
->SetAlignment(horiz
, vert
);
10118 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
10120 if ( CanHaveAttributes() )
10122 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10123 attr
->SetOverflow(allow
);
10128 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
10130 if ( CanHaveAttributes() )
10132 int cell_rows
, cell_cols
;
10134 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10135 attr
->GetSize(&cell_rows
, &cell_cols
);
10136 attr
->SetSize(num_rows
, num_cols
);
10139 // Cannot set the size of a cell to 0 or negative values
10140 // While it is perfectly legal to do that, this function cannot
10141 // handle all the possibilies, do it by hand by getting the CellAttr.
10142 // You can only set the size of a cell to 1,1 or greater with this fn
10143 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
10144 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10145 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
10146 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10148 // if this was already a multicell then "turn off" the other cells first
10149 if ((cell_rows
> 1) || (cell_rows
> 1))
10152 for (j
=row
; j
< row
+ cell_rows
; j
++)
10154 for (i
=col
; i
< col
+ cell_cols
; i
++)
10156 if ((i
!= col
) || (j
!= row
))
10158 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10159 attr_stub
->SetSize( 1, 1 );
10160 attr_stub
->DecRef();
10166 // mark the cells that will be covered by this cell to
10167 // negative or zero values to point back at this cell
10168 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
10171 for (j
=row
; j
< row
+ num_rows
; j
++)
10173 for (i
=col
; i
< col
+ num_cols
; i
++)
10175 if ((i
!= col
) || (j
!= row
))
10177 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10178 attr_stub
->SetSize( row
- j
, col
- i
);
10179 attr_stub
->DecRef();
10187 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
10189 if ( CanHaveAttributes() )
10191 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10192 attr
->SetRenderer(renderer
);
10197 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
10199 if ( CanHaveAttributes() )
10201 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10202 attr
->SetEditor(editor
);
10207 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
10209 if ( CanHaveAttributes() )
10211 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10212 attr
->SetReadOnly(isReadOnly
);
10217 // ----------------------------------------------------------------------------
10218 // Data type registration
10219 // ----------------------------------------------------------------------------
10221 void wxGrid::RegisterDataType(const wxString
& typeName
,
10222 wxGridCellRenderer
* renderer
,
10223 wxGridCellEditor
* editor
)
10225 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
10229 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
10231 wxString typeName
= m_table
->GetTypeName(row
, col
);
10232 return GetDefaultEditorForType(typeName
);
10235 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
10237 wxString typeName
= m_table
->GetTypeName(row
, col
);
10238 return GetDefaultRendererForType(typeName
);
10241 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
10243 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10244 if ( index
== wxNOT_FOUND
)
10246 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10251 return m_typeRegistry
->GetEditor(index
);
10254 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
10256 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10257 if ( index
== wxNOT_FOUND
)
10259 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10264 return m_typeRegistry
->GetRenderer(index
);
10267 // ----------------------------------------------------------------------------
10269 // ----------------------------------------------------------------------------
10271 void wxGrid::EnableDragRowSize( bool enable
)
10273 m_canDragRowSize
= enable
;
10276 void wxGrid::EnableDragColSize( bool enable
)
10278 m_canDragColSize
= enable
;
10281 void wxGrid::EnableDragGridSize( bool enable
)
10283 m_canDragGridSize
= enable
;
10286 void wxGrid::EnableDragCell( bool enable
)
10288 m_canDragCell
= enable
;
10291 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
10293 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
10295 if ( resizeExistingRows
)
10297 // since we are resizing all rows to the default row size,
10298 // we can simply clear the row heights and row bottoms
10299 // arrays (which also allows us to take advantage of
10300 // some speed optimisations)
10301 m_rowHeights
.Empty();
10302 m_rowBottoms
.Empty();
10303 if ( !GetBatchCount() )
10308 void wxGrid::SetRowSize( int row
, int height
)
10310 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
10312 // See comment in SetColSize
10313 if ( height
< GetRowMinimalAcceptableHeight())
10316 if ( m_rowHeights
.IsEmpty() )
10318 // need to really create the array
10322 int h
= wxMax( 0, height
);
10323 int diff
= h
- m_rowHeights
[row
];
10325 m_rowHeights
[row
] = h
;
10326 for ( int i
= row
; i
< m_numRows
; i
++ )
10328 m_rowBottoms
[i
] += diff
;
10331 if ( !GetBatchCount() )
10335 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
10337 // we dont allow zero default column width
10338 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
10340 if ( resizeExistingCols
)
10342 // since we are resizing all columns to the default column size,
10343 // we can simply clear the col widths and col rights
10344 // arrays (which also allows us to take advantage of
10345 // some speed optimisations)
10346 m_colWidths
.Empty();
10347 m_colRights
.Empty();
10348 if ( !GetBatchCount() )
10353 void wxGrid::SetColSize( int col
, int width
)
10355 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
10357 // should we check that it's bigger than GetColMinimalWidth(col) here?
10359 // No, because it is reasonable to assume the library user know's
10360 // what he is doing. However we should test against the weaker
10361 // constraint of minimalAcceptableWidth, as this breaks rendering
10363 // This test then fixes sf.net bug #645734
10365 if ( width
< GetColMinimalAcceptableWidth() )
10368 if ( m_colWidths
.IsEmpty() )
10370 // need to really create the array
10374 // if < 0 then calculate new width from label
10378 wxArrayString lines
;
10379 wxClientDC
dc(m_colLabelWin
);
10380 dc
.SetFont(GetLabelFont());
10381 StringToLines(GetColLabelValue(col
), lines
);
10382 GetTextBoxSize(dc
, lines
, &w
, &h
);
10386 int w
= wxMax( 0, width
);
10387 int diff
= w
- m_colWidths
[col
];
10388 m_colWidths
[col
] = w
;
10390 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
10392 m_colRights
[GetColAt(colPos
)] += diff
;
10395 if ( !GetBatchCount() )
10399 void wxGrid::SetColMinimalWidth( int col
, int width
)
10401 if (width
> GetColMinimalAcceptableWidth())
10403 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10404 m_colMinWidths
[key
] = width
;
10408 void wxGrid::SetRowMinimalHeight( int row
, int width
)
10410 if (width
> GetRowMinimalAcceptableHeight())
10412 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10413 m_rowMinHeights
[key
] = width
;
10417 int wxGrid::GetColMinimalWidth(int col
) const
10419 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10420 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
10422 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
10425 int wxGrid::GetRowMinimalHeight(int row
) const
10427 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10428 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
10430 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
10433 void wxGrid::SetColMinimalAcceptableWidth( int width
)
10435 // We do allow a width of 0 since this gives us
10436 // an easy way to temporarily hiding columns.
10438 m_minAcceptableColWidth
= width
;
10441 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
10443 // We do allow a height of 0 since this gives us
10444 // an easy way to temporarily hiding rows.
10446 m_minAcceptableRowHeight
= height
;
10449 int wxGrid::GetColMinimalAcceptableWidth() const
10451 return m_minAcceptableColWidth
;
10454 int wxGrid::GetRowMinimalAcceptableHeight() const
10456 return m_minAcceptableRowHeight
;
10459 // ----------------------------------------------------------------------------
10461 // ----------------------------------------------------------------------------
10464 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
10466 const bool column
= direction
== wxGRID_COLUMN
;
10468 wxClientDC
dc(m_gridWin
);
10470 // cancel editing of cell
10471 HideCellEditControl();
10472 SaveEditControlValue();
10474 // init both of them to avoid compiler warnings, even if we only need one
10482 wxCoord extent
, extentMax
= 0;
10483 int max
= column
? m_numRows
: m_numCols
;
10484 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
10491 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
10492 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
10495 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
10496 extent
= column
? size
.x
: size
.y
;
10497 if ( extent
> extentMax
)
10498 extentMax
= extent
;
10500 renderer
->DecRef();
10506 // now also compare with the column label extent
10508 dc
.SetFont( GetLabelFont() );
10512 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
10513 if ( GetColLabelTextOrientation() == wxVERTICAL
)
10517 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
10519 extent
= column
? w
: h
;
10520 if ( extent
> extentMax
)
10521 extentMax
= extent
;
10525 // empty column - give default extent (notice that if extentMax is less
10526 // than default extent but != 0, it's OK)
10527 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
10532 // leave some space around text
10540 // Ensure automatic width is not less than minimal width. See the
10541 // comment in SetColSize() for explanation of why this isn't done
10542 // in SetColSize().
10544 extentMax
= wxMax(extentMax
, GetColMinimalWidth(col
));
10546 SetColSize( col
, extentMax
);
10547 if ( !GetBatchCount() )
10550 m_gridWin
->GetClientSize( &cw
, &ch
);
10551 wxRect
rect ( CellToRect( 0, col
) );
10553 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10554 rect
.width
= cw
- rect
.x
;
10555 rect
.height
= m_colLabelHeight
;
10556 m_colLabelWin
->Refresh( true, &rect
);
10561 // Ensure automatic width is not less than minimal height. See the
10562 // comment in SetColSize() for explanation of why this isn't done
10563 // in SetRowSize().
10565 extentMax
= wxMax(extentMax
, GetRowMinimalHeight(row
));
10567 SetRowSize(row
, extentMax
);
10568 if ( !GetBatchCount() )
10571 m_gridWin
->GetClientSize( &cw
, &ch
);
10572 wxRect
rect( CellToRect( row
, 0 ) );
10574 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10575 rect
.width
= m_rowLabelWidth
;
10576 rect
.height
= ch
- rect
.y
;
10577 m_rowLabelWin
->Refresh( true, &rect
);
10584 SetColMinimalWidth(col
, extentMax
);
10586 SetRowMinimalHeight(row
, extentMax
);
10590 wxCoord
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
10592 // calculate size for the rows or columns?
10593 const bool calcRows
= direction
== wxGRID_ROW
;
10595 wxClientDC
dc(calcRows
? GetGridRowLabelWindow()
10596 : GetGridColLabelWindow());
10597 dc
.SetFont(GetLabelFont());
10599 // which dimension should we take into account for calculations?
10601 // for columns, the text can be only horizontal so it's easy but for rows
10602 // we also have to take into account the text orientation
10604 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
10606 wxArrayString lines
;
10607 wxCoord extentMax
= 0;
10609 const int numRowsOrCols
= calcRows
? m_numRows
: m_numCols
;
10610 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
10614 wxString label
= calcRows
? GetRowLabelValue(rowOrCol
)
10615 : GetColLabelValue(rowOrCol
);
10616 StringToLines(label
, lines
);
10619 GetTextBoxSize(dc
, lines
, &w
, &h
);
10621 const wxCoord extent
= useWidth
? w
: h
;
10622 if ( extent
> extentMax
)
10623 extentMax
= extent
;
10628 // empty column - give default extent (notice that if extentMax is less
10629 // than default extent but != 0, it's OK)
10630 extentMax
= calcRows
? GetDefaultRowLabelSize()
10631 : GetDefaultColLabelSize();
10634 // leave some space around text (taken from AutoSizeColOrRow)
10643 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10645 int width
= m_rowLabelWidth
;
10647 wxGridUpdateLocker locker
;
10649 locker
.Create(this);
10651 for ( int col
= 0; col
< m_numCols
; col
++ )
10654 AutoSizeColumn(col
, setAsMin
);
10656 width
+= GetColWidth(col
);
10662 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10664 int height
= m_colLabelHeight
;
10666 wxGridUpdateLocker locker
;
10668 locker
.Create(this);
10670 for ( int row
= 0; row
< m_numRows
; row
++ )
10673 AutoSizeRow(row
, setAsMin
);
10675 height
+= GetRowHeight(row
);
10681 void wxGrid::AutoSize()
10683 wxGridUpdateLocker
locker(this);
10685 // we need to round up the size of the scrollable area to a multiple of
10686 // scroll step to ensure that we don't get the scrollbars when we're sized
10687 // exactly to fit our contents
10688 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
10689 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
10690 wxSize
sizeFit(GetScrollX(size
.x
) * GetScrollLineX(),
10691 GetScrollY(size
.y
) * GetScrollLineY());
10693 // distribute the extra space between the columns/rows to avoid having
10694 // extra white space
10695 wxCoord diff
= sizeFit
.x
- size
.x
;
10696 if ( diff
&& m_numCols
)
10698 // try to resize the columns uniformly
10699 wxCoord diffPerCol
= diff
/ m_numCols
;
10702 for ( int col
= 0; col
< m_numCols
; col
++ )
10704 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
10708 // add remaining amount to the last columns
10709 diff
-= diffPerCol
* m_numCols
;
10712 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
10714 SetColSize(col
, GetColWidth(col
) + 1);
10720 diff
= sizeFit
.y
- size
.y
;
10721 if ( diff
&& m_numRows
)
10723 // try to resize the columns uniformly
10724 wxCoord diffPerRow
= diff
/ m_numRows
;
10727 for ( int row
= 0; row
< m_numRows
; row
++ )
10729 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
10733 // add remaining amount to the last rows
10734 diff
-= diffPerRow
* m_numRows
;
10737 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
10739 SetRowSize(row
, GetRowHeight(row
) + 1);
10744 // we know that we're not going to have scrollbars so disable them now to
10745 // avoid trouble in SetClientSize() which can otherwise set the correct
10746 // client size but also leave space for (not needed any more) scrollbars
10747 SetScrollbars(0, 0, 0, 0, 0, 0, true);
10748 SetClientSize(sizeFit
.x
+ m_rowLabelWidth
, sizeFit
.y
+ m_colLabelHeight
);
10751 void wxGrid::AutoSizeRowLabelSize( int row
)
10753 wxArrayString lines
;
10756 // Hide the edit control, so it
10757 // won't interfere with drag-shrinking.
10758 if ( IsCellEditControlShown() )
10760 HideCellEditControl();
10761 SaveEditControlValue();
10764 // autosize row height depending on label text
10765 StringToLines( GetRowLabelValue( row
), lines
);
10766 wxClientDC
dc( m_rowLabelWin
);
10767 GetTextBoxSize( dc
, lines
, &w
, &h
);
10768 if ( h
< m_defaultRowHeight
)
10769 h
= m_defaultRowHeight
;
10770 SetRowSize(row
, h
);
10774 void wxGrid::AutoSizeColLabelSize( int col
)
10776 wxArrayString lines
;
10779 // Hide the edit control, so it
10780 // won't interfere with drag-shrinking.
10781 if ( IsCellEditControlShown() )
10783 HideCellEditControl();
10784 SaveEditControlValue();
10787 // autosize column width depending on label text
10788 StringToLines( GetColLabelValue( col
), lines
);
10789 wxClientDC
dc( m_colLabelWin
);
10790 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10791 GetTextBoxSize( dc
, lines
, &w
, &h
);
10793 GetTextBoxSize( dc
, lines
, &h
, &w
);
10794 if ( w
< m_defaultColWidth
)
10795 w
= m_defaultColWidth
;
10796 SetColSize(col
, w
);
10800 wxSize
wxGrid::DoGetBestSize() const
10802 wxGrid
*self
= (wxGrid
*)this; // const_cast
10804 // we do the same as in AutoSize() here with the exception that we don't
10805 // change the column/row sizes, only calculate them
10806 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
10807 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
10808 wxSize
sizeFit(GetScrollX(size
.x
) * GetScrollLineX(),
10809 GetScrollY(size
.y
) * GetScrollLineY());
10811 // NOTE: This size should be cached, but first we need to add calls to
10812 // InvalidateBestSize everywhere that could change the results of this
10814 // CacheBestSize(size);
10816 return wxSize(sizeFit
.x
+ m_rowLabelWidth
, sizeFit
.y
+ m_colLabelHeight
)
10817 + GetWindowBorderSize();
10825 wxPen
& wxGrid::GetDividerPen() const
10830 // ----------------------------------------------------------------------------
10831 // cell value accessor functions
10832 // ----------------------------------------------------------------------------
10834 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10838 m_table
->SetValue( row
, col
, s
);
10839 if ( !GetBatchCount() )
10842 wxRect
rect( CellToRect( row
, col
) );
10844 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10845 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10846 m_gridWin
->Refresh( false, &rect
);
10849 if ( m_currentCellCoords
.GetRow() == row
&&
10850 m_currentCellCoords
.GetCol() == col
&&
10851 IsCellEditControlShown())
10852 // Note: If we are using IsCellEditControlEnabled,
10853 // this interacts badly with calling SetCellValue from
10854 // an EVT_GRID_CELL_CHANGE handler.
10856 HideCellEditControl();
10857 ShowCellEditControl(); // will reread data from table
10862 // ----------------------------------------------------------------------------
10863 // block, row and column selection
10864 // ----------------------------------------------------------------------------
10866 void wxGrid::SelectRow( int row
, bool addToSelected
)
10868 if ( IsSelection() && !addToSelected
)
10872 m_selection
->SelectRow( row
, false, addToSelected
);
10875 void wxGrid::SelectCol( int col
, bool addToSelected
)
10877 if ( IsSelection() && !addToSelected
)
10881 m_selection
->SelectCol( col
, false, addToSelected
);
10884 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10885 bool addToSelected
)
10887 if ( IsSelection() && !addToSelected
)
10891 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10892 false, addToSelected
);
10895 void wxGrid::SelectAll()
10897 if ( m_numRows
> 0 && m_numCols
> 0 )
10900 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10904 // ----------------------------------------------------------------------------
10905 // cell, row and col deselection
10906 // ----------------------------------------------------------------------------
10908 void wxGrid::DeselectRow( int row
)
10910 if ( !m_selection
)
10913 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10915 if ( m_selection
->IsInSelection(row
, 0 ) )
10916 m_selection
->ToggleCellSelection(row
, 0);
10920 int nCols
= GetNumberCols();
10921 for ( int i
= 0; i
< nCols
; i
++ )
10923 if ( m_selection
->IsInSelection(row
, i
) )
10924 m_selection
->ToggleCellSelection(row
, i
);
10929 void wxGrid::DeselectCol( int col
)
10931 if ( !m_selection
)
10934 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10936 if ( m_selection
->IsInSelection(0, col
) )
10937 m_selection
->ToggleCellSelection(0, col
);
10941 int nRows
= GetNumberRows();
10942 for ( int i
= 0; i
< nRows
; i
++ )
10944 if ( m_selection
->IsInSelection(i
, col
) )
10945 m_selection
->ToggleCellSelection(i
, col
);
10950 void wxGrid::DeselectCell( int row
, int col
)
10952 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10953 m_selection
->ToggleCellSelection(row
, col
);
10956 bool wxGrid::IsSelection() const
10958 return ( m_selection
&& (m_selection
->IsSelection() ||
10959 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10960 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10963 bool wxGrid::IsInSelection( int row
, int col
) const
10965 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10966 ( row
>= m_selectingTopLeft
.GetRow() &&
10967 col
>= m_selectingTopLeft
.GetCol() &&
10968 row
<= m_selectingBottomRight
.GetRow() &&
10969 col
<= m_selectingBottomRight
.GetCol() )) );
10972 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10976 wxGridCellCoordsArray a
;
10980 return m_selection
->m_cellSelection
;
10983 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10987 wxGridCellCoordsArray a
;
10991 return m_selection
->m_blockSelectionTopLeft
;
10994 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10998 wxGridCellCoordsArray a
;
11002 return m_selection
->m_blockSelectionBottomRight
;
11005 wxArrayInt
wxGrid::GetSelectedRows() const
11013 return m_selection
->m_rowSelection
;
11016 wxArrayInt
wxGrid::GetSelectedCols() const
11024 return m_selection
->m_colSelection
;
11027 void wxGrid::ClearSelection()
11029 m_selectingTopLeft
=
11030 m_selectingBottomRight
=
11031 m_selectingKeyboard
= wxGridNoCellCoords
;
11033 m_selection
->ClearSelection();
11036 // This function returns the rectangle that encloses the given block
11037 // in device coords clipped to the client size of the grid window.
11039 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
11040 const wxGridCellCoords
&bottomRight
) const
11042 wxRect
rect( wxGridNoCellRect
);
11045 cellRect
= CellToRect( topLeft
);
11046 if ( cellRect
!= wxGridNoCellRect
)
11052 rect
= wxRect(0, 0, 0, 0);
11055 cellRect
= CellToRect( bottomRight
);
11056 if ( cellRect
!= wxGridNoCellRect
)
11062 return wxGridNoCellRect
;
11066 int left
= rect
.GetLeft();
11067 int top
= rect
.GetTop();
11068 int right
= rect
.GetRight();
11069 int bottom
= rect
.GetBottom();
11071 int leftCol
= topLeft
.GetCol();
11072 int topRow
= topLeft
.GetRow();
11073 int rightCol
= bottomRight
.GetCol();
11074 int bottomRow
= bottomRight
.GetRow();
11082 leftCol
= rightCol
;
11092 topRow
= bottomRow
;
11096 for ( j
= topRow
; j
<= bottomRow
; j
++ )
11098 for ( i
= leftCol
; i
<= rightCol
; i
++ )
11100 if ((j
== topRow
) || (j
== bottomRow
) || (i
== leftCol
) || (i
== rightCol
))
11102 cellRect
= CellToRect( j
, i
);
11104 if (cellRect
.x
< left
)
11106 if (cellRect
.y
< top
)
11108 if (cellRect
.x
+ cellRect
.width
> right
)
11109 right
= cellRect
.x
+ cellRect
.width
;
11110 if (cellRect
.y
+ cellRect
.height
> bottom
)
11111 bottom
= cellRect
.y
+ cellRect
.height
;
11115 i
= rightCol
; // jump over inner cells.
11120 // convert to scrolled coords
11122 CalcScrolledPosition( left
, top
, &left
, &top
);
11123 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
11126 m_gridWin
->GetClientSize( &cw
, &ch
);
11128 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
11129 return wxRect(0,0,0,0);
11131 rect
.SetLeft( wxMax(0, left
) );
11132 rect
.SetTop( wxMax(0, top
) );
11133 rect
.SetRight( wxMin(cw
, right
) );
11134 rect
.SetBottom( wxMin(ch
, bottom
) );
11139 // ----------------------------------------------------------------------------
11140 // grid event classes
11141 // ----------------------------------------------------------------------------
11143 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
11145 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
11146 int row
, int col
, int x
, int y
, bool sel
,
11147 bool control
, bool shift
, bool alt
, bool meta
)
11148 : wxNotifyEvent( type
, id
)
11155 m_control
= control
;
11160 SetEventObject(obj
);
11164 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
11166 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
11167 int rowOrCol
, int x
, int y
,
11168 bool control
, bool shift
, bool alt
, bool meta
)
11169 : wxNotifyEvent( type
, id
)
11171 m_rowOrCol
= rowOrCol
;
11174 m_control
= control
;
11179 SetEventObject(obj
);
11183 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
11185 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
11186 const wxGridCellCoords
& topLeft
,
11187 const wxGridCellCoords
& bottomRight
,
11188 bool sel
, bool control
,
11189 bool shift
, bool alt
, bool meta
)
11190 : wxNotifyEvent( type
, id
)
11192 m_topLeft
= topLeft
;
11193 m_bottomRight
= bottomRight
;
11195 m_control
= control
;
11200 SetEventObject(obj
);
11204 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
11206 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
11207 wxObject
* obj
, int row
,
11208 int col
, wxControl
* ctrl
)
11209 : wxCommandEvent(type
, id
)
11211 SetEventObject(obj
);
11217 #endif // wxUSE_GRID