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 #if defined(__WXMSW__)
654 style
|= wxTE_PROCESS_ENTER
|
660 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
661 wxDefaultPosition
, wxDefaultSize
,
664 // set max length allowed in the textctrl, if the parameter was set
665 if ( m_maxChars
!= 0 )
667 Text()->SetMaxLength(m_maxChars
);
670 wxGridCellEditor::Create(parent
, id
, evtHandler
);
673 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
674 wxGridCellAttr
* WXUNUSED(attr
))
676 // as we fill the entire client area,
677 // don't do anything here to minimize flicker
680 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
682 wxRect
rect(rectOrig
);
684 // Make the edit control large enough to allow for internal margins
686 // TODO: remove this if the text ctrl sizing is improved esp. for unix
688 #if defined(__WXGTK__)
696 #elif defined(__WXMSW__)
710 int extra_x
= ( rect
.x
> 2 ) ? 2 : 1;
711 int extra_y
= ( rect
.y
> 2 ) ? 2 : 1;
713 #if defined(__WXMOTIF__)
718 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
719 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
720 rect
.SetRight( rect
.GetRight() + 2 * extra_x
);
721 rect
.SetBottom( rect
.GetBottom() + 2 * extra_y
);
724 wxGridCellEditor::SetSize(rect
);
727 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
729 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
731 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
733 DoBeginEdit(m_startValue
);
736 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
738 Text()->SetValue(startValue
);
739 Text()->SetInsertionPointEnd();
740 Text()->SetSelection(-1, -1);
744 bool wxGridCellTextEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
746 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
748 bool changed
= false;
749 wxString value
= Text()->GetValue();
750 if (value
!= m_startValue
)
754 grid
->GetTable()->SetValue(row
, col
, value
);
756 m_startValue
= wxEmptyString
;
758 // No point in setting the text of the hidden control
759 //Text()->SetValue(m_startValue);
764 void wxGridCellTextEditor::Reset()
766 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
768 DoReset(m_startValue
);
771 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
773 Text()->SetValue(startValue
);
774 Text()->SetInsertionPointEnd();
777 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
779 return wxGridCellEditor::IsAcceptedKey(event
);
782 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
784 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
785 // longer an appropriate way to get the character into the text control.
786 // Do it ourselves instead. We know that if we get this far that we have
787 // a valid character, so not a whole lot of testing needs to be done.
789 wxTextCtrl
* tc
= Text();
794 ch
= event
.GetUnicodeKey();
796 ch
= (wxChar
)event
.GetKeyCode();
798 ch
= (wxChar
)event
.GetKeyCode();
804 // delete the character at the cursor
805 pos
= tc
->GetInsertionPoint();
806 if (pos
< tc
->GetLastPosition())
807 tc
->Remove(pos
, pos
+ 1);
811 // delete the character before the cursor
812 pos
= tc
->GetInsertionPoint();
814 tc
->Remove(pos
- 1, pos
);
823 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
824 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
826 #if defined(__WXMOTIF__) || defined(__WXGTK__)
827 // wxMotif needs a little extra help...
828 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
829 wxString
s( Text()->GetValue() );
830 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
832 Text()->SetInsertionPoint( pos
);
834 // the other ports can handle a Return key press
840 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
850 if ( params
.ToLong(&tmp
) )
852 m_maxChars
= (size_t)tmp
;
856 wxLogDebug( _T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str() );
861 // return the value in the text control
862 wxString
wxGridCellTextEditor::GetValue() const
864 return Text()->GetValue();
867 // ----------------------------------------------------------------------------
868 // wxGridCellNumberEditor
869 // ----------------------------------------------------------------------------
871 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
877 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
879 wxEvtHandler
* evtHandler
)
884 // create a spin ctrl
885 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
886 wxDefaultPosition
, wxDefaultSize
,
890 wxGridCellEditor::Create(parent
, id
, evtHandler
);
895 // just a text control
896 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
899 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
904 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
906 // first get the value
907 wxGridTableBase
*table
= grid
->GetTable();
908 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
910 m_valueOld
= table
->GetValueAsLong(row
, col
);
915 wxString sValue
= table
->GetValue(row
, col
);
916 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty())
918 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
926 Spin()->SetValue((int)m_valueOld
);
932 DoBeginEdit(GetString());
936 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
946 value
= Spin()->GetValue();
947 changed
= value
!= m_valueOld
;
949 text
= wxString::Format(wxT("%ld"), value
);
954 text
= Text()->GetValue();
955 changed
= (text
.empty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
960 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
961 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
963 grid
->GetTable()->SetValue(row
, col
, text
);
969 void wxGridCellNumberEditor::Reset()
974 Spin()->SetValue((int)m_valueOld
);
979 DoReset(GetString());
983 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
985 if ( wxGridCellEditor::IsAcceptedKey(event
) )
987 int keycode
= event
.GetKeyCode();
988 if ( (keycode
< 128) &&
989 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
998 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
1000 int keycode
= event
.GetKeyCode();
1003 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
1005 wxGridCellTextEditor::StartingKey(event
);
1007 // skip Skip() below
1014 if ( wxIsdigit(keycode
) )
1016 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
1017 spin
->SetValue(keycode
- '0');
1018 spin
->SetSelection(1,1);
1027 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
1038 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1042 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1046 // skip the error message below
1051 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1055 // return the value in the spin control if it is there (the text control otherwise)
1056 wxString
wxGridCellNumberEditor::GetValue() const
1063 long value
= Spin()->GetValue();
1064 s
.Printf(wxT("%ld"), value
);
1069 s
= Text()->GetValue();
1075 // ----------------------------------------------------------------------------
1076 // wxGridCellFloatEditor
1077 // ----------------------------------------------------------------------------
1079 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1082 m_precision
= precision
;
1085 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1087 wxEvtHandler
* evtHandler
)
1089 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1091 #if wxUSE_VALIDATORS
1092 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1096 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1098 // first get the value
1099 wxGridTableBase
*table
= grid
->GetTable();
1100 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1102 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1107 wxString sValue
= table
->GetValue(row
, col
);
1108 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.empty())
1110 wxFAIL_MSG( _T("this cell doesn't have float value") );
1115 DoBeginEdit(GetString());
1118 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1122 wxString
text(Text()->GetValue());
1124 if ( (text
.empty() || text
.ToDouble(&value
)) &&
1125 !wxIsSameDouble(value
, m_valueOld
) )
1127 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1128 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1130 grid
->GetTable()->SetValue(row
, col
, text
);
1138 void wxGridCellFloatEditor::Reset()
1140 DoReset(GetString());
1143 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1145 int keycode
= event
.GetKeyCode();
1147 tmpbuf
[0] = (char) keycode
;
1149 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1152 bool is_decimal_point
= ( strbuf
==
1153 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1155 bool is_decimal_point
= ( strbuf
== _T(".") );
1158 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1159 || is_decimal_point
)
1161 wxGridCellTextEditor::StartingKey(event
);
1163 // skip Skip() below
1170 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1181 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1185 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1187 m_precision
= (int)tmp
;
1189 // skip the error message below
1194 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1198 wxString
wxGridCellFloatEditor::GetString() const
1201 if ( m_precision
== -1 && m_width
!= -1)
1203 // default precision
1204 fmt
.Printf(_T("%%%d.f"), m_width
);
1206 else if ( m_precision
!= -1 && m_width
== -1)
1209 fmt
.Printf(_T("%%.%df"), m_precision
);
1211 else if ( m_precision
!= -1 && m_width
!= -1 )
1213 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1217 // default width/precision
1221 return wxString::Format(fmt
, m_valueOld
);
1224 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1226 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1228 const int keycode
= event
.GetKeyCode();
1229 if ( isascii(keycode
) )
1232 tmpbuf
[0] = (char) keycode
;
1234 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1237 const wxString decimalPoint
=
1238 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
1240 const wxString
decimalPoint(_T('.'));
1243 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1244 if ( wxIsdigit(keycode
) ||
1245 tolower(keycode
) == 'e' ||
1246 keycode
== decimalPoint
||
1258 #endif // wxUSE_TEXTCTRL
1262 // ----------------------------------------------------------------------------
1263 // wxGridCellBoolEditor
1264 // ----------------------------------------------------------------------------
1266 // the default values for GetValue()
1267 wxString
wxGridCellBoolEditor::ms_stringValues
[2] = { _T(""), _T("1") };
1269 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1271 wxEvtHandler
* evtHandler
)
1273 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1274 wxDefaultPosition
, wxDefaultSize
,
1277 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1280 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1282 bool resize
= false;
1283 wxSize size
= m_control
->GetSize();
1284 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1286 // check if the checkbox is not too big/small for this cell
1287 wxSize sizeBest
= m_control
->GetBestSize();
1288 if ( !(size
== sizeBest
) )
1290 // reset to default size if it had been made smaller
1296 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1298 // leave 1 pixel margin
1299 size
.x
= size
.y
= minSize
- 2;
1306 m_control
->SetSize(size
);
1309 // position it in the centre of the rectangle (TODO: support alignment?)
1311 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1312 // the checkbox without label still has some space to the right in wxGTK,
1313 // so shift it to the right
1315 #elif defined(__WXMSW__)
1316 // here too, but in other way
1321 int hAlign
= wxALIGN_CENTRE
;
1322 int vAlign
= wxALIGN_CENTRE
;
1324 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1327 if (hAlign
== wxALIGN_LEFT
)
1335 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1337 else if (hAlign
== wxALIGN_RIGHT
)
1339 x
= r
.x
+ r
.width
- size
.x
- 2;
1340 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1342 else if (hAlign
== wxALIGN_CENTRE
)
1344 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1345 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1348 m_control
->Move(x
, y
);
1351 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1353 m_control
->Show(show
);
1357 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1358 CBox()->SetBackgroundColour(colBg
);
1362 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1364 wxASSERT_MSG(m_control
,
1365 wxT("The wxGridCellEditor must be created first!"));
1367 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1369 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1373 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1375 if ( cellval
== ms_stringValues
[false] )
1376 m_startValue
= false;
1377 else if ( cellval
== ms_stringValues
[true] )
1378 m_startValue
= true;
1381 // do not try to be smart here and convert it to true or false
1382 // because we'll still overwrite it with something different and
1383 // this risks to be very surprising for the user code, let them
1385 wxFAIL_MSG( _T("invalid value for a cell with bool editor!") );
1389 CBox()->SetValue(m_startValue
);
1393 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1396 wxASSERT_MSG(m_control
,
1397 wxT("The wxGridCellEditor must be created first!"));
1399 bool changed
= false;
1400 bool value
= CBox()->GetValue();
1401 if ( value
!= m_startValue
)
1406 wxGridTableBase
* const table
= grid
->GetTable();
1407 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1408 table
->SetValueAsBool(row
, col
, value
);
1410 table
->SetValue(row
, col
, GetValue());
1416 void wxGridCellBoolEditor::Reset()
1418 wxASSERT_MSG(m_control
,
1419 wxT("The wxGridCellEditor must be created first!"));
1421 CBox()->SetValue(m_startValue
);
1424 void wxGridCellBoolEditor::StartingClick()
1426 CBox()->SetValue(!CBox()->GetValue());
1429 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1431 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1433 int keycode
= event
.GetKeyCode();
1446 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1448 int keycode
= event
.GetKeyCode();
1452 CBox()->SetValue(!CBox()->GetValue());
1456 CBox()->SetValue(true);
1460 CBox()->SetValue(false);
1465 wxString
wxGridCellBoolEditor::GetValue() const
1467 return ms_stringValues
[CBox()->GetValue()];
1471 wxGridCellBoolEditor::UseStringValues(const wxString
& valueTrue
,
1472 const wxString
& valueFalse
)
1474 ms_stringValues
[false] = valueFalse
;
1475 ms_stringValues
[true] = valueTrue
;
1479 wxGridCellBoolEditor::IsTrueValue(const wxString
& value
)
1481 return value
== ms_stringValues
[true];
1484 #endif // wxUSE_CHECKBOX
1488 // ----------------------------------------------------------------------------
1489 // wxGridCellChoiceEditor
1490 // ----------------------------------------------------------------------------
1492 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1494 : m_choices(choices
),
1495 m_allowOthers(allowOthers
) { }
1497 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1498 const wxString choices
[],
1500 : m_allowOthers(allowOthers
)
1504 m_choices
.Alloc(count
);
1505 for ( size_t n
= 0; n
< count
; n
++ )
1507 m_choices
.Add(choices
[n
]);
1512 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1514 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1515 editor
->m_allowOthers
= m_allowOthers
;
1516 editor
->m_choices
= m_choices
;
1521 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1523 wxEvtHandler
* evtHandler
)
1525 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1526 wxDefaultPosition
, wxDefaultSize
,
1528 m_allowOthers
? 0 : wxCB_READONLY
);
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
);
1566 // find the right position, or default to the first if not found
1567 int pos
= Combo()->FindString(m_startValue
);
1568 if (pos
== wxNOT_FOUND
)
1570 Combo()->SetSelection(pos
);
1573 Combo()->SetInsertionPointEnd();
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 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
2210 #if defined(__WXGTK__) || defined(__WXMOTIF__)
2211 checkSize
-= size
.y
/ 2;
2216 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2219 return ms_sizeCheckMark
;
2222 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2223 wxGridCellAttr
& attr
,
2229 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2231 // draw a check mark in the centre (ignoring alignment - TODO)
2232 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2234 // don't draw outside the cell
2235 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2236 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2238 // and even leave (at least) 1 pixel margin
2239 size
.x
= size
.y
= minSize
- 2;
2242 // draw a border around checkmark
2244 attr
.GetAlignment(&hAlign
, &vAlign
);
2247 if (hAlign
== wxALIGN_CENTRE
)
2249 rectBorder
.x
= rect
.x
+ rect
.width
/ 2 - size
.x
/ 2;
2250 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2251 rectBorder
.width
= size
.x
;
2252 rectBorder
.height
= size
.y
;
2254 else if (hAlign
== wxALIGN_LEFT
)
2256 rectBorder
.x
= rect
.x
+ 2;
2257 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2258 rectBorder
.width
= size
.x
;
2259 rectBorder
.height
= size
.y
;
2261 else if (hAlign
== wxALIGN_RIGHT
)
2263 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2264 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2265 rectBorder
.width
= size
.x
;
2266 rectBorder
.height
= size
.y
;
2270 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2272 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2276 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2277 value
= wxGridCellBoolEditor::IsTrueValue(cellval
);
2282 wxRect rectMark
= rectBorder
;
2285 // MSW DrawCheckMark() is weird (and should probably be changed...)
2286 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/ 2);
2290 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2293 dc
.SetTextForeground(attr
.GetTextColour());
2294 dc
.DrawCheckMark(rectMark
);
2297 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2298 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2299 dc
.DrawRectangle(rectBorder
);
2302 // ----------------------------------------------------------------------------
2304 // ----------------------------------------------------------------------------
2306 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2310 m_isReadOnly
= Unset
;
2315 m_attrkind
= wxGridCellAttr::Cell
;
2317 m_sizeRows
= m_sizeCols
= 1;
2318 m_overflow
= UnsetOverflow
;
2320 SetDefAttr(attrDefault
);
2323 wxGridCellAttr
*wxGridCellAttr::Clone() const
2325 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2327 if ( HasTextColour() )
2328 attr
->SetTextColour(GetTextColour());
2329 if ( HasBackgroundColour() )
2330 attr
->SetBackgroundColour(GetBackgroundColour());
2332 attr
->SetFont(GetFont());
2333 if ( HasAlignment() )
2334 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2336 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2340 attr
->SetRenderer(m_renderer
);
2341 m_renderer
->IncRef();
2345 attr
->SetEditor(m_editor
);
2350 attr
->SetReadOnly();
2352 attr
->SetOverflow( m_overflow
== Overflow
);
2353 attr
->SetKind( m_attrkind
);
2358 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2360 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2361 SetTextColour(mergefrom
->GetTextColour());
2362 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2363 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2364 if ( !HasFont() && mergefrom
->HasFont() )
2365 SetFont(mergefrom
->GetFont());
2366 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2369 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2370 SetAlignment(hAlign
, vAlign
);
2372 if ( !HasSize() && mergefrom
->HasSize() )
2373 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2375 // Directly access member functions as GetRender/Editor don't just return
2376 // m_renderer/m_editor
2378 // Maybe add support for merge of Render and Editor?
2379 if (!HasRenderer() && mergefrom
->HasRenderer() )
2381 m_renderer
= mergefrom
->m_renderer
;
2382 m_renderer
->IncRef();
2384 if ( !HasEditor() && mergefrom
->HasEditor() )
2386 m_editor
= mergefrom
->m_editor
;
2389 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2390 SetReadOnly(mergefrom
->IsReadOnly());
2392 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2393 SetOverflow(mergefrom
->GetOverflow());
2395 SetDefAttr(mergefrom
->m_defGridAttr
);
2398 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2400 // The size of a cell is normally 1,1
2402 // If this cell is larger (2,2) then this is the top left cell
2403 // the other cells that will be covered (lower right cells) must be
2404 // set to negative or zero values such that
2405 // row + num_rows of the covered cell points to the larger cell (this cell)
2406 // same goes for the col + num_cols.
2408 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2410 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
2411 !((num_rows
<= 0) && (num_cols
> 0)) ||
2412 !((num_rows
== 0) && (num_cols
== 0))),
2413 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2415 m_sizeRows
= num_rows
;
2416 m_sizeCols
= num_cols
;
2419 const wxColour
& wxGridCellAttr::GetTextColour() const
2421 if (HasTextColour())
2425 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2427 return m_defGridAttr
->GetTextColour();
2431 wxFAIL_MSG(wxT("Missing default cell attribute"));
2432 return wxNullColour
;
2436 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2438 if (HasBackgroundColour())
2442 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2444 return m_defGridAttr
->GetBackgroundColour();
2448 wxFAIL_MSG(wxT("Missing default cell attribute"));
2449 return wxNullColour
;
2453 const wxFont
& wxGridCellAttr::GetFont() const
2459 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2461 return m_defGridAttr
->GetFont();
2465 wxFAIL_MSG(wxT("Missing default cell attribute"));
2470 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2479 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2481 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2485 wxFAIL_MSG(wxT("Missing default cell attribute"));
2489 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2492 *num_rows
= m_sizeRows
;
2494 *num_cols
= m_sizeCols
;
2497 // GetRenderer and GetEditor use a slightly different decision path about
2498 // which attribute to use. If a non-default attr object has one then it is
2499 // used, otherwise the default editor or renderer is fetched from the grid and
2500 // used. It should be the default for the data type of the cell. If it is
2501 // NULL (because the table has a type that the grid does not have in its
2502 // registry), then the grid's default editor or renderer is used.
2504 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(const wxGrid
* grid
, int row
, int col
) const
2506 wxGridCellRenderer
*renderer
= NULL
;
2508 if ( m_renderer
&& this != m_defGridAttr
)
2510 // use the cells renderer if it has one
2511 renderer
= m_renderer
;
2514 else // no non-default cell renderer
2516 // get default renderer for the data type
2519 // GetDefaultRendererForCell() will do IncRef() for us
2520 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2523 if ( renderer
== NULL
)
2525 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2527 // if we still don't have one then use the grid default
2528 // (no need for IncRef() here neither)
2529 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2531 else // default grid attr
2533 // use m_renderer which we had decided not to use initially
2534 renderer
= m_renderer
;
2541 // we're supposed to always find something
2542 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2547 // same as above, except for s/renderer/editor/g
2548 wxGridCellEditor
* wxGridCellAttr::GetEditor(const wxGrid
* grid
, int row
, int col
) const
2550 wxGridCellEditor
*editor
= NULL
;
2552 if ( m_editor
&& this != m_defGridAttr
)
2554 // use the cells editor if it has one
2558 else // no non default cell editor
2560 // get default editor for the data type
2563 // GetDefaultEditorForCell() will do IncRef() for us
2564 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2567 if ( editor
== NULL
)
2569 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2571 // if we still don't have one then use the grid default
2572 // (no need for IncRef() here neither)
2573 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2575 else // default grid attr
2577 // use m_editor which we had decided not to use initially
2585 // we're supposed to always find something
2586 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2591 // ----------------------------------------------------------------------------
2592 // wxGridCellAttrData
2593 // ----------------------------------------------------------------------------
2595 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2597 int n
= FindIndex(row
, col
);
2598 if ( n
== wxNOT_FOUND
)
2600 // add the attribute
2601 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2605 // free the old attribute
2606 m_attrs
[(size_t)n
].attr
->DecRef();
2610 // change the attribute
2611 m_attrs
[(size_t)n
].attr
= attr
;
2615 // remove this attribute
2616 m_attrs
.RemoveAt((size_t)n
);
2621 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2623 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2625 int n
= FindIndex(row
, col
);
2626 if ( n
!= wxNOT_FOUND
)
2628 attr
= m_attrs
[(size_t)n
].attr
;
2635 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2637 size_t count
= m_attrs
.GetCount();
2638 for ( size_t n
= 0; n
< count
; n
++ )
2640 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2641 wxCoord row
= coords
.GetRow();
2642 if ((size_t)row
>= pos
)
2646 // If rows inserted, include row counter where necessary
2647 coords
.SetRow(row
+ numRows
);
2649 else if (numRows
< 0)
2651 // If rows deleted ...
2652 if ((size_t)row
>= pos
- numRows
)
2654 // ...either decrement row counter (if row still exists)...
2655 coords
.SetRow(row
+ numRows
);
2659 // ...or remove the attribute
2660 // No need to DecRef the attribute itself since this is
2661 // done be wxGridCellWithAttr's destructor!
2662 m_attrs
.RemoveAt(n
);
2671 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2673 size_t count
= m_attrs
.GetCount();
2674 for ( size_t n
= 0; n
< count
; n
++ )
2676 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2677 wxCoord col
= coords
.GetCol();
2678 if ( (size_t)col
>= pos
)
2682 // If rows inserted, include row counter where necessary
2683 coords
.SetCol(col
+ numCols
);
2685 else if (numCols
< 0)
2687 // If rows deleted ...
2688 if ((size_t)col
>= pos
- numCols
)
2690 // ...either decrement row counter (if row still exists)...
2691 coords
.SetCol(col
+ numCols
);
2695 // ...or remove the attribute
2696 // No need to DecRef the attribute itself since this is
2697 // done be wxGridCellWithAttr's destructor!
2698 m_attrs
.RemoveAt(n
);
2707 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2709 size_t count
= m_attrs
.GetCount();
2710 for ( size_t n
= 0; n
< count
; n
++ )
2712 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2713 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2722 // ----------------------------------------------------------------------------
2723 // wxGridRowOrColAttrData
2724 // ----------------------------------------------------------------------------
2726 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2728 size_t count
= m_attrs
.GetCount();
2729 for ( size_t n
= 0; n
< count
; n
++ )
2731 m_attrs
[n
]->DecRef();
2735 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2737 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2739 int n
= m_rowsOrCols
.Index(rowOrCol
);
2740 if ( n
!= wxNOT_FOUND
)
2742 attr
= m_attrs
[(size_t)n
];
2749 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2751 int i
= m_rowsOrCols
.Index(rowOrCol
);
2752 if ( i
== wxNOT_FOUND
)
2754 // add the attribute
2755 m_rowsOrCols
.Add(rowOrCol
);
2760 size_t n
= (size_t)i
;
2763 // change the attribute
2764 m_attrs
[n
]->DecRef();
2769 // remove this attribute
2770 m_attrs
[n
]->DecRef();
2771 m_rowsOrCols
.RemoveAt(n
);
2772 m_attrs
.RemoveAt(n
);
2777 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2779 size_t count
= m_attrs
.GetCount();
2780 for ( size_t n
= 0; n
< count
; n
++ )
2782 int & rowOrCol
= m_rowsOrCols
[n
];
2783 if ( (size_t)rowOrCol
>= pos
)
2785 if ( numRowsOrCols
> 0 )
2787 // If rows inserted, include row counter where necessary
2788 rowOrCol
+= numRowsOrCols
;
2790 else if ( numRowsOrCols
< 0)
2792 // If rows deleted, either decrement row counter (if row still exists)
2793 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2794 rowOrCol
+= numRowsOrCols
;
2797 m_rowsOrCols
.RemoveAt(n
);
2798 m_attrs
[n
]->DecRef();
2799 m_attrs
.RemoveAt(n
);
2808 // ----------------------------------------------------------------------------
2809 // wxGridCellAttrProvider
2810 // ----------------------------------------------------------------------------
2812 wxGridCellAttrProvider::wxGridCellAttrProvider()
2814 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2817 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2822 void wxGridCellAttrProvider::InitData()
2824 m_data
= new wxGridCellAttrProviderData
;
2827 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2828 wxGridCellAttr::wxAttrKind kind
) const
2830 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2835 case (wxGridCellAttr::Any
):
2836 // Get cached merge attributes.
2837 // Currently not used as no cache implemented as not mutable
2838 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2841 // Basically implement old version.
2842 // Also check merge cache, so we don't have to re-merge every time..
2843 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2844 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2845 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2847 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
2849 // Two or more are non NULL
2850 attr
= new wxGridCellAttr
;
2851 attr
->SetKind(wxGridCellAttr::Merged
);
2853 // Order is important..
2856 attr
->MergeWith(attrcell
);
2861 attr
->MergeWith(attrcol
);
2866 attr
->MergeWith(attrrow
);
2870 // store merge attr if cache implemented
2872 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2876 // one or none is non null return it or null.
2895 case (wxGridCellAttr::Cell
):
2896 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2899 case (wxGridCellAttr::Col
):
2900 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2903 case (wxGridCellAttr::Row
):
2904 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2909 // (wxGridCellAttr::Default):
2910 // (wxGridCellAttr::Merged):
2918 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2924 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2927 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2932 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2935 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2940 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2943 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2947 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2949 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2953 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2957 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2959 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2963 // ----------------------------------------------------------------------------
2964 // wxGridTypeRegistry
2965 // ----------------------------------------------------------------------------
2967 wxGridTypeRegistry::~wxGridTypeRegistry()
2969 size_t count
= m_typeinfo
.GetCount();
2970 for ( size_t i
= 0; i
< count
; i
++ )
2971 delete m_typeinfo
[i
];
2974 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2975 wxGridCellRenderer
* renderer
,
2976 wxGridCellEditor
* editor
)
2978 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2980 // is it already registered?
2981 int loc
= FindRegisteredDataType(typeName
);
2982 if ( loc
!= wxNOT_FOUND
)
2984 delete m_typeinfo
[loc
];
2985 m_typeinfo
[loc
] = info
;
2989 m_typeinfo
.Add(info
);
2993 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2995 size_t count
= m_typeinfo
.GetCount();
2996 for ( size_t i
= 0; i
< count
; i
++ )
2998 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
3007 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
3009 int index
= FindRegisteredDataType(typeName
);
3010 if ( index
== wxNOT_FOUND
)
3012 // check whether this is one of the standard ones, in which case
3013 // register it "on the fly"
3015 if ( typeName
== wxGRID_VALUE_STRING
)
3017 RegisterDataType(wxGRID_VALUE_STRING
,
3018 new wxGridCellStringRenderer
,
3019 new wxGridCellTextEditor
);
3022 #endif // wxUSE_TEXTCTRL
3024 if ( typeName
== wxGRID_VALUE_BOOL
)
3026 RegisterDataType(wxGRID_VALUE_BOOL
,
3027 new wxGridCellBoolRenderer
,
3028 new wxGridCellBoolEditor
);
3031 #endif // wxUSE_CHECKBOX
3033 if ( typeName
== wxGRID_VALUE_NUMBER
)
3035 RegisterDataType(wxGRID_VALUE_NUMBER
,
3036 new wxGridCellNumberRenderer
,
3037 new wxGridCellNumberEditor
);
3039 else if ( typeName
== wxGRID_VALUE_FLOAT
)
3041 RegisterDataType(wxGRID_VALUE_FLOAT
,
3042 new wxGridCellFloatRenderer
,
3043 new wxGridCellFloatEditor
);
3046 #endif // wxUSE_TEXTCTRL
3048 if ( typeName
== wxGRID_VALUE_CHOICE
)
3050 RegisterDataType(wxGRID_VALUE_CHOICE
,
3051 new wxGridCellStringRenderer
,
3052 new wxGridCellChoiceEditor
);
3055 #endif // wxUSE_COMBOBOX
3060 // we get here only if just added the entry for this type, so return
3062 index
= m_typeinfo
.GetCount() - 1;
3068 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
3070 int index
= FindDataType(typeName
);
3071 if ( index
== wxNOT_FOUND
)
3073 // the first part of the typename is the "real" type, anything after ':'
3074 // are the parameters for the renderer
3075 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
3076 if ( index
== wxNOT_FOUND
)
3081 wxGridCellRenderer
*renderer
= GetRenderer(index
);
3082 wxGridCellRenderer
*rendererOld
= renderer
;
3083 renderer
= renderer
->Clone();
3084 rendererOld
->DecRef();
3086 wxGridCellEditor
*editor
= GetEditor(index
);
3087 wxGridCellEditor
*editorOld
= editor
;
3088 editor
= editor
->Clone();
3089 editorOld
->DecRef();
3091 // do it even if there are no parameters to reset them to defaults
3092 wxString params
= typeName
.AfterFirst(_T(':'));
3093 renderer
->SetParameters(params
);
3094 editor
->SetParameters(params
);
3096 // register the new typename
3097 RegisterDataType(typeName
, renderer
, editor
);
3099 // we just registered it, it's the last one
3100 index
= m_typeinfo
.GetCount() - 1;
3106 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
3108 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
3115 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
3117 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
3124 // ----------------------------------------------------------------------------
3126 // ----------------------------------------------------------------------------
3128 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
3130 wxGridTableBase::wxGridTableBase()
3132 m_view
= (wxGrid
*) NULL
;
3133 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
3136 wxGridTableBase::~wxGridTableBase()
3138 delete m_attrProvider
;
3141 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
3143 delete m_attrProvider
;
3144 m_attrProvider
= attrProvider
;
3147 bool wxGridTableBase::CanHaveAttributes()
3149 if ( ! GetAttrProvider() )
3151 // use the default attr provider by default
3152 SetAttrProvider(new wxGridCellAttrProvider
);
3158 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
3160 if ( m_attrProvider
)
3161 return m_attrProvider
->GetAttr(row
, col
, kind
);
3163 return (wxGridCellAttr
*)NULL
;
3166 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
3168 if ( m_attrProvider
)
3170 attr
->SetKind(wxGridCellAttr::Cell
);
3171 m_attrProvider
->SetAttr(attr
, row
, col
);
3175 // as we take ownership of the pointer and don't store it, we must
3181 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3183 if ( m_attrProvider
)
3185 attr
->SetKind(wxGridCellAttr::Row
);
3186 m_attrProvider
->SetRowAttr(attr
, row
);
3190 // as we take ownership of the pointer and don't store it, we must
3196 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3198 if ( m_attrProvider
)
3200 attr
->SetKind(wxGridCellAttr::Col
);
3201 m_attrProvider
->SetColAttr(attr
, col
);
3205 // as we take ownership of the pointer and don't store it, we must
3211 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3212 size_t WXUNUSED(numRows
) )
3214 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3219 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3221 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3226 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3227 size_t WXUNUSED(numRows
) )
3229 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3234 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3235 size_t WXUNUSED(numCols
) )
3237 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3242 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3244 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3249 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3250 size_t WXUNUSED(numCols
) )
3252 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3257 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3261 // RD: Starting the rows at zero confuses users,
3262 // no matter how much it makes sense to us geeks.
3268 wxString
wxGridTableBase::GetColLabelValue( int col
)
3270 // default col labels are:
3271 // cols 0 to 25 : A-Z
3272 // cols 26 to 675 : AA-ZZ
3277 for ( n
= 1; ; n
++ )
3279 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
3285 // reverse the string...
3287 for ( i
= 0; i
< n
; i
++ )
3295 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3297 return wxGRID_VALUE_STRING
;
3300 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3301 const wxString
& typeName
)
3303 return typeName
== wxGRID_VALUE_STRING
;
3306 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3308 return CanGetValueAs(row
, col
, typeName
);
3311 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3316 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3321 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3326 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3327 long WXUNUSED(value
) )
3331 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3332 double WXUNUSED(value
) )
3336 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3337 bool WXUNUSED(value
) )
3341 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3342 const wxString
& WXUNUSED(typeName
) )
3347 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3348 const wxString
& WXUNUSED(typeName
),
3349 void* WXUNUSED(value
) )
3353 //////////////////////////////////////////////////////////////////////
3355 // Message class for the grid table to send requests and notifications
3359 wxGridTableMessage::wxGridTableMessage()
3361 m_table
= (wxGridTableBase
*) NULL
;
3367 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3368 int commandInt1
, int commandInt2
)
3372 m_comInt1
= commandInt1
;
3373 m_comInt2
= commandInt2
;
3376 //////////////////////////////////////////////////////////////////////
3378 // A basic grid table for string data. An object of this class will
3379 // created by wxGrid if you don't specify an alternative table class.
3382 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3384 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3386 wxGridStringTable::wxGridStringTable()
3391 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3394 m_data
.Alloc( numRows
);
3397 sa
.Alloc( numCols
);
3398 sa
.Add( wxEmptyString
, numCols
);
3400 m_data
.Add( sa
, numRows
);
3403 wxGridStringTable::~wxGridStringTable()
3407 int wxGridStringTable::GetNumberRows()
3409 return m_data
.GetCount();
3412 int wxGridStringTable::GetNumberCols()
3414 if ( m_data
.GetCount() > 0 )
3415 return m_data
[0].GetCount();
3420 wxString
wxGridStringTable::GetValue( int row
, int col
)
3422 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3424 _T("invalid row or column index in wxGridStringTable") );
3426 return m_data
[row
][col
];
3429 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3431 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3432 _T("invalid row or column index in wxGridStringTable") );
3434 m_data
[row
][col
] = value
;
3437 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3439 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3441 _T("invalid row or column index in wxGridStringTable") );
3443 return (m_data
[row
][col
] == wxEmptyString
);
3446 void wxGridStringTable::Clear()
3449 int numRows
, numCols
;
3451 numRows
= m_data
.GetCount();
3454 numCols
= m_data
[0].GetCount();
3456 for ( row
= 0; row
< numRows
; row
++ )
3458 for ( col
= 0; col
< numCols
; col
++ )
3460 m_data
[row
][col
] = wxEmptyString
;
3466 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3468 size_t curNumRows
= m_data
.GetCount();
3469 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3470 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3472 if ( pos
>= curNumRows
)
3474 return AppendRows( numRows
);
3478 sa
.Alloc( curNumCols
);
3479 sa
.Add( wxEmptyString
, curNumCols
);
3480 m_data
.Insert( sa
, pos
, numRows
);
3484 wxGridTableMessage
msg( this,
3485 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3489 GetView()->ProcessTableMessage( msg
);
3495 bool wxGridStringTable::AppendRows( size_t numRows
)
3497 size_t curNumRows
= m_data
.GetCount();
3498 size_t curNumCols
= ( curNumRows
> 0
3499 ? m_data
[0].GetCount()
3500 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3503 if ( curNumCols
> 0 )
3505 sa
.Alloc( curNumCols
);
3506 sa
.Add( wxEmptyString
, curNumCols
);
3509 m_data
.Add( sa
, numRows
);
3513 wxGridTableMessage
msg( this,
3514 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3517 GetView()->ProcessTableMessage( msg
);
3523 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3525 size_t curNumRows
= m_data
.GetCount();
3527 if ( pos
>= curNumRows
)
3529 wxFAIL_MSG( wxString::Format
3531 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3533 (unsigned long)numRows
,
3534 (unsigned long)curNumRows
3540 if ( numRows
> curNumRows
- pos
)
3542 numRows
= curNumRows
- pos
;
3545 if ( numRows
>= curNumRows
)
3551 m_data
.RemoveAt( pos
, numRows
);
3556 wxGridTableMessage
msg( this,
3557 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3561 GetView()->ProcessTableMessage( msg
);
3567 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3571 size_t curNumRows
= m_data
.GetCount();
3572 size_t curNumCols
= ( curNumRows
> 0
3573 ? m_data
[0].GetCount()
3574 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3576 if ( pos
>= curNumCols
)
3578 return AppendCols( numCols
);
3581 if ( !m_colLabels
.IsEmpty() )
3583 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
3586 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
3587 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
3590 for ( row
= 0; row
< curNumRows
; row
++ )
3592 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3594 m_data
[row
].Insert( wxEmptyString
, col
);
3600 wxGridTableMessage
msg( this,
3601 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3605 GetView()->ProcessTableMessage( msg
);
3611 bool wxGridStringTable::AppendCols( size_t numCols
)
3615 size_t curNumRows
= m_data
.GetCount();
3620 // TODO: something better than this ?
3622 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3627 for ( row
= 0; row
< curNumRows
; row
++ )
3629 m_data
[row
].Add( wxEmptyString
, numCols
);
3634 wxGridTableMessage
msg( this,
3635 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3638 GetView()->ProcessTableMessage( msg
);
3644 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3648 size_t curNumRows
= m_data
.GetCount();
3649 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3650 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3652 if ( pos
>= curNumCols
)
3654 wxFAIL_MSG( wxString::Format
3656 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3658 (unsigned long)numCols
,
3659 (unsigned long)curNumCols
3666 colID
= GetView()->GetColAt( pos
);
3670 if ( numCols
> curNumCols
- colID
)
3672 numCols
= curNumCols
- colID
;
3675 if ( !m_colLabels
.IsEmpty() )
3677 // m_colLabels stores just as many elements as it needs, e.g. if only
3678 // the label of the first column had been set it would have only one
3679 // element and not numCols, so account for it
3680 int nToRm
= m_colLabels
.size() - colID
;
3682 m_colLabels
.RemoveAt( colID
, nToRm
);
3685 for ( row
= 0; row
< curNumRows
; row
++ )
3687 if ( numCols
>= curNumCols
)
3689 m_data
[row
].Clear();
3693 m_data
[row
].RemoveAt( colID
, numCols
);
3699 wxGridTableMessage
msg( this,
3700 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3704 GetView()->ProcessTableMessage( msg
);
3710 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3712 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3714 // using default label
3716 return wxGridTableBase::GetRowLabelValue( row
);
3720 return m_rowLabels
[row
];
3724 wxString
wxGridStringTable::GetColLabelValue( int col
)
3726 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3728 // using default label
3730 return wxGridTableBase::GetColLabelValue( col
);
3734 return m_colLabels
[col
];
3738 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3740 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3742 int n
= m_rowLabels
.GetCount();
3745 for ( i
= n
; i
<= row
; i
++ )
3747 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3751 m_rowLabels
[row
] = value
;
3754 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3756 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3758 int n
= m_colLabels
.GetCount();
3761 for ( i
= n
; i
<= col
; i
++ )
3763 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3767 m_colLabels
[col
] = value
;
3771 //////////////////////////////////////////////////////////////////////
3772 //////////////////////////////////////////////////////////////////////
3774 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
)
3775 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost
)
3778 void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
3780 m_owner
->CancelMouseCapture();
3783 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3785 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow
)
3786 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3787 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3788 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3789 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3790 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3791 EVT_CHAR( wxGridRowLabelWindow::OnChar
)
3794 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3796 const wxPoint
&pos
, const wxSize
&size
)
3797 : wxGridSubwindow(parent
, id
, pos
, size
)
3802 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3806 // NO - don't do this because it will set both the x and y origin
3807 // coords to match the parent scrolled window and we just want to
3808 // set the y coord - MB
3810 // m_owner->PrepareDC( dc );
3813 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3814 wxPoint pt
= dc
.GetDeviceOrigin();
3815 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
3817 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3818 m_owner
->DrawRowLabels( dc
, rows
);
3821 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3823 m_owner
->ProcessRowLabelMouseEvent( event
);
3826 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3828 m_owner
->GetEventHandler()->ProcessEvent( event
);
3831 // This seems to be required for wxMotif otherwise the mouse
3832 // cursor must be in the cell edit control to get key events
3834 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3836 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3840 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3842 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3846 void wxGridRowLabelWindow::OnChar( wxKeyEvent
& event
)
3848 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3852 //////////////////////////////////////////////////////////////////////
3854 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3856 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow
)
3857 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3858 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3859 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3860 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3861 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3862 EVT_CHAR( wxGridColLabelWindow::OnChar
)
3865 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3867 const wxPoint
&pos
, const wxSize
&size
)
3868 : wxGridSubwindow(parent
, id
, pos
, size
)
3873 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3877 // NO - don't do this because it will set both the x and y origin
3878 // coords to match the parent scrolled window and we just want to
3879 // set the x coord - MB
3881 // m_owner->PrepareDC( dc );
3884 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3885 wxPoint pt
= dc
.GetDeviceOrigin();
3886 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3887 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
3889 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
3891 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3892 m_owner
->DrawColLabels( dc
, cols
);
3895 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3897 m_owner
->ProcessColLabelMouseEvent( event
);
3900 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3902 m_owner
->GetEventHandler()->ProcessEvent( event
);
3905 // This seems to be required for wxMotif otherwise the mouse
3906 // cursor must be in the cell edit control to get key events
3908 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3910 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3914 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3916 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3920 void wxGridColLabelWindow::OnChar( wxKeyEvent
& event
)
3922 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3926 //////////////////////////////////////////////////////////////////////
3928 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3930 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow
)
3931 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3932 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3933 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3934 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3935 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3936 EVT_CHAR( wxGridCornerLabelWindow::OnChar
)
3939 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3941 const wxPoint
&pos
, const wxSize
&size
)
3942 : wxGridSubwindow(parent
, id
, pos
, size
)
3947 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3951 int client_height
= 0;
3952 int client_width
= 0;
3953 GetClientSize( &client_width
, &client_height
);
3955 // VZ: any reason for this ifdef? (FIXME)
3961 rect
.SetWidth( client_width
- 2 );
3962 rect
.SetHeight( client_height
- 2 );
3964 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3966 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
3967 dc
.DrawLine( client_width
- 1, client_height
- 1, client_width
- 1, 0 );
3968 dc
.DrawLine( client_width
- 1, client_height
- 1, 0, client_height
- 1 );
3969 dc
.DrawLine( 0, 0, client_width
, 0 );
3970 dc
.DrawLine( 0, 0, 0, client_height
);
3972 dc
.SetPen( *wxWHITE_PEN
);
3973 dc
.DrawLine( 1, 1, client_width
- 1, 1 );
3974 dc
.DrawLine( 1, 1, 1, client_height
- 1 );
3978 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3980 m_owner
->ProcessCornerLabelMouseEvent( event
);
3983 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3985 m_owner
->GetEventHandler()->ProcessEvent(event
);
3988 // This seems to be required for wxMotif otherwise the mouse
3989 // cursor must be in the cell edit control to get key events
3991 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3993 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3997 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3999 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4003 void wxGridCornerLabelWindow::OnChar( wxKeyEvent
& event
)
4005 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4009 //////////////////////////////////////////////////////////////////////
4011 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
4013 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow
)
4014 EVT_PAINT( wxGridWindow::OnPaint
)
4015 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
4016 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
4017 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
4018 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
4019 EVT_CHAR( wxGridWindow::OnChar
)
4020 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
4021 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
4022 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
4025 wxGridWindow::wxGridWindow( wxGrid
*parent
,
4026 wxGridRowLabelWindow
*rowLblWin
,
4027 wxGridColLabelWindow
*colLblWin
,
4030 const wxSize
&size
)
4031 : wxGridSubwindow(parent
, id
, pos
, size
,
4032 wxCLIP_CHILDREN
, wxT("grid window") )
4035 m_rowLabelWin
= rowLblWin
;
4036 m_colLabelWin
= colLblWin
;
4039 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
4041 wxPaintDC
dc( this );
4042 m_owner
->PrepareDC( dc
);
4043 wxRegion reg
= GetUpdateRegion();
4044 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
4045 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
4047 #if WXGRID_DRAW_LINES
4048 m_owner
->DrawAllGridLines( dc
, reg
);
4051 m_owner
->DrawGridSpace( dc
);
4052 m_owner
->DrawHighlight( dc
, dirtyCells
);
4055 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
4057 wxWindow::ScrollWindow( dx
, dy
, rect
);
4058 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
4059 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
4062 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
4064 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
4067 m_owner
->ProcessGridCellMouseEvent( event
);
4070 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
4072 m_owner
->GetEventHandler()->ProcessEvent( event
);
4075 // This seems to be required for wxMotif/wxGTK otherwise the mouse
4076 // cursor must be in the cell edit control to get key events
4078 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
4080 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4084 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
4086 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4090 void wxGridWindow::OnChar( wxKeyEvent
& event
)
4092 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4096 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
4100 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
4102 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4106 //////////////////////////////////////////////////////////////////////
4108 // Internal Helper function for computing row or column from some
4109 // (unscrolled) coordinate value, using either
4110 // m_defaultRowHeight/m_defaultColWidth or binary search on array
4111 // of m_rowBottoms/m_ColRights to speed up the search!
4113 // Internal helper macros for simpler use of that function
4115 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
4116 const wxArrayInt
& BorderArray
, int nMax
,
4119 #define internalXToCol(x) XToCol(x, true)
4120 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
4121 m_minAcceptableRowHeight, \
4122 m_rowBottoms, m_numRows, true)
4124 /////////////////////////////////////////////////////////////////////
4126 #if wxUSE_EXTENDED_RTTI
4127 WX_DEFINE_FLAGS( wxGridStyle
)
4129 wxBEGIN_FLAGS( wxGridStyle
)
4130 // new style border flags, we put them first to
4131 // use them for streaming out
4132 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4133 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4134 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4135 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4136 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4137 wxFLAGS_MEMBER(wxBORDER_NONE
)
4139 // old style border flags
4140 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4141 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4142 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4143 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4144 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4145 wxFLAGS_MEMBER(wxBORDER
)
4147 // standard window styles
4148 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4149 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4150 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4151 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4152 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4153 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4154 wxFLAGS_MEMBER(wxVSCROLL
)
4155 wxFLAGS_MEMBER(wxHSCROLL
)
4157 wxEND_FLAGS( wxGridStyle
)
4159 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4161 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4162 wxHIDE_PROPERTY( Children
)
4163 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4164 wxEND_PROPERTIES_TABLE()
4166 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4167 wxEND_HANDLERS_TABLE()
4169 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4172 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
4175 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4178 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4179 EVT_PAINT( wxGrid::OnPaint
)
4180 EVT_SIZE( wxGrid::OnSize
)
4181 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
4182 EVT_KEY_UP( wxGrid::OnKeyUp
)
4183 EVT_CHAR ( wxGrid::OnChar
)
4184 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
4189 // in order to make sure that a size event is not
4190 // trigerred in a unfinished state
4191 m_cornerLabelWin
= NULL
;
4192 m_rowLabelWin
= NULL
;
4193 m_colLabelWin
= NULL
;
4197 wxGrid::wxGrid( wxWindow
*parent
,
4202 const wxString
& name
)
4203 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
4204 m_colMinWidths(GRID_HASH_SIZE
),
4205 m_rowMinHeights(GRID_HASH_SIZE
)
4208 SetInitialSize(size
);
4211 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
4212 const wxPoint
& pos
, const wxSize
& size
,
4213 long style
, const wxString
& name
)
4215 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
4216 style
| wxWANTS_CHARS
, name
))
4219 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4220 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4223 SetInitialSize(size
);
4231 // Must do this or ~wxScrollHelper will pop the wrong event handler
4232 SetTargetWindow(this);
4234 wxSafeDecRef(m_defaultCellAttr
);
4236 #ifdef DEBUG_ATTR_CACHE
4237 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4238 wxPrintf(_T("wxGrid attribute cache statistics: "
4239 "total: %u, hits: %u (%u%%)\n"),
4240 total
, gs_nAttrCacheHits
,
4241 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4244 // if we own the table, just delete it, otherwise at least don't leave it
4245 // with dangling view pointer
4248 else if ( m_table
&& m_table
->GetView() == this )
4249 m_table
->SetView(NULL
);
4251 delete m_typeRegistry
;
4256 // ----- internal init and update functions
4259 // NOTE: If using the default visual attributes works everywhere then this can
4260 // be removed as well as the #else cases below.
4261 #define _USE_VISATTR 0
4263 void wxGrid::Create()
4265 // set to true by CreateGrid
4268 // create the type registry
4269 m_typeRegistry
= new wxGridTypeRegistry
;
4272 m_table
= (wxGridTableBase
*) NULL
;
4275 m_cellEditCtrlEnabled
= false;
4277 m_defaultCellAttr
= new wxGridCellAttr();
4279 // Set default cell attributes
4280 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4281 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4282 m_defaultCellAttr
->SetFont(GetFont());
4283 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4284 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4285 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4288 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4289 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4291 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4292 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4295 m_defaultCellAttr
->SetTextColour(
4296 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4297 m_defaultCellAttr
->SetBackgroundColour(
4298 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4303 m_currentCellCoords
= wxGridNoCellCoords
;
4305 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4306 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4308 // subwindow components that make up the wxGrid
4309 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4314 m_colLabelWin
= new wxGridColLabelWindow( this,
4319 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4324 m_gridWin
= new wxGridWindow( this,
4331 SetTargetWindow( m_gridWin
);
4334 wxColour gfg
= gva
.colFg
;
4335 wxColour gbg
= gva
.colBg
;
4336 wxColour lfg
= lva
.colFg
;
4337 wxColour lbg
= lva
.colBg
;
4339 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4340 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4341 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4342 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4345 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4346 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4347 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4348 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4349 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4350 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4352 m_gridWin
->SetOwnForegroundColour(gfg
);
4353 m_gridWin
->SetOwnBackgroundColour(gbg
);
4358 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4359 wxGrid::wxGridSelectionModes selmode
)
4361 wxCHECK_MSG( !m_created
,
4363 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4365 m_numRows
= numRows
;
4366 m_numCols
= numCols
;
4368 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4369 m_table
->SetView( this );
4371 m_selection
= new wxGridSelection( this, selmode
);
4380 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4382 wxCHECK_RET( m_created
,
4383 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4385 m_selection
->SetSelectionMode( selmode
);
4388 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4390 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4391 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4393 return m_selection
->GetSelectionMode();
4396 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4397 wxGrid::wxGridSelectionModes selmode
)
4399 bool checkSelection
= false;
4402 // stop all processing
4407 m_table
->SetView(0);
4419 checkSelection
= true;
4421 // kill row and column size arrays
4422 m_colWidths
.Empty();
4423 m_colRights
.Empty();
4424 m_rowHeights
.Empty();
4425 m_rowBottoms
.Empty();
4430 m_numRows
= table
->GetNumberRows();
4431 m_numCols
= table
->GetNumberCols();
4434 m_table
->SetView( this );
4435 m_ownTable
= takeOwnership
;
4436 m_selection
= new wxGridSelection( this, selmode
);
4439 // If the newly set table is smaller than the
4440 // original one current cell and selection regions
4441 // might be invalid,
4442 m_selectingKeyboard
= wxGridNoCellCoords
;
4443 m_currentCellCoords
=
4444 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
4445 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
4446 if (m_selectingTopLeft
.GetRow() >= m_numRows
||
4447 m_selectingTopLeft
.GetCol() >= m_numCols
)
4449 m_selectingTopLeft
= wxGridNoCellCoords
;
4450 m_selectingBottomRight
= wxGridNoCellCoords
;
4453 m_selectingBottomRight
=
4454 wxGridCellCoords(wxMin(m_numRows
,
4455 m_selectingBottomRight
.GetRow()),
4457 m_selectingBottomRight
.GetCol()));
4469 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4470 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4472 if ( m_rowLabelWin
)
4474 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4478 m_labelBackgroundColour
= *wxWHITE
;
4481 m_labelTextColour
= *wxBLACK
;
4484 m_attrCache
.row
= -1;
4485 m_attrCache
.col
= -1;
4486 m_attrCache
.attr
= NULL
;
4488 // TODO: something better than this ?
4490 m_labelFont
= this->GetFont();
4491 m_labelFont
.SetWeight( wxBOLD
);
4493 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4494 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4496 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4497 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4498 m_colLabelTextOrientation
= wxHORIZONTAL
;
4500 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4501 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4503 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4504 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4506 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4507 m_defaultRowHeight
+= 8;
4509 m_defaultRowHeight
+= 4;
4512 m_gridLineColour
= wxColour( 192,192,192 );
4513 m_gridLinesEnabled
= true;
4514 m_cellHighlightColour
= *wxBLACK
;
4515 m_cellHighlightPenWidth
= 2;
4516 m_cellHighlightROPenWidth
= 1;
4518 m_canDragColMove
= false;
4520 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4521 m_winCapture
= (wxWindow
*)NULL
;
4522 m_canDragRowSize
= true;
4523 m_canDragColSize
= true;
4524 m_canDragGridSize
= true;
4525 m_canDragCell
= false;
4527 m_dragRowOrCol
= -1;
4528 m_isDragging
= false;
4529 m_startDragPos
= wxDefaultPosition
;
4531 m_waitForSlowClick
= false;
4533 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4534 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4536 m_currentCellCoords
= wxGridNoCellCoords
;
4540 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4541 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4543 m_editable
= true; // default for whole grid
4545 m_inOnKeyDown
= false;
4551 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4552 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4555 // ----------------------------------------------------------------------------
4556 // the idea is to call these functions only when necessary because they create
4557 // quite big arrays which eat memory mostly unnecessary - in particular, if
4558 // default widths/heights are used for all rows/columns, we may not use these
4561 // with some extra code, it should be possible to only store the widths/heights
4562 // different from default ones (resulting in space savings for huge grids) but
4563 // this is not done currently
4564 // ----------------------------------------------------------------------------
4566 void wxGrid::InitRowHeights()
4568 m_rowHeights
.Empty();
4569 m_rowBottoms
.Empty();
4571 m_rowHeights
.Alloc( m_numRows
);
4572 m_rowBottoms
.Alloc( m_numRows
);
4574 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4577 for ( int i
= 0; i
< m_numRows
; i
++ )
4579 rowBottom
+= m_defaultRowHeight
;
4580 m_rowBottoms
.Add( rowBottom
);
4584 void wxGrid::InitColWidths()
4586 m_colWidths
.Empty();
4587 m_colRights
.Empty();
4589 m_colWidths
.Alloc( m_numCols
);
4590 m_colRights
.Alloc( m_numCols
);
4592 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4595 for ( int i
= 0; i
< m_numCols
; i
++ )
4597 colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
4598 m_colRights
.Add( colRight
);
4602 int wxGrid::GetColWidth(int col
) const
4604 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4607 int wxGrid::GetColLeft(int col
) const
4609 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
4610 : m_colRights
[col
] - m_colWidths
[col
];
4613 int wxGrid::GetColRight(int col
) const
4615 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
4619 int wxGrid::GetRowHeight(int row
) const
4621 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4624 int wxGrid::GetRowTop(int row
) const
4626 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4627 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4630 int wxGrid::GetRowBottom(int row
) const
4632 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4633 : m_rowBottoms
[row
];
4636 void wxGrid::CalcDimensions()
4638 // compute the size of the scrollable area
4639 int w
= m_numCols
> 0 ? GetColRight(GetColAt(m_numCols
- 1)) : 0;
4640 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
4645 // take into account editor if shown
4646 if ( IsCellEditControlShown() )
4649 int r
= m_currentCellCoords
.GetRow();
4650 int c
= m_currentCellCoords
.GetCol();
4651 int x
= GetColLeft(c
);
4652 int y
= GetRowTop(r
);
4654 // how big is the editor
4655 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4656 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4657 editor
->GetControl()->GetSize(&w2
, &h2
);
4668 // preserve (more or less) the previous position
4670 GetViewStart( &x
, &y
);
4672 // ensure the position is valid for the new scroll ranges
4674 x
= wxMax( w
- 1, 0 );
4676 y
= wxMax( h
- 1, 0 );
4678 // do set scrollbar parameters
4679 SetScrollbars( m_scrollLineX
, m_scrollLineY
,
4680 GetScrollX(w
), GetScrollY(h
),
4682 GetBatchCount() != 0);
4684 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4685 // still must reposition the children
4689 void wxGrid::CalcWindowSizes()
4691 // escape if the window is has not been fully created yet
4693 if ( m_cornerLabelWin
== NULL
)
4697 GetClientSize( &cw
, &ch
);
4699 // this block of code tries to work around the following problem: the grid
4700 // could have been just resized to have enough space to show the full grid
4701 // window contents without the scrollbars, but its client size could be
4702 // not big enough because the grid has the scrollbars right now and so the
4703 // scrollbars would remain even though we don't need them any more
4705 // to prevent this from happening, check if we have enough space for
4706 // everything without the scrollbars and explicitly disable them then
4707 wxSize size
= GetSize() - GetWindowBorderSize();
4708 if ( size
!= wxSize(cw
, ch
) )
4710 // check if we have enough space for grid window after accounting for
4711 // the fixed size elements
4712 size
.x
-= m_rowLabelWidth
;
4713 size
.y
-= m_colLabelHeight
;
4715 const wxSize vsize
= m_gridWin
->GetVirtualSize();
4717 if ( size
.x
>= vsize
.x
&& size
.y
>= vsize
.y
)
4719 // yes, we do, so remove the scrollbars and use the new client size
4720 // (which should be the same as full window size - borders now)
4721 SetScrollbars(0, 0, 0, 0);
4722 GetClientSize(&cw
, &ch
);
4726 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4727 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4729 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4730 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
- m_rowLabelWidth
, m_colLabelHeight
);
4732 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4733 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
- m_colLabelHeight
);
4735 if ( m_gridWin
&& m_gridWin
->IsShown() )
4736 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
- m_rowLabelWidth
, ch
- m_colLabelHeight
);
4739 // this is called when the grid table sends a message
4740 // to indicate that it has been redimensioned
4742 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4745 bool result
= false;
4747 // Clear the attribute cache as the attribute might refer to a different
4748 // cell than stored in the cache after adding/removing rows/columns.
4751 // By the same reasoning, the editor should be dismissed if columns are
4752 // added or removed. And for consistency, it should IMHO always be
4753 // removed, not only if the cell "underneath" it actually changes.
4754 // For now, I intentionally do not save the editor's content as the
4755 // cell it might want to save that stuff to might no longer exist.
4756 HideCellEditControl();
4759 // if we were using the default widths/heights so far, we must change them
4761 if ( m_colWidths
.IsEmpty() )
4766 if ( m_rowHeights
.IsEmpty() )
4772 switch ( msg
.GetId() )
4774 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4776 size_t pos
= msg
.GetCommandInt();
4777 int numRows
= msg
.GetCommandInt2();
4779 m_numRows
+= numRows
;
4781 if ( !m_rowHeights
.IsEmpty() )
4783 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4784 m_rowBottoms
.Insert( 0, pos
, numRows
);
4788 bottom
= m_rowBottoms
[pos
- 1];
4790 for ( i
= pos
; i
< m_numRows
; i
++ )
4792 bottom
+= m_rowHeights
[i
];
4793 m_rowBottoms
[i
] = bottom
;
4797 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4799 // if we have just inserted cols into an empty grid the current
4800 // cell will be undefined...
4802 SetCurrentCell( 0, 0 );
4806 m_selection
->UpdateRows( pos
, numRows
);
4807 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4809 attrProvider
->UpdateAttrRows( pos
, numRows
);
4811 if ( !GetBatchCount() )
4814 m_rowLabelWin
->Refresh();
4820 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4822 int numRows
= msg
.GetCommandInt();
4823 int oldNumRows
= m_numRows
;
4824 m_numRows
+= numRows
;
4826 if ( !m_rowHeights
.IsEmpty() )
4828 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4829 m_rowBottoms
.Add( 0, numRows
);
4832 if ( oldNumRows
> 0 )
4833 bottom
= m_rowBottoms
[oldNumRows
- 1];
4835 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4837 bottom
+= m_rowHeights
[i
];
4838 m_rowBottoms
[i
] = bottom
;
4842 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4844 // if we have just inserted cols into an empty grid the current
4845 // cell will be undefined...
4847 SetCurrentCell( 0, 0 );
4850 if ( !GetBatchCount() )
4853 m_rowLabelWin
->Refresh();
4859 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4861 size_t pos
= msg
.GetCommandInt();
4862 int numRows
= msg
.GetCommandInt2();
4863 m_numRows
-= numRows
;
4865 if ( !m_rowHeights
.IsEmpty() )
4867 m_rowHeights
.RemoveAt( pos
, numRows
);
4868 m_rowBottoms
.RemoveAt( pos
, numRows
);
4871 for ( i
= 0; i
< m_numRows
; i
++ )
4873 h
+= m_rowHeights
[i
];
4874 m_rowBottoms
[i
] = h
;
4880 m_currentCellCoords
= wxGridNoCellCoords
;
4884 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4885 m_currentCellCoords
.Set( 0, 0 );
4889 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4890 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4893 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4895 // ifdef'd out following patch from Paul Gammans
4897 // No need to touch column attributes, unless we
4898 // removed _all_ rows, in this case, we remove
4899 // all column attributes.
4900 // I hate to do this here, but the
4901 // needed data is not available inside UpdateAttrRows.
4902 if ( !GetNumberRows() )
4903 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4907 if ( !GetBatchCount() )
4910 m_rowLabelWin
->Refresh();
4916 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4918 size_t pos
= msg
.GetCommandInt();
4919 int numCols
= msg
.GetCommandInt2();
4920 m_numCols
+= numCols
;
4922 if ( !m_colAt
.IsEmpty() )
4924 //Shift the column IDs
4926 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
4928 if ( m_colAt
[i
] >= (int)pos
)
4929 m_colAt
[i
] += numCols
;
4932 m_colAt
.Insert( pos
, pos
, numCols
);
4934 //Set the new columns' positions
4935 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
4941 if ( !m_colWidths
.IsEmpty() )
4943 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4944 m_colRights
.Insert( 0, pos
, numCols
);
4948 right
= m_colRights
[GetColAt( pos
- 1 )];
4951 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
4953 i
= GetColAt( colPos
);
4955 right
+= m_colWidths
[i
];
4956 m_colRights
[i
] = right
;
4960 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4962 // if we have just inserted cols into an empty grid the current
4963 // cell will be undefined...
4965 SetCurrentCell( 0, 0 );
4969 m_selection
->UpdateCols( pos
, numCols
);
4970 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4972 attrProvider
->UpdateAttrCols( pos
, numCols
);
4973 if ( !GetBatchCount() )
4976 m_colLabelWin
->Refresh();
4982 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4984 int numCols
= msg
.GetCommandInt();
4985 int oldNumCols
= m_numCols
;
4986 m_numCols
+= numCols
;
4988 if ( !m_colAt
.IsEmpty() )
4990 m_colAt
.Add( 0, numCols
);
4992 //Set the new columns' positions
4994 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
5000 if ( !m_colWidths
.IsEmpty() )
5002 m_colWidths
.Add( m_defaultColWidth
, numCols
);
5003 m_colRights
.Add( 0, numCols
);
5006 if ( oldNumCols
> 0 )
5007 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
5010 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
5012 i
= GetColAt( colPos
);
5014 right
+= m_colWidths
[i
];
5015 m_colRights
[i
] = right
;
5019 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5021 // if we have just inserted cols into an empty grid the current
5022 // cell will be undefined...
5024 SetCurrentCell( 0, 0 );
5026 if ( !GetBatchCount() )
5029 m_colLabelWin
->Refresh();
5035 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5037 size_t pos
= msg
.GetCommandInt();
5038 int numCols
= msg
.GetCommandInt2();
5039 m_numCols
-= numCols
;
5041 if ( !m_colAt
.IsEmpty() )
5043 int colID
= GetColAt( pos
);
5045 m_colAt
.RemoveAt( pos
, numCols
);
5047 //Shift the column IDs
5049 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5051 if ( m_colAt
[colPos
] > colID
)
5052 m_colAt
[colPos
] -= numCols
;
5056 if ( !m_colWidths
.IsEmpty() )
5058 m_colWidths
.RemoveAt( pos
, numCols
);
5059 m_colRights
.RemoveAt( pos
, numCols
);
5063 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5065 i
= GetColAt( colPos
);
5067 w
+= m_colWidths
[i
];
5074 m_currentCellCoords
= wxGridNoCellCoords
;
5078 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
5079 m_currentCellCoords
.Set( 0, 0 );
5083 m_selection
->UpdateCols( pos
, -((int)numCols
) );
5084 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5087 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
5089 // ifdef'd out following patch from Paul Gammans
5091 // No need to touch row attributes, unless we
5092 // removed _all_ columns, in this case, we remove
5093 // all row attributes.
5094 // I hate to do this here, but the
5095 // needed data is not available inside UpdateAttrCols.
5096 if ( !GetNumberCols() )
5097 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
5101 if ( !GetBatchCount() )
5104 m_colLabelWin
->Refresh();
5111 if (result
&& !GetBatchCount() )
5112 m_gridWin
->Refresh();
5117 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
) const
5119 wxRegionIterator
iter( reg
);
5122 wxArrayInt rowlabels
;
5129 // TODO: remove this when we can...
5130 // There is a bug in wxMotif that gives garbage update
5131 // rectangles if you jump-scroll a long way by clicking the
5132 // scrollbar with middle button. This is a work-around
5134 #if defined(__WXMOTIF__)
5136 m_gridWin
->GetClientSize( &cw
, &ch
);
5137 if ( r
.GetTop() > ch
)
5139 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5142 // logical bounds of update region
5145 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
5146 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
5148 // find the row labels within these bounds
5151 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5153 if ( GetRowBottom(row
) < top
)
5156 if ( GetRowTop(row
) > bottom
)
5159 rowlabels
.Add( row
);
5168 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
) const
5170 wxRegionIterator
iter( reg
);
5173 wxArrayInt colLabels
;
5180 // TODO: remove this when we can...
5181 // There is a bug in wxMotif that gives garbage update
5182 // rectangles if you jump-scroll a long way by clicking the
5183 // scrollbar with middle button. This is a work-around
5185 #if defined(__WXMOTIF__)
5187 m_gridWin
->GetClientSize( &cw
, &ch
);
5188 if ( r
.GetLeft() > cw
)
5190 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5193 // logical bounds of update region
5196 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
5197 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
5199 // find the cells within these bounds
5203 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5205 col
= GetColAt( colPos
);
5207 if ( GetColRight(col
) < left
)
5210 if ( GetColLeft(col
) > right
)
5213 colLabels
.Add( col
);
5222 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
) const
5224 wxRegionIterator
iter( reg
);
5227 wxGridCellCoordsArray cellsExposed
;
5229 int left
, top
, right
, bottom
;
5234 // TODO: remove this when we can...
5235 // There is a bug in wxMotif that gives garbage update
5236 // rectangles if you jump-scroll a long way by clicking the
5237 // scrollbar with middle button. This is a work-around
5239 #if defined(__WXMOTIF__)
5241 m_gridWin
->GetClientSize( &cw
, &ch
);
5242 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5243 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5244 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5245 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5248 // logical bounds of update region
5250 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5251 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5253 // find the cells within these bounds
5256 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5258 if ( GetRowBottom(row
) <= top
)
5261 if ( GetRowTop(row
) > bottom
)
5265 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5267 col
= GetColAt( colPos
);
5269 if ( GetColRight(col
) <= left
)
5272 if ( GetColLeft(col
) > right
)
5275 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
5282 return cellsExposed
;
5286 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5289 wxPoint
pos( event
.GetPosition() );
5290 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5292 if ( event
.Dragging() )
5296 m_isDragging
= true;
5297 m_rowLabelWin
->CaptureMouse();
5300 if ( event
.LeftIsDown() )
5302 switch ( m_cursorMode
)
5304 case WXGRID_CURSOR_RESIZE_ROW
:
5306 int cw
, ch
, left
, dummy
;
5307 m_gridWin
->GetClientSize( &cw
, &ch
);
5308 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5310 wxClientDC
dc( m_gridWin
);
5313 GetRowTop(m_dragRowOrCol
) +
5314 GetRowMinimalHeight(m_dragRowOrCol
) );
5315 dc
.SetLogicalFunction(wxINVERT
);
5316 if ( m_dragLastPos
>= 0 )
5318 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5320 dc
.DrawLine( left
, y
, left
+cw
, y
);
5325 case WXGRID_CURSOR_SELECT_ROW
:
5327 if ( (row
= YToRow( y
)) >= 0 )
5331 m_selection
->SelectRow( row
,
5332 event
.ControlDown(),
5341 // default label to suppress warnings about "enumeration value
5342 // 'xxx' not handled in switch
5350 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5355 if (m_rowLabelWin
->HasCapture())
5356 m_rowLabelWin
->ReleaseMouse();
5357 m_isDragging
= false;
5360 // ------------ Entering or leaving the window
5362 if ( event
.Entering() || event
.Leaving() )
5364 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5367 // ------------ Left button pressed
5369 else if ( event
.LeftDown() )
5371 // don't send a label click event for a hit on the
5372 // edge of the row label - this is probably the user
5373 // wanting to resize the row
5375 if ( YToEdgeOfRow(y
) < 0 )
5379 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5381 if ( !event
.ShiftDown() && !event
.CmdDown() )
5385 if ( event
.ShiftDown() )
5387 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5390 GetNumberCols() - 1,
5391 event
.ControlDown(),
5398 m_selection
->SelectRow( row
,
5399 event
.ControlDown(),
5406 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5411 // starting to drag-resize a row
5412 if ( CanDragRowSize() )
5413 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5417 // ------------ Left double click
5419 else if (event
.LeftDClick() )
5421 row
= YToEdgeOfRow(y
);
5426 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5428 // no default action at the moment
5433 // adjust row height depending on label text
5434 AutoSizeRowLabelSize( row
);
5436 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5441 // ------------ Left button released
5443 else if ( event
.LeftUp() )
5445 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5447 DoEndDragResizeRow();
5449 // Note: we are ending the event *after* doing
5450 // default processing in this case
5452 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5455 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5459 // ------------ Right button down
5461 else if ( event
.RightDown() )
5465 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5467 // no default action at the moment
5471 // ------------ Right double click
5473 else if ( event
.RightDClick() )
5477 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5479 // no default action at the moment
5483 // ------------ No buttons down and mouse moving
5485 else if ( event
.Moving() )
5487 m_dragRowOrCol
= YToEdgeOfRow( y
);
5488 if ( m_dragRowOrCol
>= 0 )
5490 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5492 // don't capture the mouse yet
5493 if ( CanDragRowSize() )
5494 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5497 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5499 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5504 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5507 wxPoint
pos( event
.GetPosition() );
5508 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5510 if ( event
.Dragging() )
5514 m_isDragging
= true;
5515 m_colLabelWin
->CaptureMouse();
5517 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
)
5518 m_dragRowOrCol
= XToCol( x
);
5521 if ( event
.LeftIsDown() )
5523 switch ( m_cursorMode
)
5525 case WXGRID_CURSOR_RESIZE_COL
:
5527 int cw
, ch
, dummy
, top
;
5528 m_gridWin
->GetClientSize( &cw
, &ch
);
5529 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5531 wxClientDC
dc( m_gridWin
);
5534 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5535 GetColMinimalWidth(m_dragRowOrCol
));
5536 dc
.SetLogicalFunction(wxINVERT
);
5537 if ( m_dragLastPos
>= 0 )
5539 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5541 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5546 case WXGRID_CURSOR_SELECT_COL
:
5548 if ( (col
= XToCol( x
)) >= 0 )
5552 m_selection
->SelectCol( col
,
5553 event
.ControlDown(),
5562 case WXGRID_CURSOR_MOVE_COL
:
5565 m_moveToCol
= GetColAt( 0 );
5567 m_moveToCol
= XToCol( x
);
5571 if ( m_moveToCol
< 0 )
5572 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5574 markerX
= GetColLeft( m_moveToCol
);
5576 if ( markerX
!= m_dragLastPos
)
5578 wxClientDC
dc( m_colLabelWin
);
5581 m_colLabelWin
->GetClientSize( &cw
, &ch
);
5585 //Clean up the last indicator
5586 if ( m_dragLastPos
>= 0 )
5588 wxPen
pen( m_colLabelWin
->GetBackgroundColour(), 2 );
5590 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
5591 dc
.SetPen(wxNullPen
);
5593 if ( XToCol( m_dragLastPos
) != -1 )
5594 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
5597 //Moving to the same place? Don't draw a marker
5598 if ( (m_moveToCol
== m_dragRowOrCol
)
5599 || (GetColPos( m_moveToCol
) == GetColPos( m_dragRowOrCol
) + 1)
5600 || (m_moveToCol
< 0 && m_dragRowOrCol
== GetColAt( m_numCols
- 1 )))
5607 wxPen
pen( *wxBLUE
, 2 );
5610 dc
.DrawLine( markerX
, 0, markerX
, ch
);
5612 dc
.SetPen(wxNullPen
);
5614 m_dragLastPos
= markerX
- 1;
5619 // default label to suppress warnings about "enumeration value
5620 // 'xxx' not handled in switch
5628 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5633 if (m_colLabelWin
->HasCapture())
5634 m_colLabelWin
->ReleaseMouse();
5635 m_isDragging
= false;
5638 // ------------ Entering or leaving the window
5640 if ( event
.Entering() || event
.Leaving() )
5642 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5645 // ------------ Left button pressed
5647 else if ( event
.LeftDown() )
5649 // don't send a label click event for a hit on the
5650 // edge of the col label - this is probably the user
5651 // wanting to resize the col
5653 if ( XToEdgeOfCol(x
) < 0 )
5657 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5659 if ( m_canDragColMove
)
5661 //Show button as pressed
5662 wxClientDC
dc( m_colLabelWin
);
5663 int colLeft
= GetColLeft( col
);
5664 int colRight
= GetColRight( col
) - 1;
5665 dc
.SetPen( wxPen( m_colLabelWin
->GetBackgroundColour(), 1 ) );
5666 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
5667 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
5669 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, m_colLabelWin
);
5673 if ( !event
.ShiftDown() && !event
.CmdDown() )
5677 if ( event
.ShiftDown() )
5679 m_selection
->SelectBlock( 0,
5680 m_currentCellCoords
.GetCol(),
5681 GetNumberRows() - 1, col
,
5682 event
.ControlDown(),
5689 m_selection
->SelectCol( col
,
5690 event
.ControlDown(),
5697 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5703 // starting to drag-resize a col
5705 if ( CanDragColSize() )
5706 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5710 // ------------ Left double click
5712 if ( event
.LeftDClick() )
5714 col
= XToEdgeOfCol(x
);
5719 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5721 // no default action at the moment
5726 // adjust column width depending on label text
5727 AutoSizeColLabelSize( col
);
5729 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5734 // ------------ Left button released
5736 else if ( event
.LeftUp() )
5738 switch ( m_cursorMode
)
5740 case WXGRID_CURSOR_RESIZE_COL
:
5741 DoEndDragResizeCol();
5743 // Note: we are ending the event *after* doing
5744 // default processing in this case
5746 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5749 case WXGRID_CURSOR_MOVE_COL
:
5752 SendEvent( wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
, event
);
5755 case WXGRID_CURSOR_SELECT_COL
:
5756 case WXGRID_CURSOR_SELECT_CELL
:
5757 case WXGRID_CURSOR_RESIZE_ROW
:
5758 case WXGRID_CURSOR_SELECT_ROW
:
5759 // nothing to do (?)
5763 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5767 // ------------ Right button down
5769 else if ( event
.RightDown() )
5773 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5775 // no default action at the moment
5779 // ------------ Right double click
5781 else if ( event
.RightDClick() )
5785 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5787 // no default action at the moment
5791 // ------------ No buttons down and mouse moving
5793 else if ( event
.Moving() )
5795 m_dragRowOrCol
= XToEdgeOfCol( x
);
5796 if ( m_dragRowOrCol
>= 0 )
5798 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5800 // don't capture the cursor yet
5801 if ( CanDragColSize() )
5802 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5805 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5807 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5812 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5814 if ( event
.LeftDown() )
5816 // indicate corner label by having both row and
5819 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5824 else if ( event
.LeftDClick() )
5826 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5828 else if ( event
.RightDown() )
5830 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5832 // no default action at the moment
5835 else if ( event
.RightDClick() )
5837 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5839 // no default action at the moment
5844 void wxGrid::CancelMouseCapture()
5846 // cancel operation currently in progress, whatever it is
5849 m_isDragging
= false;
5850 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
5851 m_winCapture
->SetCursor( *wxSTANDARD_CURSOR
);
5852 m_winCapture
= NULL
;
5854 // remove traces of whatever we drew on screen
5859 void wxGrid::ChangeCursorMode(CursorMode mode
,
5864 static const wxChar
*cursorModes
[] =
5874 wxLogTrace(_T("grid"),
5875 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5876 win
== m_colLabelWin
? _T("colLabelWin")
5877 : win
? _T("rowLabelWin")
5879 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5882 if ( mode
== m_cursorMode
&&
5883 win
== m_winCapture
&&
5884 captureMouse
== (m_winCapture
!= NULL
))
5889 // by default use the grid itself
5895 if (m_winCapture
->HasCapture())
5896 m_winCapture
->ReleaseMouse();
5897 m_winCapture
= (wxWindow
*)NULL
;
5900 m_cursorMode
= mode
;
5902 switch ( m_cursorMode
)
5904 case WXGRID_CURSOR_RESIZE_ROW
:
5905 win
->SetCursor( m_rowResizeCursor
);
5908 case WXGRID_CURSOR_RESIZE_COL
:
5909 win
->SetCursor( m_colResizeCursor
);
5912 case WXGRID_CURSOR_MOVE_COL
:
5913 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
5917 win
->SetCursor( *wxSTANDARD_CURSOR
);
5921 // we need to capture mouse when resizing
5922 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5923 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5925 if ( captureMouse
&& resize
)
5927 win
->CaptureMouse();
5932 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5935 wxPoint
pos( event
.GetPosition() );
5936 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5938 wxGridCellCoords coords
;
5939 XYToCell( x
, y
, coords
);
5941 int cell_rows
, cell_cols
;
5942 bool isFirstDrag
= !m_isDragging
;
5943 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5944 if ((cell_rows
< 0) || (cell_cols
< 0))
5946 coords
.SetRow(coords
.GetRow() + cell_rows
);
5947 coords
.SetCol(coords
.GetCol() + cell_cols
);
5950 if ( event
.Dragging() )
5952 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5954 // Don't start doing anything until the mouse has been dragged at
5955 // least 3 pixels in any direction...
5958 if (m_startDragPos
== wxDefaultPosition
)
5960 m_startDragPos
= pos
;
5963 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5967 m_isDragging
= true;
5968 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5970 // Hide the edit control, so it
5971 // won't interfere with drag-shrinking.
5972 if ( IsCellEditControlShown() )
5974 HideCellEditControl();
5975 SaveEditControlValue();
5978 if ( coords
!= wxGridNoCellCoords
)
5980 if ( event
.CmdDown() )
5982 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5983 m_selectingKeyboard
= coords
;
5984 HighlightBlock( m_selectingKeyboard
, coords
);
5986 else if ( CanDragCell() )
5990 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5991 m_selectingKeyboard
= coords
;
5993 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
6002 if ( !IsSelection() )
6004 HighlightBlock( coords
, coords
);
6008 HighlightBlock( m_currentCellCoords
, coords
);
6012 if (! IsVisible(coords
))
6014 MakeCellVisible(coords
);
6015 // TODO: need to introduce a delay or something here. The
6016 // scrolling is way to fast, at least on MSW - also on GTK.
6019 // Have we captured the mouse yet?
6022 m_winCapture
= m_gridWin
;
6023 m_winCapture
->CaptureMouse();
6028 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6030 int cw
, ch
, left
, dummy
;
6031 m_gridWin
->GetClientSize( &cw
, &ch
);
6032 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6034 wxClientDC
dc( m_gridWin
);
6036 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
6037 GetRowMinimalHeight(m_dragRowOrCol
) );
6038 dc
.SetLogicalFunction(wxINVERT
);
6039 if ( m_dragLastPos
>= 0 )
6041 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
6043 dc
.DrawLine( left
, y
, left
+cw
, y
);
6046 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6048 int cw
, ch
, dummy
, top
;
6049 m_gridWin
->GetClientSize( &cw
, &ch
);
6050 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6052 wxClientDC
dc( m_gridWin
);
6054 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
6055 GetColMinimalWidth(m_dragRowOrCol
) );
6056 dc
.SetLogicalFunction(wxINVERT
);
6057 if ( m_dragLastPos
>= 0 )
6059 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6061 dc
.DrawLine( x
, top
, x
, top
+ ch
);
6068 m_isDragging
= false;
6069 m_startDragPos
= wxDefaultPosition
;
6071 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
6072 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
6075 if ( event
.Entering() || event
.Leaving() )
6077 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6078 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
6083 // ------------ Left button pressed
6085 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
6087 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
6092 if ( !event
.CmdDown() )
6094 if ( event
.ShiftDown() )
6098 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
6099 m_currentCellCoords
.GetCol(),
6102 event
.ControlDown(),
6108 else if ( XToEdgeOfCol(x
) < 0 &&
6109 YToEdgeOfRow(y
) < 0 )
6111 DisableCellEditControl();
6112 MakeCellVisible( coords
);
6114 if ( event
.CmdDown() )
6118 m_selection
->ToggleCellSelection( coords
.GetRow(),
6120 event
.ControlDown(),
6125 m_selectingTopLeft
= wxGridNoCellCoords
;
6126 m_selectingBottomRight
= wxGridNoCellCoords
;
6127 m_selectingKeyboard
= coords
;
6131 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
6132 SetCurrentCell( coords
);
6135 if ( m_selection
->GetSelectionMode() !=
6136 wxGrid::wxGridSelectCells
)
6138 HighlightBlock( coords
, coords
);
6146 // ------------ Left double click
6148 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
6150 DisableCellEditControl();
6152 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
6154 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
6159 // we want double click to select a cell and start editing
6160 // (i.e. to behave in same way as sequence of two slow clicks):
6161 m_waitForSlowClick
= true;
6166 // ------------ Left button released
6168 else if ( event
.LeftUp() )
6170 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6174 if (m_winCapture
->HasCapture())
6175 m_winCapture
->ReleaseMouse();
6176 m_winCapture
= NULL
;
6179 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
6182 EnableCellEditControl();
6184 wxGridCellAttr
*attr
= GetCellAttr(coords
);
6185 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
6186 editor
->StartingClick();
6190 m_waitForSlowClick
= false;
6192 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6193 m_selectingBottomRight
!= wxGridNoCellCoords
)
6197 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6198 m_selectingTopLeft
.GetCol(),
6199 m_selectingBottomRight
.GetRow(),
6200 m_selectingBottomRight
.GetCol(),
6201 event
.ControlDown(),
6207 m_selectingTopLeft
= wxGridNoCellCoords
;
6208 m_selectingBottomRight
= wxGridNoCellCoords
;
6210 // Show the edit control, if it has been hidden for
6212 ShowCellEditControl();
6215 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6217 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6218 DoEndDragResizeRow();
6220 // Note: we are ending the event *after* doing
6221 // default processing in this case
6223 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
6225 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6227 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6228 DoEndDragResizeCol();
6230 // Note: we are ending the event *after* doing
6231 // default processing in this case
6233 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
6239 // ------------ Right button down
6241 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
6243 DisableCellEditControl();
6244 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
6249 // no default action at the moment
6253 // ------------ Right double click
6255 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
6257 DisableCellEditControl();
6258 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
6263 // no default action at the moment
6267 // ------------ Moving and no button action
6269 else if ( event
.Moving() && !event
.IsButton() )
6271 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
6273 // out of grid cell area
6274 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6278 int dragRow
= YToEdgeOfRow( y
);
6279 int dragCol
= XToEdgeOfCol( x
);
6281 // Dragging on the corner of a cell to resize in both
6282 // directions is not implemented yet...
6284 if ( dragRow
>= 0 && dragCol
>= 0 )
6286 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6292 m_dragRowOrCol
= dragRow
;
6294 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6296 if ( CanDragRowSize() && CanDragGridSize() )
6297 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
6300 else if ( dragCol
>= 0 )
6302 m_dragRowOrCol
= dragCol
;
6304 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6306 if ( CanDragColSize() && CanDragGridSize() )
6307 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
6310 else // Neither on a row or col edge
6312 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6314 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6320 void wxGrid::DoEndDragResizeRow()
6322 if ( m_dragLastPos
>= 0 )
6324 // erase the last line and resize the row
6326 int cw
, ch
, left
, dummy
;
6327 m_gridWin
->GetClientSize( &cw
, &ch
);
6328 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6330 wxClientDC
dc( m_gridWin
);
6332 dc
.SetLogicalFunction( wxINVERT
);
6333 dc
.DrawLine( left
, m_dragLastPos
, left
+ cw
, m_dragLastPos
);
6334 HideCellEditControl();
6335 SaveEditControlValue();
6337 int rowTop
= GetRowTop(m_dragRowOrCol
);
6338 SetRowSize( m_dragRowOrCol
,
6339 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
6341 if ( !GetBatchCount() )
6343 // Only needed to get the correct rect.y:
6344 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
6346 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6347 rect
.width
= m_rowLabelWidth
;
6348 rect
.height
= ch
- rect
.y
;
6349 m_rowLabelWin
->Refresh( true, &rect
);
6352 // if there is a multicell block, paint all of it
6355 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
6356 int leftCol
= XToCol(left
);
6357 int rightCol
= internalXToCol(left
+ cw
);
6360 for (i
=leftCol
; i
<rightCol
; i
++)
6362 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
6363 if (cell_rows
< subtract_rows
)
6364 subtract_rows
= cell_rows
;
6366 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
6367 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6368 rect
.height
= ch
- rect
.y
;
6371 m_gridWin
->Refresh( false, &rect
);
6374 ShowCellEditControl();
6379 void wxGrid::DoEndDragResizeCol()
6381 if ( m_dragLastPos
>= 0 )
6383 // erase the last line and resize the col
6385 int cw
, ch
, dummy
, top
;
6386 m_gridWin
->GetClientSize( &cw
, &ch
);
6387 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6389 wxClientDC
dc( m_gridWin
);
6391 dc
.SetLogicalFunction( wxINVERT
);
6392 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6393 HideCellEditControl();
6394 SaveEditControlValue();
6396 int colLeft
= GetColLeft(m_dragRowOrCol
);
6397 SetColSize( m_dragRowOrCol
,
6398 wxMax( m_dragLastPos
- colLeft
,
6399 GetColMinimalWidth(m_dragRowOrCol
) ) );
6401 if ( !GetBatchCount() )
6403 // Only needed to get the correct rect.x:
6404 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
6406 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6407 rect
.width
= cw
- rect
.x
;
6408 rect
.height
= m_colLabelHeight
;
6409 m_colLabelWin
->Refresh( true, &rect
);
6412 // if there is a multicell block, paint all of it
6415 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
6416 int topRow
= YToRow(top
);
6417 int bottomRow
= internalYToRow(top
+ cw
);
6420 for (i
=topRow
; i
<bottomRow
; i
++)
6422 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
6423 if (cell_cols
< subtract_cols
)
6424 subtract_cols
= cell_cols
;
6427 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
6428 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6429 rect
.width
= cw
- rect
.x
;
6433 m_gridWin
->Refresh( false, &rect
);
6436 ShowCellEditControl();
6440 void wxGrid::DoEndDragMoveCol()
6442 //The user clicked on the column but didn't actually drag
6443 if ( m_dragLastPos
< 0 )
6445 m_colLabelWin
->Refresh(); //Do this to "unpress" the column
6450 if ( m_moveToCol
== -1 )
6451 newPos
= m_numCols
- 1;
6454 newPos
= GetColPos( m_moveToCol
);
6455 if ( newPos
> GetColPos( m_dragRowOrCol
) )
6459 SetColPos( m_dragRowOrCol
, newPos
);
6462 void wxGrid::SetColPos( int colID
, int newPos
)
6464 if ( m_colAt
.IsEmpty() )
6466 m_colAt
.Alloc( m_numCols
);
6469 for ( i
= 0; i
< m_numCols
; i
++ )
6475 int oldPos
= GetColPos( colID
);
6477 //Reshuffle the m_colAt array
6478 if ( newPos
> oldPos
)
6481 for ( i
= oldPos
; i
< newPos
; i
++ )
6483 m_colAt
[i
] = m_colAt
[i
+1];
6489 for ( i
= oldPos
; i
> newPos
; i
-- )
6491 m_colAt
[i
] = m_colAt
[i
-1];
6495 m_colAt
[newPos
] = colID
;
6497 //Recalculate the column rights
6498 if ( !m_colWidths
.IsEmpty() )
6502 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6504 int colID
= GetColAt( colPos
);
6506 colRight
+= m_colWidths
[colID
];
6507 m_colRights
[colID
] = colRight
;
6511 m_colLabelWin
->Refresh();
6512 m_gridWin
->Refresh();
6517 void wxGrid::EnableDragColMove( bool enable
)
6519 if ( m_canDragColMove
== enable
)
6522 m_canDragColMove
= enable
;
6524 if ( !m_canDragColMove
)
6528 //Recalculate the column rights
6529 if ( !m_colWidths
.IsEmpty() )
6533 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6535 colRight
+= m_colWidths
[colPos
];
6536 m_colRights
[colPos
] = colRight
;
6540 m_colLabelWin
->Refresh();
6541 m_gridWin
->Refresh();
6547 // ------ interaction with data model
6549 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6551 switch ( msg
.GetId() )
6553 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6554 return GetModelValues();
6556 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6557 return SetModelValues();
6559 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6560 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6561 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6562 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6563 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6564 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6565 return Redimension( msg
);
6572 // The behaviour of this function depends on the grid table class
6573 // Clear() function. For the default wxGridStringTable class the
6574 // behavious is to replace all cell contents with wxEmptyString but
6575 // not to change the number of rows or cols.
6577 void wxGrid::ClearGrid()
6581 if (IsCellEditControlEnabled())
6582 DisableCellEditControl();
6585 if (!GetBatchCount())
6586 m_gridWin
->Refresh();
6590 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6592 // TODO: something with updateLabels flag
6596 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6602 if (IsCellEditControlEnabled())
6603 DisableCellEditControl();
6605 bool done
= m_table
->InsertRows( pos
, numRows
);
6608 // the table will have sent the results of the insert row
6609 // operation to this view object as a grid table message
6615 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6617 // TODO: something with updateLabels flag
6621 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6627 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6630 // the table will have sent the results of the append row
6631 // operation to this view object as a grid table message
6637 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6639 // TODO: something with updateLabels flag
6643 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6649 if (IsCellEditControlEnabled())
6650 DisableCellEditControl();
6652 bool done
= m_table
->DeleteRows( pos
, numRows
);
6654 // the table will have sent the results of the delete row
6655 // operation to this view object as a grid table message
6661 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6663 // TODO: something with updateLabels flag
6667 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6673 if (IsCellEditControlEnabled())
6674 DisableCellEditControl();
6676 bool done
= m_table
->InsertCols( pos
, numCols
);
6678 // the table will have sent the results of the insert col
6679 // operation to this view object as a grid table message
6685 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6687 // TODO: something with updateLabels flag
6691 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6697 bool done
= m_table
->AppendCols( numCols
);
6699 // the table will have sent the results of the append col
6700 // operation to this view object as a grid table message
6706 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6708 // TODO: something with updateLabels flag
6712 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6718 if (IsCellEditControlEnabled())
6719 DisableCellEditControl();
6721 bool done
= m_table
->DeleteCols( pos
, numCols
);
6723 // the table will have sent the results of the delete col
6724 // operation to this view object as a grid table message
6731 // ----- event handlers
6734 // Generate a grid event based on a mouse event and
6735 // return the result of ProcessEvent()
6737 int wxGrid::SendEvent( const wxEventType type
,
6739 wxMouseEvent
& mouseEv
)
6741 bool claimed
, vetoed
;
6743 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6745 int rowOrCol
= (row
== -1 ? col
: row
);
6747 wxGridSizeEvent
gridEvt( GetId(),
6751 mouseEv
.GetX() + GetRowLabelSize(),
6752 mouseEv
.GetY() + GetColLabelSize(),
6753 mouseEv
.ControlDown(),
6754 mouseEv
.ShiftDown(),
6756 mouseEv
.MetaDown() );
6758 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6759 vetoed
= !gridEvt
.IsAllowed();
6761 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6763 // Right now, it should _never_ end up here!
6764 wxGridRangeSelectEvent
gridEvt( GetId(),
6768 m_selectingBottomRight
,
6770 mouseEv
.ControlDown(),
6771 mouseEv
.ShiftDown(),
6773 mouseEv
.MetaDown() );
6775 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6776 vetoed
= !gridEvt
.IsAllowed();
6778 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
6779 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
6780 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
6781 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
6783 wxPoint pos
= mouseEv
.GetPosition();
6785 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
6786 pos
.y
+= GetColLabelSize();
6787 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
6788 pos
.x
+= GetRowLabelSize();
6790 wxGridEvent
gridEvt( GetId(),
6797 mouseEv
.ControlDown(),
6798 mouseEv
.ShiftDown(),
6800 mouseEv
.MetaDown() );
6801 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6802 vetoed
= !gridEvt
.IsAllowed();
6806 wxGridEvent
gridEvt( GetId(),
6810 mouseEv
.GetX() + GetRowLabelSize(),
6811 mouseEv
.GetY() + GetColLabelSize(),
6813 mouseEv
.ControlDown(),
6814 mouseEv
.ShiftDown(),
6816 mouseEv
.MetaDown() );
6817 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6818 vetoed
= !gridEvt
.IsAllowed();
6821 // A Veto'd event may not be `claimed' so test this first
6825 return claimed
? 1 : 0;
6828 // Generate a grid event of specified type and return the result
6829 // of ProcessEvent().
6831 int wxGrid::SendEvent( const wxEventType type
,
6834 bool claimed
, vetoed
;
6836 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6838 int rowOrCol
= (row
== -1 ? col
: row
);
6840 wxGridSizeEvent
gridEvt( GetId(), type
, this, rowOrCol
);
6842 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6843 vetoed
= !gridEvt
.IsAllowed();
6847 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
6849 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6850 vetoed
= !gridEvt
.IsAllowed();
6853 // A Veto'd event may not be `claimed' so test this first
6857 return claimed
? 1 : 0;
6860 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6862 // needed to prevent zillions of paint events on MSW
6866 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6868 // Don't do anything if between Begin/EndBatch...
6869 // EndBatch() will do all this on the last nested one anyway.
6870 if (! GetBatchCount())
6872 // Refresh to get correct scrolled position:
6873 wxScrolledWindow::Refresh(eraseb
, rect
);
6877 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6878 int width_label
, width_cell
, height_label
, height_cell
;
6881 // Copy rectangle can get scroll offsets..
6882 rect_x
= rect
->GetX();
6883 rect_y
= rect
->GetY();
6884 rectWidth
= rect
->GetWidth();
6885 rectHeight
= rect
->GetHeight();
6887 width_label
= m_rowLabelWidth
- rect_x
;
6888 if (width_label
> rectWidth
)
6889 width_label
= rectWidth
;
6891 height_label
= m_colLabelHeight
- rect_y
;
6892 if (height_label
> rectHeight
)
6893 height_label
= rectHeight
;
6895 if (rect_x
> m_rowLabelWidth
)
6897 x
= rect_x
- m_rowLabelWidth
;
6898 width_cell
= rectWidth
;
6903 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6906 if (rect_y
> m_colLabelHeight
)
6908 y
= rect_y
- m_colLabelHeight
;
6909 height_cell
= rectHeight
;
6914 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6917 // Paint corner label part intersecting rect.
6918 if ( width_label
> 0 && height_label
> 0 )
6920 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6921 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6924 // Paint col labels part intersecting rect.
6925 if ( width_cell
> 0 && height_label
> 0 )
6927 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6928 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6931 // Paint row labels part intersecting rect.
6932 if ( width_label
> 0 && height_cell
> 0 )
6934 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6935 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6938 // Paint cell area part intersecting rect.
6939 if ( width_cell
> 0 && height_cell
> 0 )
6941 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6942 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6947 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6948 m_colLabelWin
->Refresh(eraseb
, NULL
);
6949 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6950 m_gridWin
->Refresh(eraseb
, NULL
);
6955 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
))
6957 if (m_targetWindow
!= this) // check whether initialisation has been done
6959 // update our children window positions and scrollbars
6964 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6966 if ( m_inOnKeyDown
)
6968 // shouldn't be here - we are going round in circles...
6970 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6973 m_inOnKeyDown
= true;
6975 // propagate the event up and see if it gets processed
6976 wxWindow
*parent
= GetParent();
6977 wxKeyEvent
keyEvt( event
);
6978 keyEvt
.SetEventObject( parent
);
6980 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6982 if (GetLayoutDirection() == wxLayout_RightToLeft
)
6984 if (event
.GetKeyCode() == WXK_RIGHT
)
6985 event
.m_keyCode
= WXK_LEFT
;
6986 else if (event
.GetKeyCode() == WXK_LEFT
)
6987 event
.m_keyCode
= WXK_RIGHT
;
6990 // try local handlers
6991 switch ( event
.GetKeyCode() )
6994 if ( event
.ControlDown() )
6995 MoveCursorUpBlock( event
.ShiftDown() );
6997 MoveCursorUp( event
.ShiftDown() );
7001 if ( event
.ControlDown() )
7002 MoveCursorDownBlock( event
.ShiftDown() );
7004 MoveCursorDown( event
.ShiftDown() );
7008 if ( event
.ControlDown() )
7009 MoveCursorLeftBlock( event
.ShiftDown() );
7011 MoveCursorLeft( event
.ShiftDown() );
7015 if ( event
.ControlDown() )
7016 MoveCursorRightBlock( event
.ShiftDown() );
7018 MoveCursorRight( event
.ShiftDown() );
7022 case WXK_NUMPAD_ENTER
:
7023 if ( event
.ControlDown() )
7025 event
.Skip(); // to let the edit control have the return
7029 if ( GetGridCursorRow() < GetNumberRows()-1 )
7031 MoveCursorDown( event
.ShiftDown() );
7035 // at the bottom of a column
7036 DisableCellEditControl();
7046 if (event
.ShiftDown())
7048 if ( GetGridCursorCol() > 0 )
7050 MoveCursorLeft( false );
7055 DisableCellEditControl();
7060 if ( GetGridCursorCol() < GetNumberCols() - 1 )
7062 MoveCursorRight( false );
7067 DisableCellEditControl();
7073 if ( event
.ControlDown() )
7075 MakeCellVisible( 0, 0 );
7076 SetCurrentCell( 0, 0 );
7085 if ( event
.ControlDown() )
7087 MakeCellVisible( m_numRows
- 1, m_numCols
- 1 );
7088 SetCurrentCell( m_numRows
- 1, m_numCols
- 1 );
7105 if ( event
.ControlDown() )
7109 m_selection
->ToggleCellSelection(
7110 m_currentCellCoords
.GetRow(),
7111 m_currentCellCoords
.GetCol(),
7112 event
.ControlDown(),
7120 if ( !IsEditable() )
7121 MoveCursorRight( false );
7132 m_inOnKeyDown
= false;
7135 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
7137 // try local handlers
7139 if ( event
.GetKeyCode() == WXK_SHIFT
)
7141 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
7142 m_selectingBottomRight
!= wxGridNoCellCoords
)
7146 m_selection
->SelectBlock(
7147 m_selectingTopLeft
.GetRow(),
7148 m_selectingTopLeft
.GetCol(),
7149 m_selectingBottomRight
.GetRow(),
7150 m_selectingBottomRight
.GetCol(),
7151 event
.ControlDown(),
7158 m_selectingTopLeft
= wxGridNoCellCoords
;
7159 m_selectingBottomRight
= wxGridNoCellCoords
;
7160 m_selectingKeyboard
= wxGridNoCellCoords
;
7164 void wxGrid::OnChar( wxKeyEvent
& event
)
7166 // is it possible to edit the current cell at all?
7167 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7169 // yes, now check whether the cells editor accepts the key
7170 int row
= m_currentCellCoords
.GetRow();
7171 int col
= m_currentCellCoords
.GetCol();
7172 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7173 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7175 // <F2> is special and will always start editing, for
7176 // other keys - ask the editor itself
7177 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
7178 || editor
->IsAcceptedKey(event
) )
7180 // ensure cell is visble
7181 MakeCellVisible(row
, col
);
7182 EnableCellEditControl();
7184 // a problem can arise if the cell is not completely
7185 // visible (even after calling MakeCellVisible the
7186 // control is not created and calling StartingKey will
7188 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
7189 editor
->StartingKey(event
);
7205 void wxGrid::OnEraseBackground(wxEraseEvent
&)
7209 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
7211 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
7213 // the event has been intercepted - do nothing
7217 #if !(defined(__WXMAC__) && wxMAC_USE_CORE_GRAPHICS)
7218 wxClientDC
dc( m_gridWin
);
7222 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
7224 DisableCellEditControl();
7226 if ( IsVisible( m_currentCellCoords
, false ) )
7229 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
7230 if ( !m_gridLinesEnabled
)
7238 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
7240 // Otherwise refresh redraws the highlight!
7241 m_currentCellCoords
= coords
;
7243 #if defined(__WXMAC__) && wxMAC_USE_CORE_GRAPHICS
7244 m_gridWin
->Refresh(true /*, & r */);
7246 DrawGridCellArea( dc
, cells
);
7247 DrawAllGridLines( dc
, r
);
7252 m_currentCellCoords
= coords
;
7254 wxGridCellAttr
*attr
= GetCellAttr( coords
);
7255 #if !(defined(__WXMAC__) && wxMAC_USE_CORE_GRAPHICS)
7256 DrawCellHighlight( dc
, attr
);
7261 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7264 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7268 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
7271 rightCol
= GetNumberCols() - 1;
7273 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
7276 bottomRow
= GetNumberRows() - 1;
7280 if ( topRow
> bottomRow
)
7287 if ( leftCol
> rightCol
)
7294 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7295 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7297 // First the case that we selected a completely new area
7298 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
7299 m_selectingBottomRight
== wxGridNoCellCoords
)
7302 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
7303 wxGridCellCoords ( bottomRow
, rightCol
) );
7304 m_gridWin
->Refresh( false, &rect
);
7307 // Now handle changing an existing selection area.
7308 else if ( m_selectingTopLeft
!= updateTopLeft
||
7309 m_selectingBottomRight
!= updateBottomRight
)
7311 // Compute two optimal update rectangles:
7312 // Either one rectangle is a real subset of the
7313 // other, or they are (almost) disjoint!
7315 bool need_refresh
[4];
7319 need_refresh
[3] = false;
7322 // Store intermediate values
7323 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
7324 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
7325 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
7326 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
7328 // Determine the outer/inner coordinates.
7329 if (oldLeft
> leftCol
)
7335 if (oldTop
> topRow
)
7341 if (oldRight
< rightCol
)
7344 oldRight
= rightCol
;
7347 if (oldBottom
< bottomRow
)
7350 oldBottom
= bottomRow
;
7354 // Now, either the stuff marked old is the outer
7355 // rectangle or we don't have a situation where one
7356 // is contained in the other.
7358 if ( oldLeft
< leftCol
)
7360 // Refresh the newly selected or deselected
7361 // area to the left of the old or new selection.
7362 need_refresh
[0] = true;
7363 rect
[0] = BlockToDeviceRect(
7364 wxGridCellCoords( oldTop
, oldLeft
),
7365 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
7368 if ( oldTop
< topRow
)
7370 // Refresh the newly selected or deselected
7371 // area above the old or new selection.
7372 need_refresh
[1] = true;
7373 rect
[1] = BlockToDeviceRect(
7374 wxGridCellCoords( oldTop
, leftCol
),
7375 wxGridCellCoords( topRow
- 1, rightCol
) );
7378 if ( oldRight
> rightCol
)
7380 // Refresh the newly selected or deselected
7381 // area to the right of the old or new selection.
7382 need_refresh
[2] = true;
7383 rect
[2] = BlockToDeviceRect(
7384 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
7385 wxGridCellCoords( oldBottom
, oldRight
) );
7388 if ( oldBottom
> bottomRow
)
7390 // Refresh the newly selected or deselected
7391 // area below the old or new selection.
7392 need_refresh
[3] = true;
7393 rect
[3] = BlockToDeviceRect(
7394 wxGridCellCoords( bottomRow
+ 1, leftCol
),
7395 wxGridCellCoords( oldBottom
, rightCol
) );
7398 // various Refresh() calls
7399 for (i
= 0; i
< 4; i
++ )
7400 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7401 m_gridWin
->Refresh( false, &(rect
[i
]) );
7405 m_selectingTopLeft
= updateTopLeft
;
7406 m_selectingBottomRight
= updateBottomRight
;
7410 // ------ functions to get/send data (see also public functions)
7413 bool wxGrid::GetModelValues()
7415 // Hide the editor, so it won't hide a changed value.
7416 HideCellEditControl();
7420 // all we need to do is repaint the grid
7422 m_gridWin
->Refresh();
7429 bool wxGrid::SetModelValues()
7433 // Disable the editor, so it won't hide a changed value.
7434 // Do we also want to save the current value of the editor first?
7436 DisableCellEditControl();
7440 for ( row
= 0; row
< m_numRows
; row
++ )
7442 for ( col
= 0; col
< m_numCols
; col
++ )
7444 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
7454 // Note - this function only draws cells that are in the list of
7455 // exposed cells (usually set from the update region by
7456 // CalcExposedCells)
7458 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7460 if ( !m_numRows
|| !m_numCols
)
7463 int i
, numCells
= cells
.GetCount();
7464 int row
, col
, cell_rows
, cell_cols
;
7465 wxGridCellCoordsArray redrawCells
;
7467 for ( i
= numCells
- 1; i
>= 0; i
-- )
7469 row
= cells
[i
].GetRow();
7470 col
= cells
[i
].GetCol();
7471 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7473 // If this cell is part of a multicell block, find owner for repaint
7474 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7476 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
7477 bool marked
= false;
7478 for ( int j
= 0; j
< numCells
; j
++ )
7480 if ( cell
== cells
[j
] )
7489 int count
= redrawCells
.GetCount();
7490 for (int j
= 0; j
< count
; j
++)
7492 if ( cell
== redrawCells
[j
] )
7500 redrawCells
.Add( cell
);
7503 // don't bother drawing this cell
7507 // If this cell is empty, find cell to left that might want to overflow
7508 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7510 for ( int l
= 0; l
< cell_rows
; l
++ )
7512 // find a cell in this row to leave already marked for repaint
7514 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7515 if ((redrawCells
[k
].GetCol() < left
) &&
7516 (redrawCells
[k
].GetRow() == row
))
7518 left
= redrawCells
[k
].GetCol();
7522 left
= 0; // oh well
7524 for (int j
= col
- 1; j
>= left
; j
--)
7526 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7528 if (GetCellOverflow(row
+ l
, j
))
7530 wxGridCellCoords
cell(row
+ l
, j
);
7531 bool marked
= false;
7533 for (int k
= 0; k
< numCells
; k
++)
7535 if ( cell
== cells
[k
] )
7544 int count
= redrawCells
.GetCount();
7545 for (int k
= 0; k
< count
; k
++)
7547 if ( cell
== redrawCells
[k
] )
7554 redrawCells
.Add( cell
);
7563 DrawCell( dc
, cells
[i
] );
7566 numCells
= redrawCells
.GetCount();
7568 for ( i
= numCells
- 1; i
>= 0; i
-- )
7570 DrawCell( dc
, redrawCells
[i
] );
7574 void wxGrid::DrawGridSpace( wxDC
& dc
)
7577 m_gridWin
->GetClientSize( &cw
, &ch
);
7580 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7582 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
7583 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7585 if ( right
> rightCol
|| bottom
> bottomRow
)
7588 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7590 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
7591 dc
.SetPen( *wxTRANSPARENT_PEN
);
7593 if ( right
> rightCol
)
7595 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7598 if ( bottom
> bottomRow
)
7600 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7605 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7607 int row
= coords
.GetRow();
7608 int col
= coords
.GetCol();
7610 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7613 // we draw the cell border ourselves
7614 #if !WXGRID_DRAW_LINES
7615 if ( m_gridLinesEnabled
)
7616 DrawCellBorder( dc
, coords
);
7619 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7621 bool isCurrent
= coords
== m_currentCellCoords
;
7623 wxRect rect
= CellToRect( row
, col
);
7625 // if the editor is shown, we should use it and not the renderer
7626 // Note: However, only if it is really _shown_, i.e. not hidden!
7627 if ( isCurrent
&& IsCellEditControlShown() )
7629 // NB: this "#if..." is temporary and fixes a problem where the
7630 // edit control is erased by this code after being rendered.
7631 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7632 // implicitly, causing this out-of order render.
7633 #if !defined(__WXMAC__)
7634 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7635 editor
->PaintBackground(rect
, attr
);
7641 // but all the rest is drawn by the cell renderer and hence may be customized
7642 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7643 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7650 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7652 int row
= m_currentCellCoords
.GetRow();
7653 int col
= m_currentCellCoords
.GetCol();
7655 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7658 wxRect rect
= CellToRect(row
, col
);
7660 // hmmm... what could we do here to show that the cell is disabled?
7661 // for now, I just draw a thinner border than for the other ones, but
7662 // it doesn't look really good
7664 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7668 // The center of the drawn line is where the position/width/height of
7669 // the rectangle is actually at (on wxMSW at least), so the
7670 // size of the rectangle is reduced to compensate for the thickness of
7671 // the line. If this is too strange on non-wxMSW platforms then
7672 // please #ifdef this appropriately.
7673 rect
.x
+= penWidth
/ 2;
7674 rect
.y
+= penWidth
/ 2;
7675 rect
.width
-= penWidth
- 1;
7676 rect
.height
-= penWidth
- 1;
7678 // Now draw the rectangle
7679 // use the cellHighlightColour if the cell is inside a selection, this
7680 // will ensure the cell is always visible.
7681 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
: m_cellHighlightColour
, penWidth
, wxSOLID
));
7682 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7683 dc
.DrawRectangle(rect
);
7687 // VZ: my experiments with 3D borders...
7689 // how to properly set colours for arbitrary bg?
7690 wxCoord x1
= rect
.x
,
7692 x2
= rect
.x
+ rect
.width
- 1,
7693 y2
= rect
.y
+ rect
.height
- 1;
7695 dc
.SetPen(*wxWHITE_PEN
);
7696 dc
.DrawLine(x1
, y1
, x2
, y1
);
7697 dc
.DrawLine(x1
, y1
, x1
, y2
);
7699 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7700 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7702 dc
.SetPen(*wxBLACK_PEN
);
7703 dc
.DrawLine(x1
, y2
, x2
, y2
);
7704 dc
.DrawLine(x2
, y1
, x2
, y2
+ 1);
7708 wxPen
wxGrid::GetDefaultGridLinePen()
7710 return wxPen(GetGridLineColour(), 1, wxSOLID
);
7713 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
7715 return GetDefaultGridLinePen();
7718 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
7720 return GetDefaultGridLinePen();
7723 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7725 int row
= coords
.GetRow();
7726 int col
= coords
.GetCol();
7727 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7731 wxRect rect
= CellToRect( row
, col
);
7733 // right hand border
7734 dc
.SetPen( GetColGridLinePen(col
) );
7735 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7736 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7739 dc
.SetPen( GetRowGridLinePen(row
) );
7740 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7741 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7744 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7746 // This if block was previously in wxGrid::OnPaint but that doesn't
7747 // seem to get called under wxGTK - MB
7749 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7750 m_numRows
&& m_numCols
)
7752 m_currentCellCoords
.Set(0, 0);
7755 if ( IsCellEditControlShown() )
7757 // don't show highlight when the edit control is shown
7761 // if the active cell was repainted, repaint its highlight too because it
7762 // might have been damaged by the grid lines
7763 size_t count
= cells
.GetCount();
7764 for ( size_t n
= 0; n
< count
; n
++ )
7766 if ( cells
[n
] == m_currentCellCoords
)
7768 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7769 DrawCellHighlight(dc
, attr
);
7777 // TODO: remove this ???
7778 // This is used to redraw all grid lines e.g. when the grid line colour
7781 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7783 #if !WXGRID_DRAW_LINES
7787 if ( !m_gridLinesEnabled
|| !m_numRows
|| !m_numCols
)
7790 int top
, bottom
, left
, right
;
7792 #if 0 //#ifndef __WXGTK__
7796 m_gridWin
->GetClientSize(&cw
, &ch
);
7798 // virtual coords of visible area
7800 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7801 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7806 reg
.GetBox(x
, y
, w
, h
);
7807 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7808 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7812 m_gridWin
->GetClientSize(&cw
, &ch
);
7813 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7814 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7817 // avoid drawing grid lines past the last row and col
7819 right
= wxMin( right
, GetColRight(GetColAt( m_numCols
- 1 )) );
7820 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7822 // no gridlines inside multicells, clip them out
7823 int leftCol
= GetColPos( internalXToCol(left
) );
7824 int topRow
= internalYToRow(top
);
7825 int rightCol
= GetColPos( internalXToCol(right
) );
7826 int bottomRow
= internalYToRow(bottom
);
7828 #if !defined(__WXMAC__) || wxMAC_USE_CORE_GRAPHICS
7829 wxRegion
clippedcells(0, 0, cw
, ch
);
7831 int i
, j
, cell_rows
, cell_cols
;
7834 for (j
=topRow
; j
<=bottomRow
; j
++)
7837 for (colPos
=leftCol
; colPos
<=rightCol
; colPos
++)
7839 i
= GetColAt( colPos
);
7841 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7842 if ((cell_rows
> 1) || (cell_cols
> 1))
7844 rect
= CellToRect(j
,i
);
7845 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7846 clippedcells
.Subtract(rect
);
7848 else if ((cell_rows
< 0) || (cell_cols
< 0))
7850 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7851 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7852 clippedcells
.Subtract(rect
);
7857 wxRegion
clippedcells( left
, top
, right
- left
, bottom
- top
);
7859 int i
, j
, cell_rows
, cell_cols
;
7862 for (j
=topRow
; j
<=bottomRow
; j
++)
7864 for (i
=leftCol
; i
<=rightCol
; i
++)
7866 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7867 if ((cell_rows
> 1) || (cell_cols
> 1))
7869 rect
= CellToRect(j
, i
);
7870 clippedcells
.Subtract(rect
);
7872 else if ((cell_rows
< 0) || (cell_cols
< 0))
7874 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7875 clippedcells
.Subtract(rect
);
7881 dc
.SetClippingRegion( clippedcells
);
7884 // horizontal grid lines
7886 // already declared above - int i;
7887 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7889 int bot
= GetRowBottom(i
) - 1;
7898 dc
.SetPen( GetRowGridLinePen(i
) );
7899 dc
.DrawLine( left
, bot
, right
, bot
);
7903 // vertical grid lines
7906 for ( colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
7908 i
= GetColAt( colPos
);
7910 int colRight
= GetColRight(i
);
7912 if (GetLayoutDirection() != wxLayout_RightToLeft
)
7916 if ( colRight
> right
)
7921 if ( colRight
>= left
)
7923 dc
.SetPen( GetColGridLinePen(i
) );
7924 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7928 dc
.DestroyClippingRegion();
7931 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
7937 size_t numLabels
= rows
.GetCount();
7939 for ( i
= 0; i
< numLabels
; i
++ )
7941 DrawRowLabel( dc
, rows
[i
] );
7945 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7947 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7955 rect
.SetY( GetRowTop(row
) + 1 );
7956 rect
.SetWidth( m_rowLabelWidth
- 2 );
7957 rect
.SetHeight( GetRowHeight(row
) - 2 );
7959 CalcScrolledPosition( 0, rect
.y
, NULL
, &rect
.y
);
7961 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7963 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7965 int rowTop
= GetRowTop(row
),
7966 rowBottom
= GetRowBottom(row
) - 1;
7968 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
7969 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
7970 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7971 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7973 dc
.SetPen( *wxWHITE_PEN
);
7974 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7975 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
7978 dc
.SetBackgroundMode( wxTRANSPARENT
);
7979 dc
.SetTextForeground( GetLabelTextColour() );
7980 dc
.SetFont( GetLabelFont() );
7983 GetRowLabelAlignment( &hAlign
, &vAlign
);
7986 rect
.SetY( GetRowTop(row
) + 2 );
7987 rect
.SetWidth( m_rowLabelWidth
- 4 );
7988 rect
.SetHeight( GetRowHeight(row
) - 4 );
7989 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7992 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7998 size_t numLabels
= cols
.GetCount();
8000 for ( i
= 0; i
< numLabels
; i
++ )
8002 DrawColLabel( dc
, cols
[i
] );
8006 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
8008 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
8011 int colLeft
= GetColLeft(col
);
8017 rect
.SetX( colLeft
+ 1 );
8019 rect
.SetWidth( GetColWidth(col
) - 2 );
8020 rect
.SetHeight( m_colLabelHeight
- 2 );
8022 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
8024 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
8026 int colRight
= GetColRight(col
) - 1;
8028 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
8029 dc
.DrawLine( colRight
, 0, colRight
, m_colLabelHeight
- 1 );
8030 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
8031 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
8032 colRight
+ 1, m_colLabelHeight
- 1 );
8034 dc
.SetPen( *wxWHITE_PEN
);
8035 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
8036 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
8039 dc
.SetBackgroundMode( wxTRANSPARENT
);
8040 dc
.SetTextForeground( GetLabelTextColour() );
8041 dc
.SetFont( GetLabelFont() );
8043 int hAlign
, vAlign
, orient
;
8044 GetColLabelAlignment( &hAlign
, &vAlign
);
8045 orient
= GetColLabelTextOrientation();
8047 rect
.SetX( colLeft
+ 2 );
8049 rect
.SetWidth( GetColWidth(col
) - 4 );
8050 rect
.SetHeight( m_colLabelHeight
- 4 );
8051 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
8054 void wxGrid::DrawTextRectangle( wxDC
& dc
,
8055 const wxString
& value
,
8059 int textOrientation
)
8061 wxArrayString lines
;
8063 StringToLines( value
, lines
);
8065 // Forward to new API.
8066 DrawTextRectangle( dc
,
8074 // VZ: this should be replaced with wxDC::DrawLabel() to which we just have to
8075 // add textOrientation support
8076 void wxGrid::DrawTextRectangle(wxDC
& dc
,
8077 const wxArrayString
& lines
,
8081 int textOrientation
)
8083 if ( lines
.empty() )
8086 wxDCClipper
clip(dc
, rect
);
8091 if ( textOrientation
== wxHORIZONTAL
)
8092 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
8094 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
8098 switch ( vertAlign
)
8100 case wxALIGN_BOTTOM
:
8101 if ( textOrientation
== wxHORIZONTAL
)
8102 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
8104 x
= rect
.x
+ rect
.width
- textWidth
;
8107 case wxALIGN_CENTRE
:
8108 if ( textOrientation
== wxHORIZONTAL
)
8109 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
8111 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
8116 if ( textOrientation
== wxHORIZONTAL
)
8123 // Align each line of a multi-line label
8124 size_t nLines
= lines
.GetCount();
8125 for ( size_t l
= 0; l
< nLines
; l
++ )
8127 const wxString
& line
= lines
[l
];
8131 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
8135 wxCoord lineWidth
= 0,
8137 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
8139 switch ( horizAlign
)
8142 if ( textOrientation
== wxHORIZONTAL
)
8143 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
8145 y
= rect
.y
+ lineWidth
+ 1;
8148 case wxALIGN_CENTRE
:
8149 if ( textOrientation
== wxHORIZONTAL
)
8150 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
8152 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
8157 if ( textOrientation
== wxHORIZONTAL
)
8160 y
= rect
.y
+ rect
.height
- 1;
8164 if ( textOrientation
== wxHORIZONTAL
)
8166 dc
.DrawText( line
, x
, y
);
8171 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
8177 // Split multi-line text up into an array of strings.
8178 // Any existing contents of the string array are preserved.
8180 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
8184 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
8185 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
8187 while ( startPos
< (int)tVal
.length() )
8189 pos
= tVal
.Mid(startPos
).Find( eol
);
8194 else if ( pos
== 0 )
8196 lines
.Add( wxEmptyString
);
8200 lines
.Add( value
.Mid(startPos
, pos
) );
8203 startPos
+= pos
+ 1;
8206 if ( startPos
< (int)value
.length() )
8208 lines
.Add( value
.Mid( startPos
) );
8212 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
8213 const wxArrayString
& lines
,
8214 long *width
, long *height
) const
8218 wxCoord lineW
= 0, lineH
= 0;
8221 for ( i
= 0; i
< lines
.GetCount(); i
++ )
8223 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
8224 w
= wxMax( w
, lineW
);
8233 // ------ Batch processing.
8235 void wxGrid::EndBatch()
8237 if ( m_batchCount
> 0 )
8240 if ( !m_batchCount
)
8243 m_rowLabelWin
->Refresh();
8244 m_colLabelWin
->Refresh();
8245 m_cornerLabelWin
->Refresh();
8246 m_gridWin
->Refresh();
8251 // Use this, rather than wxWindow::Refresh(), to force an immediate
8252 // repainting of the grid. Has no effect if you are already inside a
8253 // BeginBatch / EndBatch block.
8255 void wxGrid::ForceRefresh()
8261 bool wxGrid::Enable(bool enable
)
8263 if ( !wxScrolledWindow::Enable(enable
) )
8266 // redraw in the new state
8267 m_gridWin
->Refresh();
8273 // ------ Edit control functions
8276 void wxGrid::EnableEditing( bool edit
)
8278 // TODO: improve this ?
8280 if ( edit
!= m_editable
)
8283 EnableCellEditControl(edit
);
8288 void wxGrid::EnableCellEditControl( bool enable
)
8293 if ( enable
!= m_cellEditCtrlEnabled
)
8297 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
8300 // this should be checked by the caller!
8301 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8303 // do it before ShowCellEditControl()
8304 m_cellEditCtrlEnabled
= enable
;
8306 ShowCellEditControl();
8310 //FIXME:add veto support
8311 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
8313 HideCellEditControl();
8314 SaveEditControlValue();
8316 // do it after HideCellEditControl()
8317 m_cellEditCtrlEnabled
= enable
;
8322 bool wxGrid::IsCurrentCellReadOnly() const
8325 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
8326 bool readonly
= attr
->IsReadOnly();
8332 bool wxGrid::CanEnableCellControl() const
8334 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
8335 !IsCurrentCellReadOnly();
8338 bool wxGrid::IsCellEditControlEnabled() const
8340 // the cell edit control might be disable for all cells or just for the
8341 // current one if it's read only
8342 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
8345 bool wxGrid::IsCellEditControlShown() const
8347 bool isShown
= false;
8349 if ( m_cellEditCtrlEnabled
)
8351 int row
= m_currentCellCoords
.GetRow();
8352 int col
= m_currentCellCoords
.GetCol();
8353 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8354 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
8359 if ( editor
->IsCreated() )
8361 isShown
= editor
->GetControl()->IsShown();
8371 void wxGrid::ShowCellEditControl()
8373 if ( IsCellEditControlEnabled() )
8375 if ( !IsVisible( m_currentCellCoords
, false ) )
8377 m_cellEditCtrlEnabled
= false;
8382 wxRect rect
= CellToRect( m_currentCellCoords
);
8383 int row
= m_currentCellCoords
.GetRow();
8384 int col
= m_currentCellCoords
.GetCol();
8386 // if this is part of a multicell, find owner (topleft)
8387 int cell_rows
, cell_cols
;
8388 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8389 if ( cell_rows
<= 0 || cell_cols
<= 0 )
8393 m_currentCellCoords
.SetRow( row
);
8394 m_currentCellCoords
.SetCol( col
);
8397 // erase the highlight and the cell contents because the editor
8398 // might not cover the entire cell
8399 wxClientDC
dc( m_gridWin
);
8401 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8402 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
8403 dc
.SetPen(*wxTRANSPARENT_PEN
);
8404 dc
.DrawRectangle(rect
);
8406 // convert to scrolled coords
8407 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8413 // cell is shifted by one pixel
8414 // However, don't allow x or y to become negative
8415 // since the SetSize() method interprets that as
8422 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8423 if ( !editor
->IsCreated() )
8425 editor
->Create(m_gridWin
, wxID_ANY
,
8426 new wxGridCellEditorEvtHandler(this, editor
));
8428 wxGridEditorCreatedEvent
evt(GetId(),
8429 wxEVT_GRID_EDITOR_CREATED
,
8433 editor
->GetControl());
8434 GetEventHandler()->ProcessEvent(evt
);
8437 // resize editor to overflow into righthand cells if allowed
8438 int maxWidth
= rect
.width
;
8439 wxString value
= GetCellValue(row
, col
);
8440 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
8443 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
8444 if (maxWidth
< rect
.width
)
8445 maxWidth
= rect
.width
;
8448 int client_right
= m_gridWin
->GetClientSize().GetWidth();
8449 if (rect
.x
+ maxWidth
> client_right
)
8450 maxWidth
= client_right
- rect
.x
;
8452 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
8454 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8455 // may have changed earlier
8456 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
8459 GetCellSize( row
, i
, &c_rows
, &c_cols
);
8461 // looks weird going over a multicell
8462 if (m_table
->IsEmptyCell( row
, i
) &&
8463 (rect
.width
< maxWidth
) && (c_rows
== 1))
8465 rect
.width
+= GetColWidth( i
);
8471 if (rect
.GetRight() > client_right
)
8472 rect
.SetRight( client_right
- 1 );
8475 editor
->SetCellAttr( attr
);
8476 editor
->SetSize( rect
);
8478 editor
->GetControl()->Move(
8479 editor
->GetControl()->GetPosition().x
+ nXMove
,
8480 editor
->GetControl()->GetPosition().y
);
8481 editor
->Show( true, attr
);
8483 // recalc dimensions in case we need to
8484 // expand the scrolled window to account for editor
8487 editor
->BeginEdit(row
, col
, this);
8488 editor
->SetCellAttr(NULL
);
8496 void wxGrid::HideCellEditControl()
8498 if ( IsCellEditControlEnabled() )
8500 int row
= m_currentCellCoords
.GetRow();
8501 int col
= m_currentCellCoords
.GetCol();
8503 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8504 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
8505 editor
->Show( false );
8509 m_gridWin
->SetFocus();
8511 // refresh whole row to the right
8512 wxRect
rect( CellToRect(row
, col
) );
8513 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8514 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8517 // ensure that the pixels under the focus ring get refreshed as well
8518 rect
.Inflate(10, 10);
8521 m_gridWin
->Refresh( false, &rect
);
8525 void wxGrid::SaveEditControlValue()
8527 if ( IsCellEditControlEnabled() )
8529 int row
= m_currentCellCoords
.GetRow();
8530 int col
= m_currentCellCoords
.GetCol();
8532 wxString oldval
= GetCellValue(row
, col
);
8534 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8535 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8536 bool changed
= editor
->EndEdit(row
, col
, this);
8543 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
8544 m_currentCellCoords
.GetRow(),
8545 m_currentCellCoords
.GetCol() ) < 0 )
8547 // Event has been vetoed, set the data back.
8548 SetCellValue(row
, col
, oldval
);
8555 // ------ Grid location functions
8556 // Note that all of these functions work with the logical coordinates of
8557 // grid cells and labels so you will need to convert from device
8558 // coordinates for mouse events etc.
8561 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
) const
8563 int row
= YToRow(y
);
8564 int col
= XToCol(x
);
8566 if ( row
== -1 || col
== -1 )
8568 coords
= wxGridNoCellCoords
;
8572 coords
.Set( row
, col
);
8576 // Internal Helper function for computing row or column from some
8577 // (unscrolled) coordinate value, using either
8578 // m_defaultRowHeight/m_defaultColWidth or binary search on array
8579 // of m_rowBottoms/m_ColRights to speed up the search!
8581 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
8582 const wxArrayInt
& BorderArray
, int nMax
,
8586 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
8591 size_t i_max
= coord
/ defaultDist
,
8594 if (BorderArray
.IsEmpty())
8596 if ((int) i_max
< nMax
)
8598 return clipToMinMax
? nMax
- 1 : -1;
8601 if ( i_max
>= BorderArray
.GetCount())
8603 i_max
= BorderArray
.GetCount() - 1;
8607 if ( coord
>= BorderArray
[i_max
])
8611 i_max
= coord
/ minDist
;
8613 i_max
= BorderArray
.GetCount() - 1;
8616 if ( i_max
>= BorderArray
.GetCount())
8617 i_max
= BorderArray
.GetCount() - 1;
8620 if ( coord
>= BorderArray
[i_max
])
8621 return clipToMinMax
? (int)i_max
: -1;
8622 if ( coord
< BorderArray
[0] )
8625 while ( i_max
- i_min
> 0 )
8627 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
8628 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8629 if (coord
>= BorderArray
[ i_max
- 1])
8633 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
8634 if (coord
< BorderArray
[median
])
8643 int wxGrid::YToRow( int y
) const
8645 return CoordToRowOrCol(y
, m_defaultRowHeight
,
8646 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8649 int wxGrid::XToCol( int x
, bool clipToMinMax
) const
8652 return clipToMinMax
&& (m_numCols
> 0) ? GetColAt( 0 ) : -1;
8654 wxASSERT_MSG(m_defaultColWidth
> 0, wxT("Default column width can not be zero"));
8656 int maxPos
= x
/ m_defaultColWidth
;
8659 if (m_colRights
.IsEmpty())
8661 if(maxPos
< m_numCols
)
8662 return GetColAt( maxPos
);
8663 return clipToMinMax
? GetColAt( m_numCols
- 1 ) : -1;
8666 if ( maxPos
>= m_numCols
)
8667 maxPos
= m_numCols
- 1;
8670 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8673 if (m_minAcceptableColWidth
)
8674 maxPos
= x
/ m_minAcceptableColWidth
;
8676 maxPos
= m_numCols
- 1;
8678 if ( maxPos
>= m_numCols
)
8679 maxPos
= m_numCols
- 1;
8682 //X is beyond the last column
8683 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8684 return clipToMinMax
? GetColAt( maxPos
) : -1;
8686 //X is before the first column
8687 if ( x
< m_colRights
[GetColAt( 0 )] )
8688 return GetColAt( 0 );
8690 //Perform a binary search
8691 while ( maxPos
- minPos
> 0 )
8693 wxCHECK_MSG(m_colRights
[GetColAt( minPos
)] <= x
&& x
< m_colRights
[GetColAt( maxPos
)],
8694 0, _T("wxGrid: internal error in XToCol"));
8696 if (x
>= m_colRights
[GetColAt( maxPos
- 1 )])
8697 return GetColAt( maxPos
);
8700 int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
8701 if (x
< m_colRights
[GetColAt( median
)])
8706 return GetColAt( maxPos
);
8709 // return the row number that that the y coord is near
8710 // the edge of, or -1 if not near an edge.
8711 // coords can only possibly be near an edge if
8712 // (a) the row/column is large enough to still allow for an "inner" area
8713 // that is _not_ nead the edge (i.e., if the height/width is smaller
8714 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8717 // (b) resizing rows/columns (the thing for which edge detection is
8718 // relevant at all) is enabled.
8720 int wxGrid::YToEdgeOfRow( int y
) const
8723 i
= internalYToRow(y
);
8725 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragRowSize() )
8727 // We know that we are in row i, test whether we are
8728 // close enough to lower or upper border, respectively.
8729 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8731 else if ( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8738 // return the col number that that the x coord is near the edge of, or
8739 // -1 if not near an edge
8740 // See comment at YToEdgeOfRow for conditions on edge detection.
8742 int wxGrid::XToEdgeOfCol( int x
) const
8745 i
= internalXToCol(x
);
8747 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragColSize() )
8749 // We know that we are in column i; test whether we are
8750 // close enough to right or left border, respectively.
8751 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8753 else if ( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8760 wxRect
wxGrid::CellToRect( int row
, int col
) const
8762 wxRect
rect( -1, -1, -1, -1 );
8764 if ( row
>= 0 && row
< m_numRows
&&
8765 col
>= 0 && col
< m_numCols
)
8767 int i
, cell_rows
, cell_cols
;
8768 rect
.width
= rect
.height
= 0;
8769 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8770 // if negative then find multicell owner
8775 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8777 rect
.x
= GetColLeft(col
);
8778 rect
.y
= GetRowTop(row
);
8779 for (i
=col
; i
< col
+ cell_cols
; i
++)
8780 rect
.width
+= GetColWidth(i
);
8781 for (i
=row
; i
< row
+ cell_rows
; i
++)
8782 rect
.height
+= GetRowHeight(i
);
8785 // if grid lines are enabled, then the area of the cell is a bit smaller
8786 if (m_gridLinesEnabled
)
8795 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
8797 // get the cell rectangle in logical coords
8799 wxRect
r( CellToRect( row
, col
) );
8801 // convert to device coords
8803 int left
, top
, right
, bottom
;
8804 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8805 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8807 // check against the client area of the grid window
8809 m_gridWin
->GetClientSize( &cw
, &ch
);
8811 if ( wholeCellVisible
)
8813 // is the cell wholly visible ?
8814 return ( left
>= 0 && right
<= cw
&&
8815 top
>= 0 && bottom
<= ch
);
8819 // is the cell partly visible ?
8821 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8822 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8826 // make the specified cell location visible by doing a minimal amount
8829 void wxGrid::MakeCellVisible( int row
, int col
)
8832 int xpos
= -1, ypos
= -1;
8834 if ( row
>= 0 && row
< m_numRows
&&
8835 col
>= 0 && col
< m_numCols
)
8837 // get the cell rectangle in logical coords
8838 wxRect
r( CellToRect( row
, col
) );
8840 // convert to device coords
8841 int left
, top
, right
, bottom
;
8842 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8843 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8846 m_gridWin
->GetClientSize( &cw
, &ch
);
8852 else if ( bottom
> ch
)
8854 int h
= r
.GetHeight();
8856 for ( i
= row
- 1; i
>= 0; i
-- )
8858 int rowHeight
= GetRowHeight(i
);
8859 if ( h
+ rowHeight
> ch
)
8866 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8867 // have rounding errors (this is important, because if we do,
8868 // we might not scroll at all and some cells won't be redrawn)
8870 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
8871 // so just add a full scroll unit...
8872 ypos
+= m_scrollLineY
;
8875 // special handling for wide cells - show always left part of the cell!
8876 // Otherwise, e.g. when stepping from row to row, it would jump between
8877 // left and right part of the cell on every step!
8879 if ( left
< 0 || (right
- left
) >= cw
)
8883 else if ( right
> cw
)
8885 // position the view so that the cell is on the right
8887 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8888 xpos
= x0
+ (right
- cw
);
8890 // see comment for ypos above
8891 xpos
+= m_scrollLineX
;
8894 if ( xpos
!= -1 || ypos
!= -1 )
8897 xpos
/= m_scrollLineX
;
8899 ypos
/= m_scrollLineY
;
8900 Scroll( xpos
, ypos
);
8907 // ------ Grid cursor movement functions
8910 bool wxGrid::MoveCursorUp( bool expandSelection
)
8912 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8913 m_currentCellCoords
.GetRow() >= 0 )
8915 if ( expandSelection
)
8917 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8918 m_selectingKeyboard
= m_currentCellCoords
;
8919 if ( m_selectingKeyboard
.GetRow() > 0 )
8921 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8922 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8923 m_selectingKeyboard
.GetCol() );
8924 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8927 else if ( m_currentCellCoords
.GetRow() > 0 )
8929 int row
= m_currentCellCoords
.GetRow() - 1;
8930 int col
= m_currentCellCoords
.GetCol();
8932 MakeCellVisible( row
, col
);
8933 SetCurrentCell( row
, col
);
8944 bool wxGrid::MoveCursorDown( bool expandSelection
)
8946 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8947 m_currentCellCoords
.GetRow() < m_numRows
)
8949 if ( expandSelection
)
8951 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8952 m_selectingKeyboard
= m_currentCellCoords
;
8953 if ( m_selectingKeyboard
.GetRow() < m_numRows
- 1 )
8955 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8956 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8957 m_selectingKeyboard
.GetCol() );
8958 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8961 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8963 int row
= m_currentCellCoords
.GetRow() + 1;
8964 int col
= m_currentCellCoords
.GetCol();
8966 MakeCellVisible( row
, col
);
8967 SetCurrentCell( row
, col
);
8978 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8980 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8981 m_currentCellCoords
.GetCol() >= 0 )
8983 if ( expandSelection
)
8985 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8986 m_selectingKeyboard
= m_currentCellCoords
;
8987 if ( m_selectingKeyboard
.GetCol() > 0 )
8989 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8990 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8991 m_selectingKeyboard
.GetCol() );
8992 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8995 else if ( GetColPos( m_currentCellCoords
.GetCol() ) > 0 )
8997 int row
= m_currentCellCoords
.GetRow();
8998 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) - 1 );
9001 MakeCellVisible( row
, col
);
9002 SetCurrentCell( row
, col
);
9013 bool wxGrid::MoveCursorRight( bool expandSelection
)
9015 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
9016 m_currentCellCoords
.GetCol() < m_numCols
)
9018 if ( expandSelection
)
9020 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
9021 m_selectingKeyboard
= m_currentCellCoords
;
9022 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
9024 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
9025 MakeCellVisible( m_selectingKeyboard
.GetRow(),
9026 m_selectingKeyboard
.GetCol() );
9027 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9030 else if ( GetColPos( m_currentCellCoords
.GetCol() ) < m_numCols
- 1 )
9032 int row
= m_currentCellCoords
.GetRow();
9033 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) + 1 );
9036 MakeCellVisible( row
, col
);
9037 SetCurrentCell( row
, col
);
9048 bool wxGrid::MovePageUp()
9050 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9053 int row
= m_currentCellCoords
.GetRow();
9057 m_gridWin
->GetClientSize( &cw
, &ch
);
9059 int y
= GetRowTop(row
);
9060 int newRow
= internalYToRow( y
- ch
+ 1 );
9062 if ( newRow
== row
)
9064 // row > 0, so newRow can never be less than 0 here.
9068 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
9069 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
9077 bool wxGrid::MovePageDown()
9079 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9082 int row
= m_currentCellCoords
.GetRow();
9083 if ( (row
+ 1) < m_numRows
)
9086 m_gridWin
->GetClientSize( &cw
, &ch
);
9088 int y
= GetRowTop(row
);
9089 int newRow
= internalYToRow( y
+ ch
);
9090 if ( newRow
== row
)
9092 // row < m_numRows, so newRow can't overflow here.
9096 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
9097 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
9105 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
9108 m_currentCellCoords
!= wxGridNoCellCoords
&&
9109 m_currentCellCoords
.GetRow() > 0 )
9111 int row
= m_currentCellCoords
.GetRow();
9112 int col
= m_currentCellCoords
.GetCol();
9114 if ( m_table
->IsEmptyCell(row
, col
) )
9116 // starting in an empty cell: find the next block of
9122 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9126 else if ( m_table
->IsEmptyCell(row
- 1, col
) )
9128 // starting at the top of a block: find the next block
9134 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9140 // starting within a block: find the top of the block
9145 if ( m_table
->IsEmptyCell(row
, col
) )
9153 MakeCellVisible( row
, col
);
9154 if ( expandSelection
)
9156 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9157 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9162 SetCurrentCell( row
, col
);
9171 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
9174 m_currentCellCoords
!= wxGridNoCellCoords
&&
9175 m_currentCellCoords
.GetRow() < m_numRows
- 1 )
9177 int row
= m_currentCellCoords
.GetRow();
9178 int col
= m_currentCellCoords
.GetCol();
9180 if ( m_table
->IsEmptyCell(row
, col
) )
9182 // starting in an empty cell: find the next block of
9185 while ( row
< m_numRows
- 1 )
9188 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9192 else if ( m_table
->IsEmptyCell(row
+ 1, col
) )
9194 // starting at the bottom of a block: find the next block
9197 while ( row
< m_numRows
- 1 )
9200 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9206 // starting within a block: find the bottom of the block
9208 while ( row
< m_numRows
- 1 )
9211 if ( m_table
->IsEmptyCell(row
, col
) )
9219 MakeCellVisible( row
, col
);
9220 if ( expandSelection
)
9222 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9223 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9228 SetCurrentCell( row
, col
);
9237 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
9240 m_currentCellCoords
!= wxGridNoCellCoords
&&
9241 m_currentCellCoords
.GetCol() > 0 )
9243 int row
= m_currentCellCoords
.GetRow();
9244 int col
= m_currentCellCoords
.GetCol();
9246 if ( m_table
->IsEmptyCell(row
, col
) )
9248 // starting in an empty cell: find the next block of
9254 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9258 else if ( m_table
->IsEmptyCell(row
, col
- 1) )
9260 // starting at the left of a block: find the next block
9266 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9272 // starting within a block: find the left of the block
9277 if ( m_table
->IsEmptyCell(row
, col
) )
9285 MakeCellVisible( row
, col
);
9286 if ( expandSelection
)
9288 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9289 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9294 SetCurrentCell( row
, col
);
9303 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
9306 m_currentCellCoords
!= wxGridNoCellCoords
&&
9307 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
9309 int row
= m_currentCellCoords
.GetRow();
9310 int col
= m_currentCellCoords
.GetCol();
9312 if ( m_table
->IsEmptyCell(row
, col
) )
9314 // starting in an empty cell: find the next block of
9317 while ( col
< m_numCols
- 1 )
9320 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9324 else if ( m_table
->IsEmptyCell(row
, col
+ 1) )
9326 // starting at the right of a block: find the next block
9329 while ( col
< m_numCols
- 1 )
9332 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9338 // starting within a block: find the right of the block
9340 while ( col
< m_numCols
- 1 )
9343 if ( m_table
->IsEmptyCell(row
, col
) )
9351 MakeCellVisible( row
, col
);
9352 if ( expandSelection
)
9354 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9355 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9360 SetCurrentCell( row
, col
);
9370 // ------ Label values and formatting
9373 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
9376 *horiz
= m_rowLabelHorizAlign
;
9378 *vert
= m_rowLabelVertAlign
;
9381 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
9384 *horiz
= m_colLabelHorizAlign
;
9386 *vert
= m_colLabelVertAlign
;
9389 int wxGrid::GetColLabelTextOrientation() const
9391 return m_colLabelTextOrientation
;
9394 wxString
wxGrid::GetRowLabelValue( int row
) const
9398 return m_table
->GetRowLabelValue( row
);
9408 wxString
wxGrid::GetColLabelValue( int col
) const
9412 return m_table
->GetColLabelValue( col
);
9422 void wxGrid::SetRowLabelSize( int width
)
9424 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
9426 if ( width
== wxGRID_AUTOSIZE
)
9428 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
9431 if ( width
!= m_rowLabelWidth
)
9435 m_rowLabelWin
->Show( false );
9436 m_cornerLabelWin
->Show( false );
9438 else if ( m_rowLabelWidth
== 0 )
9440 m_rowLabelWin
->Show( true );
9441 if ( m_colLabelHeight
> 0 )
9442 m_cornerLabelWin
->Show( true );
9445 m_rowLabelWidth
= width
;
9447 wxScrolledWindow::Refresh( true );
9451 void wxGrid::SetColLabelSize( int height
)
9453 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
9455 if ( height
== wxGRID_AUTOSIZE
)
9457 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
9460 if ( height
!= m_colLabelHeight
)
9464 m_colLabelWin
->Show( false );
9465 m_cornerLabelWin
->Show( false );
9467 else if ( m_colLabelHeight
== 0 )
9469 m_colLabelWin
->Show( true );
9470 if ( m_rowLabelWidth
> 0 )
9471 m_cornerLabelWin
->Show( true );
9474 m_colLabelHeight
= height
;
9476 wxScrolledWindow::Refresh( true );
9480 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
9482 if ( m_labelBackgroundColour
!= colour
)
9484 m_labelBackgroundColour
= colour
;
9485 m_rowLabelWin
->SetBackgroundColour( colour
);
9486 m_colLabelWin
->SetBackgroundColour( colour
);
9487 m_cornerLabelWin
->SetBackgroundColour( colour
);
9489 if ( !GetBatchCount() )
9491 m_rowLabelWin
->Refresh();
9492 m_colLabelWin
->Refresh();
9493 m_cornerLabelWin
->Refresh();
9498 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
9500 if ( m_labelTextColour
!= colour
)
9502 m_labelTextColour
= colour
;
9503 if ( !GetBatchCount() )
9505 m_rowLabelWin
->Refresh();
9506 m_colLabelWin
->Refresh();
9511 void wxGrid::SetLabelFont( const wxFont
& font
)
9514 if ( !GetBatchCount() )
9516 m_rowLabelWin
->Refresh();
9517 m_colLabelWin
->Refresh();
9521 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
9523 // allow old (incorrect) defs to be used
9526 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9527 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9528 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9533 case wxTOP
: vert
= wxALIGN_TOP
; break;
9534 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9535 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9538 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9540 m_rowLabelHorizAlign
= horiz
;
9543 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9545 m_rowLabelVertAlign
= vert
;
9548 if ( !GetBatchCount() )
9550 m_rowLabelWin
->Refresh();
9554 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
9556 // allow old (incorrect) defs to be used
9559 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9560 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9561 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9566 case wxTOP
: vert
= wxALIGN_TOP
; break;
9567 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9568 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9571 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9573 m_colLabelHorizAlign
= horiz
;
9576 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9578 m_colLabelVertAlign
= vert
;
9581 if ( !GetBatchCount() )
9583 m_colLabelWin
->Refresh();
9587 // Note: under MSW, the default column label font must be changed because it
9588 // does not support vertical printing
9590 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9591 // pGrid->SetLabelFont(font);
9592 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9594 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9596 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9597 m_colLabelTextOrientation
= textOrientation
;
9599 if ( !GetBatchCount() )
9600 m_colLabelWin
->Refresh();
9603 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9607 m_table
->SetRowLabelValue( row
, s
);
9608 if ( !GetBatchCount() )
9610 wxRect rect
= CellToRect( row
, 0 );
9611 if ( rect
.height
> 0 )
9613 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9615 rect
.width
= m_rowLabelWidth
;
9616 m_rowLabelWin
->Refresh( true, &rect
);
9622 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9626 m_table
->SetColLabelValue( col
, s
);
9627 if ( !GetBatchCount() )
9629 wxRect rect
= CellToRect( 0, col
);
9630 if ( rect
.width
> 0 )
9632 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9634 rect
.height
= m_colLabelHeight
;
9635 m_colLabelWin
->Refresh( true, &rect
);
9641 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9643 if ( m_gridLineColour
!= colour
)
9645 m_gridLineColour
= colour
;
9647 wxClientDC
dc( m_gridWin
);
9649 DrawAllGridLines( dc
, wxRegion() );
9653 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9655 if ( m_cellHighlightColour
!= colour
)
9657 m_cellHighlightColour
= colour
;
9659 wxClientDC
dc( m_gridWin
);
9661 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9662 DrawCellHighlight(dc
, attr
);
9667 void wxGrid::SetCellHighlightPenWidth(int width
)
9669 if (m_cellHighlightPenWidth
!= width
)
9671 m_cellHighlightPenWidth
= width
;
9673 // Just redrawing the cell highlight is not enough since that won't
9674 // make any visible change if the the thickness is getting smaller.
9675 int row
= m_currentCellCoords
.GetRow();
9676 int col
= m_currentCellCoords
.GetCol();
9677 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9680 wxRect rect
= CellToRect(row
, col
);
9681 m_gridWin
->Refresh(true, &rect
);
9685 void wxGrid::SetCellHighlightROPenWidth(int width
)
9687 if (m_cellHighlightROPenWidth
!= width
)
9689 m_cellHighlightROPenWidth
= width
;
9691 // Just redrawing the cell highlight is not enough since that won't
9692 // make any visible change if the the thickness is getting smaller.
9693 int row
= m_currentCellCoords
.GetRow();
9694 int col
= m_currentCellCoords
.GetCol();
9695 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9698 wxRect rect
= CellToRect(row
, col
);
9699 m_gridWin
->Refresh(true, &rect
);
9703 void wxGrid::EnableGridLines( bool enable
)
9705 if ( enable
!= m_gridLinesEnabled
)
9707 m_gridLinesEnabled
= enable
;
9709 if ( !GetBatchCount() )
9713 wxClientDC
dc( m_gridWin
);
9715 DrawAllGridLines( dc
, wxRegion() );
9719 m_gridWin
->Refresh();
9725 int wxGrid::GetDefaultRowSize() const
9727 return m_defaultRowHeight
;
9730 int wxGrid::GetRowSize( int row
) const
9732 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9734 return GetRowHeight(row
);
9737 int wxGrid::GetDefaultColSize() const
9739 return m_defaultColWidth
;
9742 int wxGrid::GetColSize( int col
) const
9744 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9746 return GetColWidth(col
);
9749 // ============================================================================
9750 // access to the grid attributes: each of them has a default value in the grid
9751 // itself and may be overidden on a per-cell basis
9752 // ============================================================================
9754 // ----------------------------------------------------------------------------
9755 // setting default attributes
9756 // ----------------------------------------------------------------------------
9758 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9760 m_defaultCellAttr
->SetBackgroundColour(col
);
9762 m_gridWin
->SetBackgroundColour(col
);
9766 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9768 m_defaultCellAttr
->SetTextColour(col
);
9771 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9773 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9776 void wxGrid::SetDefaultCellOverflow( bool allow
)
9778 m_defaultCellAttr
->SetOverflow(allow
);
9781 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9783 m_defaultCellAttr
->SetFont(font
);
9786 // For editors and renderers the type registry takes precedence over the
9787 // default attr, so we need to register the new editor/renderer for the string
9788 // data type in order to make setting a default editor/renderer appear to
9791 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9793 RegisterDataType(wxGRID_VALUE_STRING
,
9795 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9798 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9800 RegisterDataType(wxGRID_VALUE_STRING
,
9801 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9805 // ----------------------------------------------------------------------------
9806 // access to the default attributes
9807 // ----------------------------------------------------------------------------
9809 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
9811 return m_defaultCellAttr
->GetBackgroundColour();
9814 wxColour
wxGrid::GetDefaultCellTextColour() const
9816 return m_defaultCellAttr
->GetTextColour();
9819 wxFont
wxGrid::GetDefaultCellFont() const
9821 return m_defaultCellAttr
->GetFont();
9824 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
9826 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9829 bool wxGrid::GetDefaultCellOverflow() const
9831 return m_defaultCellAttr
->GetOverflow();
9834 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9836 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9839 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9841 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9844 // ----------------------------------------------------------------------------
9845 // access to cell attributes
9846 // ----------------------------------------------------------------------------
9848 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
9850 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9851 wxColour colour
= attr
->GetBackgroundColour();
9857 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
9859 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9860 wxColour colour
= attr
->GetTextColour();
9866 wxFont
wxGrid::GetCellFont( int row
, int col
) const
9868 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9869 wxFont font
= attr
->GetFont();
9875 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
9877 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9878 attr
->GetAlignment(horiz
, vert
);
9882 bool wxGrid::GetCellOverflow( int row
, int col
) const
9884 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9885 bool allow
= attr
->GetOverflow();
9891 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
9893 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9894 attr
->GetSize( num_rows
, num_cols
);
9898 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
9900 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9901 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9907 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
9909 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9910 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9916 bool wxGrid::IsReadOnly(int row
, int col
) const
9918 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9919 bool isReadOnly
= attr
->IsReadOnly();
9925 // ----------------------------------------------------------------------------
9926 // attribute support: cache, automatic provider creation, ...
9927 // ----------------------------------------------------------------------------
9929 bool wxGrid::CanHaveAttributes() const
9936 return m_table
->CanHaveAttributes();
9939 void wxGrid::ClearAttrCache()
9941 if ( m_attrCache
.row
!= -1 )
9943 wxSafeDecRef(m_attrCache
.attr
);
9944 m_attrCache
.attr
= NULL
;
9945 m_attrCache
.row
= -1;
9949 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9953 wxGrid
*self
= (wxGrid
*)this; // const_cast
9955 self
->ClearAttrCache();
9956 self
->m_attrCache
.row
= row
;
9957 self
->m_attrCache
.col
= col
;
9958 self
->m_attrCache
.attr
= attr
;
9963 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9965 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9967 *attr
= m_attrCache
.attr
;
9968 wxSafeIncRef(m_attrCache
.attr
);
9970 #ifdef DEBUG_ATTR_CACHE
9971 gs_nAttrCacheHits
++;
9978 #ifdef DEBUG_ATTR_CACHE
9979 gs_nAttrCacheMisses
++;
9986 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9988 wxGridCellAttr
*attr
= NULL
;
9989 // Additional test to avoid looking at the cache e.g. for
9990 // wxNoCellCoords, as this will confuse memory management.
9993 if ( !LookupAttr(row
, col
, &attr
) )
9995 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9996 : (wxGridCellAttr
*)NULL
;
9997 CacheAttr(row
, col
, attr
);
10003 attr
->SetDefAttr(m_defaultCellAttr
);
10007 attr
= m_defaultCellAttr
;
10014 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
10016 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
10017 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
10019 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
10020 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
10022 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
10025 attr
= new wxGridCellAttr(m_defaultCellAttr
);
10027 // artificially inc the ref count to match DecRef() in caller
10029 m_table
->SetAttr(attr
, row
, col
);
10035 // ----------------------------------------------------------------------------
10036 // setting column attributes (wrappers around SetColAttr)
10037 // ----------------------------------------------------------------------------
10039 void wxGrid::SetColFormatBool(int col
)
10041 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
10044 void wxGrid::SetColFormatNumber(int col
)
10046 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
10049 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
10051 wxString typeName
= wxGRID_VALUE_FLOAT
;
10052 if ( (width
!= -1) || (precision
!= -1) )
10054 typeName
<< _T(':') << width
<< _T(',') << precision
;
10057 SetColFormatCustom(col
, typeName
);
10060 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
10062 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
10064 attr
= new wxGridCellAttr
;
10065 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
10066 attr
->SetRenderer(renderer
);
10068 SetColAttr(col
, attr
);
10072 // ----------------------------------------------------------------------------
10073 // setting cell attributes: this is forwarded to the table
10074 // ----------------------------------------------------------------------------
10076 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
10078 if ( CanHaveAttributes() )
10080 m_table
->SetAttr(attr
, row
, col
);
10085 wxSafeDecRef(attr
);
10089 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
10091 if ( CanHaveAttributes() )
10093 m_table
->SetRowAttr(attr
, row
);
10098 wxSafeDecRef(attr
);
10102 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
10104 if ( CanHaveAttributes() )
10106 m_table
->SetColAttr(attr
, col
);
10111 wxSafeDecRef(attr
);
10115 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
10117 if ( CanHaveAttributes() )
10119 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10120 attr
->SetBackgroundColour(colour
);
10125 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
10127 if ( CanHaveAttributes() )
10129 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10130 attr
->SetTextColour(colour
);
10135 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
10137 if ( CanHaveAttributes() )
10139 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10140 attr
->SetFont(font
);
10145 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
10147 if ( CanHaveAttributes() )
10149 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10150 attr
->SetAlignment(horiz
, vert
);
10155 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
10157 if ( CanHaveAttributes() )
10159 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10160 attr
->SetOverflow(allow
);
10165 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
10167 if ( CanHaveAttributes() )
10169 int cell_rows
, cell_cols
;
10171 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10172 attr
->GetSize(&cell_rows
, &cell_cols
);
10173 attr
->SetSize(num_rows
, num_cols
);
10176 // Cannot set the size of a cell to 0 or negative values
10177 // While it is perfectly legal to do that, this function cannot
10178 // handle all the possibilies, do it by hand by getting the CellAttr.
10179 // You can only set the size of a cell to 1,1 or greater with this fn
10180 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
10181 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10182 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
10183 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10185 // if this was already a multicell then "turn off" the other cells first
10186 if ((cell_rows
> 1) || (cell_rows
> 1))
10189 for (j
=row
; j
< row
+ cell_rows
; j
++)
10191 for (i
=col
; i
< col
+ cell_cols
; i
++)
10193 if ((i
!= col
) || (j
!= row
))
10195 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10196 attr_stub
->SetSize( 1, 1 );
10197 attr_stub
->DecRef();
10203 // mark the cells that will be covered by this cell to
10204 // negative or zero values to point back at this cell
10205 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
10208 for (j
=row
; j
< row
+ num_rows
; j
++)
10210 for (i
=col
; i
< col
+ num_cols
; i
++)
10212 if ((i
!= col
) || (j
!= row
))
10214 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10215 attr_stub
->SetSize( row
- j
, col
- i
);
10216 attr_stub
->DecRef();
10224 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
10226 if ( CanHaveAttributes() )
10228 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10229 attr
->SetRenderer(renderer
);
10234 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
10236 if ( CanHaveAttributes() )
10238 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10239 attr
->SetEditor(editor
);
10244 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
10246 if ( CanHaveAttributes() )
10248 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10249 attr
->SetReadOnly(isReadOnly
);
10254 // ----------------------------------------------------------------------------
10255 // Data type registration
10256 // ----------------------------------------------------------------------------
10258 void wxGrid::RegisterDataType(const wxString
& typeName
,
10259 wxGridCellRenderer
* renderer
,
10260 wxGridCellEditor
* editor
)
10262 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
10266 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
10268 wxString typeName
= m_table
->GetTypeName(row
, col
);
10269 return GetDefaultEditorForType(typeName
);
10272 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
10274 wxString typeName
= m_table
->GetTypeName(row
, col
);
10275 return GetDefaultRendererForType(typeName
);
10278 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
10280 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10281 if ( index
== wxNOT_FOUND
)
10283 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10288 return m_typeRegistry
->GetEditor(index
);
10291 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
10293 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10294 if ( index
== wxNOT_FOUND
)
10296 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10301 return m_typeRegistry
->GetRenderer(index
);
10304 // ----------------------------------------------------------------------------
10306 // ----------------------------------------------------------------------------
10308 void wxGrid::EnableDragRowSize( bool enable
)
10310 m_canDragRowSize
= enable
;
10313 void wxGrid::EnableDragColSize( bool enable
)
10315 m_canDragColSize
= enable
;
10318 void wxGrid::EnableDragGridSize( bool enable
)
10320 m_canDragGridSize
= enable
;
10323 void wxGrid::EnableDragCell( bool enable
)
10325 m_canDragCell
= enable
;
10328 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
10330 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
10332 if ( resizeExistingRows
)
10334 // since we are resizing all rows to the default row size,
10335 // we can simply clear the row heights and row bottoms
10336 // arrays (which also allows us to take advantage of
10337 // some speed optimisations)
10338 m_rowHeights
.Empty();
10339 m_rowBottoms
.Empty();
10340 if ( !GetBatchCount() )
10345 void wxGrid::SetRowSize( int row
, int height
)
10347 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
10349 // See comment in SetColSize
10350 if ( height
< GetRowMinimalAcceptableHeight())
10353 if ( m_rowHeights
.IsEmpty() )
10355 // need to really create the array
10359 int h
= wxMax( 0, height
);
10360 int diff
= h
- m_rowHeights
[row
];
10362 m_rowHeights
[row
] = h
;
10363 for ( int i
= row
; i
< m_numRows
; i
++ )
10365 m_rowBottoms
[i
] += diff
;
10368 if ( !GetBatchCount() )
10372 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
10374 // we dont allow zero default column width
10375 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
10377 if ( resizeExistingCols
)
10379 // since we are resizing all columns to the default column size,
10380 // we can simply clear the col widths and col rights
10381 // arrays (which also allows us to take advantage of
10382 // some speed optimisations)
10383 m_colWidths
.Empty();
10384 m_colRights
.Empty();
10385 if ( !GetBatchCount() )
10390 void wxGrid::SetColSize( int col
, int width
)
10392 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
10394 // should we check that it's bigger than GetColMinimalWidth(col) here?
10396 // No, because it is reasonable to assume the library user know's
10397 // what he is doing. However we should test against the weaker
10398 // constraint of minimalAcceptableWidth, as this breaks rendering
10400 // This test then fixes sf.net bug #645734
10402 if ( width
< GetColMinimalAcceptableWidth() )
10405 if ( m_colWidths
.IsEmpty() )
10407 // need to really create the array
10411 // if < 0 then calculate new width from label
10415 wxArrayString lines
;
10416 wxClientDC
dc(m_colLabelWin
);
10417 dc
.SetFont(GetLabelFont());
10418 StringToLines(GetColLabelValue(col
), lines
);
10419 GetTextBoxSize(dc
, lines
, &w
, &h
);
10423 int w
= wxMax( 0, width
);
10424 int diff
= w
- m_colWidths
[col
];
10425 m_colWidths
[col
] = w
;
10427 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
10429 m_colRights
[GetColAt(colPos
)] += diff
;
10432 if ( !GetBatchCount() )
10436 void wxGrid::SetColMinimalWidth( int col
, int width
)
10438 if (width
> GetColMinimalAcceptableWidth())
10440 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10441 m_colMinWidths
[key
] = width
;
10445 void wxGrid::SetRowMinimalHeight( int row
, int width
)
10447 if (width
> GetRowMinimalAcceptableHeight())
10449 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10450 m_rowMinHeights
[key
] = width
;
10454 int wxGrid::GetColMinimalWidth(int col
) const
10456 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10457 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
10459 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
10462 int wxGrid::GetRowMinimalHeight(int row
) const
10464 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10465 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
10467 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
10470 void wxGrid::SetColMinimalAcceptableWidth( int width
)
10472 // We do allow a width of 0 since this gives us
10473 // an easy way to temporarily hiding columns.
10475 m_minAcceptableColWidth
= width
;
10478 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
10480 // We do allow a height of 0 since this gives us
10481 // an easy way to temporarily hiding rows.
10483 m_minAcceptableRowHeight
= height
;
10486 int wxGrid::GetColMinimalAcceptableWidth() const
10488 return m_minAcceptableColWidth
;
10491 int wxGrid::GetRowMinimalAcceptableHeight() const
10493 return m_minAcceptableRowHeight
;
10496 // ----------------------------------------------------------------------------
10498 // ----------------------------------------------------------------------------
10501 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
10503 const bool column
= direction
== wxGRID_COLUMN
;
10505 wxClientDC
dc(m_gridWin
);
10507 // cancel editing of cell
10508 HideCellEditControl();
10509 SaveEditControlValue();
10511 // init both of them to avoid compiler warnings, even if we only need one
10519 wxCoord extent
, extentMax
= 0;
10520 int max
= column
? m_numRows
: m_numCols
;
10521 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
10528 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
10529 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
10532 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
10533 extent
= column
? size
.x
: size
.y
;
10534 if ( extent
> extentMax
)
10535 extentMax
= extent
;
10537 renderer
->DecRef();
10543 // now also compare with the column label extent
10545 dc
.SetFont( GetLabelFont() );
10549 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
10550 if ( GetColLabelTextOrientation() == wxVERTICAL
)
10554 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
10556 extent
= column
? w
: h
;
10557 if ( extent
> extentMax
)
10558 extentMax
= extent
;
10562 // empty column - give default extent (notice that if extentMax is less
10563 // than default extent but != 0, it's OK)
10564 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
10569 // leave some space around text
10577 SetColSize( col
, extentMax
);
10578 if ( !GetBatchCount() )
10581 m_gridWin
->GetClientSize( &cw
, &ch
);
10582 wxRect
rect ( CellToRect( 0, col
) );
10584 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10585 rect
.width
= cw
- rect
.x
;
10586 rect
.height
= m_colLabelHeight
;
10587 m_colLabelWin
->Refresh( true, &rect
);
10592 SetRowSize(row
, extentMax
);
10593 if ( !GetBatchCount() )
10596 m_gridWin
->GetClientSize( &cw
, &ch
);
10597 wxRect
rect( CellToRect( row
, 0 ) );
10599 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10600 rect
.width
= m_rowLabelWidth
;
10601 rect
.height
= ch
- rect
.y
;
10602 m_rowLabelWin
->Refresh( true, &rect
);
10609 SetColMinimalWidth(col
, extentMax
);
10611 SetRowMinimalHeight(row
, extentMax
);
10615 wxCoord
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
10617 // calculate size for the rows or columns?
10618 const bool calcRows
= direction
== wxGRID_ROW
;
10620 wxClientDC
dc(calcRows
? GetGridRowLabelWindow()
10621 : GetGridColLabelWindow());
10622 dc
.SetFont(GetLabelFont());
10624 // which dimension should we take into account for calculations?
10626 // for columns, the text can be only horizontal so it's easy but for rows
10627 // we also have to take into account the text orientation
10629 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
10631 wxArrayString lines
;
10632 wxCoord extentMax
= 0;
10634 const int numRowsOrCols
= calcRows
? m_numRows
: m_numCols
;
10635 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
10639 wxString label
= calcRows
? GetRowLabelValue(rowOrCol
)
10640 : GetColLabelValue(rowOrCol
);
10641 StringToLines(label
, lines
);
10644 GetTextBoxSize(dc
, lines
, &w
, &h
);
10646 const wxCoord extent
= useWidth
? w
: h
;
10647 if ( extent
> extentMax
)
10648 extentMax
= extent
;
10653 // empty column - give default extent (notice that if extentMax is less
10654 // than default extent but != 0, it's OK)
10655 extentMax
= calcRows
? GetDefaultRowLabelSize()
10656 : GetDefaultColLabelSize();
10659 // leave some space around text (taken from AutoSizeColOrRow)
10668 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10670 int width
= m_rowLabelWidth
;
10672 wxGridUpdateLocker locker
;
10674 locker
.Create(this);
10676 for ( int col
= 0; col
< m_numCols
; col
++ )
10679 AutoSizeColumn(col
, setAsMin
);
10681 width
+= GetColWidth(col
);
10687 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10689 int height
= m_colLabelHeight
;
10691 wxGridUpdateLocker locker
;
10693 locker
.Create(this);
10695 for ( int row
= 0; row
< m_numRows
; row
++ )
10698 AutoSizeRow(row
, setAsMin
);
10700 height
+= GetRowHeight(row
);
10706 void wxGrid::AutoSize()
10708 wxGridUpdateLocker
locker(this);
10710 // we need to round up the size of the scrollable area to a multiple of
10711 // scroll step to ensure that we don't get the scrollbars when we're sized
10712 // exactly to fit our contents
10713 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
10714 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
10715 wxSize
sizeFit(GetScrollX(size
.x
) * GetScrollLineX(),
10716 GetScrollY(size
.y
) * GetScrollLineY());
10718 // distribute the extra space between the columns/rows to avoid having
10719 // extra white space
10720 wxCoord diff
= sizeFit
.x
- size
.x
;
10721 if ( diff
&& m_numCols
)
10723 // try to resize the columns uniformly
10724 wxCoord diffPerCol
= diff
/ m_numCols
;
10727 for ( int col
= 0; col
< m_numCols
; col
++ )
10729 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
10733 // add remaining amount to the last columns
10734 diff
-= diffPerCol
* m_numCols
;
10737 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
10739 SetColSize(col
, GetColWidth(col
) + 1);
10745 diff
= sizeFit
.y
- size
.y
;
10746 if ( diff
&& m_numRows
)
10748 // try to resize the columns uniformly
10749 wxCoord diffPerRow
= diff
/ m_numRows
;
10752 for ( int row
= 0; row
< m_numRows
; row
++ )
10754 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
10758 // add remaining amount to the last rows
10759 diff
-= diffPerRow
* m_numRows
;
10762 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
10764 SetRowSize(row
, GetRowHeight(row
) + 1);
10769 // we know that we're not going to have scrollbars so disable them now to
10770 // avoid trouble in SetClientSize() which can otherwise set the correct
10771 // client size but also leave space for (not needed any more) scrollbars
10772 SetScrollbars(0, 0, 0, 0, 0, 0, true);
10773 SetClientSize(sizeFit
.x
+ m_rowLabelWidth
, sizeFit
.y
+ m_colLabelHeight
);
10776 void wxGrid::AutoSizeRowLabelSize( int row
)
10778 wxArrayString lines
;
10781 // Hide the edit control, so it
10782 // won't interfere with drag-shrinking.
10783 if ( IsCellEditControlShown() )
10785 HideCellEditControl();
10786 SaveEditControlValue();
10789 // autosize row height depending on label text
10790 StringToLines( GetRowLabelValue( row
), lines
);
10791 wxClientDC
dc( m_rowLabelWin
);
10792 GetTextBoxSize( dc
, lines
, &w
, &h
);
10793 if ( h
< m_defaultRowHeight
)
10794 h
= m_defaultRowHeight
;
10795 SetRowSize(row
, h
);
10799 void wxGrid::AutoSizeColLabelSize( int col
)
10801 wxArrayString lines
;
10804 // Hide the edit control, so it
10805 // won't interfere with drag-shrinking.
10806 if ( IsCellEditControlShown() )
10808 HideCellEditControl();
10809 SaveEditControlValue();
10812 // autosize column width depending on label text
10813 StringToLines( GetColLabelValue( col
), lines
);
10814 wxClientDC
dc( m_colLabelWin
);
10815 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10816 GetTextBoxSize( dc
, lines
, &w
, &h
);
10818 GetTextBoxSize( dc
, lines
, &h
, &w
);
10819 if ( w
< m_defaultColWidth
)
10820 w
= m_defaultColWidth
;
10821 SetColSize(col
, w
);
10825 wxSize
wxGrid::DoGetBestSize() const
10827 wxGrid
*self
= (wxGrid
*)this; // const_cast
10829 // we do the same as in AutoSize() here with the exception that we don't
10830 // change the column/row sizes, only calculate them
10831 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
10832 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
10833 wxSize
sizeFit(GetScrollX(size
.x
) * GetScrollLineX(),
10834 GetScrollY(size
.y
) * GetScrollLineY());
10836 // NOTE: This size should be cached, but first we need to add calls to
10837 // InvalidateBestSize everywhere that could change the results of this
10839 // CacheBestSize(size);
10841 return wxSize(sizeFit
.x
+ m_rowLabelWidth
, sizeFit
.y
+ m_colLabelHeight
)
10842 + GetWindowBorderSize();
10850 wxPen
& wxGrid::GetDividerPen() const
10855 // ----------------------------------------------------------------------------
10856 // cell value accessor functions
10857 // ----------------------------------------------------------------------------
10859 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10863 m_table
->SetValue( row
, col
, s
);
10864 if ( !GetBatchCount() )
10867 wxRect
rect( CellToRect( row
, col
) );
10869 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10870 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10871 m_gridWin
->Refresh( false, &rect
);
10874 if ( m_currentCellCoords
.GetRow() == row
&&
10875 m_currentCellCoords
.GetCol() == col
&&
10876 IsCellEditControlShown())
10877 // Note: If we are using IsCellEditControlEnabled,
10878 // this interacts badly with calling SetCellValue from
10879 // an EVT_GRID_CELL_CHANGE handler.
10881 HideCellEditControl();
10882 ShowCellEditControl(); // will reread data from table
10887 // ----------------------------------------------------------------------------
10888 // block, row and column selection
10889 // ----------------------------------------------------------------------------
10891 void wxGrid::SelectRow( int row
, bool addToSelected
)
10893 if ( IsSelection() && !addToSelected
)
10897 m_selection
->SelectRow( row
, false, addToSelected
);
10900 void wxGrid::SelectCol( int col
, bool addToSelected
)
10902 if ( IsSelection() && !addToSelected
)
10906 m_selection
->SelectCol( col
, false, addToSelected
);
10909 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10910 bool addToSelected
)
10912 if ( IsSelection() && !addToSelected
)
10916 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10917 false, addToSelected
);
10920 void wxGrid::SelectAll()
10922 if ( m_numRows
> 0 && m_numCols
> 0 )
10925 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10929 // ----------------------------------------------------------------------------
10930 // cell, row and col deselection
10931 // ----------------------------------------------------------------------------
10933 void wxGrid::DeselectRow( int row
)
10935 if ( !m_selection
)
10938 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10940 if ( m_selection
->IsInSelection(row
, 0 ) )
10941 m_selection
->ToggleCellSelection(row
, 0);
10945 int nCols
= GetNumberCols();
10946 for ( int i
= 0; i
< nCols
; i
++ )
10948 if ( m_selection
->IsInSelection(row
, i
) )
10949 m_selection
->ToggleCellSelection(row
, i
);
10954 void wxGrid::DeselectCol( int col
)
10956 if ( !m_selection
)
10959 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10961 if ( m_selection
->IsInSelection(0, col
) )
10962 m_selection
->ToggleCellSelection(0, col
);
10966 int nRows
= GetNumberRows();
10967 for ( int i
= 0; i
< nRows
; i
++ )
10969 if ( m_selection
->IsInSelection(i
, col
) )
10970 m_selection
->ToggleCellSelection(i
, col
);
10975 void wxGrid::DeselectCell( int row
, int col
)
10977 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10978 m_selection
->ToggleCellSelection(row
, col
);
10981 bool wxGrid::IsSelection() const
10983 return ( m_selection
&& (m_selection
->IsSelection() ||
10984 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10985 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10988 bool wxGrid::IsInSelection( int row
, int col
) const
10990 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10991 ( row
>= m_selectingTopLeft
.GetRow() &&
10992 col
>= m_selectingTopLeft
.GetCol() &&
10993 row
<= m_selectingBottomRight
.GetRow() &&
10994 col
<= m_selectingBottomRight
.GetCol() )) );
10997 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
11001 wxGridCellCoordsArray a
;
11005 return m_selection
->m_cellSelection
;
11008 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
11012 wxGridCellCoordsArray a
;
11016 return m_selection
->m_blockSelectionTopLeft
;
11019 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
11023 wxGridCellCoordsArray a
;
11027 return m_selection
->m_blockSelectionBottomRight
;
11030 wxArrayInt
wxGrid::GetSelectedRows() const
11038 return m_selection
->m_rowSelection
;
11041 wxArrayInt
wxGrid::GetSelectedCols() const
11049 return m_selection
->m_colSelection
;
11052 void wxGrid::ClearSelection()
11054 m_selectingTopLeft
=
11055 m_selectingBottomRight
=
11056 m_selectingKeyboard
= wxGridNoCellCoords
;
11058 m_selection
->ClearSelection();
11061 // This function returns the rectangle that encloses the given block
11062 // in device coords clipped to the client size of the grid window.
11064 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
11065 const wxGridCellCoords
&bottomRight
) const
11067 wxRect
rect( wxGridNoCellRect
);
11070 cellRect
= CellToRect( topLeft
);
11071 if ( cellRect
!= wxGridNoCellRect
)
11077 rect
= wxRect(0, 0, 0, 0);
11080 cellRect
= CellToRect( bottomRight
);
11081 if ( cellRect
!= wxGridNoCellRect
)
11087 return wxGridNoCellRect
;
11091 int left
= rect
.GetLeft();
11092 int top
= rect
.GetTop();
11093 int right
= rect
.GetRight();
11094 int bottom
= rect
.GetBottom();
11096 int leftCol
= topLeft
.GetCol();
11097 int topRow
= topLeft
.GetRow();
11098 int rightCol
= bottomRight
.GetCol();
11099 int bottomRow
= bottomRight
.GetRow();
11107 leftCol
= rightCol
;
11117 topRow
= bottomRow
;
11121 for ( j
= topRow
; j
<= bottomRow
; j
++ )
11123 for ( i
= leftCol
; i
<= rightCol
; i
++ )
11125 if ((j
== topRow
) || (j
== bottomRow
) || (i
== leftCol
) || (i
== rightCol
))
11127 cellRect
= CellToRect( j
, i
);
11129 if (cellRect
.x
< left
)
11131 if (cellRect
.y
< top
)
11133 if (cellRect
.x
+ cellRect
.width
> right
)
11134 right
= cellRect
.x
+ cellRect
.width
;
11135 if (cellRect
.y
+ cellRect
.height
> bottom
)
11136 bottom
= cellRect
.y
+ cellRect
.height
;
11140 i
= rightCol
; // jump over inner cells.
11145 // convert to scrolled coords
11147 CalcScrolledPosition( left
, top
, &left
, &top
);
11148 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
11151 m_gridWin
->GetClientSize( &cw
, &ch
);
11153 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
11154 return wxRect(0,0,0,0);
11156 rect
.SetLeft( wxMax(0, left
) );
11157 rect
.SetTop( wxMax(0, top
) );
11158 rect
.SetRight( wxMin(cw
, right
) );
11159 rect
.SetBottom( wxMin(ch
, bottom
) );
11164 // ----------------------------------------------------------------------------
11165 // grid event classes
11166 // ----------------------------------------------------------------------------
11168 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
11170 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
11171 int row
, int col
, int x
, int y
, bool sel
,
11172 bool control
, bool shift
, bool alt
, bool meta
)
11173 : wxNotifyEvent( type
, id
)
11180 m_control
= control
;
11185 SetEventObject(obj
);
11189 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
11191 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
11192 int rowOrCol
, int x
, int y
,
11193 bool control
, bool shift
, bool alt
, bool meta
)
11194 : wxNotifyEvent( type
, id
)
11196 m_rowOrCol
= rowOrCol
;
11199 m_control
= control
;
11204 SetEventObject(obj
);
11208 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
11210 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
11211 const wxGridCellCoords
& topLeft
,
11212 const wxGridCellCoords
& bottomRight
,
11213 bool sel
, bool control
,
11214 bool shift
, bool alt
, bool meta
)
11215 : wxNotifyEvent( type
, id
)
11217 m_topLeft
= topLeft
;
11218 m_bottomRight
= bottomRight
;
11220 m_control
= control
;
11225 SetEventObject(obj
);
11229 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
11231 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
11232 wxObject
* obj
, int row
,
11233 int col
, wxControl
* ctrl
)
11234 : wxCommandEvent(type
, id
)
11236 SetEventObject(obj
);
11242 #endif // wxUSE_GRID