1 ///////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
5 // Modified by: Robin Dunn, Vadim Zeitlin, Santiago Palacios
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
25 #include "wx/dcclient.h"
26 #include "wx/settings.h"
28 #include "wx/textctrl.h"
29 #include "wx/checkbox.h"
30 #include "wx/combobox.h"
31 #include "wx/valtext.h"
34 #include "wx/listbox.h"
37 #include "wx/textfile.h"
38 #include "wx/spinctrl.h"
39 #include "wx/tokenzr.h"
40 #include "wx/renderer.h"
42 #include "wx/generic/gridsel.h"
44 const wxChar wxGridNameStr
[] = wxT("grid");
46 #if defined(__WXMOTIF__)
47 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
49 #define WXUNUSED_MOTIF(identifier) identifier
52 #if defined(__WXGTK__)
53 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
55 #define WXUNUSED_GTK(identifier) identifier
58 // Required for wxIs... functions
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr
*, wxArrayAttrs
,
66 class WXDLLIMPEXP_ADV
);
68 struct wxGridCellWithAttr
70 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
71 : coords(row
, col
), attr(attr_
)
76 wxGridCellWithAttr(const wxGridCellWithAttr
& other
)
77 : coords(other
.coords
),
83 wxGridCellWithAttr
& operator=(const wxGridCellWithAttr
& other
)
85 coords
= other
.coords
;
98 wxGridCellCoords coords
;
102 WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr
, wxGridCellWithAttrArray
,
103 class WXDLLIMPEXP_ADV
);
105 #include "wx/arrimpl.cpp"
107 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
108 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
110 // ----------------------------------------------------------------------------
112 // ----------------------------------------------------------------------------
114 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
115 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
116 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
117 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
118 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_BEGIN_DRAG
)
119 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
120 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
121 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
122 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
123 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
124 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
125 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_MOVE
)
126 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
127 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
128 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
129 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
130 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
131 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
133 // ----------------------------------------------------------------------------
135 // ----------------------------------------------------------------------------
137 // common base class for various grid subwindows
138 class WXDLLIMPEXP_ADV wxGridSubwindow
: public wxWindow
141 wxGridSubwindow() { m_owner
= NULL
; }
142 wxGridSubwindow(wxGrid
*owner
,
146 int additionalStyle
= 0,
147 const wxString
& name
= wxPanelNameStr
)
148 : wxWindow(owner
, id
, pos
, size
,
149 wxBORDER_NONE
| additionalStyle
,
155 virtual bool AcceptsFocus() const { return false; }
157 wxGrid
*GetOwner() { return m_owner
; }
160 void OnMouseCaptureLost(wxMouseCaptureLostEvent
& event
);
164 DECLARE_EVENT_TABLE()
165 DECLARE_NO_COPY_CLASS(wxGridSubwindow
)
168 class WXDLLIMPEXP_ADV wxGridRowLabelWindow
: public wxGridSubwindow
171 wxGridRowLabelWindow() { }
172 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
173 const wxPoint
&pos
, const wxSize
&size
);
176 void OnPaint( wxPaintEvent
& event
);
177 void OnMouseEvent( wxMouseEvent
& event
);
178 void OnMouseWheel( wxMouseEvent
& event
);
180 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
181 DECLARE_EVENT_TABLE()
182 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow
)
186 class WXDLLIMPEXP_ADV wxGridColLabelWindow
: public wxGridSubwindow
189 wxGridColLabelWindow() { }
190 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
191 const wxPoint
&pos
, const wxSize
&size
);
194 void OnPaint( wxPaintEvent
& event
);
195 void OnMouseEvent( wxMouseEvent
& event
);
196 void OnMouseWheel( wxMouseEvent
& event
);
198 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
199 DECLARE_EVENT_TABLE()
200 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow
)
204 class WXDLLIMPEXP_ADV wxGridCornerLabelWindow
: public wxGridSubwindow
207 wxGridCornerLabelWindow() { }
208 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
209 const wxPoint
&pos
, const wxSize
&size
);
212 void OnMouseEvent( wxMouseEvent
& event
);
213 void OnMouseWheel( wxMouseEvent
& event
);
214 void OnPaint( wxPaintEvent
& event
);
216 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
217 DECLARE_EVENT_TABLE()
218 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow
)
221 class WXDLLIMPEXP_ADV wxGridWindow
: public wxGridSubwindow
226 m_rowLabelWin
= NULL
;
227 m_colLabelWin
= NULL
;
230 wxGridWindow( wxGrid
*parent
,
231 wxGridRowLabelWindow
*rowLblWin
,
232 wxGridColLabelWindow
*colLblWin
,
233 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
235 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
237 virtual bool AcceptsFocus() const { return true; }
240 wxGridRowLabelWindow
*m_rowLabelWin
;
241 wxGridColLabelWindow
*m_colLabelWin
;
243 void OnPaint( wxPaintEvent
&event
);
244 void OnMouseWheel( wxMouseEvent
& event
);
245 void OnMouseEvent( wxMouseEvent
& event
);
246 void OnKeyDown( wxKeyEvent
& );
247 void OnKeyUp( wxKeyEvent
& );
248 void OnChar( wxKeyEvent
& );
249 void OnEraseBackground( wxEraseEvent
& );
250 void OnFocus( wxFocusEvent
& );
252 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
253 DECLARE_EVENT_TABLE()
254 DECLARE_NO_COPY_CLASS(wxGridWindow
)
258 class wxGridCellEditorEvtHandler
: public wxEvtHandler
261 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
268 void OnKillFocus(wxFocusEvent
& event
);
269 void OnKeyDown(wxKeyEvent
& event
);
270 void OnChar(wxKeyEvent
& event
);
272 void SetInSetFocus(bool inSetFocus
) { m_inSetFocus
= inSetFocus
; }
276 wxGridCellEditor
*m_editor
;
278 // Work around the fact that a focus kill event can be sent to
279 // a combobox within a set focus event.
282 DECLARE_EVENT_TABLE()
283 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
284 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler
)
288 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler
, wxEvtHandler
)
290 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
291 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus
)
292 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
293 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
297 // ----------------------------------------------------------------------------
298 // the internal data representation used by wxGridCellAttrProvider
299 // ----------------------------------------------------------------------------
301 // this class stores attributes set for cells
302 class WXDLLIMPEXP_ADV wxGridCellAttrData
305 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
306 wxGridCellAttr
*GetAttr(int row
, int col
) const;
307 void UpdateAttrRows( size_t pos
, int numRows
);
308 void UpdateAttrCols( size_t pos
, int numCols
);
311 // searches for the attr for given cell, returns wxNOT_FOUND if not found
312 int FindIndex(int row
, int col
) const;
314 wxGridCellWithAttrArray m_attrs
;
317 // this class stores attributes set for rows or columns
318 class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
321 // empty ctor to suppress warnings
322 wxGridRowOrColAttrData() {}
323 ~wxGridRowOrColAttrData();
325 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
326 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
327 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
330 wxArrayInt m_rowsOrCols
;
331 wxArrayAttrs m_attrs
;
334 // NB: this is just a wrapper around 3 objects: one which stores cell
335 // attributes, and 2 others for row/col ones
336 class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
339 wxGridCellAttrData m_cellAttrs
;
340 wxGridRowOrColAttrData m_rowAttrs
,
345 // ----------------------------------------------------------------------------
346 // data structures used for the data type registry
347 // ----------------------------------------------------------------------------
349 struct wxGridDataTypeInfo
351 wxGridDataTypeInfo(const wxString
& typeName
,
352 wxGridCellRenderer
* renderer
,
353 wxGridCellEditor
* editor
)
354 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
357 ~wxGridDataTypeInfo()
359 wxSafeDecRef(m_renderer
);
360 wxSafeDecRef(m_editor
);
364 wxGridCellRenderer
* m_renderer
;
365 wxGridCellEditor
* m_editor
;
367 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo
)
371 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
,
372 class WXDLLIMPEXP_ADV
);
375 class WXDLLIMPEXP_ADV wxGridTypeRegistry
378 wxGridTypeRegistry() {}
379 ~wxGridTypeRegistry();
381 void RegisterDataType(const wxString
& typeName
,
382 wxGridCellRenderer
* renderer
,
383 wxGridCellEditor
* editor
);
385 // find one of already registered data types
386 int FindRegisteredDataType(const wxString
& typeName
);
388 // try to FindRegisteredDataType(), if this fails and typeName is one of
389 // standard typenames, register it and return its index
390 int FindDataType(const wxString
& typeName
);
392 // try to FindDataType(), if it fails see if it is not one of already
393 // registered data types with some params in which case clone the
394 // registered data type and set params for it
395 int FindOrCloneDataType(const wxString
& typeName
);
397 wxGridCellRenderer
* GetRenderer(int index
);
398 wxGridCellEditor
* GetEditor(int index
);
401 wxGridDataTypeInfoArray m_typeinfo
;
405 // ----------------------------------------------------------------------------
406 // conditional compilation
407 // ----------------------------------------------------------------------------
409 #ifndef WXGRID_DRAW_LINES
410 #define WXGRID_DRAW_LINES 1
413 // ----------------------------------------------------------------------------
415 // ----------------------------------------------------------------------------
417 //#define DEBUG_ATTR_CACHE
418 #ifdef DEBUG_ATTR_CACHE
419 static size_t gs_nAttrCacheHits
= 0;
420 static size_t gs_nAttrCacheMisses
= 0;
423 // ----------------------------------------------------------------------------
425 // ----------------------------------------------------------------------------
427 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
428 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
431 // TODO: this doesn't work at all, grid cells have different sizes and approx
432 // calculations don't work as because of the size mismatch scrollbars
433 // sometimes fail to be shown when they should be or vice versa
435 // The scroll bars may be a little flakey once in a while, but that is
436 // surely much less horrible than having scroll lines of only 1!!!
439 // Well, it's still seriously broken so it might be better but needs
442 static const size_t GRID_SCROLL_LINE_X
= 15; // 1;
443 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
445 // the size of hash tables used a bit everywhere (the max number of elements
446 // in these hash tables is the number of rows/columns)
447 static const int GRID_HASH_SIZE
= 100;
450 // ----------------------------------------------------------------------------
452 // ----------------------------------------------------------------------------
454 static inline int GetScrollX(int x
)
456 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
459 static inline int GetScrollY(int y
)
461 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
465 // ============================================================================
467 // ============================================================================
469 // ----------------------------------------------------------------------------
471 // ----------------------------------------------------------------------------
473 wxGridCellEditor::wxGridCellEditor()
479 wxGridCellEditor::~wxGridCellEditor()
484 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
485 wxWindowID
WXUNUSED(id
),
486 wxEvtHandler
* evtHandler
)
489 m_control
->PushEventHandler(evtHandler
);
492 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
493 wxGridCellAttr
*attr
)
495 // erase the background because we might not fill the cell
496 wxClientDC
dc(m_control
->GetParent());
497 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
499 gridWindow
->GetOwner()->PrepareDC(dc
);
501 dc
.SetPen(*wxTRANSPARENT_PEN
);
502 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxBRUSHSTYLE_SOLID
));
503 dc
.DrawRectangle(rectCell
);
505 // redraw the control we just painted over
506 m_control
->Refresh();
509 void wxGridCellEditor::Destroy()
513 m_control
->PopEventHandler( true /* delete it*/ );
515 m_control
->Destroy();
520 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
522 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
524 m_control
->Show(show
);
528 // set the colours/fonts if we have any
531 m_colFgOld
= m_control
->GetForegroundColour();
532 m_control
->SetForegroundColour(attr
->GetTextColour());
534 m_colBgOld
= m_control
->GetBackgroundColour();
535 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
537 // Workaround for GTK+1 font setting problem on some platforms
538 #if !defined(__WXGTK__) || defined(__WXGTK20__)
539 m_fontOld
= m_control
->GetFont();
540 m_control
->SetFont(attr
->GetFont());
543 // can't do anything more in the base class version, the other
544 // attributes may only be used by the derived classes
549 // restore the standard colours fonts
550 if ( m_colFgOld
.Ok() )
552 m_control
->SetForegroundColour(m_colFgOld
);
553 m_colFgOld
= wxNullColour
;
556 if ( m_colBgOld
.Ok() )
558 m_control
->SetBackgroundColour(m_colBgOld
);
559 m_colBgOld
= wxNullColour
;
562 // Workaround for GTK+1 font setting problem on some platforms
563 #if !defined(__WXGTK__) || defined(__WXGTK20__)
564 if ( m_fontOld
.Ok() )
566 m_control
->SetFont(m_fontOld
);
567 m_fontOld
= wxNullFont
;
573 void wxGridCellEditor::SetSize(const wxRect
& rect
)
575 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
577 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
580 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
585 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
587 bool ctrl
= event
.ControlDown();
588 bool alt
= event
.AltDown();
591 // On the Mac the Alt key is more like shift and is used for entry of
592 // valid characters, so check for Ctrl and Meta instead.
593 alt
= event
.MetaDown();
596 // Assume it's not a valid char if ctrl or alt is down, but if both are
597 // down then it may be because of an AltGr key combination, so let them
598 // through in that case.
599 if ((ctrl
|| alt
) && !(ctrl
&& alt
))
606 // If it's a F-Key or other special key then it shouldn't start the
608 if (event
.GetKeyCode() >= WXK_START
)
612 // if the unicode key code is not really a unicode character (it may
613 // be a function key or etc., the platforms appear to always give us a
614 // small value in this case) then fallback to the ASCII key code but
615 // don't do anything for function keys or etc.
616 key
= event
.GetUnicodeKey();
619 key
= event
.GetKeyCode();
620 keyOk
= (key
<= 127);
623 key
= event
.GetKeyCode();
624 keyOk
= (key
<= 255);
630 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
635 void wxGridCellEditor::StartingClick()
641 // ----------------------------------------------------------------------------
642 // wxGridCellTextEditor
643 // ----------------------------------------------------------------------------
645 wxGridCellTextEditor::wxGridCellTextEditor()
650 void wxGridCellTextEditor::Create(wxWindow
* parent
,
652 wxEvtHandler
* evtHandler
)
654 DoCreate(parent
, id
, evtHandler
);
657 void wxGridCellTextEditor::DoCreate(wxWindow
* parent
,
659 wxEvtHandler
* evtHandler
,
662 style
|= wxTE_PROCESS_ENTER
|
667 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
668 wxDefaultPosition
, wxDefaultSize
,
671 // set max length allowed in the textctrl, if the parameter was set
672 if ( m_maxChars
!= 0 )
674 Text()->SetMaxLength(m_maxChars
);
677 wxGridCellEditor::Create(parent
, id
, evtHandler
);
680 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
681 wxGridCellAttr
* WXUNUSED(attr
))
683 // as we fill the entire client area,
684 // don't do anything here to minimize flicker
687 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
689 wxRect
rect(rectOrig
);
691 // Make the edit control large enough to allow for internal margins
693 // TODO: remove this if the text ctrl sizing is improved esp. for unix
695 #if defined(__WXGTK__)
703 #elif defined(__WXMSW__)
717 int extra_x
= ( rect
.x
> 2 ) ? 2 : 1;
718 int extra_y
= ( rect
.y
> 2 ) ? 2 : 1;
720 #if defined(__WXMOTIF__)
725 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
726 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
727 rect
.SetRight( rect
.GetRight() + 2 * extra_x
);
728 rect
.SetBottom( rect
.GetBottom() + 2 * extra_y
);
731 wxGridCellEditor::SetSize(rect
);
734 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
736 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
738 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
740 DoBeginEdit(m_startValue
);
743 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
745 Text()->SetValue(startValue
);
746 Text()->SetInsertionPointEnd();
747 Text()->SetSelection(-1, -1);
751 bool wxGridCellTextEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
753 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
755 bool changed
= false;
756 wxString value
= Text()->GetValue();
757 if (value
!= m_startValue
)
761 grid
->GetTable()->SetValue(row
, col
, value
);
763 m_startValue
= wxEmptyString
;
765 // No point in setting the text of the hidden control
766 //Text()->SetValue(m_startValue);
771 void wxGridCellTextEditor::Reset()
773 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
775 DoReset(m_startValue
);
778 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
780 Text()->SetValue(startValue
);
781 Text()->SetInsertionPointEnd();
784 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
786 return wxGridCellEditor::IsAcceptedKey(event
);
789 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
791 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
792 // longer an appropriate way to get the character into the text control.
793 // Do it ourselves instead. We know that if we get this far that we have
794 // a valid character, so not a whole lot of testing needs to be done.
796 wxTextCtrl
* tc
= Text();
801 ch
= event
.GetUnicodeKey();
803 ch
= (wxChar
)event
.GetKeyCode();
805 ch
= (wxChar
)event
.GetKeyCode();
811 // delete the character at the cursor
812 pos
= tc
->GetInsertionPoint();
813 if (pos
< tc
->GetLastPosition())
814 tc
->Remove(pos
, pos
+ 1);
818 // delete the character before the cursor
819 pos
= tc
->GetInsertionPoint();
821 tc
->Remove(pos
- 1, pos
);
830 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
831 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
833 #if defined(__WXMOTIF__) || defined(__WXGTK__)
834 // wxMotif needs a little extra help...
835 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
836 wxString
s( Text()->GetValue() );
837 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
839 Text()->SetInsertionPoint( pos
);
841 // the other ports can handle a Return key press
847 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
857 if ( params
.ToLong(&tmp
) )
859 m_maxChars
= (size_t)tmp
;
863 wxLogDebug( _T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str() );
868 // return the value in the text control
869 wxString
wxGridCellTextEditor::GetValue() const
871 return Text()->GetValue();
874 // ----------------------------------------------------------------------------
875 // wxGridCellNumberEditor
876 // ----------------------------------------------------------------------------
878 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
884 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
886 wxEvtHandler
* evtHandler
)
891 // create a spin ctrl
892 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
893 wxDefaultPosition
, wxDefaultSize
,
897 wxGridCellEditor::Create(parent
, id
, evtHandler
);
902 // just a text control
903 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
906 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
911 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
913 // first get the value
914 wxGridTableBase
*table
= grid
->GetTable();
915 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
917 m_valueOld
= table
->GetValueAsLong(row
, col
);
922 wxString sValue
= table
->GetValue(row
, col
);
923 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty())
925 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
933 Spin()->SetValue((int)m_valueOld
);
939 DoBeginEdit(GetString());
943 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
953 value
= Spin()->GetValue();
954 changed
= value
!= m_valueOld
;
956 text
= wxString::Format(wxT("%ld"), value
);
961 text
= Text()->GetValue();
962 changed
= (text
.empty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
967 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
968 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
970 grid
->GetTable()->SetValue(row
, col
, text
);
976 void wxGridCellNumberEditor::Reset()
981 Spin()->SetValue((int)m_valueOld
);
986 DoReset(GetString());
990 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
992 if ( wxGridCellEditor::IsAcceptedKey(event
) )
994 int keycode
= event
.GetKeyCode();
995 if ( (keycode
< 128) &&
996 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
1005 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
1007 int keycode
= event
.GetKeyCode();
1010 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
1012 wxGridCellTextEditor::StartingKey(event
);
1014 // skip Skip() below
1021 if ( wxIsdigit(keycode
) )
1023 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
1024 spin
->SetValue(keycode
- '0');
1025 spin
->SetSelection(1,1);
1034 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
1045 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1049 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1053 // skip the error message below
1058 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1062 // return the value in the spin control if it is there (the text control otherwise)
1063 wxString
wxGridCellNumberEditor::GetValue() const
1070 long value
= Spin()->GetValue();
1071 s
.Printf(wxT("%ld"), value
);
1076 s
= Text()->GetValue();
1082 // ----------------------------------------------------------------------------
1083 // wxGridCellFloatEditor
1084 // ----------------------------------------------------------------------------
1086 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1089 m_precision
= precision
;
1092 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1094 wxEvtHandler
* evtHandler
)
1096 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1098 #if wxUSE_VALIDATORS
1099 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1103 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1105 // first get the value
1106 wxGridTableBase
*table
= grid
->GetTable();
1107 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1109 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1114 wxString sValue
= table
->GetValue(row
, col
);
1115 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.empty())
1117 wxFAIL_MSG( _T("this cell doesn't have float value") );
1122 DoBeginEdit(GetString());
1125 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1129 wxString
text(Text()->GetValue());
1131 if ( (text
.empty() || text
.ToDouble(&value
)) &&
1132 !wxIsSameDouble(value
, m_valueOld
) )
1134 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1135 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1137 grid
->GetTable()->SetValue(row
, col
, text
);
1145 void wxGridCellFloatEditor::Reset()
1147 DoReset(GetString());
1150 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1152 int keycode
= event
.GetKeyCode();
1154 tmpbuf
[0] = (char) keycode
;
1156 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1159 bool is_decimal_point
= ( strbuf
==
1160 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1162 bool is_decimal_point
= ( strbuf
== _T(".") );
1165 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1166 || is_decimal_point
)
1168 wxGridCellTextEditor::StartingKey(event
);
1170 // skip Skip() below
1177 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1188 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1192 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1194 m_precision
= (int)tmp
;
1196 // skip the error message below
1201 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1205 wxString
wxGridCellFloatEditor::GetString() const
1208 if ( m_precision
== -1 && m_width
!= -1)
1210 // default precision
1211 fmt
.Printf(_T("%%%d.f"), m_width
);
1213 else if ( m_precision
!= -1 && m_width
== -1)
1216 fmt
.Printf(_T("%%.%df"), m_precision
);
1218 else if ( m_precision
!= -1 && m_width
!= -1 )
1220 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1224 // default width/precision
1228 return wxString::Format(fmt
, m_valueOld
);
1231 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1233 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1235 const int keycode
= event
.GetKeyCode();
1236 if ( isascii(keycode
) )
1239 tmpbuf
[0] = (char) keycode
;
1241 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1244 const wxString decimalPoint
=
1245 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
1247 const wxString
decimalPoint(_T('.'));
1250 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1251 if ( wxIsdigit(keycode
) ||
1252 tolower(keycode
) == 'e' ||
1253 keycode
== decimalPoint
||
1265 #endif // wxUSE_TEXTCTRL
1269 // ----------------------------------------------------------------------------
1270 // wxGridCellBoolEditor
1271 // ----------------------------------------------------------------------------
1273 // the default values for GetValue()
1274 wxString
wxGridCellBoolEditor::ms_stringValues
[2] = { _T(""), _T("1") };
1276 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1278 wxEvtHandler
* evtHandler
)
1280 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1281 wxDefaultPosition
, wxDefaultSize
,
1284 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1287 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1289 bool resize
= false;
1290 wxSize size
= m_control
->GetSize();
1291 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1293 // check if the checkbox is not too big/small for this cell
1294 wxSize sizeBest
= m_control
->GetBestSize();
1295 if ( !(size
== sizeBest
) )
1297 // reset to default size if it had been made smaller
1303 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1305 // leave 1 pixel margin
1306 size
.x
= size
.y
= minSize
- 2;
1313 m_control
->SetSize(size
);
1316 // position it in the centre of the rectangle (TODO: support alignment?)
1318 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1319 // the checkbox without label still has some space to the right in wxGTK,
1320 // so shift it to the right
1322 #elif defined(__WXMSW__)
1323 // here too, but in other way
1328 int hAlign
= wxALIGN_CENTRE
;
1329 int vAlign
= wxALIGN_CENTRE
;
1331 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1334 if (hAlign
== wxALIGN_LEFT
)
1342 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1344 else if (hAlign
== wxALIGN_RIGHT
)
1346 x
= r
.x
+ r
.width
- size
.x
- 2;
1347 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1349 else if (hAlign
== wxALIGN_CENTRE
)
1351 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1352 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1355 m_control
->Move(x
, y
);
1358 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1360 m_control
->Show(show
);
1364 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1365 CBox()->SetBackgroundColour(colBg
);
1369 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1371 wxASSERT_MSG(m_control
,
1372 wxT("The wxGridCellEditor must be created first!"));
1374 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1376 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1380 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1382 if ( cellval
== ms_stringValues
[false] )
1383 m_startValue
= false;
1384 else if ( cellval
== ms_stringValues
[true] )
1385 m_startValue
= true;
1388 // do not try to be smart here and convert it to true or false
1389 // because we'll still overwrite it with something different and
1390 // this risks to be very surprising for the user code, let them
1392 wxFAIL_MSG( _T("invalid value for a cell with bool editor!") );
1396 CBox()->SetValue(m_startValue
);
1400 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1403 wxASSERT_MSG(m_control
,
1404 wxT("The wxGridCellEditor must be created first!"));
1406 bool changed
= false;
1407 bool value
= CBox()->GetValue();
1408 if ( value
!= m_startValue
)
1413 wxGridTableBase
* const table
= grid
->GetTable();
1414 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1415 table
->SetValueAsBool(row
, col
, value
);
1417 table
->SetValue(row
, col
, GetValue());
1423 void wxGridCellBoolEditor::Reset()
1425 wxASSERT_MSG(m_control
,
1426 wxT("The wxGridCellEditor must be created first!"));
1428 CBox()->SetValue(m_startValue
);
1431 void wxGridCellBoolEditor::StartingClick()
1433 CBox()->SetValue(!CBox()->GetValue());
1436 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1438 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1440 int keycode
= event
.GetKeyCode();
1453 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1455 int keycode
= event
.GetKeyCode();
1459 CBox()->SetValue(!CBox()->GetValue());
1463 CBox()->SetValue(true);
1467 CBox()->SetValue(false);
1472 wxString
wxGridCellBoolEditor::GetValue() const
1474 return ms_stringValues
[CBox()->GetValue()];
1478 wxGridCellBoolEditor::UseStringValues(const wxString
& valueTrue
,
1479 const wxString
& valueFalse
)
1481 ms_stringValues
[false] = valueFalse
;
1482 ms_stringValues
[true] = valueTrue
;
1486 wxGridCellBoolEditor::IsTrueValue(const wxString
& value
)
1488 return value
== ms_stringValues
[true];
1491 #endif // wxUSE_CHECKBOX
1495 // ----------------------------------------------------------------------------
1496 // wxGridCellChoiceEditor
1497 // ----------------------------------------------------------------------------
1499 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1501 : m_choices(choices
),
1502 m_allowOthers(allowOthers
) { }
1504 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1505 const wxString choices
[],
1507 : m_allowOthers(allowOthers
)
1511 m_choices
.Alloc(count
);
1512 for ( size_t n
= 0; n
< count
; n
++ )
1514 m_choices
.Add(choices
[n
]);
1519 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1521 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1522 editor
->m_allowOthers
= m_allowOthers
;
1523 editor
->m_choices
= m_choices
;
1528 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1530 wxEvtHandler
* evtHandler
)
1532 int style
= wxTE_PROCESS_ENTER
|
1536 if ( !m_allowOthers
)
1537 style
|= wxCB_READONLY
;
1538 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1539 wxDefaultPosition
, wxDefaultSize
,
1543 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1546 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1547 wxGridCellAttr
* attr
)
1549 // as we fill the entire client area, don't do anything here to minimize
1552 // TODO: It doesn't actually fill the client area since the height of a
1553 // combo always defaults to the standard. Until someone has time to
1554 // figure out the right rectangle to paint, just do it the normal way.
1555 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1558 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1560 wxASSERT_MSG(m_control
,
1561 wxT("The wxGridCellEditor must be created first!"));
1563 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1565 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1567 // Don't immediately end if we get a kill focus event within BeginEdit
1569 evtHandler
->SetInSetFocus(true);
1571 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1575 Combo()->SetValue(m_startValue
);
1576 Combo()->SetInsertionPointEnd();
1578 else // the combobox is read-only
1580 // find the right position, or default to the first if not found
1581 int pos
= Combo()->FindString(m_startValue
);
1582 if (pos
== wxNOT_FOUND
)
1584 Combo()->SetSelection(pos
);
1587 Combo()->SetFocus();
1591 // When dropping down the menu, a kill focus event
1592 // happens after this point, so we can't reset the flag yet.
1593 #if !defined(__WXGTK20__)
1594 evtHandler
->SetInSetFocus(false);
1599 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1602 wxString value
= Combo()->GetValue();
1603 if ( value
== m_startValue
)
1606 grid
->GetTable()->SetValue(row
, col
, value
);
1611 void wxGridCellChoiceEditor::Reset()
1613 Combo()->SetValue(m_startValue
);
1614 Combo()->SetInsertionPointEnd();
1617 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1627 wxStringTokenizer
tk(params
, _T(','));
1628 while ( tk
.HasMoreTokens() )
1630 m_choices
.Add(tk
.GetNextToken());
1634 // return the value in the text control
1635 wxString
wxGridCellChoiceEditor::GetValue() const
1637 return Combo()->GetValue();
1640 #endif // wxUSE_COMBOBOX
1642 // ----------------------------------------------------------------------------
1643 // wxGridCellEditorEvtHandler
1644 // ----------------------------------------------------------------------------
1646 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
1648 // Don't disable the cell if we're just starting to edit it
1653 m_grid
->DisableCellEditControl();
1658 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1660 switch ( event
.GetKeyCode() )
1664 m_grid
->DisableCellEditControl();
1668 m_grid
->GetEventHandler()->ProcessEvent( event
);
1672 case WXK_NUMPAD_ENTER
:
1673 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1674 m_editor
->HandleReturn(event
);
1683 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1685 int row
= m_grid
->GetGridCursorRow();
1686 int col
= m_grid
->GetGridCursorCol();
1687 wxRect rect
= m_grid
->CellToRect( row
, col
);
1689 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
1691 // if cell width is smaller than grid client area, cell is wholly visible
1692 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
1694 switch ( event
.GetKeyCode() )
1699 case WXK_NUMPAD_ENTER
:
1704 if ( wholeCellVisible
)
1706 // no special processing needed...
1711 // do special processing for partly visible cell...
1713 // get the widths of all cells previous to this one
1715 for ( int i
= 0; i
< col
; i
++ )
1717 colXPos
+= m_grid
->GetColSize(i
);
1720 int xUnit
= 1, yUnit
= 1;
1721 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1724 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1728 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
1736 if ( wholeCellVisible
)
1738 // no special processing needed...
1743 // do special processing for partly visible cell...
1746 wxString value
= m_grid
->GetCellValue(row
, col
);
1747 if ( wxEmptyString
!= value
)
1749 // get width of cell CONTENTS (text)
1751 wxFont font
= m_grid
->GetCellFont(row
, col
);
1752 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
1754 // try to RIGHT align the text by scrolling
1755 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
1757 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
1758 // otherwise the last part of the cell content might be hidden below the scroll bar
1759 // FIXME: maybe there is a more suitable correction?
1760 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
1761 if ( textWidth
< 0 )
1767 // get the widths of all cells previous to this one
1769 for ( int i
= 0; i
< col
; i
++ )
1771 colXPos
+= m_grid
->GetColSize(i
);
1774 // and add the (modified) text width of the cell contents
1775 // as we'd like to see the last part of the cell contents
1776 colXPos
+= textWidth
;
1778 int xUnit
= 1, yUnit
= 1;
1779 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1780 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1791 // ----------------------------------------------------------------------------
1792 // wxGridCellWorker is an (almost) empty common base class for
1793 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1794 // ----------------------------------------------------------------------------
1796 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1801 wxGridCellWorker::~wxGridCellWorker()
1805 // ============================================================================
1807 // ============================================================================
1809 // ----------------------------------------------------------------------------
1810 // wxGridCellRenderer
1811 // ----------------------------------------------------------------------------
1813 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1814 wxGridCellAttr
& attr
,
1817 int WXUNUSED(row
), int WXUNUSED(col
),
1820 dc
.SetBackgroundMode( wxBRUSHSTYLE_SOLID
);
1822 // grey out fields if the grid is disabled
1823 if ( grid
.IsEnabled() )
1828 if ( grid
.HasFocus() )
1829 clr
= grid
.GetSelectionBackground();
1831 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
1832 dc
.SetBrush( wxBrush(clr
, wxBRUSHSTYLE_SOLID
) );
1836 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxBRUSHSTYLE_SOLID
) );
1841 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
), wxBRUSHSTYLE_SOLID
));
1844 dc
.SetPen( *wxTRANSPARENT_PEN
);
1845 dc
.DrawRectangle(rect
);
1848 // ----------------------------------------------------------------------------
1849 // wxGridCellStringRenderer
1850 // ----------------------------------------------------------------------------
1852 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
1853 const wxGridCellAttr
& attr
,
1857 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
1859 // TODO some special colours for attr.IsReadOnly() case?
1861 // different coloured text when the grid is disabled
1862 if ( grid
.IsEnabled() )
1867 if ( grid
.HasFocus() )
1868 clr
= grid
.GetSelectionBackground();
1870 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
1871 dc
.SetTextBackground( clr
);
1872 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1876 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1877 dc
.SetTextForeground( attr
.GetTextColour() );
1882 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1883 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
1886 dc
.SetFont( attr
.GetFont() );
1889 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
1891 const wxString
& text
)
1893 wxCoord x
= 0, y
= 0, max_x
= 0;
1894 dc
.SetFont(attr
.GetFont());
1895 wxStringTokenizer
tk(text
, _T('\n'));
1896 while ( tk
.HasMoreTokens() )
1898 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1899 max_x
= wxMax(max_x
, x
);
1902 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1904 return wxSize(max_x
, y
);
1907 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1908 wxGridCellAttr
& attr
,
1912 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1915 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1916 wxGridCellAttr
& attr
,
1918 const wxRect
& rectCell
,
1922 wxRect rect
= rectCell
;
1925 // erase only this cells background, overflow cells should have been erased
1926 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1929 attr
.GetAlignment(&hAlign
, &vAlign
);
1931 int overflowCols
= 0;
1933 if (attr
.GetOverflow())
1935 int cols
= grid
.GetNumberCols();
1936 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1937 int cell_rows
, cell_cols
;
1938 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <= 0
1939 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1941 int i
, c_cols
, c_rows
;
1942 for (i
= col
+cell_cols
; i
< cols
; i
++)
1944 bool is_empty
= true;
1945 for (int j
=row
; j
< row
+ cell_rows
; j
++)
1947 // check w/ anchor cell for multicell block
1948 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1951 if (!grid
.GetTable()->IsEmptyCell(j
+ c_rows
, i
))
1960 rect
.width
+= grid
.GetColSize(i
);
1968 if (rect
.width
>= best_width
)
1972 overflowCols
= i
- col
- cell_cols
+ 1;
1973 if (overflowCols
>= cols
)
1974 overflowCols
= cols
- 1;
1977 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1979 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1981 clip
.x
+= rectCell
.width
;
1982 // draw each overflow cell individually
1983 int col_end
= col
+ cell_cols
+ overflowCols
;
1984 if (col_end
>= grid
.GetNumberCols())
1985 col_end
= grid
.GetNumberCols() - 1;
1986 for (int i
= col
+ cell_cols
; i
<= col_end
; i
++)
1988 clip
.width
= grid
.GetColSize(i
) - 1;
1989 dc
.DestroyClippingRegion();
1990 dc
.SetClippingRegion(clip
);
1992 SetTextColoursAndFont(grid
, attr
, dc
,
1993 grid
.IsInSelection(row
,i
));
1995 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1996 rect
, hAlign
, vAlign
);
1997 clip
.x
+= grid
.GetColSize(i
) - 1;
2003 dc
.DestroyClippingRegion();
2007 // now we only have to draw the text
2008 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2010 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
2011 rect
, hAlign
, vAlign
);
2014 // ----------------------------------------------------------------------------
2015 // wxGridCellNumberRenderer
2016 // ----------------------------------------------------------------------------
2018 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2020 wxGridTableBase
*table
= grid
.GetTable();
2022 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
2024 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
2028 text
= table
->GetValue(row
, col
);
2034 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
2035 wxGridCellAttr
& attr
,
2037 const wxRect
& rectCell
,
2041 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2043 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2045 // draw the text right aligned by default
2047 attr
.GetAlignment(&hAlign
, &vAlign
);
2048 hAlign
= wxALIGN_RIGHT
;
2050 wxRect rect
= rectCell
;
2053 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2056 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
2057 wxGridCellAttr
& attr
,
2061 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2064 // ----------------------------------------------------------------------------
2065 // wxGridCellFloatRenderer
2066 // ----------------------------------------------------------------------------
2068 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
2071 SetPrecision(precision
);
2074 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
2076 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
2077 renderer
->m_width
= m_width
;
2078 renderer
->m_precision
= m_precision
;
2079 renderer
->m_format
= m_format
;
2084 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2086 wxGridTableBase
*table
= grid
.GetTable();
2091 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
2093 val
= table
->GetValueAsDouble(row
, col
);
2098 text
= table
->GetValue(row
, col
);
2099 hasDouble
= text
.ToDouble(&val
);
2106 if ( m_width
== -1 )
2108 if ( m_precision
== -1 )
2110 // default width/precision
2111 m_format
= _T("%f");
2115 m_format
.Printf(_T("%%.%df"), m_precision
);
2118 else if ( m_precision
== -1 )
2120 // default precision
2121 m_format
.Printf(_T("%%%d.f"), m_width
);
2125 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
2129 text
.Printf(m_format
, val
);
2132 //else: text already contains the string
2137 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
2138 wxGridCellAttr
& attr
,
2140 const wxRect
& rectCell
,
2144 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2146 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2148 // draw the text right aligned by default
2150 attr
.GetAlignment(&hAlign
, &vAlign
);
2151 hAlign
= wxALIGN_RIGHT
;
2153 wxRect rect
= rectCell
;
2156 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2159 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
2160 wxGridCellAttr
& attr
,
2164 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2167 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
2171 // reset to defaults
2177 wxString tmp
= params
.BeforeFirst(_T(','));
2181 if ( tmp
.ToLong(&width
) )
2183 SetWidth((int)width
);
2187 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
2191 tmp
= params
.AfterFirst(_T(','));
2195 if ( tmp
.ToLong(&precision
) )
2197 SetPrecision((int)precision
);
2201 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
2207 // ----------------------------------------------------------------------------
2208 // wxGridCellBoolRenderer
2209 // ----------------------------------------------------------------------------
2211 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
2213 // FIXME these checkbox size calculations are really ugly...
2215 // between checkmark and box
2216 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2218 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2219 wxGridCellAttr
& WXUNUSED(attr
),
2224 // compute it only once (no locks for MT safeness in GUI thread...)
2225 if ( !ms_sizeCheckMark
.x
)
2227 // get checkbox size
2228 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2229 wxSize size
= checkbox
->GetBestSize();
2230 wxCoord checkSize
= size
.y
+ 2 * wxGRID_CHECKMARK_MARGIN
;
2232 #if defined(__WXMOTIF__)
2233 checkSize
-= size
.y
/ 2;
2238 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2241 return ms_sizeCheckMark
;
2244 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2245 wxGridCellAttr
& attr
,
2251 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2253 // draw a check mark in the centre (ignoring alignment - TODO)
2254 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2256 // don't draw outside the cell
2257 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2258 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2260 // and even leave (at least) 1 pixel margin
2261 size
.x
= size
.y
= minSize
;
2264 // draw a border around checkmark
2266 attr
.GetAlignment(&hAlign
, &vAlign
);
2269 if (hAlign
== wxALIGN_CENTRE
)
2271 rectBorder
.x
= rect
.x
+ rect
.width
/ 2 - size
.x
/ 2;
2272 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2273 rectBorder
.width
= size
.x
;
2274 rectBorder
.height
= size
.y
;
2276 else if (hAlign
== wxALIGN_LEFT
)
2278 rectBorder
.x
= rect
.x
+ 2;
2279 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2280 rectBorder
.width
= size
.x
;
2281 rectBorder
.height
= size
.y
;
2283 else if (hAlign
== wxALIGN_RIGHT
)
2285 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2286 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2287 rectBorder
.width
= size
.x
;
2288 rectBorder
.height
= size
.y
;
2292 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2294 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2298 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2299 value
= wxGridCellBoolEditor::IsTrueValue(cellval
);
2304 flags
|= wxCONTROL_CHECKED
;
2306 wxRendererNative::Get().DrawCheckBox( &grid
, dc
, rectBorder
, flags
);
2309 // ----------------------------------------------------------------------------
2311 // ----------------------------------------------------------------------------
2313 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2317 m_isReadOnly
= Unset
;
2322 m_attrkind
= wxGridCellAttr::Cell
;
2324 m_sizeRows
= m_sizeCols
= 1;
2325 m_overflow
= UnsetOverflow
;
2327 SetDefAttr(attrDefault
);
2330 wxGridCellAttr
*wxGridCellAttr::Clone() const
2332 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2334 if ( HasTextColour() )
2335 attr
->SetTextColour(GetTextColour());
2336 if ( HasBackgroundColour() )
2337 attr
->SetBackgroundColour(GetBackgroundColour());
2339 attr
->SetFont(GetFont());
2340 if ( HasAlignment() )
2341 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2343 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2347 attr
->SetRenderer(m_renderer
);
2348 m_renderer
->IncRef();
2352 attr
->SetEditor(m_editor
);
2357 attr
->SetReadOnly();
2359 attr
->SetOverflow( m_overflow
== Overflow
);
2360 attr
->SetKind( m_attrkind
);
2365 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2367 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2368 SetTextColour(mergefrom
->GetTextColour());
2369 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2370 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2371 if ( !HasFont() && mergefrom
->HasFont() )
2372 SetFont(mergefrom
->GetFont());
2373 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2376 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2377 SetAlignment(hAlign
, vAlign
);
2379 if ( !HasSize() && mergefrom
->HasSize() )
2380 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2382 // Directly access member functions as GetRender/Editor don't just return
2383 // m_renderer/m_editor
2385 // Maybe add support for merge of Render and Editor?
2386 if (!HasRenderer() && mergefrom
->HasRenderer() )
2388 m_renderer
= mergefrom
->m_renderer
;
2389 m_renderer
->IncRef();
2391 if ( !HasEditor() && mergefrom
->HasEditor() )
2393 m_editor
= mergefrom
->m_editor
;
2396 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2397 SetReadOnly(mergefrom
->IsReadOnly());
2399 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2400 SetOverflow(mergefrom
->GetOverflow());
2402 SetDefAttr(mergefrom
->m_defGridAttr
);
2405 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2407 // The size of a cell is normally 1,1
2409 // If this cell is larger (2,2) then this is the top left cell
2410 // the other cells that will be covered (lower right cells) must be
2411 // set to negative or zero values such that
2412 // row + num_rows of the covered cell points to the larger cell (this cell)
2413 // same goes for the col + num_cols.
2415 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2417 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
2418 !((num_rows
<= 0) && (num_cols
> 0)) ||
2419 !((num_rows
== 0) && (num_cols
== 0))),
2420 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2422 m_sizeRows
= num_rows
;
2423 m_sizeCols
= num_cols
;
2426 const wxColour
& wxGridCellAttr::GetTextColour() const
2428 if (HasTextColour())
2432 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2434 return m_defGridAttr
->GetTextColour();
2438 wxFAIL_MSG(wxT("Missing default cell attribute"));
2439 return wxNullColour
;
2443 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2445 if (HasBackgroundColour())
2449 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2451 return m_defGridAttr
->GetBackgroundColour();
2455 wxFAIL_MSG(wxT("Missing default cell attribute"));
2456 return wxNullColour
;
2460 const wxFont
& wxGridCellAttr::GetFont() const
2466 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2468 return m_defGridAttr
->GetFont();
2472 wxFAIL_MSG(wxT("Missing default cell attribute"));
2477 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2486 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2488 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2492 wxFAIL_MSG(wxT("Missing default cell attribute"));
2496 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2499 *num_rows
= m_sizeRows
;
2501 *num_cols
= m_sizeCols
;
2504 // GetRenderer and GetEditor use a slightly different decision path about
2505 // which attribute to use. If a non-default attr object has one then it is
2506 // used, otherwise the default editor or renderer is fetched from the grid and
2507 // used. It should be the default for the data type of the cell. If it is
2508 // NULL (because the table has a type that the grid does not have in its
2509 // registry), then the grid's default editor or renderer is used.
2511 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(const wxGrid
* grid
, int row
, int col
) const
2513 wxGridCellRenderer
*renderer
= NULL
;
2515 if ( m_renderer
&& this != m_defGridAttr
)
2517 // use the cells renderer if it has one
2518 renderer
= m_renderer
;
2521 else // no non-default cell renderer
2523 // get default renderer for the data type
2526 // GetDefaultRendererForCell() will do IncRef() for us
2527 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2530 if ( renderer
== NULL
)
2532 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2534 // if we still don't have one then use the grid default
2535 // (no need for IncRef() here neither)
2536 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2538 else // default grid attr
2540 // use m_renderer which we had decided not to use initially
2541 renderer
= m_renderer
;
2548 // we're supposed to always find something
2549 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2554 // same as above, except for s/renderer/editor/g
2555 wxGridCellEditor
* wxGridCellAttr::GetEditor(const wxGrid
* grid
, int row
, int col
) const
2557 wxGridCellEditor
*editor
= NULL
;
2559 if ( m_editor
&& this != m_defGridAttr
)
2561 // use the cells editor if it has one
2565 else // no non default cell editor
2567 // get default editor for the data type
2570 // GetDefaultEditorForCell() will do IncRef() for us
2571 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2574 if ( editor
== NULL
)
2576 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2578 // if we still don't have one then use the grid default
2579 // (no need for IncRef() here neither)
2580 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2582 else // default grid attr
2584 // use m_editor which we had decided not to use initially
2592 // we're supposed to always find something
2593 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2598 // ----------------------------------------------------------------------------
2599 // wxGridCellAttrData
2600 // ----------------------------------------------------------------------------
2602 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2604 int n
= FindIndex(row
, col
);
2605 if ( n
== wxNOT_FOUND
)
2609 // add the attribute
2610 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2612 //else: nothing to do
2614 else // we already have an attribute for this cell
2618 // change the attribute
2619 m_attrs
[(size_t)n
].attr
= attr
;
2623 // remove this attribute
2624 m_attrs
.RemoveAt((size_t)n
);
2629 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2631 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2633 int n
= FindIndex(row
, col
);
2634 if ( n
!= wxNOT_FOUND
)
2636 attr
= m_attrs
[(size_t)n
].attr
;
2643 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2645 size_t count
= m_attrs
.GetCount();
2646 for ( size_t n
= 0; n
< count
; n
++ )
2648 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2649 wxCoord row
= coords
.GetRow();
2650 if ((size_t)row
>= pos
)
2654 // If rows inserted, include row counter where necessary
2655 coords
.SetRow(row
+ numRows
);
2657 else if (numRows
< 0)
2659 // If rows deleted ...
2660 if ((size_t)row
>= pos
- numRows
)
2662 // ...either decrement row counter (if row still exists)...
2663 coords
.SetRow(row
+ numRows
);
2667 // ...or remove the attribute
2668 m_attrs
.RemoveAt(n
);
2677 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2679 size_t count
= m_attrs
.GetCount();
2680 for ( size_t n
= 0; n
< count
; n
++ )
2682 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2683 wxCoord col
= coords
.GetCol();
2684 if ( (size_t)col
>= pos
)
2688 // If rows inserted, include row counter where necessary
2689 coords
.SetCol(col
+ numCols
);
2691 else if (numCols
< 0)
2693 // If rows deleted ...
2694 if ((size_t)col
>= pos
- numCols
)
2696 // ...either decrement row counter (if row still exists)...
2697 coords
.SetCol(col
+ numCols
);
2701 // ...or remove the attribute
2702 m_attrs
.RemoveAt(n
);
2711 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2713 size_t count
= m_attrs
.GetCount();
2714 for ( size_t n
= 0; n
< count
; n
++ )
2716 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2717 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2726 // ----------------------------------------------------------------------------
2727 // wxGridRowOrColAttrData
2728 // ----------------------------------------------------------------------------
2730 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2732 size_t count
= m_attrs
.GetCount();
2733 for ( size_t n
= 0; n
< count
; n
++ )
2735 m_attrs
[n
]->DecRef();
2739 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2741 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2743 int n
= m_rowsOrCols
.Index(rowOrCol
);
2744 if ( n
!= wxNOT_FOUND
)
2746 attr
= m_attrs
[(size_t)n
];
2753 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2755 int i
= m_rowsOrCols
.Index(rowOrCol
);
2756 if ( i
== wxNOT_FOUND
)
2760 // add the attribute
2761 m_rowsOrCols
.Add(rowOrCol
);
2764 // nothing to remove
2768 size_t n
= (size_t)i
;
2771 // change the attribute
2772 m_attrs
[n
]->DecRef();
2777 // remove this attribute
2778 m_attrs
[n
]->DecRef();
2779 m_rowsOrCols
.RemoveAt(n
);
2780 m_attrs
.RemoveAt(n
);
2785 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2787 size_t count
= m_attrs
.GetCount();
2788 for ( size_t n
= 0; n
< count
; n
++ )
2790 int & rowOrCol
= m_rowsOrCols
[n
];
2791 if ( (size_t)rowOrCol
>= pos
)
2793 if ( numRowsOrCols
> 0 )
2795 // If rows inserted, include row counter where necessary
2796 rowOrCol
+= numRowsOrCols
;
2798 else if ( numRowsOrCols
< 0)
2800 // If rows deleted, either decrement row counter (if row still exists)
2801 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2802 rowOrCol
+= numRowsOrCols
;
2805 m_rowsOrCols
.RemoveAt(n
);
2806 m_attrs
[n
]->DecRef();
2807 m_attrs
.RemoveAt(n
);
2816 // ----------------------------------------------------------------------------
2817 // wxGridCellAttrProvider
2818 // ----------------------------------------------------------------------------
2820 wxGridCellAttrProvider::wxGridCellAttrProvider()
2822 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2825 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2830 void wxGridCellAttrProvider::InitData()
2832 m_data
= new wxGridCellAttrProviderData
;
2835 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2836 wxGridCellAttr::wxAttrKind kind
) const
2838 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2843 case (wxGridCellAttr::Any
):
2844 // Get cached merge attributes.
2845 // Currently not used as no cache implemented as not mutable
2846 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2849 // Basically implement old version.
2850 // Also check merge cache, so we don't have to re-merge every time..
2851 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2852 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2853 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2855 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
2857 // Two or more are non NULL
2858 attr
= new wxGridCellAttr
;
2859 attr
->SetKind(wxGridCellAttr::Merged
);
2861 // Order is important..
2864 attr
->MergeWith(attrcell
);
2869 attr
->MergeWith(attrcol
);
2874 attr
->MergeWith(attrrow
);
2878 // store merge attr if cache implemented
2880 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2884 // one or none is non null return it or null.
2903 case (wxGridCellAttr::Cell
):
2904 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2907 case (wxGridCellAttr::Col
):
2908 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2911 case (wxGridCellAttr::Row
):
2912 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2917 // (wxGridCellAttr::Default):
2918 // (wxGridCellAttr::Merged):
2926 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2932 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2935 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2940 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2943 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2948 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2951 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2955 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2957 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2961 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2965 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2967 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2971 // ----------------------------------------------------------------------------
2972 // wxGridTypeRegistry
2973 // ----------------------------------------------------------------------------
2975 wxGridTypeRegistry::~wxGridTypeRegistry()
2977 size_t count
= m_typeinfo
.GetCount();
2978 for ( size_t i
= 0; i
< count
; i
++ )
2979 delete m_typeinfo
[i
];
2982 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2983 wxGridCellRenderer
* renderer
,
2984 wxGridCellEditor
* editor
)
2986 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2988 // is it already registered?
2989 int loc
= FindRegisteredDataType(typeName
);
2990 if ( loc
!= wxNOT_FOUND
)
2992 delete m_typeinfo
[loc
];
2993 m_typeinfo
[loc
] = info
;
2997 m_typeinfo
.Add(info
);
3001 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
3003 size_t count
= m_typeinfo
.GetCount();
3004 for ( size_t i
= 0; i
< count
; i
++ )
3006 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
3015 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
3017 int index
= FindRegisteredDataType(typeName
);
3018 if ( index
== wxNOT_FOUND
)
3020 // check whether this is one of the standard ones, in which case
3021 // register it "on the fly"
3023 if ( typeName
== wxGRID_VALUE_STRING
)
3025 RegisterDataType(wxGRID_VALUE_STRING
,
3026 new wxGridCellStringRenderer
,
3027 new wxGridCellTextEditor
);
3030 #endif // wxUSE_TEXTCTRL
3032 if ( typeName
== wxGRID_VALUE_BOOL
)
3034 RegisterDataType(wxGRID_VALUE_BOOL
,
3035 new wxGridCellBoolRenderer
,
3036 new wxGridCellBoolEditor
);
3039 #endif // wxUSE_CHECKBOX
3041 if ( typeName
== wxGRID_VALUE_NUMBER
)
3043 RegisterDataType(wxGRID_VALUE_NUMBER
,
3044 new wxGridCellNumberRenderer
,
3045 new wxGridCellNumberEditor
);
3047 else if ( typeName
== wxGRID_VALUE_FLOAT
)
3049 RegisterDataType(wxGRID_VALUE_FLOAT
,
3050 new wxGridCellFloatRenderer
,
3051 new wxGridCellFloatEditor
);
3054 #endif // wxUSE_TEXTCTRL
3056 if ( typeName
== wxGRID_VALUE_CHOICE
)
3058 RegisterDataType(wxGRID_VALUE_CHOICE
,
3059 new wxGridCellStringRenderer
,
3060 new wxGridCellChoiceEditor
);
3063 #endif // wxUSE_COMBOBOX
3068 // we get here only if just added the entry for this type, so return
3070 index
= m_typeinfo
.GetCount() - 1;
3076 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
3078 int index
= FindDataType(typeName
);
3079 if ( index
== wxNOT_FOUND
)
3081 // the first part of the typename is the "real" type, anything after ':'
3082 // are the parameters for the renderer
3083 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
3084 if ( index
== wxNOT_FOUND
)
3089 wxGridCellRenderer
*renderer
= GetRenderer(index
);
3090 wxGridCellRenderer
*rendererOld
= renderer
;
3091 renderer
= renderer
->Clone();
3092 rendererOld
->DecRef();
3094 wxGridCellEditor
*editor
= GetEditor(index
);
3095 wxGridCellEditor
*editorOld
= editor
;
3096 editor
= editor
->Clone();
3097 editorOld
->DecRef();
3099 // do it even if there are no parameters to reset them to defaults
3100 wxString params
= typeName
.AfterFirst(_T(':'));
3101 renderer
->SetParameters(params
);
3102 editor
->SetParameters(params
);
3104 // register the new typename
3105 RegisterDataType(typeName
, renderer
, editor
);
3107 // we just registered it, it's the last one
3108 index
= m_typeinfo
.GetCount() - 1;
3114 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
3116 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
3123 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
3125 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
3132 // ----------------------------------------------------------------------------
3134 // ----------------------------------------------------------------------------
3136 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
3138 wxGridTableBase::wxGridTableBase()
3140 m_view
= (wxGrid
*) NULL
;
3141 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
3144 wxGridTableBase::~wxGridTableBase()
3146 delete m_attrProvider
;
3149 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
3151 delete m_attrProvider
;
3152 m_attrProvider
= attrProvider
;
3155 bool wxGridTableBase::CanHaveAttributes()
3157 if ( ! GetAttrProvider() )
3159 // use the default attr provider by default
3160 SetAttrProvider(new wxGridCellAttrProvider
);
3166 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
3168 if ( m_attrProvider
)
3169 return m_attrProvider
->GetAttr(row
, col
, kind
);
3171 return (wxGridCellAttr
*)NULL
;
3174 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
3176 if ( m_attrProvider
)
3179 attr
->SetKind(wxGridCellAttr::Cell
);
3180 m_attrProvider
->SetAttr(attr
, row
, col
);
3184 // as we take ownership of the pointer and don't store it, we must
3190 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3192 if ( m_attrProvider
)
3194 attr
->SetKind(wxGridCellAttr::Row
);
3195 m_attrProvider
->SetRowAttr(attr
, row
);
3199 // as we take ownership of the pointer and don't store it, we must
3205 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3207 if ( m_attrProvider
)
3209 attr
->SetKind(wxGridCellAttr::Col
);
3210 m_attrProvider
->SetColAttr(attr
, col
);
3214 // as we take ownership of the pointer and don't store it, we must
3220 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3221 size_t WXUNUSED(numRows
) )
3223 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3228 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3230 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3235 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3236 size_t WXUNUSED(numRows
) )
3238 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3243 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3244 size_t WXUNUSED(numCols
) )
3246 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3251 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3253 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3258 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3259 size_t WXUNUSED(numCols
) )
3261 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3266 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3270 // RD: Starting the rows at zero confuses users,
3271 // no matter how much it makes sense to us geeks.
3277 wxString
wxGridTableBase::GetColLabelValue( int col
)
3279 // default col labels are:
3280 // cols 0 to 25 : A-Z
3281 // cols 26 to 675 : AA-ZZ
3286 for ( n
= 1; ; n
++ )
3288 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
3294 // reverse the string...
3296 for ( i
= 0; i
< n
; i
++ )
3304 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3306 return wxGRID_VALUE_STRING
;
3309 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3310 const wxString
& typeName
)
3312 return typeName
== wxGRID_VALUE_STRING
;
3315 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3317 return CanGetValueAs(row
, col
, typeName
);
3320 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3325 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3330 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3335 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3336 long WXUNUSED(value
) )
3340 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3341 double WXUNUSED(value
) )
3345 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3346 bool WXUNUSED(value
) )
3350 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3351 const wxString
& WXUNUSED(typeName
) )
3356 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3357 const wxString
& WXUNUSED(typeName
),
3358 void* WXUNUSED(value
) )
3362 //////////////////////////////////////////////////////////////////////
3364 // Message class for the grid table to send requests and notifications
3368 wxGridTableMessage::wxGridTableMessage()
3370 m_table
= (wxGridTableBase
*) NULL
;
3376 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3377 int commandInt1
, int commandInt2
)
3381 m_comInt1
= commandInt1
;
3382 m_comInt2
= commandInt2
;
3385 //////////////////////////////////////////////////////////////////////
3387 // A basic grid table for string data. An object of this class will
3388 // created by wxGrid if you don't specify an alternative table class.
3391 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3393 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3395 wxGridStringTable::wxGridStringTable()
3400 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3403 m_data
.Alloc( numRows
);
3406 sa
.Alloc( numCols
);
3407 sa
.Add( wxEmptyString
, numCols
);
3409 m_data
.Add( sa
, numRows
);
3412 wxGridStringTable::~wxGridStringTable()
3416 int wxGridStringTable::GetNumberRows()
3418 return m_data
.GetCount();
3421 int wxGridStringTable::GetNumberCols()
3423 if ( m_data
.GetCount() > 0 )
3424 return m_data
[0].GetCount();
3429 wxString
wxGridStringTable::GetValue( int row
, int col
)
3431 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3433 _T("invalid row or column index in wxGridStringTable") );
3435 return m_data
[row
][col
];
3438 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3440 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3441 _T("invalid row or column index in wxGridStringTable") );
3443 m_data
[row
][col
] = value
;
3446 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3448 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3450 _T("invalid row or column index in wxGridStringTable") );
3452 return (m_data
[row
][col
] == wxEmptyString
);
3455 void wxGridStringTable::Clear()
3458 int numRows
, numCols
;
3460 numRows
= m_data
.GetCount();
3463 numCols
= m_data
[0].GetCount();
3465 for ( row
= 0; row
< numRows
; row
++ )
3467 for ( col
= 0; col
< numCols
; col
++ )
3469 m_data
[row
][col
] = wxEmptyString
;
3475 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3477 size_t curNumRows
= m_data
.GetCount();
3478 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3479 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3481 if ( pos
>= curNumRows
)
3483 return AppendRows( numRows
);
3487 sa
.Alloc( curNumCols
);
3488 sa
.Add( wxEmptyString
, curNumCols
);
3489 m_data
.Insert( sa
, pos
, numRows
);
3493 wxGridTableMessage
msg( this,
3494 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3498 GetView()->ProcessTableMessage( msg
);
3504 bool wxGridStringTable::AppendRows( size_t numRows
)
3506 size_t curNumRows
= m_data
.GetCount();
3507 size_t curNumCols
= ( curNumRows
> 0
3508 ? m_data
[0].GetCount()
3509 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3512 if ( curNumCols
> 0 )
3514 sa
.Alloc( curNumCols
);
3515 sa
.Add( wxEmptyString
, curNumCols
);
3518 m_data
.Add( sa
, numRows
);
3522 wxGridTableMessage
msg( this,
3523 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3526 GetView()->ProcessTableMessage( msg
);
3532 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3534 size_t curNumRows
= m_data
.GetCount();
3536 if ( pos
>= curNumRows
)
3538 wxFAIL_MSG( wxString::Format
3540 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3542 (unsigned long)numRows
,
3543 (unsigned long)curNumRows
3549 if ( numRows
> curNumRows
- pos
)
3551 numRows
= curNumRows
- pos
;
3554 if ( numRows
>= curNumRows
)
3560 m_data
.RemoveAt( pos
, numRows
);
3565 wxGridTableMessage
msg( this,
3566 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3570 GetView()->ProcessTableMessage( msg
);
3576 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3580 size_t curNumRows
= m_data
.GetCount();
3581 size_t curNumCols
= ( curNumRows
> 0
3582 ? m_data
[0].GetCount()
3583 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3585 if ( pos
>= curNumCols
)
3587 return AppendCols( numCols
);
3590 if ( !m_colLabels
.IsEmpty() )
3592 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
3595 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
3596 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
3599 for ( row
= 0; row
< curNumRows
; row
++ )
3601 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3603 m_data
[row
].Insert( wxEmptyString
, col
);
3609 wxGridTableMessage
msg( this,
3610 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3614 GetView()->ProcessTableMessage( msg
);
3620 bool wxGridStringTable::AppendCols( size_t numCols
)
3624 size_t curNumRows
= m_data
.GetCount();
3629 // TODO: something better than this ?
3631 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3636 for ( row
= 0; row
< curNumRows
; row
++ )
3638 m_data
[row
].Add( wxEmptyString
, numCols
);
3643 wxGridTableMessage
msg( this,
3644 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3647 GetView()->ProcessTableMessage( msg
);
3653 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3657 size_t curNumRows
= m_data
.GetCount();
3658 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3659 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3661 if ( pos
>= curNumCols
)
3663 wxFAIL_MSG( wxString::Format
3665 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3667 (unsigned long)numCols
,
3668 (unsigned long)curNumCols
3675 colID
= GetView()->GetColAt( pos
);
3679 if ( numCols
> curNumCols
- colID
)
3681 numCols
= curNumCols
- colID
;
3684 if ( !m_colLabels
.IsEmpty() )
3686 // m_colLabels stores just as many elements as it needs, e.g. if only
3687 // the label of the first column had been set it would have only one
3688 // element and not numCols, so account for it
3689 int nToRm
= m_colLabels
.size() - colID
;
3691 m_colLabels
.RemoveAt( colID
, nToRm
);
3694 for ( row
= 0; row
< curNumRows
; row
++ )
3696 if ( numCols
>= curNumCols
)
3698 m_data
[row
].Clear();
3702 m_data
[row
].RemoveAt( colID
, numCols
);
3708 wxGridTableMessage
msg( this,
3709 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3713 GetView()->ProcessTableMessage( msg
);
3719 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3721 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3723 // using default label
3725 return wxGridTableBase::GetRowLabelValue( row
);
3729 return m_rowLabels
[row
];
3733 wxString
wxGridStringTable::GetColLabelValue( int col
)
3735 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3737 // using default label
3739 return wxGridTableBase::GetColLabelValue( col
);
3743 return m_colLabels
[col
];
3747 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3749 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3751 int n
= m_rowLabels
.GetCount();
3754 for ( i
= n
; i
<= row
; i
++ )
3756 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3760 m_rowLabels
[row
] = value
;
3763 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3765 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3767 int n
= m_colLabels
.GetCount();
3770 for ( i
= n
; i
<= col
; i
++ )
3772 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3776 m_colLabels
[col
] = value
;
3780 //////////////////////////////////////////////////////////////////////
3781 //////////////////////////////////////////////////////////////////////
3783 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
)
3784 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost
)
3787 void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
3789 m_owner
->CancelMouseCapture();
3792 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3794 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow
)
3795 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3796 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3797 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3800 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3802 const wxPoint
&pos
, const wxSize
&size
)
3803 : wxGridSubwindow(parent
, id
, pos
, size
)
3808 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3812 // NO - don't do this because it will set both the x and y origin
3813 // coords to match the parent scrolled window and we just want to
3814 // set the y coord - MB
3816 // m_owner->PrepareDC( dc );
3819 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3820 wxPoint pt
= dc
.GetDeviceOrigin();
3821 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
3823 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3824 m_owner
->DrawRowLabels( dc
, rows
);
3827 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3829 m_owner
->ProcessRowLabelMouseEvent( event
);
3832 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3834 m_owner
->GetEventHandler()->ProcessEvent( event
);
3837 //////////////////////////////////////////////////////////////////////
3839 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3841 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow
)
3842 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3843 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3844 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3847 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3849 const wxPoint
&pos
, const wxSize
&size
)
3850 : wxGridSubwindow(parent
, id
, pos
, size
)
3855 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3859 // NO - don't do this because it will set both the x and y origin
3860 // coords to match the parent scrolled window and we just want to
3861 // set the x coord - MB
3863 // m_owner->PrepareDC( dc );
3866 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3867 wxPoint pt
= dc
.GetDeviceOrigin();
3868 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3869 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
3871 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
3873 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3874 m_owner
->DrawColLabels( dc
, cols
);
3877 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3879 m_owner
->ProcessColLabelMouseEvent( event
);
3882 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3884 m_owner
->GetEventHandler()->ProcessEvent( event
);
3887 //////////////////////////////////////////////////////////////////////
3889 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3891 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow
)
3892 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3893 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3894 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3897 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3899 const wxPoint
&pos
, const wxSize
&size
)
3900 : wxGridSubwindow(parent
, id
, pos
, size
)
3905 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3909 int client_height
= 0;
3910 int client_width
= 0;
3911 GetClientSize( &client_width
, &client_height
);
3913 // VZ: any reason for this ifdef? (FIXME)
3919 rect
.SetWidth( client_width
- 2 );
3920 rect
.SetHeight( client_height
- 2 );
3922 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3924 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxPENSTYLE_SOLID
) );
3925 dc
.DrawLine( client_width
- 1, client_height
- 1, client_width
- 1, 0 );
3926 dc
.DrawLine( client_width
- 1, client_height
- 1, 0, client_height
- 1 );
3927 dc
.DrawLine( 0, 0, client_width
, 0 );
3928 dc
.DrawLine( 0, 0, 0, client_height
);
3930 dc
.SetPen( *wxWHITE_PEN
);
3931 dc
.DrawLine( 1, 1, client_width
- 1, 1 );
3932 dc
.DrawLine( 1, 1, 1, client_height
- 1 );
3936 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3938 m_owner
->ProcessCornerLabelMouseEvent( event
);
3941 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3943 m_owner
->GetEventHandler()->ProcessEvent(event
);
3946 //////////////////////////////////////////////////////////////////////
3948 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3950 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow
)
3951 EVT_PAINT( wxGridWindow::OnPaint
)
3952 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3953 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3954 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3955 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3956 EVT_CHAR( wxGridWindow::OnChar
)
3957 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3958 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3959 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3962 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3963 wxGridRowLabelWindow
*rowLblWin
,
3964 wxGridColLabelWindow
*colLblWin
,
3967 const wxSize
&size
)
3968 : wxGridSubwindow(parent
, id
, pos
, size
,
3969 wxWANTS_CHARS
| wxCLIP_CHILDREN
,
3970 wxT("grid window") )
3973 m_rowLabelWin
= rowLblWin
;
3974 m_colLabelWin
= colLblWin
;
3977 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3979 wxPaintDC
dc( this );
3980 m_owner
->PrepareDC( dc
);
3981 wxRegion reg
= GetUpdateRegion();
3982 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
3983 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
3985 #if WXGRID_DRAW_LINES
3986 m_owner
->DrawAllGridLines( dc
, reg
);
3989 m_owner
->DrawGridSpace( dc
);
3990 m_owner
->DrawHighlight( dc
, dirtyCells
);
3993 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3995 wxWindow::ScrollWindow( dx
, dy
, rect
);
3996 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3997 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
4000 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
4002 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
4005 m_owner
->ProcessGridCellMouseEvent( event
);
4008 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
4010 m_owner
->GetEventHandler()->ProcessEvent( event
);
4013 // This seems to be required for wxMotif/wxGTK otherwise the mouse
4014 // cursor must be in the cell edit control to get key events
4016 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
4018 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4022 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
4024 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4028 void wxGridWindow::OnChar( wxKeyEvent
& event
)
4030 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4034 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
4038 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
4040 // and if we have any selection, it has to be repainted, because it
4041 // uses different colour when the grid is not focused:
4042 if ( m_owner
->IsSelection() )
4048 // NB: Note that this code is in "else" branch only because the other
4049 // branch refreshes everything and so there's no point in calling
4050 // Refresh() again, *not* because it should only be done if
4051 // !IsSelection(). If the above code is ever optimized to refresh
4052 // only selected area, this needs to be moved out of the "else"
4053 // branch so that it's always executed.
4055 // current cell cursor {dis,re}appears on focus change:
4056 const wxGridCellCoords
cursorCoords(m_owner
->GetGridCursorRow(),
4057 m_owner
->GetGridCursorCol());
4058 const wxRect cursor
=
4059 m_owner
->BlockToDeviceRect(cursorCoords
, cursorCoords
);
4060 Refresh(true, &cursor
);
4063 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4067 //////////////////////////////////////////////////////////////////////
4069 // Internal Helper function for computing row or column from some
4070 // (unscrolled) coordinate value, using either
4071 // m_defaultRowHeight/m_defaultColWidth or binary search on array
4072 // of m_rowBottoms/m_ColRights to speed up the search!
4074 // Internal helper macros for simpler use of that function
4076 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
4077 const wxArrayInt
& BorderArray
, int nMax
,
4080 #define internalXToCol(x) XToCol(x, true)
4081 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
4082 m_minAcceptableRowHeight, \
4083 m_rowBottoms, m_numRows, true)
4085 /////////////////////////////////////////////////////////////////////
4087 #if wxUSE_EXTENDED_RTTI
4088 WX_DEFINE_FLAGS( wxGridStyle
)
4090 wxBEGIN_FLAGS( wxGridStyle
)
4091 // new style border flags, we put them first to
4092 // use them for streaming out
4093 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4094 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4095 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4096 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4097 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4098 wxFLAGS_MEMBER(wxBORDER_NONE
)
4100 // old style border flags
4101 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4102 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4103 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4104 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4105 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4106 wxFLAGS_MEMBER(wxBORDER
)
4108 // standard window styles
4109 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4110 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4111 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4112 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4113 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4114 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4115 wxFLAGS_MEMBER(wxVSCROLL
)
4116 wxFLAGS_MEMBER(wxHSCROLL
)
4118 wxEND_FLAGS( wxGridStyle
)
4120 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4122 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4123 wxHIDE_PROPERTY( Children
)
4124 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4125 wxEND_PROPERTIES_TABLE()
4127 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4128 wxEND_HANDLERS_TABLE()
4130 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4133 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
4136 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4139 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4140 EVT_PAINT( wxGrid::OnPaint
)
4141 EVT_SIZE( wxGrid::OnSize
)
4142 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
4143 EVT_KEY_UP( wxGrid::OnKeyUp
)
4144 EVT_CHAR ( wxGrid::OnChar
)
4145 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
4153 wxGrid::wxGrid( wxWindow
*parent
,
4158 const wxString
& name
)
4161 Create(parent
, id
, pos
, size
, style
, name
);
4164 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
4165 const wxPoint
& pos
, const wxSize
& size
,
4166 long style
, const wxString
& name
)
4168 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
4169 style
| wxWANTS_CHARS
, name
))
4172 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4173 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4176 SetInitialSize(size
);
4184 // Must do this or ~wxScrollHelper will pop the wrong event handler
4185 SetTargetWindow(this);
4187 wxSafeDecRef(m_defaultCellAttr
);
4189 #ifdef DEBUG_ATTR_CACHE
4190 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4191 wxPrintf(_T("wxGrid attribute cache statistics: "
4192 "total: %u, hits: %u (%u%%)\n"),
4193 total
, gs_nAttrCacheHits
,
4194 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4197 // if we own the table, just delete it, otherwise at least don't leave it
4198 // with dangling view pointer
4201 else if ( m_table
&& m_table
->GetView() == this )
4202 m_table
->SetView(NULL
);
4204 delete m_typeRegistry
;
4209 // ----- internal init and update functions
4212 // NOTE: If using the default visual attributes works everywhere then this can
4213 // be removed as well as the #else cases below.
4214 #define _USE_VISATTR 0
4216 void wxGrid::Create()
4218 // create the type registry
4219 m_typeRegistry
= new wxGridTypeRegistry
;
4221 m_cellEditCtrlEnabled
= false;
4223 m_defaultCellAttr
= new wxGridCellAttr();
4225 // Set default cell attributes
4226 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4227 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4228 m_defaultCellAttr
->SetFont(GetFont());
4229 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4230 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4231 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4234 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4235 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4237 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4238 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4241 m_defaultCellAttr
->SetTextColour(
4242 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4243 m_defaultCellAttr
->SetBackgroundColour(
4244 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4249 m_currentCellCoords
= wxGridNoCellCoords
;
4251 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4252 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4254 // subwindow components that make up the wxGrid
4255 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4260 m_colLabelWin
= new wxGridColLabelWindow( this,
4265 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4270 m_gridWin
= new wxGridWindow( this,
4277 SetTargetWindow( m_gridWin
);
4280 wxColour gfg
= gva
.colFg
;
4281 wxColour gbg
= gva
.colBg
;
4282 wxColour lfg
= lva
.colFg
;
4283 wxColour lbg
= lva
.colBg
;
4285 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4286 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4287 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4288 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4291 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4292 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4293 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4294 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4295 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4296 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4298 m_gridWin
->SetOwnForegroundColour(gfg
);
4299 m_gridWin
->SetOwnBackgroundColour(gbg
);
4304 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4305 wxGrid::wxGridSelectionModes selmode
)
4307 wxCHECK_MSG( !m_created
,
4309 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4311 m_numRows
= numRows
;
4312 m_numCols
= numCols
;
4314 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4315 m_table
->SetView( this );
4317 m_selection
= new wxGridSelection( this, selmode
);
4326 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4328 wxCHECK_RET( m_created
,
4329 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4331 m_selection
->SetSelectionMode( selmode
);
4334 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4336 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4337 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4339 return m_selection
->GetSelectionMode();
4342 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4343 wxGrid::wxGridSelectionModes selmode
)
4345 bool checkSelection
= false;
4348 // stop all processing
4353 m_table
->SetView(0);
4365 checkSelection
= true;
4367 // kill row and column size arrays
4368 m_colWidths
.Empty();
4369 m_colRights
.Empty();
4370 m_rowHeights
.Empty();
4371 m_rowBottoms
.Empty();
4376 m_numRows
= table
->GetNumberRows();
4377 m_numCols
= table
->GetNumberCols();
4380 m_table
->SetView( this );
4381 m_ownTable
= takeOwnership
;
4382 m_selection
= new wxGridSelection( this, selmode
);
4385 // If the newly set table is smaller than the
4386 // original one current cell and selection regions
4387 // might be invalid,
4388 m_selectingKeyboard
= wxGridNoCellCoords
;
4389 m_currentCellCoords
=
4390 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
4391 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
4392 if (m_selectingTopLeft
.GetRow() >= m_numRows
||
4393 m_selectingTopLeft
.GetCol() >= m_numCols
)
4395 m_selectingTopLeft
= wxGridNoCellCoords
;
4396 m_selectingBottomRight
= wxGridNoCellCoords
;
4399 m_selectingBottomRight
=
4400 wxGridCellCoords(wxMin(m_numRows
,
4401 m_selectingBottomRight
.GetRow()),
4403 m_selectingBottomRight
.GetCol()));
4413 void wxGrid::InitVars()
4417 m_cornerLabelWin
= NULL
;
4418 m_rowLabelWin
= NULL
;
4419 m_colLabelWin
= NULL
;
4426 m_defaultCellAttr
= NULL
;
4427 m_typeRegistry
= NULL
;
4428 m_winCapture
= NULL
;
4433 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4434 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4436 if ( m_rowLabelWin
)
4438 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4442 m_labelBackgroundColour
= *wxWHITE
;
4445 m_labelTextColour
= *wxBLACK
;
4448 m_attrCache
.row
= -1;
4449 m_attrCache
.col
= -1;
4450 m_attrCache
.attr
= NULL
;
4452 // TODO: something better than this ?
4454 m_labelFont
= this->GetFont();
4455 m_labelFont
.SetWeight( wxBOLD
);
4457 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4458 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4460 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4461 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4462 m_colLabelTextOrientation
= wxHORIZONTAL
;
4464 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4465 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4467 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4468 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4470 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4471 m_defaultRowHeight
+= 8;
4473 m_defaultRowHeight
+= 4;
4476 m_gridLineColour
= wxColour( 192,192,192 );
4477 m_gridLinesEnabled
= true;
4478 m_cellHighlightColour
= *wxBLACK
;
4479 m_cellHighlightPenWidth
= 2;
4480 m_cellHighlightROPenWidth
= 1;
4482 m_canDragColMove
= false;
4484 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4485 m_winCapture
= (wxWindow
*)NULL
;
4486 m_canDragRowSize
= true;
4487 m_canDragColSize
= true;
4488 m_canDragGridSize
= true;
4489 m_canDragCell
= false;
4491 m_dragRowOrCol
= -1;
4492 m_isDragging
= false;
4493 m_startDragPos
= wxDefaultPosition
;
4494 m_nativeColumnLabels
= false;
4496 m_waitForSlowClick
= false;
4498 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4499 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4501 m_currentCellCoords
= wxGridNoCellCoords
;
4505 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4506 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4508 m_editable
= true; // default for whole grid
4510 m_inOnKeyDown
= false;
4516 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4517 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4520 // ----------------------------------------------------------------------------
4521 // the idea is to call these functions only when necessary because they create
4522 // quite big arrays which eat memory mostly unnecessary - in particular, if
4523 // default widths/heights are used for all rows/columns, we may not use these
4526 // with some extra code, it should be possible to only store the widths/heights
4527 // different from default ones (resulting in space savings for huge grids) but
4528 // this is not done currently
4529 // ----------------------------------------------------------------------------
4531 void wxGrid::InitRowHeights()
4533 m_rowHeights
.Empty();
4534 m_rowBottoms
.Empty();
4536 m_rowHeights
.Alloc( m_numRows
);
4537 m_rowBottoms
.Alloc( m_numRows
);
4539 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4542 for ( int i
= 0; i
< m_numRows
; i
++ )
4544 rowBottom
+= m_defaultRowHeight
;
4545 m_rowBottoms
.Add( rowBottom
);
4549 void wxGrid::InitColWidths()
4551 m_colWidths
.Empty();
4552 m_colRights
.Empty();
4554 m_colWidths
.Alloc( m_numCols
);
4555 m_colRights
.Alloc( m_numCols
);
4557 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4560 for ( int i
= 0; i
< m_numCols
; i
++ )
4562 colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
4563 m_colRights
.Add( colRight
);
4567 int wxGrid::GetColWidth(int col
) const
4569 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4572 int wxGrid::GetColLeft(int col
) const
4574 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
4575 : m_colRights
[col
] - m_colWidths
[col
];
4578 int wxGrid::GetColRight(int col
) const
4580 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
4584 int wxGrid::GetRowHeight(int row
) const
4586 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4589 int wxGrid::GetRowTop(int row
) const
4591 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4592 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4595 int wxGrid::GetRowBottom(int row
) const
4597 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4598 : m_rowBottoms
[row
];
4601 void wxGrid::CalcDimensions()
4603 // compute the size of the scrollable area
4604 int w
= m_numCols
> 0 ? GetColRight(GetColAt(m_numCols
- 1)) : 0;
4605 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
4610 // take into account editor if shown
4611 if ( IsCellEditControlShown() )
4614 int r
= m_currentCellCoords
.GetRow();
4615 int c
= m_currentCellCoords
.GetCol();
4616 int x
= GetColLeft(c
);
4617 int y
= GetRowTop(r
);
4619 // how big is the editor
4620 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4621 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4622 editor
->GetControl()->GetSize(&w2
, &h2
);
4633 // preserve (more or less) the previous position
4635 GetViewStart( &x
, &y
);
4637 // ensure the position is valid for the new scroll ranges
4639 x
= wxMax( w
- 1, 0 );
4641 y
= wxMax( h
- 1, 0 );
4643 // do set scrollbar parameters
4644 SetScrollbars( m_scrollLineX
, m_scrollLineY
,
4645 GetScrollX(w
), GetScrollY(h
),
4647 GetBatchCount() != 0);
4649 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4650 // still must reposition the children
4654 void wxGrid::CalcWindowSizes()
4656 // escape if the window is has not been fully created yet
4658 if ( m_cornerLabelWin
== NULL
)
4662 GetClientSize( &cw
, &ch
);
4664 // this block of code tries to work around the following problem: the grid
4665 // could have been just resized to have enough space to show the full grid
4666 // window contents without the scrollbars, but its client size could be
4667 // not big enough because the grid has the scrollbars right now and so the
4668 // scrollbars would remain even though we don't need them any more
4670 // to prevent this from happening, check if we have enough space for
4671 // everything without the scrollbars and explicitly disable them then
4672 wxSize size
= GetSize() - GetWindowBorderSize();
4673 if ( size
!= wxSize(cw
, ch
) )
4675 // check if we have enough space for grid window after accounting for
4676 // the fixed size elements
4677 size
.x
-= m_rowLabelWidth
;
4678 size
.y
-= m_colLabelHeight
;
4680 const wxSize vsize
= m_gridWin
->GetVirtualSize();
4682 if ( size
.x
>= vsize
.x
&& size
.y
>= vsize
.y
)
4684 // yes, we do, so remove the scrollbars and use the new client size
4685 // (which should be the same as full window size - borders now)
4686 SetScrollbars(0, 0, 0, 0);
4687 GetClientSize(&cw
, &ch
);
4691 // the grid may be too small to have enough space for the labels yet, don't
4692 // size the windows to negative sizes in this case
4693 int gw
= cw
- m_rowLabelWidth
;
4694 int gh
= ch
- m_colLabelHeight
;
4700 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4701 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4703 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4704 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, gw
, m_colLabelHeight
);
4706 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4707 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, gh
);
4709 if ( m_gridWin
&& m_gridWin
->IsShown() )
4710 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, gw
, gh
);
4713 // this is called when the grid table sends a message
4714 // to indicate that it has been redimensioned
4716 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4719 bool result
= false;
4721 // Clear the attribute cache as the attribute might refer to a different
4722 // cell than stored in the cache after adding/removing rows/columns.
4725 // By the same reasoning, the editor should be dismissed if columns are
4726 // added or removed. And for consistency, it should IMHO always be
4727 // removed, not only if the cell "underneath" it actually changes.
4728 // For now, I intentionally do not save the editor's content as the
4729 // cell it might want to save that stuff to might no longer exist.
4730 HideCellEditControl();
4733 // if we were using the default widths/heights so far, we must change them
4735 if ( m_colWidths
.IsEmpty() )
4740 if ( m_rowHeights
.IsEmpty() )
4746 switch ( msg
.GetId() )
4748 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4750 size_t pos
= msg
.GetCommandInt();
4751 int numRows
= msg
.GetCommandInt2();
4753 m_numRows
+= numRows
;
4755 if ( !m_rowHeights
.IsEmpty() )
4757 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4758 m_rowBottoms
.Insert( 0, pos
, numRows
);
4762 bottom
= m_rowBottoms
[pos
- 1];
4764 for ( i
= pos
; i
< m_numRows
; i
++ )
4766 bottom
+= m_rowHeights
[i
];
4767 m_rowBottoms
[i
] = bottom
;
4771 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4773 // if we have just inserted cols into an empty grid the current
4774 // cell will be undefined...
4776 SetCurrentCell( 0, 0 );
4780 m_selection
->UpdateRows( pos
, numRows
);
4781 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4783 attrProvider
->UpdateAttrRows( pos
, numRows
);
4785 if ( !GetBatchCount() )
4788 m_rowLabelWin
->Refresh();
4794 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4796 int numRows
= msg
.GetCommandInt();
4797 int oldNumRows
= m_numRows
;
4798 m_numRows
+= numRows
;
4800 if ( !m_rowHeights
.IsEmpty() )
4802 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4803 m_rowBottoms
.Add( 0, numRows
);
4806 if ( oldNumRows
> 0 )
4807 bottom
= m_rowBottoms
[oldNumRows
- 1];
4809 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4811 bottom
+= m_rowHeights
[i
];
4812 m_rowBottoms
[i
] = bottom
;
4816 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4818 // if we have just inserted cols into an empty grid the current
4819 // cell will be undefined...
4821 SetCurrentCell( 0, 0 );
4824 if ( !GetBatchCount() )
4827 m_rowLabelWin
->Refresh();
4833 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4835 size_t pos
= msg
.GetCommandInt();
4836 int numRows
= msg
.GetCommandInt2();
4837 m_numRows
-= numRows
;
4839 if ( !m_rowHeights
.IsEmpty() )
4841 m_rowHeights
.RemoveAt( pos
, numRows
);
4842 m_rowBottoms
.RemoveAt( pos
, numRows
);
4845 for ( i
= 0; i
< m_numRows
; i
++ )
4847 h
+= m_rowHeights
[i
];
4848 m_rowBottoms
[i
] = h
;
4854 m_currentCellCoords
= wxGridNoCellCoords
;
4858 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4859 m_currentCellCoords
.Set( 0, 0 );
4863 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4864 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4867 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4869 // ifdef'd out following patch from Paul Gammans
4871 // No need to touch column attributes, unless we
4872 // removed _all_ rows, in this case, we remove
4873 // all column attributes.
4874 // I hate to do this here, but the
4875 // needed data is not available inside UpdateAttrRows.
4876 if ( !GetNumberRows() )
4877 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4881 if ( !GetBatchCount() )
4884 m_rowLabelWin
->Refresh();
4890 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4892 size_t pos
= msg
.GetCommandInt();
4893 int numCols
= msg
.GetCommandInt2();
4894 m_numCols
+= numCols
;
4896 if ( !m_colAt
.IsEmpty() )
4898 //Shift the column IDs
4900 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
4902 if ( m_colAt
[i
] >= (int)pos
)
4903 m_colAt
[i
] += numCols
;
4906 m_colAt
.Insert( pos
, pos
, numCols
);
4908 //Set the new columns' positions
4909 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
4915 if ( !m_colWidths
.IsEmpty() )
4917 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4918 m_colRights
.Insert( 0, pos
, numCols
);
4922 right
= m_colRights
[GetColAt( pos
- 1 )];
4925 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
4927 i
= GetColAt( colPos
);
4929 right
+= m_colWidths
[i
];
4930 m_colRights
[i
] = right
;
4934 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4936 // if we have just inserted cols into an empty grid the current
4937 // cell will be undefined...
4939 SetCurrentCell( 0, 0 );
4943 m_selection
->UpdateCols( pos
, numCols
);
4944 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4946 attrProvider
->UpdateAttrCols( pos
, numCols
);
4947 if ( !GetBatchCount() )
4950 m_colLabelWin
->Refresh();
4956 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4958 int numCols
= msg
.GetCommandInt();
4959 int oldNumCols
= m_numCols
;
4960 m_numCols
+= numCols
;
4962 if ( !m_colAt
.IsEmpty() )
4964 m_colAt
.Add( 0, numCols
);
4966 //Set the new columns' positions
4968 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4974 if ( !m_colWidths
.IsEmpty() )
4976 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4977 m_colRights
.Add( 0, numCols
);
4980 if ( oldNumCols
> 0 )
4981 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
4984 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
4986 i
= GetColAt( colPos
);
4988 right
+= m_colWidths
[i
];
4989 m_colRights
[i
] = right
;
4993 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4995 // if we have just inserted cols into an empty grid the current
4996 // cell will be undefined...
4998 SetCurrentCell( 0, 0 );
5000 if ( !GetBatchCount() )
5003 m_colLabelWin
->Refresh();
5009 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5011 size_t pos
= msg
.GetCommandInt();
5012 int numCols
= msg
.GetCommandInt2();
5013 m_numCols
-= numCols
;
5015 if ( !m_colAt
.IsEmpty() )
5017 int colID
= GetColAt( pos
);
5019 m_colAt
.RemoveAt( pos
, numCols
);
5021 //Shift the column IDs
5023 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5025 if ( m_colAt
[colPos
] > colID
)
5026 m_colAt
[colPos
] -= numCols
;
5030 if ( !m_colWidths
.IsEmpty() )
5032 m_colWidths
.RemoveAt( pos
, numCols
);
5033 m_colRights
.RemoveAt( pos
, numCols
);
5037 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5039 i
= GetColAt( colPos
);
5041 w
+= m_colWidths
[i
];
5048 m_currentCellCoords
= wxGridNoCellCoords
;
5052 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
5053 m_currentCellCoords
.Set( 0, 0 );
5057 m_selection
->UpdateCols( pos
, -((int)numCols
) );
5058 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5061 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
5063 // ifdef'd out following patch from Paul Gammans
5065 // No need to touch row attributes, unless we
5066 // removed _all_ columns, in this case, we remove
5067 // all row attributes.
5068 // I hate to do this here, but the
5069 // needed data is not available inside UpdateAttrCols.
5070 if ( !GetNumberCols() )
5071 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
5075 if ( !GetBatchCount() )
5078 m_colLabelWin
->Refresh();
5085 if (result
&& !GetBatchCount() )
5086 m_gridWin
->Refresh();
5091 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
) const
5093 wxRegionIterator
iter( reg
);
5096 wxArrayInt rowlabels
;
5103 // TODO: remove this when we can...
5104 // There is a bug in wxMotif that gives garbage update
5105 // rectangles if you jump-scroll a long way by clicking the
5106 // scrollbar with middle button. This is a work-around
5108 #if defined(__WXMOTIF__)
5110 m_gridWin
->GetClientSize( &cw
, &ch
);
5111 if ( r
.GetTop() > ch
)
5113 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5116 // logical bounds of update region
5119 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
5120 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
5122 // find the row labels within these bounds
5125 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5127 if ( GetRowBottom(row
) < top
)
5130 if ( GetRowTop(row
) > bottom
)
5133 rowlabels
.Add( row
);
5142 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
) const
5144 wxRegionIterator
iter( reg
);
5147 wxArrayInt colLabels
;
5154 // TODO: remove this when we can...
5155 // There is a bug in wxMotif that gives garbage update
5156 // rectangles if you jump-scroll a long way by clicking the
5157 // scrollbar with middle button. This is a work-around
5159 #if defined(__WXMOTIF__)
5161 m_gridWin
->GetClientSize( &cw
, &ch
);
5162 if ( r
.GetLeft() > cw
)
5164 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5167 // logical bounds of update region
5170 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
5171 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
5173 // find the cells within these bounds
5177 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5179 col
= GetColAt( colPos
);
5181 if ( GetColRight(col
) < left
)
5184 if ( GetColLeft(col
) > right
)
5187 colLabels
.Add( col
);
5196 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
) const
5198 wxRegionIterator
iter( reg
);
5201 wxGridCellCoordsArray cellsExposed
;
5203 int left
, top
, right
, bottom
;
5208 // TODO: remove this when we can...
5209 // There is a bug in wxMotif that gives garbage update
5210 // rectangles if you jump-scroll a long way by clicking the
5211 // scrollbar with middle button. This is a work-around
5213 #if defined(__WXMOTIF__)
5215 m_gridWin
->GetClientSize( &cw
, &ch
);
5216 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5217 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5218 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5219 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5222 // logical bounds of update region
5224 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5225 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5227 // find the cells within these bounds
5230 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5232 if ( GetRowBottom(row
) <= top
)
5235 if ( GetRowTop(row
) > bottom
)
5239 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5241 col
= GetColAt( colPos
);
5243 if ( GetColRight(col
) <= left
)
5246 if ( GetColLeft(col
) > right
)
5249 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
5256 return cellsExposed
;
5260 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5263 wxPoint
pos( event
.GetPosition() );
5264 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5266 if ( event
.Dragging() )
5270 m_isDragging
= true;
5271 m_rowLabelWin
->CaptureMouse();
5274 if ( event
.LeftIsDown() )
5276 switch ( m_cursorMode
)
5278 case WXGRID_CURSOR_RESIZE_ROW
:
5280 int cw
, ch
, left
, dummy
;
5281 m_gridWin
->GetClientSize( &cw
, &ch
);
5282 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5284 wxClientDC
dc( m_gridWin
);
5287 GetRowTop(m_dragRowOrCol
) +
5288 GetRowMinimalHeight(m_dragRowOrCol
) );
5289 dc
.SetLogicalFunction(wxINVERT
);
5290 if ( m_dragLastPos
>= 0 )
5292 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5294 dc
.DrawLine( left
, y
, left
+cw
, y
);
5299 case WXGRID_CURSOR_SELECT_ROW
:
5301 if ( (row
= YToRow( y
)) >= 0 )
5305 m_selection
->SelectRow( row
,
5306 event
.ControlDown(),
5315 // default label to suppress warnings about "enumeration value
5316 // 'xxx' not handled in switch
5324 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5329 if (m_rowLabelWin
->HasCapture())
5330 m_rowLabelWin
->ReleaseMouse();
5331 m_isDragging
= false;
5334 // ------------ Entering or leaving the window
5336 if ( event
.Entering() || event
.Leaving() )
5338 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5341 // ------------ Left button pressed
5343 else if ( event
.LeftDown() )
5345 // don't send a label click event for a hit on the
5346 // edge of the row label - this is probably the user
5347 // wanting to resize the row
5349 if ( YToEdgeOfRow(y
) < 0 )
5353 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5355 if ( !event
.ShiftDown() && !event
.CmdDown() )
5359 if ( event
.ShiftDown() )
5361 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5364 GetNumberCols() - 1,
5365 event
.ControlDown(),
5372 m_selection
->SelectRow( row
,
5373 event
.ControlDown(),
5380 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5385 // starting to drag-resize a row
5386 if ( CanDragRowSize() )
5387 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5391 // ------------ Left double click
5393 else if (event
.LeftDClick() )
5395 row
= YToEdgeOfRow(y
);
5400 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5402 // no default action at the moment
5407 // adjust row height depending on label text
5408 AutoSizeRowLabelSize( row
);
5410 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5415 // ------------ Left button released
5417 else if ( event
.LeftUp() )
5419 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5421 DoEndDragResizeRow();
5423 // Note: we are ending the event *after* doing
5424 // default processing in this case
5426 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5429 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5433 // ------------ Right button down
5435 else if ( event
.RightDown() )
5439 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5441 // no default action at the moment
5445 // ------------ Right double click
5447 else if ( event
.RightDClick() )
5451 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5453 // no default action at the moment
5457 // ------------ No buttons down and mouse moving
5459 else if ( event
.Moving() )
5461 m_dragRowOrCol
= YToEdgeOfRow( y
);
5462 if ( m_dragRowOrCol
>= 0 )
5464 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5466 // don't capture the mouse yet
5467 if ( CanDragRowSize() )
5468 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5471 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5473 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5478 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5481 wxPoint
pos( event
.GetPosition() );
5482 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5484 if ( event
.Dragging() )
5488 m_isDragging
= true;
5489 m_colLabelWin
->CaptureMouse();
5491 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
)
5492 m_dragRowOrCol
= XToCol( x
);
5495 if ( event
.LeftIsDown() )
5497 switch ( m_cursorMode
)
5499 case WXGRID_CURSOR_RESIZE_COL
:
5501 int cw
, ch
, dummy
, top
;
5502 m_gridWin
->GetClientSize( &cw
, &ch
);
5503 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5505 wxClientDC
dc( m_gridWin
);
5508 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5509 GetColMinimalWidth(m_dragRowOrCol
));
5510 dc
.SetLogicalFunction(wxINVERT
);
5511 if ( m_dragLastPos
>= 0 )
5513 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5515 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5520 case WXGRID_CURSOR_SELECT_COL
:
5522 if ( (col
= XToCol( x
)) >= 0 )
5526 m_selection
->SelectCol( col
,
5527 event
.ControlDown(),
5536 case WXGRID_CURSOR_MOVE_COL
:
5539 m_moveToCol
= GetColAt( 0 );
5541 m_moveToCol
= XToCol( x
);
5545 if ( m_moveToCol
< 0 )
5546 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5548 markerX
= GetColLeft( m_moveToCol
);
5550 if ( markerX
!= m_dragLastPos
)
5552 wxClientDC
dc( m_colLabelWin
);
5555 m_colLabelWin
->GetClientSize( &cw
, &ch
);
5559 //Clean up the last indicator
5560 if ( m_dragLastPos
>= 0 )
5562 wxPen
pen( m_colLabelWin
->GetBackgroundColour(), 2 );
5564 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
5565 dc
.SetPen(wxNullPen
);
5567 if ( XToCol( m_dragLastPos
) != -1 )
5568 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
5571 //Moving to the same place? Don't draw a marker
5572 if ( (m_moveToCol
== m_dragRowOrCol
)
5573 || (GetColPos( m_moveToCol
) == GetColPos( m_dragRowOrCol
) + 1)
5574 || (m_moveToCol
< 0 && m_dragRowOrCol
== GetColAt( m_numCols
- 1 )))
5581 wxPen
pen( *wxBLUE
, 2 );
5584 dc
.DrawLine( markerX
, 0, markerX
, ch
);
5586 dc
.SetPen(wxNullPen
);
5588 m_dragLastPos
= markerX
- 1;
5593 // default label to suppress warnings about "enumeration value
5594 // 'xxx' not handled in switch
5602 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5607 if (m_colLabelWin
->HasCapture())
5608 m_colLabelWin
->ReleaseMouse();
5609 m_isDragging
= false;
5612 // ------------ Entering or leaving the window
5614 if ( event
.Entering() || event
.Leaving() )
5616 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5619 // ------------ Left button pressed
5621 else if ( event
.LeftDown() )
5623 // don't send a label click event for a hit on the
5624 // edge of the col label - this is probably the user
5625 // wanting to resize the col
5627 if ( XToEdgeOfCol(x
) < 0 )
5631 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5633 if ( m_canDragColMove
)
5635 //Show button as pressed
5636 wxClientDC
dc( m_colLabelWin
);
5637 int colLeft
= GetColLeft( col
);
5638 int colRight
= GetColRight( col
) - 1;
5639 dc
.SetPen( wxPen( m_colLabelWin
->GetBackgroundColour(), 1 ) );
5640 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
5641 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
5643 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, m_colLabelWin
);
5647 if ( !event
.ShiftDown() && !event
.CmdDown() )
5651 if ( event
.ShiftDown() )
5653 m_selection
->SelectBlock( 0,
5654 m_currentCellCoords
.GetCol(),
5655 GetNumberRows() - 1, col
,
5656 event
.ControlDown(),
5663 m_selection
->SelectCol( col
,
5664 event
.ControlDown(),
5671 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5677 // starting to drag-resize a col
5679 if ( CanDragColSize() )
5680 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5684 // ------------ Left double click
5686 if ( event
.LeftDClick() )
5688 col
= XToEdgeOfCol(x
);
5693 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5695 // no default action at the moment
5700 // adjust column width depending on label text
5701 AutoSizeColLabelSize( col
);
5703 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5708 // ------------ Left button released
5710 else if ( event
.LeftUp() )
5712 switch ( m_cursorMode
)
5714 case WXGRID_CURSOR_RESIZE_COL
:
5715 DoEndDragResizeCol();
5717 // Note: we are ending the event *after* doing
5718 // default processing in this case
5720 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5723 case WXGRID_CURSOR_MOVE_COL
:
5726 SendEvent( wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
, event
);
5729 case WXGRID_CURSOR_SELECT_COL
:
5730 case WXGRID_CURSOR_SELECT_CELL
:
5731 case WXGRID_CURSOR_RESIZE_ROW
:
5732 case WXGRID_CURSOR_SELECT_ROW
:
5733 // nothing to do (?)
5737 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5741 // ------------ Right button down
5743 else if ( event
.RightDown() )
5747 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5749 // no default action at the moment
5753 // ------------ Right double click
5755 else if ( event
.RightDClick() )
5759 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5761 // no default action at the moment
5765 // ------------ No buttons down and mouse moving
5767 else if ( event
.Moving() )
5769 m_dragRowOrCol
= XToEdgeOfCol( x
);
5770 if ( m_dragRowOrCol
>= 0 )
5772 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5774 // don't capture the cursor yet
5775 if ( CanDragColSize() )
5776 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5779 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5781 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5786 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5788 if ( event
.LeftDown() )
5790 // indicate corner label by having both row and
5793 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5798 else if ( event
.LeftDClick() )
5800 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5802 else if ( event
.RightDown() )
5804 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5806 // no default action at the moment
5809 else if ( event
.RightDClick() )
5811 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5813 // no default action at the moment
5818 void wxGrid::CancelMouseCapture()
5820 // cancel operation currently in progress, whatever it is
5823 m_isDragging
= false;
5824 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
5825 m_winCapture
->SetCursor( *wxSTANDARD_CURSOR
);
5826 m_winCapture
= NULL
;
5828 // remove traces of whatever we drew on screen
5833 void wxGrid::ChangeCursorMode(CursorMode mode
,
5838 static const wxChar
*cursorModes
[] =
5848 wxLogTrace(_T("grid"),
5849 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5850 win
== m_colLabelWin
? _T("colLabelWin")
5851 : win
? _T("rowLabelWin")
5853 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5856 if ( mode
== m_cursorMode
&&
5857 win
== m_winCapture
&&
5858 captureMouse
== (m_winCapture
!= NULL
))
5863 // by default use the grid itself
5869 if (m_winCapture
->HasCapture())
5870 m_winCapture
->ReleaseMouse();
5871 m_winCapture
= (wxWindow
*)NULL
;
5874 m_cursorMode
= mode
;
5876 switch ( m_cursorMode
)
5878 case WXGRID_CURSOR_RESIZE_ROW
:
5879 win
->SetCursor( m_rowResizeCursor
);
5882 case WXGRID_CURSOR_RESIZE_COL
:
5883 win
->SetCursor( m_colResizeCursor
);
5886 case WXGRID_CURSOR_MOVE_COL
:
5887 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
5891 win
->SetCursor( *wxSTANDARD_CURSOR
);
5895 // we need to capture mouse when resizing
5896 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5897 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5899 if ( captureMouse
&& resize
)
5901 win
->CaptureMouse();
5906 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5909 wxPoint
pos( event
.GetPosition() );
5910 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5912 wxGridCellCoords coords
;
5913 XYToCell( x
, y
, coords
);
5915 int cell_rows
, cell_cols
;
5916 bool isFirstDrag
= !m_isDragging
;
5917 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5918 if ((cell_rows
< 0) || (cell_cols
< 0))
5920 coords
.SetRow(coords
.GetRow() + cell_rows
);
5921 coords
.SetCol(coords
.GetCol() + cell_cols
);
5924 if ( event
.Dragging() )
5926 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5928 // Don't start doing anything until the mouse has been dragged at
5929 // least 3 pixels in any direction...
5932 if (m_startDragPos
== wxDefaultPosition
)
5934 m_startDragPos
= pos
;
5937 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5941 m_isDragging
= true;
5942 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5944 // Hide the edit control, so it
5945 // won't interfere with drag-shrinking.
5946 if ( IsCellEditControlShown() )
5948 HideCellEditControl();
5949 SaveEditControlValue();
5952 if ( coords
!= wxGridNoCellCoords
)
5954 if ( event
.CmdDown() )
5956 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5957 m_selectingKeyboard
= coords
;
5958 HighlightBlock( m_selectingKeyboard
, coords
);
5960 else if ( CanDragCell() )
5964 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5965 m_selectingKeyboard
= coords
;
5967 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5976 if ( !IsSelection() )
5978 HighlightBlock( coords
, coords
);
5982 HighlightBlock( m_currentCellCoords
, coords
);
5986 if (! IsVisible(coords
))
5988 MakeCellVisible(coords
);
5989 // TODO: need to introduce a delay or something here. The
5990 // scrolling is way to fast, at least on MSW - also on GTK.
5993 // Have we captured the mouse yet?
5996 m_winCapture
= m_gridWin
;
5997 m_winCapture
->CaptureMouse();
6002 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6004 int cw
, ch
, left
, dummy
;
6005 m_gridWin
->GetClientSize( &cw
, &ch
);
6006 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6008 wxClientDC
dc( m_gridWin
);
6010 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
6011 GetRowMinimalHeight(m_dragRowOrCol
) );
6012 dc
.SetLogicalFunction(wxINVERT
);
6013 if ( m_dragLastPos
>= 0 )
6015 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
6017 dc
.DrawLine( left
, y
, left
+cw
, y
);
6020 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6022 int cw
, ch
, dummy
, top
;
6023 m_gridWin
->GetClientSize( &cw
, &ch
);
6024 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6026 wxClientDC
dc( m_gridWin
);
6028 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
6029 GetColMinimalWidth(m_dragRowOrCol
) );
6030 dc
.SetLogicalFunction(wxINVERT
);
6031 if ( m_dragLastPos
>= 0 )
6033 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6035 dc
.DrawLine( x
, top
, x
, top
+ ch
);
6042 m_isDragging
= false;
6043 m_startDragPos
= wxDefaultPosition
;
6045 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
6046 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
6049 if ( event
.Entering() || event
.Leaving() )
6051 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6052 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
6057 // ------------ Left button pressed
6059 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
6061 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
6066 if ( !event
.CmdDown() )
6068 if ( event
.ShiftDown() )
6072 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
6073 m_currentCellCoords
.GetCol(),
6076 event
.ControlDown(),
6082 else if ( XToEdgeOfCol(x
) < 0 &&
6083 YToEdgeOfRow(y
) < 0 )
6085 DisableCellEditControl();
6086 MakeCellVisible( coords
);
6088 if ( event
.CmdDown() )
6092 m_selection
->ToggleCellSelection( coords
.GetRow(),
6094 event
.ControlDown(),
6099 m_selectingTopLeft
= wxGridNoCellCoords
;
6100 m_selectingBottomRight
= wxGridNoCellCoords
;
6101 m_selectingKeyboard
= coords
;
6105 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
6106 SetCurrentCell( coords
);
6109 if ( m_selection
->GetSelectionMode() !=
6110 wxGrid::wxGridSelectCells
)
6112 HighlightBlock( coords
, coords
);
6120 // ------------ Left double click
6122 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
6124 DisableCellEditControl();
6126 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
6128 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
6133 // we want double click to select a cell and start editing
6134 // (i.e. to behave in same way as sequence of two slow clicks):
6135 m_waitForSlowClick
= true;
6140 // ------------ Left button released
6142 else if ( event
.LeftUp() )
6144 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6148 if (m_winCapture
->HasCapture())
6149 m_winCapture
->ReleaseMouse();
6150 m_winCapture
= NULL
;
6153 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
6156 EnableCellEditControl();
6158 wxGridCellAttr
*attr
= GetCellAttr(coords
);
6159 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
6160 editor
->StartingClick();
6164 m_waitForSlowClick
= false;
6166 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6167 m_selectingBottomRight
!= wxGridNoCellCoords
)
6171 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6172 m_selectingTopLeft
.GetCol(),
6173 m_selectingBottomRight
.GetRow(),
6174 m_selectingBottomRight
.GetCol(),
6175 event
.ControlDown(),
6181 m_selectingTopLeft
= wxGridNoCellCoords
;
6182 m_selectingBottomRight
= wxGridNoCellCoords
;
6184 // Show the edit control, if it has been hidden for
6186 ShowCellEditControl();
6189 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6191 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6192 DoEndDragResizeRow();
6194 // Note: we are ending the event *after* doing
6195 // default processing in this case
6197 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
6199 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6201 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6202 DoEndDragResizeCol();
6204 // Note: we are ending the event *after* doing
6205 // default processing in this case
6207 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
6213 // ------------ Right button down
6215 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
6217 DisableCellEditControl();
6218 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
6223 // no default action at the moment
6227 // ------------ Right double click
6229 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
6231 DisableCellEditControl();
6232 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
6237 // no default action at the moment
6241 // ------------ Moving and no button action
6243 else if ( event
.Moving() && !event
.IsButton() )
6245 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
6247 // out of grid cell area
6248 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6252 int dragRow
= YToEdgeOfRow( y
);
6253 int dragCol
= XToEdgeOfCol( x
);
6255 // Dragging on the corner of a cell to resize in both
6256 // directions is not implemented yet...
6258 if ( dragRow
>= 0 && dragCol
>= 0 )
6260 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6266 m_dragRowOrCol
= dragRow
;
6268 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6270 if ( CanDragRowSize() && CanDragGridSize() )
6271 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false);
6274 else if ( dragCol
>= 0 )
6276 m_dragRowOrCol
= dragCol
;
6278 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6280 if ( CanDragColSize() && CanDragGridSize() )
6281 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false);
6284 else // Neither on a row or col edge
6286 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6288 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6294 void wxGrid::DoEndDragResizeRow()
6296 if ( m_dragLastPos
>= 0 )
6298 // erase the last line and resize the row
6300 int cw
, ch
, left
, dummy
;
6301 m_gridWin
->GetClientSize( &cw
, &ch
);
6302 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6304 wxClientDC
dc( m_gridWin
);
6306 dc
.SetLogicalFunction( wxINVERT
);
6307 dc
.DrawLine( left
, m_dragLastPos
, left
+ cw
, m_dragLastPos
);
6308 HideCellEditControl();
6309 SaveEditControlValue();
6311 int rowTop
= GetRowTop(m_dragRowOrCol
);
6312 SetRowSize( m_dragRowOrCol
,
6313 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
6315 if ( !GetBatchCount() )
6317 // Only needed to get the correct rect.y:
6318 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
6320 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6321 rect
.width
= m_rowLabelWidth
;
6322 rect
.height
= ch
- rect
.y
;
6323 m_rowLabelWin
->Refresh( true, &rect
);
6326 // if there is a multicell block, paint all of it
6329 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
6330 int leftCol
= XToCol(left
);
6331 int rightCol
= internalXToCol(left
+ cw
);
6334 for (i
=leftCol
; i
<rightCol
; i
++)
6336 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
6337 if (cell_rows
< subtract_rows
)
6338 subtract_rows
= cell_rows
;
6340 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
6341 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6342 rect
.height
= ch
- rect
.y
;
6345 m_gridWin
->Refresh( false, &rect
);
6348 ShowCellEditControl();
6353 void wxGrid::DoEndDragResizeCol()
6355 if ( m_dragLastPos
>= 0 )
6357 // erase the last line and resize the col
6359 int cw
, ch
, dummy
, top
;
6360 m_gridWin
->GetClientSize( &cw
, &ch
);
6361 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6363 wxClientDC
dc( m_gridWin
);
6365 dc
.SetLogicalFunction( wxINVERT
);
6366 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6367 HideCellEditControl();
6368 SaveEditControlValue();
6370 int colLeft
= GetColLeft(m_dragRowOrCol
);
6371 SetColSize( m_dragRowOrCol
,
6372 wxMax( m_dragLastPos
- colLeft
,
6373 GetColMinimalWidth(m_dragRowOrCol
) ) );
6375 if ( !GetBatchCount() )
6377 // Only needed to get the correct rect.x:
6378 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
6380 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6381 rect
.width
= cw
- rect
.x
;
6382 rect
.height
= m_colLabelHeight
;
6383 m_colLabelWin
->Refresh( true, &rect
);
6386 // if there is a multicell block, paint all of it
6389 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
6390 int topRow
= YToRow(top
);
6391 int bottomRow
= internalYToRow(top
+ cw
);
6394 for (i
=topRow
; i
<bottomRow
; i
++)
6396 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
6397 if (cell_cols
< subtract_cols
)
6398 subtract_cols
= cell_cols
;
6401 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
6402 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6403 rect
.width
= cw
- rect
.x
;
6407 m_gridWin
->Refresh( false, &rect
);
6410 ShowCellEditControl();
6414 void wxGrid::DoEndDragMoveCol()
6416 //The user clicked on the column but didn't actually drag
6417 if ( m_dragLastPos
< 0 )
6419 m_colLabelWin
->Refresh(); //Do this to "unpress" the column
6424 if ( m_moveToCol
== -1 )
6425 newPos
= m_numCols
- 1;
6428 newPos
= GetColPos( m_moveToCol
);
6429 if ( newPos
> GetColPos( m_dragRowOrCol
) )
6433 SetColPos( m_dragRowOrCol
, newPos
);
6436 void wxGrid::SetColPos( int colID
, int newPos
)
6438 if ( m_colAt
.IsEmpty() )
6440 m_colAt
.Alloc( m_numCols
);
6443 for ( i
= 0; i
< m_numCols
; i
++ )
6449 int oldPos
= GetColPos( colID
);
6451 //Reshuffle the m_colAt array
6452 if ( newPos
> oldPos
)
6455 for ( i
= oldPos
; i
< newPos
; i
++ )
6457 m_colAt
[i
] = m_colAt
[i
+1];
6463 for ( i
= oldPos
; i
> newPos
; i
-- )
6465 m_colAt
[i
] = m_colAt
[i
-1];
6469 m_colAt
[newPos
] = colID
;
6471 //Recalculate the column rights
6472 if ( !m_colWidths
.IsEmpty() )
6476 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6478 int colID
= GetColAt( colPos
);
6480 colRight
+= m_colWidths
[colID
];
6481 m_colRights
[colID
] = colRight
;
6485 m_colLabelWin
->Refresh();
6486 m_gridWin
->Refresh();
6491 void wxGrid::EnableDragColMove( bool enable
)
6493 if ( m_canDragColMove
== enable
)
6496 m_canDragColMove
= enable
;
6498 if ( !m_canDragColMove
)
6502 //Recalculate the column rights
6503 if ( !m_colWidths
.IsEmpty() )
6507 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6509 colRight
+= m_colWidths
[colPos
];
6510 m_colRights
[colPos
] = colRight
;
6514 m_colLabelWin
->Refresh();
6515 m_gridWin
->Refresh();
6521 // ------ interaction with data model
6523 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6525 switch ( msg
.GetId() )
6527 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6528 return GetModelValues();
6530 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6531 return SetModelValues();
6533 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6534 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6535 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6536 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6537 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6538 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6539 return Redimension( msg
);
6546 // The behaviour of this function depends on the grid table class
6547 // Clear() function. For the default wxGridStringTable class the
6548 // behavious is to replace all cell contents with wxEmptyString but
6549 // not to change the number of rows or cols.
6551 void wxGrid::ClearGrid()
6555 if (IsCellEditControlEnabled())
6556 DisableCellEditControl();
6559 if (!GetBatchCount())
6560 m_gridWin
->Refresh();
6564 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6566 // TODO: something with updateLabels flag
6570 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6576 if (IsCellEditControlEnabled())
6577 DisableCellEditControl();
6579 bool done
= m_table
->InsertRows( pos
, numRows
);
6582 // the table will have sent the results of the insert row
6583 // operation to this view object as a grid table message
6589 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6591 // TODO: something with updateLabels flag
6595 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6601 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6604 // the table will have sent the results of the append row
6605 // operation to this view object as a grid table message
6611 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6613 // TODO: something with updateLabels flag
6617 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6623 if (IsCellEditControlEnabled())
6624 DisableCellEditControl();
6626 bool done
= m_table
->DeleteRows( pos
, numRows
);
6628 // the table will have sent the results of the delete row
6629 // operation to this view object as a grid table message
6635 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6637 // TODO: something with updateLabels flag
6641 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6647 if (IsCellEditControlEnabled())
6648 DisableCellEditControl();
6650 bool done
= m_table
->InsertCols( pos
, numCols
);
6652 // the table will have sent the results of the insert col
6653 // operation to this view object as a grid table message
6659 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6661 // TODO: something with updateLabels flag
6665 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6671 bool done
= m_table
->AppendCols( numCols
);
6673 // the table will have sent the results of the append col
6674 // operation to this view object as a grid table message
6680 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6682 // TODO: something with updateLabels flag
6686 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6692 if (IsCellEditControlEnabled())
6693 DisableCellEditControl();
6695 bool done
= m_table
->DeleteCols( pos
, numCols
);
6697 // the table will have sent the results of the delete col
6698 // operation to this view object as a grid table message
6705 // ----- event handlers
6708 // Generate a grid event based on a mouse event and
6709 // return the result of ProcessEvent()
6711 int wxGrid::SendEvent( const wxEventType type
,
6713 wxMouseEvent
& mouseEv
)
6715 bool claimed
, vetoed
;
6717 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6719 int rowOrCol
= (row
== -1 ? col
: row
);
6721 wxGridSizeEvent
gridEvt( GetId(),
6725 mouseEv
.GetX() + GetRowLabelSize(),
6726 mouseEv
.GetY() + GetColLabelSize(),
6727 mouseEv
.ControlDown(),
6728 mouseEv
.ShiftDown(),
6730 mouseEv
.MetaDown() );
6732 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6733 vetoed
= !gridEvt
.IsAllowed();
6735 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6737 // Right now, it should _never_ end up here!
6738 wxGridRangeSelectEvent
gridEvt( GetId(),
6742 m_selectingBottomRight
,
6744 mouseEv
.ControlDown(),
6745 mouseEv
.ShiftDown(),
6747 mouseEv
.MetaDown() );
6749 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6750 vetoed
= !gridEvt
.IsAllowed();
6752 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
6753 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
6754 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
6755 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
6757 wxPoint pos
= mouseEv
.GetPosition();
6759 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
6760 pos
.y
+= GetColLabelSize();
6761 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
6762 pos
.x
+= GetRowLabelSize();
6764 wxGridEvent
gridEvt( GetId(),
6771 mouseEv
.ControlDown(),
6772 mouseEv
.ShiftDown(),
6774 mouseEv
.MetaDown() );
6775 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6776 vetoed
= !gridEvt
.IsAllowed();
6780 wxGridEvent
gridEvt( GetId(),
6784 mouseEv
.GetX() + GetRowLabelSize(),
6785 mouseEv
.GetY() + GetColLabelSize(),
6787 mouseEv
.ControlDown(),
6788 mouseEv
.ShiftDown(),
6790 mouseEv
.MetaDown() );
6791 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6792 vetoed
= !gridEvt
.IsAllowed();
6795 // A Veto'd event may not be `claimed' so test this first
6799 return claimed
? 1 : 0;
6802 // Generate a grid event of specified type and return the result
6803 // of ProcessEvent().
6805 int wxGrid::SendEvent( const wxEventType type
,
6808 bool claimed
, vetoed
;
6810 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6812 int rowOrCol
= (row
== -1 ? col
: row
);
6814 wxGridSizeEvent
gridEvt( GetId(), type
, this, rowOrCol
);
6816 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6817 vetoed
= !gridEvt
.IsAllowed();
6821 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
6823 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6824 vetoed
= !gridEvt
.IsAllowed();
6827 // A Veto'd event may not be `claimed' so test this first
6831 return claimed
? 1 : 0;
6834 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6836 // needed to prevent zillions of paint events on MSW
6840 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6842 // Don't do anything if between Begin/EndBatch...
6843 // EndBatch() will do all this on the last nested one anyway.
6844 if ( m_created
&& !GetBatchCount() )
6846 // Refresh to get correct scrolled position:
6847 wxScrolledWindow::Refresh(eraseb
, rect
);
6851 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6852 int width_label
, width_cell
, height_label
, height_cell
;
6855 // Copy rectangle can get scroll offsets..
6856 rect_x
= rect
->GetX();
6857 rect_y
= rect
->GetY();
6858 rectWidth
= rect
->GetWidth();
6859 rectHeight
= rect
->GetHeight();
6861 width_label
= m_rowLabelWidth
- rect_x
;
6862 if (width_label
> rectWidth
)
6863 width_label
= rectWidth
;
6865 height_label
= m_colLabelHeight
- rect_y
;
6866 if (height_label
> rectHeight
)
6867 height_label
= rectHeight
;
6869 if (rect_x
> m_rowLabelWidth
)
6871 x
= rect_x
- m_rowLabelWidth
;
6872 width_cell
= rectWidth
;
6877 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6880 if (rect_y
> m_colLabelHeight
)
6882 y
= rect_y
- m_colLabelHeight
;
6883 height_cell
= rectHeight
;
6888 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6891 // Paint corner label part intersecting rect.
6892 if ( width_label
> 0 && height_label
> 0 )
6894 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6895 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6898 // Paint col labels part intersecting rect.
6899 if ( width_cell
> 0 && height_label
> 0 )
6901 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6902 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6905 // Paint row labels part intersecting rect.
6906 if ( width_label
> 0 && height_cell
> 0 )
6908 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6909 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6912 // Paint cell area part intersecting rect.
6913 if ( width_cell
> 0 && height_cell
> 0 )
6915 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6916 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6921 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6922 m_colLabelWin
->Refresh(eraseb
, NULL
);
6923 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6924 m_gridWin
->Refresh(eraseb
, NULL
);
6929 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
))
6931 if (m_targetWindow
!= this) // check whether initialisation has been done
6933 // update our children window positions and scrollbars
6938 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6940 if ( m_inOnKeyDown
)
6942 // shouldn't be here - we are going round in circles...
6944 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6947 m_inOnKeyDown
= true;
6949 // propagate the event up and see if it gets processed
6950 wxWindow
*parent
= GetParent();
6951 wxKeyEvent
keyEvt( event
);
6952 keyEvt
.SetEventObject( parent
);
6954 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6956 if (GetLayoutDirection() == wxLayout_RightToLeft
)
6958 if (event
.GetKeyCode() == WXK_RIGHT
)
6959 event
.m_keyCode
= WXK_LEFT
;
6960 else if (event
.GetKeyCode() == WXK_LEFT
)
6961 event
.m_keyCode
= WXK_RIGHT
;
6964 // try local handlers
6965 switch ( event
.GetKeyCode() )
6968 if ( event
.ControlDown() )
6969 MoveCursorUpBlock( event
.ShiftDown() );
6971 MoveCursorUp( event
.ShiftDown() );
6975 if ( event
.ControlDown() )
6976 MoveCursorDownBlock( event
.ShiftDown() );
6978 MoveCursorDown( event
.ShiftDown() );
6982 if ( event
.ControlDown() )
6983 MoveCursorLeftBlock( event
.ShiftDown() );
6985 MoveCursorLeft( event
.ShiftDown() );
6989 if ( event
.ControlDown() )
6990 MoveCursorRightBlock( event
.ShiftDown() );
6992 MoveCursorRight( event
.ShiftDown() );
6996 case WXK_NUMPAD_ENTER
:
6997 if ( event
.ControlDown() )
6999 event
.Skip(); // to let the edit control have the return
7003 if ( GetGridCursorRow() < GetNumberRows()-1 )
7005 MoveCursorDown( event
.ShiftDown() );
7009 // at the bottom of a column
7010 DisableCellEditControl();
7020 if (event
.ShiftDown())
7022 if ( GetGridCursorCol() > 0 )
7024 MoveCursorLeft( false );
7029 DisableCellEditControl();
7034 if ( GetGridCursorCol() < GetNumberCols() - 1 )
7036 MoveCursorRight( false );
7041 DisableCellEditControl();
7047 if ( event
.ControlDown() )
7049 MakeCellVisible( 0, 0 );
7050 SetCurrentCell( 0, 0 );
7059 if ( event
.ControlDown() )
7061 MakeCellVisible( m_numRows
- 1, m_numCols
- 1 );
7062 SetCurrentCell( m_numRows
- 1, m_numCols
- 1 );
7079 if ( event
.ControlDown() )
7083 m_selection
->ToggleCellSelection(
7084 m_currentCellCoords
.GetRow(),
7085 m_currentCellCoords
.GetCol(),
7086 event
.ControlDown(),
7094 if ( !IsEditable() )
7095 MoveCursorRight( false );
7106 m_inOnKeyDown
= false;
7109 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
7111 // try local handlers
7113 if ( event
.GetKeyCode() == WXK_SHIFT
)
7115 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
7116 m_selectingBottomRight
!= wxGridNoCellCoords
)
7120 m_selection
->SelectBlock(
7121 m_selectingTopLeft
.GetRow(),
7122 m_selectingTopLeft
.GetCol(),
7123 m_selectingBottomRight
.GetRow(),
7124 m_selectingBottomRight
.GetCol(),
7125 event
.ControlDown(),
7132 m_selectingTopLeft
= wxGridNoCellCoords
;
7133 m_selectingBottomRight
= wxGridNoCellCoords
;
7134 m_selectingKeyboard
= wxGridNoCellCoords
;
7138 void wxGrid::OnChar( wxKeyEvent
& event
)
7140 // is it possible to edit the current cell at all?
7141 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7143 // yes, now check whether the cells editor accepts the key
7144 int row
= m_currentCellCoords
.GetRow();
7145 int col
= m_currentCellCoords
.GetCol();
7146 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7147 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7149 // <F2> is special and will always start editing, for
7150 // other keys - ask the editor itself
7151 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
7152 || editor
->IsAcceptedKey(event
) )
7154 // ensure cell is visble
7155 MakeCellVisible(row
, col
);
7156 EnableCellEditControl();
7158 // a problem can arise if the cell is not completely
7159 // visible (even after calling MakeCellVisible the
7160 // control is not created and calling StartingKey will
7162 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
7163 editor
->StartingKey(event
);
7179 void wxGrid::OnEraseBackground(wxEraseEvent
&)
7183 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
7185 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
7187 // the event has been intercepted - do nothing
7191 #if !defined(__WXMAC__)
7192 wxClientDC
dc( m_gridWin
);
7196 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
7198 DisableCellEditControl();
7200 if ( IsVisible( m_currentCellCoords
, false ) )
7203 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
7204 if ( !m_gridLinesEnabled
)
7212 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
7214 // Otherwise refresh redraws the highlight!
7215 m_currentCellCoords
= coords
;
7217 #if defined(__WXMAC__)
7218 m_gridWin
->Refresh(true /*, & r */);
7220 DrawGridCellArea( dc
, cells
);
7221 DrawAllGridLines( dc
, r
);
7226 m_currentCellCoords
= coords
;
7228 wxGridCellAttr
*attr
= GetCellAttr( coords
);
7229 #if !defined(__WXMAC__)
7230 DrawCellHighlight( dc
, attr
);
7235 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7238 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7242 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
7245 rightCol
= GetNumberCols() - 1;
7247 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
7250 bottomRow
= GetNumberRows() - 1;
7254 if ( topRow
> bottomRow
)
7261 if ( leftCol
> rightCol
)
7268 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7269 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7271 // First the case that we selected a completely new area
7272 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
7273 m_selectingBottomRight
== wxGridNoCellCoords
)
7276 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
7277 wxGridCellCoords ( bottomRow
, rightCol
) );
7278 m_gridWin
->Refresh( false, &rect
);
7281 // Now handle changing an existing selection area.
7282 else if ( m_selectingTopLeft
!= updateTopLeft
||
7283 m_selectingBottomRight
!= updateBottomRight
)
7285 // Compute two optimal update rectangles:
7286 // Either one rectangle is a real subset of the
7287 // other, or they are (almost) disjoint!
7289 bool need_refresh
[4];
7293 need_refresh
[3] = false;
7296 // Store intermediate values
7297 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
7298 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
7299 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
7300 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
7302 // Determine the outer/inner coordinates.
7303 if (oldLeft
> leftCol
)
7309 if (oldTop
> topRow
)
7315 if (oldRight
< rightCol
)
7318 oldRight
= rightCol
;
7321 if (oldBottom
< bottomRow
)
7324 oldBottom
= bottomRow
;
7328 // Now, either the stuff marked old is the outer
7329 // rectangle or we don't have a situation where one
7330 // is contained in the other.
7332 if ( oldLeft
< leftCol
)
7334 // Refresh the newly selected or deselected
7335 // area to the left of the old or new selection.
7336 need_refresh
[0] = true;
7337 rect
[0] = BlockToDeviceRect(
7338 wxGridCellCoords( oldTop
, oldLeft
),
7339 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
7342 if ( oldTop
< topRow
)
7344 // Refresh the newly selected or deselected
7345 // area above the old or new selection.
7346 need_refresh
[1] = true;
7347 rect
[1] = BlockToDeviceRect(
7348 wxGridCellCoords( oldTop
, leftCol
),
7349 wxGridCellCoords( topRow
- 1, rightCol
) );
7352 if ( oldRight
> rightCol
)
7354 // Refresh the newly selected or deselected
7355 // area to the right of the old or new selection.
7356 need_refresh
[2] = true;
7357 rect
[2] = BlockToDeviceRect(
7358 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
7359 wxGridCellCoords( oldBottom
, oldRight
) );
7362 if ( oldBottom
> bottomRow
)
7364 // Refresh the newly selected or deselected
7365 // area below the old or new selection.
7366 need_refresh
[3] = true;
7367 rect
[3] = BlockToDeviceRect(
7368 wxGridCellCoords( bottomRow
+ 1, leftCol
),
7369 wxGridCellCoords( oldBottom
, rightCol
) );
7372 // various Refresh() calls
7373 for (i
= 0; i
< 4; i
++ )
7374 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7375 m_gridWin
->Refresh( false, &(rect
[i
]) );
7379 m_selectingTopLeft
= updateTopLeft
;
7380 m_selectingBottomRight
= updateBottomRight
;
7384 // ------ functions to get/send data (see also public functions)
7387 bool wxGrid::GetModelValues()
7389 // Hide the editor, so it won't hide a changed value.
7390 HideCellEditControl();
7394 // all we need to do is repaint the grid
7396 m_gridWin
->Refresh();
7403 bool wxGrid::SetModelValues()
7407 // Disable the editor, so it won't hide a changed value.
7408 // Do we also want to save the current value of the editor first?
7410 DisableCellEditControl();
7414 for ( row
= 0; row
< m_numRows
; row
++ )
7416 for ( col
= 0; col
< m_numCols
; col
++ )
7418 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
7428 // Note - this function only draws cells that are in the list of
7429 // exposed cells (usually set from the update region by
7430 // CalcExposedCells)
7432 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7434 if ( !m_numRows
|| !m_numCols
)
7437 int i
, numCells
= cells
.GetCount();
7438 int row
, col
, cell_rows
, cell_cols
;
7439 wxGridCellCoordsArray redrawCells
;
7441 for ( i
= numCells
- 1; i
>= 0; i
-- )
7443 row
= cells
[i
].GetRow();
7444 col
= cells
[i
].GetCol();
7445 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7447 // If this cell is part of a multicell block, find owner for repaint
7448 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7450 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
7451 bool marked
= false;
7452 for ( int j
= 0; j
< numCells
; j
++ )
7454 if ( cell
== cells
[j
] )
7463 int count
= redrawCells
.GetCount();
7464 for (int j
= 0; j
< count
; j
++)
7466 if ( cell
== redrawCells
[j
] )
7474 redrawCells
.Add( cell
);
7477 // don't bother drawing this cell
7481 // If this cell is empty, find cell to left that might want to overflow
7482 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7484 for ( int l
= 0; l
< cell_rows
; l
++ )
7486 // find a cell in this row to leave already marked for repaint
7488 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7489 if ((redrawCells
[k
].GetCol() < left
) &&
7490 (redrawCells
[k
].GetRow() == row
))
7492 left
= redrawCells
[k
].GetCol();
7496 left
= 0; // oh well
7498 for (int j
= col
- 1; j
>= left
; j
--)
7500 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7502 if (GetCellOverflow(row
+ l
, j
))
7504 wxGridCellCoords
cell(row
+ l
, j
);
7505 bool marked
= false;
7507 for (int k
= 0; k
< numCells
; k
++)
7509 if ( cell
== cells
[k
] )
7518 int count
= redrawCells
.GetCount();
7519 for (int k
= 0; k
< count
; k
++)
7521 if ( cell
== redrawCells
[k
] )
7528 redrawCells
.Add( cell
);
7537 DrawCell( dc
, cells
[i
] );
7540 numCells
= redrawCells
.GetCount();
7542 for ( i
= numCells
- 1; i
>= 0; i
-- )
7544 DrawCell( dc
, redrawCells
[i
] );
7548 void wxGrid::DrawGridSpace( wxDC
& dc
)
7551 m_gridWin
->GetClientSize( &cw
, &ch
);
7554 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7556 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
7557 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7559 if ( right
> rightCol
|| bottom
> bottomRow
)
7562 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7564 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxBRUSHSTYLE_SOLID
) );
7565 dc
.SetPen( *wxTRANSPARENT_PEN
);
7567 if ( right
> rightCol
)
7569 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7572 if ( bottom
> bottomRow
)
7574 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7579 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7581 int row
= coords
.GetRow();
7582 int col
= coords
.GetCol();
7584 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7587 // we draw the cell border ourselves
7588 #if !WXGRID_DRAW_LINES
7589 if ( m_gridLinesEnabled
)
7590 DrawCellBorder( dc
, coords
);
7593 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7595 bool isCurrent
= coords
== m_currentCellCoords
;
7597 wxRect rect
= CellToRect( row
, col
);
7599 // if the editor is shown, we should use it and not the renderer
7600 // Note: However, only if it is really _shown_, i.e. not hidden!
7601 if ( isCurrent
&& IsCellEditControlShown() )
7603 // NB: this "#if..." is temporary and fixes a problem where the
7604 // edit control is erased by this code after being rendered.
7605 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7606 // implicitly, causing this out-of order render.
7607 #if !defined(__WXMAC__)
7608 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7609 editor
->PaintBackground(rect
, attr
);
7615 // but all the rest is drawn by the cell renderer and hence may be customized
7616 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7617 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7624 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7626 // don't show highlight when the grid doesn't have focus
7630 int row
= m_currentCellCoords
.GetRow();
7631 int col
= m_currentCellCoords
.GetCol();
7633 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7636 wxRect rect
= CellToRect(row
, col
);
7638 // hmmm... what could we do here to show that the cell is disabled?
7639 // for now, I just draw a thinner border than for the other ones, but
7640 // it doesn't look really good
7642 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7646 // The center of the drawn line is where the position/width/height of
7647 // the rectangle is actually at (on wxMSW at least), so the
7648 // size of the rectangle is reduced to compensate for the thickness of
7649 // the line. If this is too strange on non-wxMSW platforms then
7650 // please #ifdef this appropriately.
7651 rect
.x
+= penWidth
/ 2;
7652 rect
.y
+= penWidth
/ 2;
7653 rect
.width
-= penWidth
- 1;
7654 rect
.height
-= penWidth
- 1;
7656 // Now draw the rectangle
7657 // use the cellHighlightColour if the cell is inside a selection, this
7658 // will ensure the cell is always visible.
7659 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
: m_cellHighlightColour
, penWidth
, wxPENSTYLE_SOLID
));
7660 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7661 dc
.DrawRectangle(rect
);
7665 // VZ: my experiments with 3D borders...
7667 // how to properly set colours for arbitrary bg?
7668 wxCoord x1
= rect
.x
,
7670 x2
= rect
.x
+ rect
.width
- 1,
7671 y2
= rect
.y
+ rect
.height
- 1;
7673 dc
.SetPen(*wxWHITE_PEN
);
7674 dc
.DrawLine(x1
, y1
, x2
, y1
);
7675 dc
.DrawLine(x1
, y1
, x1
, y2
);
7677 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7678 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7680 dc
.SetPen(*wxBLACK_PEN
);
7681 dc
.DrawLine(x1
, y2
, x2
, y2
);
7682 dc
.DrawLine(x2
, y1
, x2
, y2
+ 1);
7686 wxPen
wxGrid::GetDefaultGridLinePen()
7688 return wxPen(GetGridLineColour(), 1, wxPENSTYLE_SOLID
);
7691 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
7693 return GetDefaultGridLinePen();
7696 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
7698 return GetDefaultGridLinePen();
7701 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7703 int row
= coords
.GetRow();
7704 int col
= coords
.GetCol();
7705 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7709 wxRect rect
= CellToRect( row
, col
);
7711 // right hand border
7712 dc
.SetPen( GetColGridLinePen(col
) );
7713 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7714 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7717 dc
.SetPen( GetRowGridLinePen(row
) );
7718 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7719 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7722 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7724 // This if block was previously in wxGrid::OnPaint but that doesn't
7725 // seem to get called under wxGTK - MB
7727 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7728 m_numRows
&& m_numCols
)
7730 m_currentCellCoords
.Set(0, 0);
7733 if ( IsCellEditControlShown() )
7735 // don't show highlight when the edit control is shown
7739 // if the active cell was repainted, repaint its highlight too because it
7740 // might have been damaged by the grid lines
7741 size_t count
= cells
.GetCount();
7742 for ( size_t n
= 0; n
< count
; n
++ )
7744 if ( cells
[n
] == m_currentCellCoords
)
7746 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7747 DrawCellHighlight(dc
, attr
);
7755 // TODO: remove this ???
7756 // This is used to redraw all grid lines e.g. when the grid line colour
7759 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7761 #if !WXGRID_DRAW_LINES
7765 if ( !m_gridLinesEnabled
|| !m_numRows
|| !m_numCols
)
7768 int top
, bottom
, left
, right
;
7770 #if 0 //#ifndef __WXGTK__
7774 m_gridWin
->GetClientSize(&cw
, &ch
);
7776 // virtual coords of visible area
7778 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7779 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7784 reg
.GetBox(x
, y
, w
, h
);
7785 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7786 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7790 m_gridWin
->GetClientSize(&cw
, &ch
);
7791 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7792 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7795 // avoid drawing grid lines past the last row and col
7797 right
= wxMin( right
, GetColRight(GetColAt( m_numCols
- 1 )) );
7798 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7800 // no gridlines inside multicells, clip them out
7801 int leftCol
= GetColPos( internalXToCol(left
) );
7802 int topRow
= internalYToRow(top
);
7803 int rightCol
= GetColPos( internalXToCol(right
) );
7804 int bottomRow
= internalYToRow(bottom
);
7806 wxRegion
clippedcells(0, 0, cw
, ch
);
7808 int i
, j
, cell_rows
, cell_cols
;
7811 for (j
=topRow
; j
<=bottomRow
; j
++)
7814 for (colPos
=leftCol
; colPos
<=rightCol
; colPos
++)
7816 i
= GetColAt( colPos
);
7818 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7819 if ((cell_rows
> 1) || (cell_cols
> 1))
7821 rect
= CellToRect(j
,i
);
7822 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7823 clippedcells
.Subtract(rect
);
7825 else if ((cell_rows
< 0) || (cell_cols
< 0))
7827 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7828 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7829 clippedcells
.Subtract(rect
);
7834 dc
.SetDeviceClippingRegion( clippedcells
);
7837 // horizontal grid lines
7839 // already declared above - int i;
7840 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7842 int bot
= GetRowBottom(i
) - 1;
7851 dc
.SetPen( GetRowGridLinePen(i
) );
7852 dc
.DrawLine( left
, bot
, right
, bot
);
7856 // vertical grid lines
7859 for ( colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
7861 i
= GetColAt( colPos
);
7863 int colRight
= GetColRight(i
);
7865 if (GetLayoutDirection() != wxLayout_RightToLeft
)
7869 if ( colRight
> right
)
7874 if ( colRight
>= left
)
7876 dc
.SetPen( GetColGridLinePen(i
) );
7877 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7881 dc
.DestroyClippingRegion();
7884 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
7890 size_t numLabels
= rows
.GetCount();
7892 for ( i
= 0; i
< numLabels
; i
++ )
7894 DrawRowLabel( dc
, rows
[i
] );
7898 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7900 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7905 int rowTop
= GetRowTop(row
),
7906 rowBottom
= GetRowBottom(row
) - 1;
7908 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxPENSTYLE_SOLID
) );
7909 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
7910 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7911 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7913 dc
.SetPen( *wxWHITE_PEN
);
7914 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7915 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
7917 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
7918 dc
.SetTextForeground( GetLabelTextColour() );
7919 dc
.SetFont( GetLabelFont() );
7922 GetRowLabelAlignment( &hAlign
, &vAlign
);
7925 rect
.SetY( GetRowTop(row
) + 2 );
7926 rect
.SetWidth( m_rowLabelWidth
- 4 );
7927 rect
.SetHeight( GetRowHeight(row
) - 4 );
7928 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7931 void wxGrid::SetUseNativeColLabels( bool native
)
7933 m_nativeColumnLabels
= native
;
7936 int height
= wxRendererNative::Get().GetHeaderButtonHeight( this );
7937 SetColLabelSize( height
);
7940 m_colLabelWin
->Refresh();
7943 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7949 size_t numLabels
= cols
.GetCount();
7951 for ( i
= 0; i
< numLabels
; i
++ )
7953 DrawColLabel( dc
, cols
[i
] );
7957 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7959 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7962 int colLeft
= GetColLeft(col
);
7966 if (m_nativeColumnLabels
)
7968 rect
.SetX( colLeft
);
7970 rect
.SetWidth( GetColWidth(col
));
7971 rect
.SetHeight( m_colLabelHeight
);
7973 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7974 wxRendererNative::Get().DrawHeaderButton( win_dc
->GetWindow(), dc
, rect
, 0 );
7978 int colRight
= GetColRight(col
) - 1;
7980 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxPENSTYLE_SOLID
) );
7981 dc
.DrawLine( colRight
, 0, colRight
, m_colLabelHeight
- 1 );
7982 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7983 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
7984 colRight
+ 1, m_colLabelHeight
- 1 );
7986 dc
.SetPen( *wxWHITE_PEN
);
7987 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
7988 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7991 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
7992 dc
.SetTextForeground( GetLabelTextColour() );
7993 dc
.SetFont( GetLabelFont() );
7995 int hAlign
, vAlign
, orient
;
7996 GetColLabelAlignment( &hAlign
, &vAlign
);
7997 orient
= GetColLabelTextOrientation();
7999 rect
.SetX( colLeft
+ 2 );
8001 rect
.SetWidth( GetColWidth(col
) - 4 );
8002 rect
.SetHeight( m_colLabelHeight
- 4 );
8003 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
8006 void wxGrid::DrawTextRectangle( wxDC
& dc
,
8007 const wxString
& value
,
8011 int textOrientation
)
8013 wxArrayString lines
;
8015 StringToLines( value
, lines
);
8017 // Forward to new API.
8018 DrawTextRectangle( dc
,
8026 // VZ: this should be replaced with wxDC::DrawLabel() to which we just have to
8027 // add textOrientation support
8028 void wxGrid::DrawTextRectangle(wxDC
& dc
,
8029 const wxArrayString
& lines
,
8033 int textOrientation
)
8035 if ( lines
.empty() )
8038 wxDCClipper
clip(dc
, rect
);
8043 if ( textOrientation
== wxHORIZONTAL
)
8044 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
8046 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
8050 switch ( vertAlign
)
8052 case wxALIGN_BOTTOM
:
8053 if ( textOrientation
== wxHORIZONTAL
)
8054 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
8056 x
= rect
.x
+ rect
.width
- textWidth
;
8059 case wxALIGN_CENTRE
:
8060 if ( textOrientation
== wxHORIZONTAL
)
8061 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
8063 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
8068 if ( textOrientation
== wxHORIZONTAL
)
8075 // Align each line of a multi-line label
8076 size_t nLines
= lines
.GetCount();
8077 for ( size_t l
= 0; l
< nLines
; l
++ )
8079 const wxString
& line
= lines
[l
];
8083 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
8087 wxCoord lineWidth
= 0,
8089 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
8091 switch ( horizAlign
)
8094 if ( textOrientation
== wxHORIZONTAL
)
8095 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
8097 y
= rect
.y
+ lineWidth
+ 1;
8100 case wxALIGN_CENTRE
:
8101 if ( textOrientation
== wxHORIZONTAL
)
8102 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
8104 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
8109 if ( textOrientation
== wxHORIZONTAL
)
8112 y
= rect
.y
+ rect
.height
- 1;
8116 if ( textOrientation
== wxHORIZONTAL
)
8118 dc
.DrawText( line
, x
, y
);
8123 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
8129 // Split multi-line text up into an array of strings.
8130 // Any existing contents of the string array are preserved.
8132 // TODO: refactor wxTextFile::Read() and reuse the same code from here
8133 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
8137 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
8138 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
8140 while ( startPos
< (int)tVal
.length() )
8142 pos
= tVal
.Mid(startPos
).Find( eol
);
8147 else if ( pos
== 0 )
8149 lines
.Add( wxEmptyString
);
8153 lines
.Add( tVal
.Mid(startPos
, pos
) );
8156 startPos
+= pos
+ 1;
8159 if ( startPos
< (int)tVal
.length() )
8161 lines
.Add( tVal
.Mid( startPos
) );
8165 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
8166 const wxArrayString
& lines
,
8167 long *width
, long *height
) const
8171 wxCoord lineW
= 0, lineH
= 0;
8174 for ( i
= 0; i
< lines
.GetCount(); i
++ )
8176 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
8177 w
= wxMax( w
, lineW
);
8186 // ------ Batch processing.
8188 void wxGrid::EndBatch()
8190 if ( m_batchCount
> 0 )
8193 if ( !m_batchCount
)
8196 m_rowLabelWin
->Refresh();
8197 m_colLabelWin
->Refresh();
8198 m_cornerLabelWin
->Refresh();
8199 m_gridWin
->Refresh();
8204 // Use this, rather than wxWindow::Refresh(), to force an immediate
8205 // repainting of the grid. Has no effect if you are already inside a
8206 // BeginBatch / EndBatch block.
8208 void wxGrid::ForceRefresh()
8214 bool wxGrid::Enable(bool enable
)
8216 if ( !wxScrolledWindow::Enable(enable
) )
8219 // redraw in the new state
8220 m_gridWin
->Refresh();
8226 // ------ Edit control functions
8229 void wxGrid::EnableEditing( bool edit
)
8231 // TODO: improve this ?
8233 if ( edit
!= m_editable
)
8236 EnableCellEditControl(edit
);
8241 void wxGrid::EnableCellEditControl( bool enable
)
8246 if ( enable
!= m_cellEditCtrlEnabled
)
8250 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
8253 // this should be checked by the caller!
8254 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8256 // do it before ShowCellEditControl()
8257 m_cellEditCtrlEnabled
= enable
;
8259 ShowCellEditControl();
8263 //FIXME:add veto support
8264 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
8266 HideCellEditControl();
8267 SaveEditControlValue();
8269 // do it after HideCellEditControl()
8270 m_cellEditCtrlEnabled
= enable
;
8275 bool wxGrid::IsCurrentCellReadOnly() const
8278 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
8279 bool readonly
= attr
->IsReadOnly();
8285 bool wxGrid::CanEnableCellControl() const
8287 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
8288 !IsCurrentCellReadOnly();
8291 bool wxGrid::IsCellEditControlEnabled() const
8293 // the cell edit control might be disable for all cells or just for the
8294 // current one if it's read only
8295 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
8298 bool wxGrid::IsCellEditControlShown() const
8300 bool isShown
= false;
8302 if ( m_cellEditCtrlEnabled
)
8304 int row
= m_currentCellCoords
.GetRow();
8305 int col
= m_currentCellCoords
.GetCol();
8306 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8307 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
8312 if ( editor
->IsCreated() )
8314 isShown
= editor
->GetControl()->IsShown();
8324 void wxGrid::ShowCellEditControl()
8326 if ( IsCellEditControlEnabled() )
8328 if ( !IsVisible( m_currentCellCoords
, false ) )
8330 m_cellEditCtrlEnabled
= false;
8335 wxRect rect
= CellToRect( m_currentCellCoords
);
8336 int row
= m_currentCellCoords
.GetRow();
8337 int col
= m_currentCellCoords
.GetCol();
8339 // if this is part of a multicell, find owner (topleft)
8340 int cell_rows
, cell_cols
;
8341 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8342 if ( cell_rows
<= 0 || cell_cols
<= 0 )
8346 m_currentCellCoords
.SetRow( row
);
8347 m_currentCellCoords
.SetCol( col
);
8350 // erase the highlight and the cell contents because the editor
8351 // might not cover the entire cell
8352 wxClientDC
dc( m_gridWin
);
8354 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8355 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxBRUSHSTYLE_SOLID
));
8356 dc
.SetPen(*wxTRANSPARENT_PEN
);
8357 dc
.DrawRectangle(rect
);
8359 // convert to scrolled coords
8360 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8366 // cell is shifted by one pixel
8367 // However, don't allow x or y to become negative
8368 // since the SetSize() method interprets that as
8375 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8376 if ( !editor
->IsCreated() )
8378 editor
->Create(m_gridWin
, wxID_ANY
,
8379 new wxGridCellEditorEvtHandler(this, editor
));
8381 wxGridEditorCreatedEvent
evt(GetId(),
8382 wxEVT_GRID_EDITOR_CREATED
,
8386 editor
->GetControl());
8387 GetEventHandler()->ProcessEvent(evt
);
8390 // resize editor to overflow into righthand cells if allowed
8391 int maxWidth
= rect
.width
;
8392 wxString value
= GetCellValue(row
, col
);
8393 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
8396 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
8397 if (maxWidth
< rect
.width
)
8398 maxWidth
= rect
.width
;
8401 int client_right
= m_gridWin
->GetClientSize().GetWidth();
8402 if (rect
.x
+ maxWidth
> client_right
)
8403 maxWidth
= client_right
- rect
.x
;
8405 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
8407 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8408 // may have changed earlier
8409 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
8412 GetCellSize( row
, i
, &c_rows
, &c_cols
);
8414 // looks weird going over a multicell
8415 if (m_table
->IsEmptyCell( row
, i
) &&
8416 (rect
.width
< maxWidth
) && (c_rows
== 1))
8418 rect
.width
+= GetColWidth( i
);
8424 if (rect
.GetRight() > client_right
)
8425 rect
.SetRight( client_right
- 1 );
8428 editor
->SetCellAttr( attr
);
8429 editor
->SetSize( rect
);
8431 editor
->GetControl()->Move(
8432 editor
->GetControl()->GetPosition().x
+ nXMove
,
8433 editor
->GetControl()->GetPosition().y
);
8434 editor
->Show( true, attr
);
8436 // recalc dimensions in case we need to
8437 // expand the scrolled window to account for editor
8440 editor
->BeginEdit(row
, col
, this);
8441 editor
->SetCellAttr(NULL
);
8449 void wxGrid::HideCellEditControl()
8451 if ( IsCellEditControlEnabled() )
8453 int row
= m_currentCellCoords
.GetRow();
8454 int col
= m_currentCellCoords
.GetCol();
8456 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8457 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
8458 editor
->Show( false );
8462 m_gridWin
->SetFocus();
8464 // refresh whole row to the right
8465 wxRect
rect( CellToRect(row
, col
) );
8466 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8467 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8470 // ensure that the pixels under the focus ring get refreshed as well
8471 rect
.Inflate(10, 10);
8474 m_gridWin
->Refresh( false, &rect
);
8478 void wxGrid::SaveEditControlValue()
8480 if ( IsCellEditControlEnabled() )
8482 int row
= m_currentCellCoords
.GetRow();
8483 int col
= m_currentCellCoords
.GetCol();
8485 wxString oldval
= GetCellValue(row
, col
);
8487 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8488 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8489 bool changed
= editor
->EndEdit(row
, col
, this);
8496 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
8497 m_currentCellCoords
.GetRow(),
8498 m_currentCellCoords
.GetCol() ) < 0 )
8500 // Event has been vetoed, set the data back.
8501 SetCellValue(row
, col
, oldval
);
8508 // ------ Grid location functions
8509 // Note that all of these functions work with the logical coordinates of
8510 // grid cells and labels so you will need to convert from device
8511 // coordinates for mouse events etc.
8514 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
) const
8516 int row
= YToRow(y
);
8517 int col
= XToCol(x
);
8519 if ( row
== -1 || col
== -1 )
8521 coords
= wxGridNoCellCoords
;
8525 coords
.Set( row
, col
);
8529 // Internal Helper function for computing row or column from some
8530 // (unscrolled) coordinate value, using either
8531 // m_defaultRowHeight/m_defaultColWidth or binary search on array
8532 // of m_rowBottoms/m_ColRights to speed up the search!
8534 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
8535 const wxArrayInt
& BorderArray
, int nMax
,
8539 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
8544 size_t i_max
= coord
/ defaultDist
,
8547 if (BorderArray
.IsEmpty())
8549 if ((int) i_max
< nMax
)
8551 return clipToMinMax
? nMax
- 1 : -1;
8554 if ( i_max
>= BorderArray
.GetCount())
8556 i_max
= BorderArray
.GetCount() - 1;
8560 if ( coord
>= BorderArray
[i_max
])
8564 i_max
= coord
/ minDist
;
8566 i_max
= BorderArray
.GetCount() - 1;
8569 if ( i_max
>= BorderArray
.GetCount())
8570 i_max
= BorderArray
.GetCount() - 1;
8573 if ( coord
>= BorderArray
[i_max
])
8574 return clipToMinMax
? (int)i_max
: -1;
8575 if ( coord
< BorderArray
[0] )
8578 while ( i_max
- i_min
> 0 )
8580 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
8581 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8582 if (coord
>= BorderArray
[ i_max
- 1])
8586 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
8587 if (coord
< BorderArray
[median
])
8596 int wxGrid::YToRow( int y
) const
8598 return CoordToRowOrCol(y
, m_defaultRowHeight
,
8599 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8602 int wxGrid::XToCol( int x
, bool clipToMinMax
) const
8605 return clipToMinMax
&& (m_numCols
> 0) ? GetColAt( 0 ) : -1;
8607 wxASSERT_MSG(m_defaultColWidth
> 0, wxT("Default column width can not be zero"));
8609 int maxPos
= x
/ m_defaultColWidth
;
8612 if (m_colRights
.IsEmpty())
8614 if(maxPos
< m_numCols
)
8615 return GetColAt( maxPos
);
8616 return clipToMinMax
? GetColAt( m_numCols
- 1 ) : -1;
8619 if ( maxPos
>= m_numCols
)
8620 maxPos
= m_numCols
- 1;
8623 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8626 if (m_minAcceptableColWidth
)
8627 maxPos
= x
/ m_minAcceptableColWidth
;
8629 maxPos
= m_numCols
- 1;
8631 if ( maxPos
>= m_numCols
)
8632 maxPos
= m_numCols
- 1;
8635 //X is beyond the last column
8636 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8637 return clipToMinMax
? GetColAt( maxPos
) : -1;
8639 //X is before the first column
8640 if ( x
< m_colRights
[GetColAt( 0 )] )
8641 return GetColAt( 0 );
8643 //Perform a binary search
8644 while ( maxPos
- minPos
> 0 )
8646 wxCHECK_MSG(m_colRights
[GetColAt( minPos
)] <= x
&& x
< m_colRights
[GetColAt( maxPos
)],
8647 0, _T("wxGrid: internal error in XToCol"));
8649 if (x
>= m_colRights
[GetColAt( maxPos
- 1 )])
8650 return GetColAt( maxPos
);
8653 int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
8654 if (x
< m_colRights
[GetColAt( median
)])
8659 return GetColAt( maxPos
);
8662 // return the row number that that the y coord is near
8663 // the edge of, or -1 if not near an edge.
8664 // coords can only possibly be near an edge if
8665 // (a) the row/column is large enough to still allow for an "inner" area
8666 // that is _not_ nead the edge (i.e., if the height/width is smaller
8667 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8670 // (b) resizing rows/columns (the thing for which edge detection is
8671 // relevant at all) is enabled.
8673 int wxGrid::YToEdgeOfRow( int y
) const
8676 i
= internalYToRow(y
);
8678 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragRowSize() )
8680 // We know that we are in row i, test whether we are
8681 // close enough to lower or upper border, respectively.
8682 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8684 else if ( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8691 // return the col number that that the x coord is near the edge of, or
8692 // -1 if not near an edge
8693 // See comment at YToEdgeOfRow for conditions on edge detection.
8695 int wxGrid::XToEdgeOfCol( int x
) const
8698 i
= internalXToCol(x
);
8700 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragColSize() )
8702 // We know that we are in column i; test whether we are
8703 // close enough to right or left border, respectively.
8704 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8706 else if ( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8713 wxRect
wxGrid::CellToRect( int row
, int col
) const
8715 wxRect
rect( -1, -1, -1, -1 );
8717 if ( row
>= 0 && row
< m_numRows
&&
8718 col
>= 0 && col
< m_numCols
)
8720 int i
, cell_rows
, cell_cols
;
8721 rect
.width
= rect
.height
= 0;
8722 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8723 // if negative then find multicell owner
8728 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8730 rect
.x
= GetColLeft(col
);
8731 rect
.y
= GetRowTop(row
);
8732 for (i
=col
; i
< col
+ cell_cols
; i
++)
8733 rect
.width
+= GetColWidth(i
);
8734 for (i
=row
; i
< row
+ cell_rows
; i
++)
8735 rect
.height
+= GetRowHeight(i
);
8738 // if grid lines are enabled, then the area of the cell is a bit smaller
8739 if (m_gridLinesEnabled
)
8748 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
8750 // get the cell rectangle in logical coords
8752 wxRect
r( CellToRect( row
, col
) );
8754 // convert to device coords
8756 int left
, top
, right
, bottom
;
8757 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8758 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8760 // check against the client area of the grid window
8762 m_gridWin
->GetClientSize( &cw
, &ch
);
8764 if ( wholeCellVisible
)
8766 // is the cell wholly visible ?
8767 return ( left
>= 0 && right
<= cw
&&
8768 top
>= 0 && bottom
<= ch
);
8772 // is the cell partly visible ?
8774 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8775 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8779 // make the specified cell location visible by doing a minimal amount
8782 void wxGrid::MakeCellVisible( int row
, int col
)
8785 int xpos
= -1, ypos
= -1;
8787 if ( row
>= 0 && row
< m_numRows
&&
8788 col
>= 0 && col
< m_numCols
)
8790 // get the cell rectangle in logical coords
8791 wxRect
r( CellToRect( row
, col
) );
8793 // convert to device coords
8794 int left
, top
, right
, bottom
;
8795 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8796 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8799 m_gridWin
->GetClientSize( &cw
, &ch
);
8805 else if ( bottom
> ch
)
8807 int h
= r
.GetHeight();
8809 for ( i
= row
- 1; i
>= 0; i
-- )
8811 int rowHeight
= GetRowHeight(i
);
8812 if ( h
+ rowHeight
> ch
)
8819 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8820 // have rounding errors (this is important, because if we do,
8821 // we might not scroll at all and some cells won't be redrawn)
8823 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
8824 // so just add a full scroll unit...
8825 ypos
+= m_scrollLineY
;
8828 // special handling for wide cells - show always left part of the cell!
8829 // Otherwise, e.g. when stepping from row to row, it would jump between
8830 // left and right part of the cell on every step!
8832 if ( left
< 0 || (right
- left
) >= cw
)
8836 else if ( right
> cw
)
8838 // position the view so that the cell is on the right
8840 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8841 xpos
= x0
+ (right
- cw
);
8843 // see comment for ypos above
8844 xpos
+= m_scrollLineX
;
8847 if ( xpos
!= -1 || ypos
!= -1 )
8850 xpos
/= m_scrollLineX
;
8852 ypos
/= m_scrollLineY
;
8853 Scroll( xpos
, ypos
);
8860 // ------ Grid cursor movement functions
8863 bool wxGrid::MoveCursorUp( bool expandSelection
)
8865 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8866 m_currentCellCoords
.GetRow() >= 0 )
8868 if ( expandSelection
)
8870 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8871 m_selectingKeyboard
= m_currentCellCoords
;
8872 if ( m_selectingKeyboard
.GetRow() > 0 )
8874 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8875 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8876 m_selectingKeyboard
.GetCol() );
8877 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8880 else if ( m_currentCellCoords
.GetRow() > 0 )
8882 int row
= m_currentCellCoords
.GetRow() - 1;
8883 int col
= m_currentCellCoords
.GetCol();
8885 MakeCellVisible( row
, col
);
8886 SetCurrentCell( row
, col
);
8897 bool wxGrid::MoveCursorDown( bool expandSelection
)
8899 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8900 m_currentCellCoords
.GetRow() < m_numRows
)
8902 if ( expandSelection
)
8904 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8905 m_selectingKeyboard
= m_currentCellCoords
;
8906 if ( m_selectingKeyboard
.GetRow() < m_numRows
- 1 )
8908 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8909 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8910 m_selectingKeyboard
.GetCol() );
8911 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8914 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8916 int row
= m_currentCellCoords
.GetRow() + 1;
8917 int col
= m_currentCellCoords
.GetCol();
8919 MakeCellVisible( row
, col
);
8920 SetCurrentCell( row
, col
);
8931 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8933 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8934 m_currentCellCoords
.GetCol() >= 0 )
8936 if ( expandSelection
)
8938 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8939 m_selectingKeyboard
= m_currentCellCoords
;
8940 if ( m_selectingKeyboard
.GetCol() > 0 )
8942 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8943 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8944 m_selectingKeyboard
.GetCol() );
8945 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8948 else if ( GetColPos( m_currentCellCoords
.GetCol() ) > 0 )
8950 int row
= m_currentCellCoords
.GetRow();
8951 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) - 1 );
8954 MakeCellVisible( row
, col
);
8955 SetCurrentCell( row
, col
);
8966 bool wxGrid::MoveCursorRight( bool expandSelection
)
8968 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8969 m_currentCellCoords
.GetCol() < m_numCols
)
8971 if ( expandSelection
)
8973 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8974 m_selectingKeyboard
= m_currentCellCoords
;
8975 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8977 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8978 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8979 m_selectingKeyboard
.GetCol() );
8980 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8983 else if ( GetColPos( m_currentCellCoords
.GetCol() ) < m_numCols
- 1 )
8985 int row
= m_currentCellCoords
.GetRow();
8986 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) + 1 );
8989 MakeCellVisible( row
, col
);
8990 SetCurrentCell( row
, col
);
9001 bool wxGrid::MovePageUp()
9003 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9006 int row
= m_currentCellCoords
.GetRow();
9010 m_gridWin
->GetClientSize( &cw
, &ch
);
9012 int y
= GetRowTop(row
);
9013 int newRow
= internalYToRow( y
- ch
+ 1 );
9015 if ( newRow
== row
)
9017 // row > 0, so newRow can never be less than 0 here.
9021 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
9022 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
9030 bool wxGrid::MovePageDown()
9032 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9035 int row
= m_currentCellCoords
.GetRow();
9036 if ( (row
+ 1) < m_numRows
)
9039 m_gridWin
->GetClientSize( &cw
, &ch
);
9041 int y
= GetRowTop(row
);
9042 int newRow
= internalYToRow( y
+ ch
);
9043 if ( newRow
== row
)
9045 // row < m_numRows, so newRow can't overflow here.
9049 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
9050 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
9058 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
9061 m_currentCellCoords
!= wxGridNoCellCoords
&&
9062 m_currentCellCoords
.GetRow() > 0 )
9064 int row
= m_currentCellCoords
.GetRow();
9065 int col
= m_currentCellCoords
.GetCol();
9067 if ( m_table
->IsEmptyCell(row
, col
) )
9069 // starting in an empty cell: find the next block of
9075 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9079 else if ( m_table
->IsEmptyCell(row
- 1, col
) )
9081 // starting at the top of a block: find the next block
9087 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9093 // starting within a block: find the top of the block
9098 if ( m_table
->IsEmptyCell(row
, col
) )
9106 MakeCellVisible( row
, col
);
9107 if ( expandSelection
)
9109 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9110 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9115 SetCurrentCell( row
, col
);
9124 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
9127 m_currentCellCoords
!= wxGridNoCellCoords
&&
9128 m_currentCellCoords
.GetRow() < m_numRows
- 1 )
9130 int row
= m_currentCellCoords
.GetRow();
9131 int col
= m_currentCellCoords
.GetCol();
9133 if ( m_table
->IsEmptyCell(row
, col
) )
9135 // starting in an empty cell: find the next block of
9138 while ( row
< m_numRows
- 1 )
9141 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9145 else if ( m_table
->IsEmptyCell(row
+ 1, col
) )
9147 // starting at the bottom of a block: find the next block
9150 while ( row
< m_numRows
- 1 )
9153 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9159 // starting within a block: find the bottom of the block
9161 while ( row
< m_numRows
- 1 )
9164 if ( m_table
->IsEmptyCell(row
, col
) )
9172 MakeCellVisible( row
, col
);
9173 if ( expandSelection
)
9175 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9176 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9181 SetCurrentCell( row
, col
);
9190 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
9193 m_currentCellCoords
!= wxGridNoCellCoords
&&
9194 m_currentCellCoords
.GetCol() > 0 )
9196 int row
= m_currentCellCoords
.GetRow();
9197 int col
= m_currentCellCoords
.GetCol();
9199 if ( m_table
->IsEmptyCell(row
, col
) )
9201 // starting in an empty cell: find the next block of
9207 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9211 else if ( m_table
->IsEmptyCell(row
, col
- 1) )
9213 // starting at the left of a block: find the next block
9219 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9225 // starting within a block: find the left of the block
9230 if ( m_table
->IsEmptyCell(row
, col
) )
9238 MakeCellVisible( row
, col
);
9239 if ( expandSelection
)
9241 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9242 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9247 SetCurrentCell( row
, col
);
9256 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
9259 m_currentCellCoords
!= wxGridNoCellCoords
&&
9260 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
9262 int row
= m_currentCellCoords
.GetRow();
9263 int col
= m_currentCellCoords
.GetCol();
9265 if ( m_table
->IsEmptyCell(row
, col
) )
9267 // starting in an empty cell: find the next block of
9270 while ( col
< m_numCols
- 1 )
9273 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9277 else if ( m_table
->IsEmptyCell(row
, col
+ 1) )
9279 // starting at the right of a block: find the next block
9282 while ( col
< m_numCols
- 1 )
9285 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9291 // starting within a block: find the right of the block
9293 while ( col
< m_numCols
- 1 )
9296 if ( m_table
->IsEmptyCell(row
, col
) )
9304 MakeCellVisible( row
, col
);
9305 if ( expandSelection
)
9307 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9308 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9313 SetCurrentCell( row
, col
);
9323 // ------ Label values and formatting
9326 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
9329 *horiz
= m_rowLabelHorizAlign
;
9331 *vert
= m_rowLabelVertAlign
;
9334 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
9337 *horiz
= m_colLabelHorizAlign
;
9339 *vert
= m_colLabelVertAlign
;
9342 int wxGrid::GetColLabelTextOrientation() const
9344 return m_colLabelTextOrientation
;
9347 wxString
wxGrid::GetRowLabelValue( int row
) const
9351 return m_table
->GetRowLabelValue( row
);
9361 wxString
wxGrid::GetColLabelValue( int col
) const
9365 return m_table
->GetColLabelValue( col
);
9375 void wxGrid::SetRowLabelSize( int width
)
9377 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
9379 if ( width
== wxGRID_AUTOSIZE
)
9381 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
9384 if ( width
!= m_rowLabelWidth
)
9388 m_rowLabelWin
->Show( false );
9389 m_cornerLabelWin
->Show( false );
9391 else if ( m_rowLabelWidth
== 0 )
9393 m_rowLabelWin
->Show( true );
9394 if ( m_colLabelHeight
> 0 )
9395 m_cornerLabelWin
->Show( true );
9398 m_rowLabelWidth
= width
;
9400 wxScrolledWindow::Refresh( true );
9404 void wxGrid::SetColLabelSize( int height
)
9406 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
9408 if ( height
== wxGRID_AUTOSIZE
)
9410 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
9413 if ( height
!= m_colLabelHeight
)
9417 m_colLabelWin
->Show( false );
9418 m_cornerLabelWin
->Show( false );
9420 else if ( m_colLabelHeight
== 0 )
9422 m_colLabelWin
->Show( true );
9423 if ( m_rowLabelWidth
> 0 )
9424 m_cornerLabelWin
->Show( true );
9427 m_colLabelHeight
= height
;
9429 wxScrolledWindow::Refresh( true );
9433 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
9435 if ( m_labelBackgroundColour
!= colour
)
9437 m_labelBackgroundColour
= colour
;
9438 m_rowLabelWin
->SetBackgroundColour( colour
);
9439 m_colLabelWin
->SetBackgroundColour( colour
);
9440 m_cornerLabelWin
->SetBackgroundColour( colour
);
9442 if ( !GetBatchCount() )
9444 m_rowLabelWin
->Refresh();
9445 m_colLabelWin
->Refresh();
9446 m_cornerLabelWin
->Refresh();
9451 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
9453 if ( m_labelTextColour
!= colour
)
9455 m_labelTextColour
= colour
;
9456 if ( !GetBatchCount() )
9458 m_rowLabelWin
->Refresh();
9459 m_colLabelWin
->Refresh();
9464 void wxGrid::SetLabelFont( const wxFont
& font
)
9467 if ( !GetBatchCount() )
9469 m_rowLabelWin
->Refresh();
9470 m_colLabelWin
->Refresh();
9474 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
9476 // allow old (incorrect) defs to be used
9479 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9480 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9481 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9486 case wxTOP
: vert
= wxALIGN_TOP
; break;
9487 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9488 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9491 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9493 m_rowLabelHorizAlign
= horiz
;
9496 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9498 m_rowLabelVertAlign
= vert
;
9501 if ( !GetBatchCount() )
9503 m_rowLabelWin
->Refresh();
9507 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
9509 // allow old (incorrect) defs to be used
9512 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9513 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9514 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9519 case wxTOP
: vert
= wxALIGN_TOP
; break;
9520 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9521 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9524 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9526 m_colLabelHorizAlign
= horiz
;
9529 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9531 m_colLabelVertAlign
= vert
;
9534 if ( !GetBatchCount() )
9536 m_colLabelWin
->Refresh();
9540 // Note: under MSW, the default column label font must be changed because it
9541 // does not support vertical printing
9543 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9544 // pGrid->SetLabelFont(font);
9545 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9547 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9549 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9550 m_colLabelTextOrientation
= textOrientation
;
9552 if ( !GetBatchCount() )
9553 m_colLabelWin
->Refresh();
9556 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9560 m_table
->SetRowLabelValue( row
, s
);
9561 if ( !GetBatchCount() )
9563 wxRect rect
= CellToRect( row
, 0 );
9564 if ( rect
.height
> 0 )
9566 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9568 rect
.width
= m_rowLabelWidth
;
9569 m_rowLabelWin
->Refresh( true, &rect
);
9575 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9579 m_table
->SetColLabelValue( col
, s
);
9580 if ( !GetBatchCount() )
9582 wxRect rect
= CellToRect( 0, col
);
9583 if ( rect
.width
> 0 )
9585 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9587 rect
.height
= m_colLabelHeight
;
9588 m_colLabelWin
->Refresh( true, &rect
);
9594 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9596 if ( m_gridLineColour
!= colour
)
9598 m_gridLineColour
= colour
;
9600 wxClientDC
dc( m_gridWin
);
9602 DrawAllGridLines( dc
, wxRegion() );
9606 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9608 if ( m_cellHighlightColour
!= colour
)
9610 m_cellHighlightColour
= colour
;
9612 wxClientDC
dc( m_gridWin
);
9614 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9615 DrawCellHighlight(dc
, attr
);
9620 void wxGrid::SetCellHighlightPenWidth(int width
)
9622 if (m_cellHighlightPenWidth
!= width
)
9624 m_cellHighlightPenWidth
= width
;
9626 // Just redrawing the cell highlight is not enough since that won't
9627 // make any visible change if the the thickness is getting smaller.
9628 int row
= m_currentCellCoords
.GetRow();
9629 int col
= m_currentCellCoords
.GetCol();
9630 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9633 wxRect rect
= CellToRect(row
, col
);
9634 m_gridWin
->Refresh(true, &rect
);
9638 void wxGrid::SetCellHighlightROPenWidth(int width
)
9640 if (m_cellHighlightROPenWidth
!= width
)
9642 m_cellHighlightROPenWidth
= width
;
9644 // Just redrawing the cell highlight is not enough since that won't
9645 // make any visible change if the the thickness is getting smaller.
9646 int row
= m_currentCellCoords
.GetRow();
9647 int col
= m_currentCellCoords
.GetCol();
9648 if ( row
== -1 || col
== -1 ||
9649 GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9652 wxRect rect
= CellToRect(row
, col
);
9653 m_gridWin
->Refresh(true, &rect
);
9657 void wxGrid::EnableGridLines( bool enable
)
9659 if ( enable
!= m_gridLinesEnabled
)
9661 m_gridLinesEnabled
= enable
;
9663 if ( !GetBatchCount() )
9667 wxClientDC
dc( m_gridWin
);
9669 DrawAllGridLines( dc
, wxRegion() );
9673 m_gridWin
->Refresh();
9679 int wxGrid::GetDefaultRowSize() const
9681 return m_defaultRowHeight
;
9684 int wxGrid::GetRowSize( int row
) const
9686 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9688 return GetRowHeight(row
);
9691 int wxGrid::GetDefaultColSize() const
9693 return m_defaultColWidth
;
9696 int wxGrid::GetColSize( int col
) const
9698 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9700 return GetColWidth(col
);
9703 // ============================================================================
9704 // access to the grid attributes: each of them has a default value in the grid
9705 // itself and may be overidden on a per-cell basis
9706 // ============================================================================
9708 // ----------------------------------------------------------------------------
9709 // setting default attributes
9710 // ----------------------------------------------------------------------------
9712 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9714 m_defaultCellAttr
->SetBackgroundColour(col
);
9716 m_gridWin
->SetBackgroundColour(col
);
9720 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9722 m_defaultCellAttr
->SetTextColour(col
);
9725 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9727 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9730 void wxGrid::SetDefaultCellOverflow( bool allow
)
9732 m_defaultCellAttr
->SetOverflow(allow
);
9735 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9737 m_defaultCellAttr
->SetFont(font
);
9740 // For editors and renderers the type registry takes precedence over the
9741 // default attr, so we need to register the new editor/renderer for the string
9742 // data type in order to make setting a default editor/renderer appear to
9745 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9747 RegisterDataType(wxGRID_VALUE_STRING
,
9749 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9752 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9754 RegisterDataType(wxGRID_VALUE_STRING
,
9755 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9759 // ----------------------------------------------------------------------------
9760 // access to the default attributes
9761 // ----------------------------------------------------------------------------
9763 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
9765 return m_defaultCellAttr
->GetBackgroundColour();
9768 wxColour
wxGrid::GetDefaultCellTextColour() const
9770 return m_defaultCellAttr
->GetTextColour();
9773 wxFont
wxGrid::GetDefaultCellFont() const
9775 return m_defaultCellAttr
->GetFont();
9778 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
9780 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9783 bool wxGrid::GetDefaultCellOverflow() const
9785 return m_defaultCellAttr
->GetOverflow();
9788 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9790 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9793 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9795 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9798 // ----------------------------------------------------------------------------
9799 // access to cell attributes
9800 // ----------------------------------------------------------------------------
9802 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
9804 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9805 wxColour colour
= attr
->GetBackgroundColour();
9811 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
9813 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9814 wxColour colour
= attr
->GetTextColour();
9820 wxFont
wxGrid::GetCellFont( int row
, int col
) const
9822 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9823 wxFont font
= attr
->GetFont();
9829 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
9831 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9832 attr
->GetAlignment(horiz
, vert
);
9836 bool wxGrid::GetCellOverflow( int row
, int col
) const
9838 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9839 bool allow
= attr
->GetOverflow();
9845 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
9847 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9848 attr
->GetSize( num_rows
, num_cols
);
9852 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
9854 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9855 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9861 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
9863 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9864 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9870 bool wxGrid::IsReadOnly(int row
, int col
) const
9872 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9873 bool isReadOnly
= attr
->IsReadOnly();
9879 // ----------------------------------------------------------------------------
9880 // attribute support: cache, automatic provider creation, ...
9881 // ----------------------------------------------------------------------------
9883 bool wxGrid::CanHaveAttributes() const
9890 return m_table
->CanHaveAttributes();
9893 void wxGrid::ClearAttrCache()
9895 if ( m_attrCache
.row
!= -1 )
9897 wxSafeDecRef(m_attrCache
.attr
);
9898 m_attrCache
.attr
= NULL
;
9899 m_attrCache
.row
= -1;
9903 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9907 wxGrid
*self
= (wxGrid
*)this; // const_cast
9909 self
->ClearAttrCache();
9910 self
->m_attrCache
.row
= row
;
9911 self
->m_attrCache
.col
= col
;
9912 self
->m_attrCache
.attr
= attr
;
9917 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9919 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9921 *attr
= m_attrCache
.attr
;
9922 wxSafeIncRef(m_attrCache
.attr
);
9924 #ifdef DEBUG_ATTR_CACHE
9925 gs_nAttrCacheHits
++;
9932 #ifdef DEBUG_ATTR_CACHE
9933 gs_nAttrCacheMisses
++;
9940 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9942 wxGridCellAttr
*attr
= NULL
;
9943 // Additional test to avoid looking at the cache e.g. for
9944 // wxNoCellCoords, as this will confuse memory management.
9947 if ( !LookupAttr(row
, col
, &attr
) )
9949 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9950 : (wxGridCellAttr
*)NULL
;
9951 CacheAttr(row
, col
, attr
);
9957 attr
->SetDefAttr(m_defaultCellAttr
);
9961 attr
= m_defaultCellAttr
;
9968 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9970 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9971 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9973 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9974 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9976 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9979 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9981 // artificially inc the ref count to match DecRef() in caller
9983 m_table
->SetAttr(attr
, row
, col
);
9989 // ----------------------------------------------------------------------------
9990 // setting column attributes (wrappers around SetColAttr)
9991 // ----------------------------------------------------------------------------
9993 void wxGrid::SetColFormatBool(int col
)
9995 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9998 void wxGrid::SetColFormatNumber(int col
)
10000 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
10003 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
10005 wxString typeName
= wxGRID_VALUE_FLOAT
;
10006 if ( (width
!= -1) || (precision
!= -1) )
10008 typeName
<< _T(':') << width
<< _T(',') << precision
;
10011 SetColFormatCustom(col
, typeName
);
10014 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
10016 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
10018 attr
= new wxGridCellAttr
;
10019 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
10020 attr
->SetRenderer(renderer
);
10022 SetColAttr(col
, attr
);
10026 // ----------------------------------------------------------------------------
10027 // setting cell attributes: this is forwarded to the table
10028 // ----------------------------------------------------------------------------
10030 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
10032 if ( CanHaveAttributes() )
10034 m_table
->SetAttr(attr
, row
, col
);
10039 wxSafeDecRef(attr
);
10043 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
10045 if ( CanHaveAttributes() )
10047 m_table
->SetRowAttr(attr
, row
);
10052 wxSafeDecRef(attr
);
10056 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
10058 if ( CanHaveAttributes() )
10060 m_table
->SetColAttr(attr
, col
);
10065 wxSafeDecRef(attr
);
10069 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
10071 if ( CanHaveAttributes() )
10073 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10074 attr
->SetBackgroundColour(colour
);
10079 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
10081 if ( CanHaveAttributes() )
10083 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10084 attr
->SetTextColour(colour
);
10089 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
10091 if ( CanHaveAttributes() )
10093 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10094 attr
->SetFont(font
);
10099 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
10101 if ( CanHaveAttributes() )
10103 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10104 attr
->SetAlignment(horiz
, vert
);
10109 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
10111 if ( CanHaveAttributes() )
10113 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10114 attr
->SetOverflow(allow
);
10119 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
10121 if ( CanHaveAttributes() )
10123 int cell_rows
, cell_cols
;
10125 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10126 attr
->GetSize(&cell_rows
, &cell_cols
);
10127 attr
->SetSize(num_rows
, num_cols
);
10130 // Cannot set the size of a cell to 0 or negative values
10131 // While it is perfectly legal to do that, this function cannot
10132 // handle all the possibilies, do it by hand by getting the CellAttr.
10133 // You can only set the size of a cell to 1,1 or greater with this fn
10134 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
10135 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10136 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
10137 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10139 // if this was already a multicell then "turn off" the other cells first
10140 if ((cell_rows
> 1) || (cell_rows
> 1))
10143 for (j
=row
; j
< row
+ cell_rows
; j
++)
10145 for (i
=col
; i
< col
+ cell_cols
; i
++)
10147 if ((i
!= col
) || (j
!= row
))
10149 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10150 attr_stub
->SetSize( 1, 1 );
10151 attr_stub
->DecRef();
10157 // mark the cells that will be covered by this cell to
10158 // negative or zero values to point back at this cell
10159 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
10162 for (j
=row
; j
< row
+ num_rows
; j
++)
10164 for (i
=col
; i
< col
+ num_cols
; i
++)
10166 if ((i
!= col
) || (j
!= row
))
10168 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10169 attr_stub
->SetSize( row
- j
, col
- i
);
10170 attr_stub
->DecRef();
10178 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
10180 if ( CanHaveAttributes() )
10182 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10183 attr
->SetRenderer(renderer
);
10188 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
10190 if ( CanHaveAttributes() )
10192 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10193 attr
->SetEditor(editor
);
10198 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
10200 if ( CanHaveAttributes() )
10202 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10203 attr
->SetReadOnly(isReadOnly
);
10208 // ----------------------------------------------------------------------------
10209 // Data type registration
10210 // ----------------------------------------------------------------------------
10212 void wxGrid::RegisterDataType(const wxString
& typeName
,
10213 wxGridCellRenderer
* renderer
,
10214 wxGridCellEditor
* editor
)
10216 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
10220 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
10222 wxString typeName
= m_table
->GetTypeName(row
, col
);
10223 return GetDefaultEditorForType(typeName
);
10226 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
10228 wxString typeName
= m_table
->GetTypeName(row
, col
);
10229 return GetDefaultRendererForType(typeName
);
10232 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
10234 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10235 if ( index
== wxNOT_FOUND
)
10237 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10242 return m_typeRegistry
->GetEditor(index
);
10245 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
10247 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10248 if ( index
== wxNOT_FOUND
)
10250 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10255 return m_typeRegistry
->GetRenderer(index
);
10258 // ----------------------------------------------------------------------------
10260 // ----------------------------------------------------------------------------
10262 void wxGrid::EnableDragRowSize( bool enable
)
10264 m_canDragRowSize
= enable
;
10267 void wxGrid::EnableDragColSize( bool enable
)
10269 m_canDragColSize
= enable
;
10272 void wxGrid::EnableDragGridSize( bool enable
)
10274 m_canDragGridSize
= enable
;
10277 void wxGrid::EnableDragCell( bool enable
)
10279 m_canDragCell
= enable
;
10282 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
10284 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
10286 if ( resizeExistingRows
)
10288 // since we are resizing all rows to the default row size,
10289 // we can simply clear the row heights and row bottoms
10290 // arrays (which also allows us to take advantage of
10291 // some speed optimisations)
10292 m_rowHeights
.Empty();
10293 m_rowBottoms
.Empty();
10294 if ( !GetBatchCount() )
10299 void wxGrid::SetRowSize( int row
, int height
)
10301 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
10303 // See comment in SetColSize
10304 if ( height
< GetRowMinimalAcceptableHeight())
10307 if ( m_rowHeights
.IsEmpty() )
10309 // need to really create the array
10313 int h
= wxMax( 0, height
);
10314 int diff
= h
- m_rowHeights
[row
];
10316 m_rowHeights
[row
] = h
;
10317 for ( int i
= row
; i
< m_numRows
; i
++ )
10319 m_rowBottoms
[i
] += diff
;
10322 if ( !GetBatchCount() )
10326 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
10328 // we dont allow zero default column width
10329 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
10331 if ( resizeExistingCols
)
10333 // since we are resizing all columns to the default column size,
10334 // we can simply clear the col widths and col rights
10335 // arrays (which also allows us to take advantage of
10336 // some speed optimisations)
10337 m_colWidths
.Empty();
10338 m_colRights
.Empty();
10339 if ( !GetBatchCount() )
10344 void wxGrid::SetColSize( int col
, int width
)
10346 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
10348 // should we check that it's bigger than GetColMinimalWidth(col) here?
10350 // No, because it is reasonable to assume the library user know's
10351 // what he is doing. However we should test against the weaker
10352 // constraint of minimalAcceptableWidth, as this breaks rendering
10354 // This test then fixes sf.net bug #645734
10356 if ( width
< GetColMinimalAcceptableWidth() )
10359 if ( m_colWidths
.IsEmpty() )
10361 // need to really create the array
10365 // if < 0 then calculate new width from label
10369 wxArrayString lines
;
10370 wxClientDC
dc(m_colLabelWin
);
10371 dc
.SetFont(GetLabelFont());
10372 StringToLines(GetColLabelValue(col
), lines
);
10373 GetTextBoxSize(dc
, lines
, &w
, &h
);
10377 int w
= wxMax( 0, width
);
10378 int diff
= w
- m_colWidths
[col
];
10379 m_colWidths
[col
] = w
;
10381 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
10383 m_colRights
[GetColAt(colPos
)] += diff
;
10386 if ( !GetBatchCount() )
10390 void wxGrid::SetColMinimalWidth( int col
, int width
)
10392 if (width
> GetColMinimalAcceptableWidth())
10394 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10395 m_colMinWidths
[key
] = width
;
10399 void wxGrid::SetRowMinimalHeight( int row
, int width
)
10401 if (width
> GetRowMinimalAcceptableHeight())
10403 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10404 m_rowMinHeights
[key
] = width
;
10408 int wxGrid::GetColMinimalWidth(int col
) const
10410 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10411 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
10413 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
10416 int wxGrid::GetRowMinimalHeight(int row
) const
10418 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10419 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
10421 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
10424 void wxGrid::SetColMinimalAcceptableWidth( int width
)
10426 // We do allow a width of 0 since this gives us
10427 // an easy way to temporarily hiding columns.
10429 m_minAcceptableColWidth
= width
;
10432 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
10434 // We do allow a height of 0 since this gives us
10435 // an easy way to temporarily hiding rows.
10437 m_minAcceptableRowHeight
= height
;
10440 int wxGrid::GetColMinimalAcceptableWidth() const
10442 return m_minAcceptableColWidth
;
10445 int wxGrid::GetRowMinimalAcceptableHeight() const
10447 return m_minAcceptableRowHeight
;
10450 // ----------------------------------------------------------------------------
10452 // ----------------------------------------------------------------------------
10455 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
10457 const bool column
= direction
== wxGRID_COLUMN
;
10459 wxClientDC
dc(m_gridWin
);
10461 // cancel editing of cell
10462 HideCellEditControl();
10463 SaveEditControlValue();
10465 // init both of them to avoid compiler warnings, even if we only need one
10473 wxCoord extent
, extentMax
= 0;
10474 int max
= column
? m_numRows
: m_numCols
;
10475 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
10482 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
10483 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
10486 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
10487 extent
= column
? size
.x
: size
.y
;
10488 if ( extent
> extentMax
)
10489 extentMax
= extent
;
10491 renderer
->DecRef();
10497 // now also compare with the column label extent
10499 dc
.SetFont( GetLabelFont() );
10503 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
10504 if ( GetColLabelTextOrientation() == wxVERTICAL
)
10508 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
10510 extent
= column
? w
: h
;
10511 if ( extent
> extentMax
)
10512 extentMax
= extent
;
10516 // empty column - give default extent (notice that if extentMax is less
10517 // than default extent but != 0, it's OK)
10518 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
10523 // leave some space around text
10531 // Ensure automatic width is not less than minimal width. See the
10532 // comment in SetColSize() for explanation of why this isn't done
10533 // in SetColSize().
10535 extentMax
= wxMax(extentMax
, GetColMinimalWidth(col
));
10537 SetColSize( col
, extentMax
);
10538 if ( !GetBatchCount() )
10541 m_gridWin
->GetClientSize( &cw
, &ch
);
10542 wxRect
rect ( CellToRect( 0, col
) );
10544 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10545 rect
.width
= cw
- rect
.x
;
10546 rect
.height
= m_colLabelHeight
;
10547 m_colLabelWin
->Refresh( true, &rect
);
10552 // Ensure automatic width is not less than minimal height. See the
10553 // comment in SetColSize() for explanation of why this isn't done
10554 // in SetRowSize().
10556 extentMax
= wxMax(extentMax
, GetRowMinimalHeight(row
));
10558 SetRowSize(row
, extentMax
);
10559 if ( !GetBatchCount() )
10562 m_gridWin
->GetClientSize( &cw
, &ch
);
10563 wxRect
rect( CellToRect( row
, 0 ) );
10565 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10566 rect
.width
= m_rowLabelWidth
;
10567 rect
.height
= ch
- rect
.y
;
10568 m_rowLabelWin
->Refresh( true, &rect
);
10575 SetColMinimalWidth(col
, extentMax
);
10577 SetRowMinimalHeight(row
, extentMax
);
10581 wxCoord
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
10583 // calculate size for the rows or columns?
10584 const bool calcRows
= direction
== wxGRID_ROW
;
10586 wxClientDC
dc(calcRows
? GetGridRowLabelWindow()
10587 : GetGridColLabelWindow());
10588 dc
.SetFont(GetLabelFont());
10590 // which dimension should we take into account for calculations?
10592 // for columns, the text can be only horizontal so it's easy but for rows
10593 // we also have to take into account the text orientation
10595 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
10597 wxArrayString lines
;
10598 wxCoord extentMax
= 0;
10600 const int numRowsOrCols
= calcRows
? m_numRows
: m_numCols
;
10601 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
10605 wxString label
= calcRows
? GetRowLabelValue(rowOrCol
)
10606 : GetColLabelValue(rowOrCol
);
10607 StringToLines(label
, lines
);
10610 GetTextBoxSize(dc
, lines
, &w
, &h
);
10612 const wxCoord extent
= useWidth
? w
: h
;
10613 if ( extent
> extentMax
)
10614 extentMax
= extent
;
10619 // empty column - give default extent (notice that if extentMax is less
10620 // than default extent but != 0, it's OK)
10621 extentMax
= calcRows
? GetDefaultRowLabelSize()
10622 : GetDefaultColLabelSize();
10625 // leave some space around text (taken from AutoSizeColOrRow)
10634 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10636 int width
= m_rowLabelWidth
;
10638 wxGridUpdateLocker locker
;
10640 locker
.Create(this);
10642 for ( int col
= 0; col
< m_numCols
; col
++ )
10645 AutoSizeColumn(col
, setAsMin
);
10647 width
+= GetColWidth(col
);
10653 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10655 int height
= m_colLabelHeight
;
10657 wxGridUpdateLocker locker
;
10659 locker
.Create(this);
10661 for ( int row
= 0; row
< m_numRows
; row
++ )
10664 AutoSizeRow(row
, setAsMin
);
10666 height
+= GetRowHeight(row
);
10672 void wxGrid::AutoSize()
10674 wxGridUpdateLocker
locker(this);
10676 // we need to round up the size of the scrollable area to a multiple of
10677 // scroll step to ensure that we don't get the scrollbars when we're sized
10678 // exactly to fit our contents
10679 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
10680 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
10681 wxSize
sizeFit(GetScrollX(size
.x
) * GetScrollLineX(),
10682 GetScrollY(size
.y
) * GetScrollLineY());
10684 // distribute the extra space between the columns/rows to avoid having
10685 // extra white space
10686 wxCoord diff
= sizeFit
.x
- size
.x
;
10687 if ( diff
&& m_numCols
)
10689 // try to resize the columns uniformly
10690 wxCoord diffPerCol
= diff
/ m_numCols
;
10693 for ( int col
= 0; col
< m_numCols
; col
++ )
10695 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
10699 // add remaining amount to the last columns
10700 diff
-= diffPerCol
* m_numCols
;
10703 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
10705 SetColSize(col
, GetColWidth(col
) + 1);
10711 diff
= sizeFit
.y
- size
.y
;
10712 if ( diff
&& m_numRows
)
10714 // try to resize the columns uniformly
10715 wxCoord diffPerRow
= diff
/ m_numRows
;
10718 for ( int row
= 0; row
< m_numRows
; row
++ )
10720 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
10724 // add remaining amount to the last rows
10725 diff
-= diffPerRow
* m_numRows
;
10728 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
10730 SetRowSize(row
, GetRowHeight(row
) + 1);
10735 // we know that we're not going to have scrollbars so disable them now to
10736 // avoid trouble in SetClientSize() which can otherwise set the correct
10737 // client size but also leave space for (not needed any more) scrollbars
10738 SetScrollbars(0, 0, 0, 0, 0, 0, true);
10739 SetClientSize(sizeFit
.x
+ m_rowLabelWidth
, sizeFit
.y
+ m_colLabelHeight
);
10742 void wxGrid::AutoSizeRowLabelSize( int row
)
10744 wxArrayString lines
;
10747 // Hide the edit control, so it
10748 // won't interfere with drag-shrinking.
10749 if ( IsCellEditControlShown() )
10751 HideCellEditControl();
10752 SaveEditControlValue();
10755 // autosize row height depending on label text
10756 StringToLines( GetRowLabelValue( row
), lines
);
10757 wxClientDC
dc( m_rowLabelWin
);
10758 GetTextBoxSize( dc
, lines
, &w
, &h
);
10759 if ( h
< m_defaultRowHeight
)
10760 h
= m_defaultRowHeight
;
10761 SetRowSize(row
, h
);
10765 void wxGrid::AutoSizeColLabelSize( int col
)
10767 wxArrayString lines
;
10770 // Hide the edit control, so it
10771 // won't interfere with drag-shrinking.
10772 if ( IsCellEditControlShown() )
10774 HideCellEditControl();
10775 SaveEditControlValue();
10778 // autosize column width depending on label text
10779 StringToLines( GetColLabelValue( col
), lines
);
10780 wxClientDC
dc( m_colLabelWin
);
10781 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10782 GetTextBoxSize( dc
, lines
, &w
, &h
);
10784 GetTextBoxSize( dc
, lines
, &h
, &w
);
10785 if ( w
< m_defaultColWidth
)
10786 w
= m_defaultColWidth
;
10787 SetColSize(col
, w
);
10791 wxSize
wxGrid::DoGetBestSize() const
10793 wxGrid
*self
= (wxGrid
*)this; // const_cast
10795 // we do the same as in AutoSize() here with the exception that we don't
10796 // change the column/row sizes, only calculate them
10797 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
10798 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
10799 wxSize
sizeFit(GetScrollX(size
.x
) * GetScrollLineX(),
10800 GetScrollY(size
.y
) * GetScrollLineY());
10802 // NOTE: This size should be cached, but first we need to add calls to
10803 // InvalidateBestSize everywhere that could change the results of this
10805 // CacheBestSize(size);
10807 return wxSize(sizeFit
.x
+ m_rowLabelWidth
, sizeFit
.y
+ m_colLabelHeight
)
10808 + GetWindowBorderSize();
10816 wxPen
& wxGrid::GetDividerPen() const
10821 // ----------------------------------------------------------------------------
10822 // cell value accessor functions
10823 // ----------------------------------------------------------------------------
10825 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10829 m_table
->SetValue( row
, col
, s
);
10830 if ( !GetBatchCount() )
10833 wxRect
rect( CellToRect( row
, col
) );
10835 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10836 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10837 m_gridWin
->Refresh( false, &rect
);
10840 if ( m_currentCellCoords
.GetRow() == row
&&
10841 m_currentCellCoords
.GetCol() == col
&&
10842 IsCellEditControlShown())
10843 // Note: If we are using IsCellEditControlEnabled,
10844 // this interacts badly with calling SetCellValue from
10845 // an EVT_GRID_CELL_CHANGE handler.
10847 HideCellEditControl();
10848 ShowCellEditControl(); // will reread data from table
10853 // ----------------------------------------------------------------------------
10854 // block, row and column selection
10855 // ----------------------------------------------------------------------------
10857 void wxGrid::SelectRow( int row
, bool addToSelected
)
10859 if ( IsSelection() && !addToSelected
)
10863 m_selection
->SelectRow( row
, false, addToSelected
);
10866 void wxGrid::SelectCol( int col
, bool addToSelected
)
10868 if ( IsSelection() && !addToSelected
)
10872 m_selection
->SelectCol( col
, false, addToSelected
);
10875 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10876 bool addToSelected
)
10878 if ( IsSelection() && !addToSelected
)
10882 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10883 false, addToSelected
);
10886 void wxGrid::SelectAll()
10888 if ( m_numRows
> 0 && m_numCols
> 0 )
10891 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10895 // ----------------------------------------------------------------------------
10896 // cell, row and col deselection
10897 // ----------------------------------------------------------------------------
10899 void wxGrid::DeselectRow( int row
)
10901 if ( !m_selection
)
10904 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10906 if ( m_selection
->IsInSelection(row
, 0 ) )
10907 m_selection
->ToggleCellSelection(row
, 0);
10911 int nCols
= GetNumberCols();
10912 for ( int i
= 0; i
< nCols
; i
++ )
10914 if ( m_selection
->IsInSelection(row
, i
) )
10915 m_selection
->ToggleCellSelection(row
, i
);
10920 void wxGrid::DeselectCol( int col
)
10922 if ( !m_selection
)
10925 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10927 if ( m_selection
->IsInSelection(0, col
) )
10928 m_selection
->ToggleCellSelection(0, col
);
10932 int nRows
= GetNumberRows();
10933 for ( int i
= 0; i
< nRows
; i
++ )
10935 if ( m_selection
->IsInSelection(i
, col
) )
10936 m_selection
->ToggleCellSelection(i
, col
);
10941 void wxGrid::DeselectCell( int row
, int col
)
10943 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10944 m_selection
->ToggleCellSelection(row
, col
);
10947 bool wxGrid::IsSelection() const
10949 return ( m_selection
&& (m_selection
->IsSelection() ||
10950 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10951 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10954 bool wxGrid::IsInSelection( int row
, int col
) const
10956 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10957 ( row
>= m_selectingTopLeft
.GetRow() &&
10958 col
>= m_selectingTopLeft
.GetCol() &&
10959 row
<= m_selectingBottomRight
.GetRow() &&
10960 col
<= m_selectingBottomRight
.GetCol() )) );
10963 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10967 wxGridCellCoordsArray a
;
10971 return m_selection
->m_cellSelection
;
10974 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10978 wxGridCellCoordsArray a
;
10982 return m_selection
->m_blockSelectionTopLeft
;
10985 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10989 wxGridCellCoordsArray a
;
10993 return m_selection
->m_blockSelectionBottomRight
;
10996 wxArrayInt
wxGrid::GetSelectedRows() const
11004 return m_selection
->m_rowSelection
;
11007 wxArrayInt
wxGrid::GetSelectedCols() const
11015 return m_selection
->m_colSelection
;
11018 void wxGrid::ClearSelection()
11020 m_selectingTopLeft
=
11021 m_selectingBottomRight
=
11022 m_selectingKeyboard
= wxGridNoCellCoords
;
11024 m_selection
->ClearSelection();
11027 // This function returns the rectangle that encloses the given block
11028 // in device coords clipped to the client size of the grid window.
11030 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
11031 const wxGridCellCoords
& bottomRight
) const
11034 wxRect tempCellRect
= CellToRect(topLeft
);
11035 if ( tempCellRect
!= wxGridNoCellRect
)
11037 resultRect
= tempCellRect
;
11041 resultRect
= wxRect(0, 0, 0, 0);
11044 tempCellRect
= CellToRect(bottomRight
);
11045 if ( tempCellRect
!= wxGridNoCellRect
)
11047 resultRect
+= tempCellRect
;
11051 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
11052 return wxGridNoCellRect
;
11055 // Ensure that left/right and top/bottom pairs are in order.
11056 int left
= resultRect
.GetLeft();
11057 int top
= resultRect
.GetTop();
11058 int right
= resultRect
.GetRight();
11059 int bottom
= resultRect
.GetBottom();
11061 int leftCol
= topLeft
.GetCol();
11062 int topRow
= topLeft
.GetRow();
11063 int rightCol
= bottomRight
.GetCol();
11064 int bottomRow
= bottomRight
.GetRow();
11073 leftCol
= rightCol
;
11084 topRow
= bottomRow
;
11088 // The following loop is ONLY necessary to detect and handle merged cells.
11090 m_gridWin
->GetClientSize( &cw
, &ch
);
11092 // Get the origin coordinates: notice that they will be negative if the
11093 // grid is scrolled downwards/to the right.
11094 int gridOriginX
= 0;
11095 int gridOriginY
= 0;
11096 CalcScrolledPosition(gridOriginX
, gridOriginY
, &gridOriginX
, &gridOriginY
);
11098 int onScreenLeftmostCol
= internalXToCol(-gridOriginX
);
11099 int onScreenUppermostRow
= internalYToRow(-gridOriginY
);
11101 int onScreenRightmostCol
= internalXToCol(-gridOriginX
+ cw
);
11102 int onScreenBottommostRow
= internalYToRow(-gridOriginY
+ ch
);
11104 // Bound our loop so that we only examine the portion of the selected block
11105 // that is shown on screen. Therefore, we compare the Top-Left block values
11106 // to the Top-Left screen values, and the Bottom-Right block values to the
11107 // Bottom-Right screen values, choosing appropriately.
11108 const int visibleTopRow
= wxMax(topRow
, onScreenUppermostRow
);
11109 const int visibleBottomRow
= wxMin(bottomRow
, onScreenBottommostRow
);
11110 const int visibleLeftCol
= wxMax(leftCol
, onScreenLeftmostCol
);
11111 const int visibleRightCol
= wxMin(rightCol
, onScreenRightmostCol
);
11113 for ( int j
= visibleTopRow
; j
<= visibleBottomRow
; j
++ )
11115 for ( int i
= visibleLeftCol
; i
<= visibleRightCol
; i
++ )
11117 if ( (j
== visibleTopRow
) || (j
== visibleBottomRow
) ||
11118 (i
== visibleLeftCol
) || (i
== visibleRightCol
) )
11120 tempCellRect
= CellToRect( j
, i
);
11122 if (tempCellRect
.x
< left
)
11123 left
= tempCellRect
.x
;
11124 if (tempCellRect
.y
< top
)
11125 top
= tempCellRect
.y
;
11126 if (tempCellRect
.x
+ tempCellRect
.width
> right
)
11127 right
= tempCellRect
.x
+ tempCellRect
.width
;
11128 if (tempCellRect
.y
+ tempCellRect
.height
> bottom
)
11129 bottom
= tempCellRect
.y
+ tempCellRect
.height
;
11133 i
= visibleRightCol
; // jump over inner cells.
11138 // Convert to scrolled coords
11139 CalcScrolledPosition( left
, top
, &left
, &top
);
11140 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
11142 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
11143 return wxRect(0,0,0,0);
11145 resultRect
.SetLeft( wxMax(0, left
) );
11146 resultRect
.SetTop( wxMax(0, top
) );
11147 resultRect
.SetRight( wxMin(cw
, right
) );
11148 resultRect
.SetBottom( wxMin(ch
, bottom
) );
11153 // ----------------------------------------------------------------------------
11155 // ----------------------------------------------------------------------------
11157 #if wxUSE_DRAG_AND_DROP
11159 // this allow setting drop target directly on wxGrid
11160 void wxGrid::SetDropTarget(wxDropTarget
*dropTarget
)
11162 GetGridWindow()->SetDropTarget(dropTarget
);
11165 #endif // wxUSE_DRAG_AND_DROP
11167 // ----------------------------------------------------------------------------
11168 // grid event classes
11169 // ----------------------------------------------------------------------------
11171 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
11173 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
11174 int row
, int col
, int x
, int y
, bool sel
,
11175 bool control
, bool shift
, bool alt
, bool meta
)
11176 : wxNotifyEvent( type
, id
)
11183 m_control
= control
;
11188 SetEventObject(obj
);
11192 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
11194 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
11195 int rowOrCol
, int x
, int y
,
11196 bool control
, bool shift
, bool alt
, bool meta
)
11197 : wxNotifyEvent( type
, id
)
11199 m_rowOrCol
= rowOrCol
;
11202 m_control
= control
;
11207 SetEventObject(obj
);
11211 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
11213 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
11214 const wxGridCellCoords
& topLeft
,
11215 const wxGridCellCoords
& bottomRight
,
11216 bool sel
, bool control
,
11217 bool shift
, bool alt
, bool meta
)
11218 : wxNotifyEvent( type
, id
)
11220 m_topLeft
= topLeft
;
11221 m_bottomRight
= bottomRight
;
11223 m_control
= control
;
11228 SetEventObject(obj
);
11232 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
11234 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
11235 wxObject
* obj
, int row
,
11236 int col
, wxControl
* ctrl
)
11237 : wxCommandEvent(type
, id
)
11239 SetEventObject(obj
);
11245 #endif // wxUSE_GRID