1 ///////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
5 // Modified by: Robin Dunn, Vadim Zeitlin, Santiago Palacios
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
25 #include "wx/dcclient.h"
26 #include "wx/settings.h"
28 #include "wx/textctrl.h"
29 #include "wx/checkbox.h"
30 #include "wx/combobox.h"
31 #include "wx/valtext.h"
34 #include "wx/listbox.h"
37 #include "wx/textfile.h"
38 #include "wx/spinctrl.h"
39 #include "wx/tokenzr.h"
40 #include "wx/renderer.h"
42 #include "wx/generic/gridsel.h"
44 const wxChar wxGridNameStr
[] = wxT("grid");
46 #if defined(__WXMOTIF__)
47 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
49 #define WXUNUSED_MOTIF(identifier) identifier
52 #if defined(__WXGTK__)
53 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
55 #define WXUNUSED_GTK(identifier) identifier
58 // Required for wxIs... functions
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr
*, wxArrayAttrs
,
66 class WXDLLIMPEXP_ADV
);
68 struct wxGridCellWithAttr
70 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
71 : coords(row
, col
), attr(attr_
)
80 wxGridCellCoords coords
;
84 // DECLARE_NO_COPY_CLASS(wxGridCellWithAttr)
85 // without rewriting the macros, which require a public copy constructor.
88 WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr
, wxGridCellWithAttrArray
,
89 class WXDLLIMPEXP_ADV
);
91 #include "wx/arrimpl.cpp"
93 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
94 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
100 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
101 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
102 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
103 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
104 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_BEGIN_DRAG
)
105 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
106 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
107 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
108 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
109 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
110 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
111 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_MOVE
)
112 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
113 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
114 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
115 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
116 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
117 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
119 // ----------------------------------------------------------------------------
121 // ----------------------------------------------------------------------------
123 class WXDLLIMPEXP_ADV wxGridRowLabelWindow
: public wxWindow
126 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
127 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
128 const wxPoint
&pos
, const wxSize
&size
);
133 void OnPaint( wxPaintEvent
& event
);
134 void OnMouseEvent( wxMouseEvent
& event
);
135 void OnMouseWheel( wxMouseEvent
& event
);
136 void OnKeyDown( wxKeyEvent
& event
);
137 void OnKeyUp( wxKeyEvent
& );
138 void OnChar( wxKeyEvent
& );
140 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
141 DECLARE_EVENT_TABLE()
142 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow
)
146 class WXDLLIMPEXP_ADV wxGridColLabelWindow
: public wxWindow
149 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
150 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
151 const wxPoint
&pos
, const wxSize
&size
);
156 void OnPaint( wxPaintEvent
& event
);
157 void OnMouseEvent( wxMouseEvent
& event
);
158 void OnMouseWheel( wxMouseEvent
& event
);
159 void OnKeyDown( wxKeyEvent
& event
);
160 void OnKeyUp( wxKeyEvent
& );
161 void OnChar( wxKeyEvent
& );
163 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
164 DECLARE_EVENT_TABLE()
165 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow
)
169 class WXDLLIMPEXP_ADV wxGridCornerLabelWindow
: public wxWindow
172 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
173 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
174 const wxPoint
&pos
, const wxSize
&size
);
179 void OnMouseEvent( wxMouseEvent
& event
);
180 void OnMouseWheel( wxMouseEvent
& event
);
181 void OnKeyDown( wxKeyEvent
& event
);
182 void OnKeyUp( wxKeyEvent
& );
183 void OnChar( wxKeyEvent
& );
184 void OnPaint( wxPaintEvent
& event
);
186 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
187 DECLARE_EVENT_TABLE()
188 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow
)
191 class WXDLLIMPEXP_ADV wxGridWindow
: public wxWindow
197 m_rowLabelWin
= NULL
;
198 m_colLabelWin
= NULL
;
201 wxGridWindow( wxGrid
*parent
,
202 wxGridRowLabelWindow
*rowLblWin
,
203 wxGridColLabelWindow
*colLblWin
,
204 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
205 virtual ~wxGridWindow() {}
207 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
209 wxGrid
* GetOwner() { return m_owner
; }
213 wxGridRowLabelWindow
*m_rowLabelWin
;
214 wxGridColLabelWindow
*m_colLabelWin
;
216 void OnPaint( wxPaintEvent
&event
);
217 void OnMouseWheel( wxMouseEvent
& event
);
218 void OnMouseEvent( wxMouseEvent
& event
);
219 void OnKeyDown( wxKeyEvent
& );
220 void OnKeyUp( wxKeyEvent
& );
221 void OnChar( wxKeyEvent
& );
222 void OnEraseBackground( wxEraseEvent
& );
223 void OnFocus( wxFocusEvent
& );
225 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
226 DECLARE_EVENT_TABLE()
227 DECLARE_NO_COPY_CLASS(wxGridWindow
)
231 class wxGridCellEditorEvtHandler
: public wxEvtHandler
234 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
241 void OnKillFocus(wxFocusEvent
& event
);
242 void OnKeyDown(wxKeyEvent
& event
);
243 void OnChar(wxKeyEvent
& event
);
245 void SetInSetFocus(bool inSetFocus
) { m_inSetFocus
= inSetFocus
; }
249 wxGridCellEditor
*m_editor
;
251 // Work around the fact that a focus kill event can be sent to
252 // a combobox within a set focus event.
255 DECLARE_EVENT_TABLE()
256 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
257 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler
)
261 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler
, wxEvtHandler
)
263 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
264 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus
)
265 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
266 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
270 // ----------------------------------------------------------------------------
271 // the internal data representation used by wxGridCellAttrProvider
272 // ----------------------------------------------------------------------------
274 // this class stores attributes set for cells
275 class WXDLLIMPEXP_ADV wxGridCellAttrData
278 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
279 wxGridCellAttr
*GetAttr(int row
, int col
) const;
280 void UpdateAttrRows( size_t pos
, int numRows
);
281 void UpdateAttrCols( size_t pos
, int numCols
);
284 // searches for the attr for given cell, returns wxNOT_FOUND if not found
285 int FindIndex(int row
, int col
) const;
287 wxGridCellWithAttrArray m_attrs
;
290 // this class stores attributes set for rows or columns
291 class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
294 // empty ctor to suppress warnings
295 wxGridRowOrColAttrData() {}
296 ~wxGridRowOrColAttrData();
298 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
299 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
300 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
303 wxArrayInt m_rowsOrCols
;
304 wxArrayAttrs m_attrs
;
307 // NB: this is just a wrapper around 3 objects: one which stores cell
308 // attributes, and 2 others for row/col ones
309 class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
312 wxGridCellAttrData m_cellAttrs
;
313 wxGridRowOrColAttrData m_rowAttrs
,
318 // ----------------------------------------------------------------------------
319 // data structures used for the data type registry
320 // ----------------------------------------------------------------------------
322 struct wxGridDataTypeInfo
324 wxGridDataTypeInfo(const wxString
& typeName
,
325 wxGridCellRenderer
* renderer
,
326 wxGridCellEditor
* editor
)
327 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
330 ~wxGridDataTypeInfo()
332 wxSafeDecRef(m_renderer
);
333 wxSafeDecRef(m_editor
);
337 wxGridCellRenderer
* m_renderer
;
338 wxGridCellEditor
* m_editor
;
340 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo
)
344 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
,
345 class WXDLLIMPEXP_ADV
);
348 class WXDLLIMPEXP_ADV wxGridTypeRegistry
351 wxGridTypeRegistry() {}
352 ~wxGridTypeRegistry();
354 void RegisterDataType(const wxString
& typeName
,
355 wxGridCellRenderer
* renderer
,
356 wxGridCellEditor
* editor
);
358 // find one of already registered data types
359 int FindRegisteredDataType(const wxString
& typeName
);
361 // try to FindRegisteredDataType(), if this fails and typeName is one of
362 // standard typenames, register it and return its index
363 int FindDataType(const wxString
& typeName
);
365 // try to FindDataType(), if it fails see if it is not one of already
366 // registered data types with some params in which case clone the
367 // registered data type and set params for it
368 int FindOrCloneDataType(const wxString
& typeName
);
370 wxGridCellRenderer
* GetRenderer(int index
);
371 wxGridCellEditor
* GetEditor(int index
);
374 wxGridDataTypeInfoArray m_typeinfo
;
378 // ----------------------------------------------------------------------------
379 // conditional compilation
380 // ----------------------------------------------------------------------------
382 #ifndef WXGRID_DRAW_LINES
383 #define WXGRID_DRAW_LINES 1
386 // ----------------------------------------------------------------------------
388 // ----------------------------------------------------------------------------
390 //#define DEBUG_ATTR_CACHE
391 #ifdef DEBUG_ATTR_CACHE
392 static size_t gs_nAttrCacheHits
= 0;
393 static size_t gs_nAttrCacheMisses
= 0;
396 // ----------------------------------------------------------------------------
398 // ----------------------------------------------------------------------------
400 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
401 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
404 // TODO: this doesn't work at all, grid cells have different sizes and approx
405 // calculations don't work as because of the size mismatch scrollbars
406 // sometimes fail to be shown when they should be or vice versa
408 // The scroll bars may be a little flakey once in a while, but that is
409 // surely much less horrible than having scroll lines of only 1!!!
412 // Well, it's still seriously broken so it might be better but needs
415 static const size_t GRID_SCROLL_LINE_X
= 15; // 1;
416 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
418 // the size of hash tables used a bit everywhere (the max number of elements
419 // in these hash tables is the number of rows/columns)
420 static const int GRID_HASH_SIZE
= 100;
423 // ----------------------------------------------------------------------------
425 // ----------------------------------------------------------------------------
427 static inline int GetScrollX(int x
)
429 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
432 static inline int GetScrollY(int y
)
434 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
438 // ============================================================================
440 // ============================================================================
442 // ----------------------------------------------------------------------------
444 // ----------------------------------------------------------------------------
446 wxGridCellEditor::wxGridCellEditor()
452 wxGridCellEditor::~wxGridCellEditor()
457 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
458 wxWindowID
WXUNUSED(id
),
459 wxEvtHandler
* evtHandler
)
462 m_control
->PushEventHandler(evtHandler
);
465 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
466 wxGridCellAttr
*attr
)
468 // erase the background because we might not fill the cell
469 wxClientDC
dc(m_control
->GetParent());
470 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
472 gridWindow
->GetOwner()->PrepareDC(dc
);
474 dc
.SetPen(*wxTRANSPARENT_PEN
);
475 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
476 dc
.DrawRectangle(rectCell
);
478 // redraw the control we just painted over
479 m_control
->Refresh();
482 void wxGridCellEditor::Destroy()
486 m_control
->PopEventHandler( true /* delete it*/ );
488 m_control
->Destroy();
493 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
495 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
497 m_control
->Show(show
);
501 // set the colours/fonts if we have any
504 m_colFgOld
= m_control
->GetForegroundColour();
505 m_control
->SetForegroundColour(attr
->GetTextColour());
507 m_colBgOld
= m_control
->GetBackgroundColour();
508 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
510 // Workaround for GTK+1 font setting problem on some platforms
511 #if !defined(__WXGTK__) || defined(__WXGTK20__)
512 m_fontOld
= m_control
->GetFont();
513 m_control
->SetFont(attr
->GetFont());
516 // can't do anything more in the base class version, the other
517 // attributes may only be used by the derived classes
522 // restore the standard colours fonts
523 if ( m_colFgOld
.Ok() )
525 m_control
->SetForegroundColour(m_colFgOld
);
526 m_colFgOld
= wxNullColour
;
529 if ( m_colBgOld
.Ok() )
531 m_control
->SetBackgroundColour(m_colBgOld
);
532 m_colBgOld
= wxNullColour
;
535 // Workaround for GTK+1 font setting problem on some platforms
536 #if !defined(__WXGTK__) || defined(__WXGTK20__)
537 if ( m_fontOld
.Ok() )
539 m_control
->SetFont(m_fontOld
);
540 m_fontOld
= wxNullFont
;
546 void wxGridCellEditor::SetSize(const wxRect
& rect
)
548 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
550 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
553 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
558 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
560 bool ctrl
= event
.ControlDown();
561 bool alt
= event
.AltDown();
564 // On the Mac the Alt key is more like shift and is used for entry of
565 // valid characters, so check for Ctrl and Meta instead.
566 alt
= event
.MetaDown();
569 // Assume it's not a valid char if ctrl or alt is down, but if both are
570 // down then it may be because of an AltGr key combination, so let them
571 // through in that case.
572 if ((ctrl
|| alt
) && !(ctrl
&& alt
))
579 // If it's a F-Key or other special key then it shouldn't start the
581 if (event
.GetKeyCode() >= WXK_START
)
585 // if the unicode key code is not really a unicode character (it may
586 // be a function key or etc., the platforms appear to always give us a
587 // small value in this case) then fallback to the ASCII key code but
588 // don't do anything for function keys or etc.
589 key
= event
.GetUnicodeKey();
592 key
= event
.GetKeyCode();
593 keyOk
= (key
<= 127);
596 key
= event
.GetKeyCode();
597 keyOk
= (key
<= 255);
603 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
608 void wxGridCellEditor::StartingClick()
614 // ----------------------------------------------------------------------------
615 // wxGridCellTextEditor
616 // ----------------------------------------------------------------------------
618 wxGridCellTextEditor::wxGridCellTextEditor()
623 void wxGridCellTextEditor::Create(wxWindow
* parent
,
625 wxEvtHandler
* evtHandler
)
627 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
628 wxDefaultPosition
, wxDefaultSize
629 #if defined(__WXMSW__)
630 , wxTE_PROCESS_TAB
| wxTE_AUTO_SCROLL
| wxNO_BORDER
634 // set max length allowed in the textctrl, if the parameter was set
637 ((wxTextCtrl
*)m_control
)->SetMaxLength(m_maxChars
);
640 wxGridCellEditor::Create(parent
, id
, evtHandler
);
643 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
644 wxGridCellAttr
* WXUNUSED(attr
))
646 // as we fill the entire client area,
647 // don't do anything here to minimize flicker
650 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
652 wxRect
rect(rectOrig
);
654 // Make the edit control large enough to allow for internal margins
656 // TODO: remove this if the text ctrl sizing is improved esp. for unix
658 #if defined(__WXGTK__)
666 #elif defined(__WXMSW__)
680 int extra_x
= ( rect
.x
> 2 ) ? 2 : 1;
681 int extra_y
= ( rect
.y
> 2 ) ? 2 : 1;
683 #if defined(__WXMOTIF__)
688 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
689 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
690 rect
.SetRight( rect
.GetRight() + 2 * extra_x
);
691 rect
.SetBottom( rect
.GetBottom() + 2 * extra_y
);
694 wxGridCellEditor::SetSize(rect
);
697 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
699 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
701 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
703 DoBeginEdit(m_startValue
);
706 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
708 Text()->SetValue(startValue
);
709 Text()->SetInsertionPointEnd();
710 Text()->SetSelection(-1, -1);
714 bool wxGridCellTextEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
716 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
718 bool changed
= false;
719 wxString value
= Text()->GetValue();
720 if (value
!= m_startValue
)
724 grid
->GetTable()->SetValue(row
, col
, value
);
726 m_startValue
= wxEmptyString
;
728 // No point in setting the text of the hidden control
729 //Text()->SetValue(m_startValue);
734 void wxGridCellTextEditor::Reset()
736 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
738 DoReset(m_startValue
);
741 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
743 Text()->SetValue(startValue
);
744 Text()->SetInsertionPointEnd();
747 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
749 return wxGridCellEditor::IsAcceptedKey(event
);
752 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
754 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
755 // longer an appropriate way to get the character into the text control.
756 // Do it ourselves instead. We know that if we get this far that we have
757 // a valid character, so not a whole lot of testing needs to be done.
759 wxTextCtrl
* tc
= Text();
764 ch
= event
.GetUnicodeKey();
766 ch
= (wxChar
)event
.GetKeyCode();
768 ch
= (wxChar
)event
.GetKeyCode();
774 // delete the character at the cursor
775 pos
= tc
->GetInsertionPoint();
776 if (pos
< tc
->GetLastPosition())
777 tc
->Remove(pos
, pos
+ 1);
781 // delete the character before the cursor
782 pos
= tc
->GetInsertionPoint();
784 tc
->Remove(pos
- 1, pos
);
793 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
794 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
796 #if defined(__WXMOTIF__) || defined(__WXGTK__)
797 // wxMotif needs a little extra help...
798 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
799 wxString
s( Text()->GetValue() );
800 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
802 Text()->SetInsertionPoint( pos
);
804 // the other ports can handle a Return key press
810 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
820 if ( params
.ToLong(&tmp
) )
822 m_maxChars
= (size_t)tmp
;
826 wxLogDebug( _T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str() );
831 // return the value in the text control
832 wxString
wxGridCellTextEditor::GetValue() const
834 return Text()->GetValue();
837 // ----------------------------------------------------------------------------
838 // wxGridCellNumberEditor
839 // ----------------------------------------------------------------------------
841 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
847 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
849 wxEvtHandler
* evtHandler
)
854 // create a spin ctrl
855 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
856 wxDefaultPosition
, wxDefaultSize
,
860 wxGridCellEditor::Create(parent
, id
, evtHandler
);
865 // just a text control
866 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
869 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
874 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
876 // first get the value
877 wxGridTableBase
*table
= grid
->GetTable();
878 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
880 m_valueOld
= table
->GetValueAsLong(row
, col
);
885 wxString sValue
= table
->GetValue(row
, col
);
886 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty())
888 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
896 Spin()->SetValue((int)m_valueOld
);
902 DoBeginEdit(GetString());
906 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
916 value
= Spin()->GetValue();
917 changed
= value
!= m_valueOld
;
919 text
= wxString::Format(wxT("%ld"), value
);
924 text
= Text()->GetValue();
925 changed
= (text
.empty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
930 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
931 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
933 grid
->GetTable()->SetValue(row
, col
, text
);
939 void wxGridCellNumberEditor::Reset()
944 Spin()->SetValue((int)m_valueOld
);
949 DoReset(GetString());
953 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
955 if ( wxGridCellEditor::IsAcceptedKey(event
) )
957 int keycode
= event
.GetKeyCode();
958 if ( (keycode
< 128) &&
959 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
968 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
970 int keycode
= event
.GetKeyCode();
973 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
975 wxGridCellTextEditor::StartingKey(event
);
984 if ( wxIsdigit(keycode
) )
986 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
987 spin
->SetValue(keycode
- '0');
988 spin
->SetSelection(1,1);
997 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
1008 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1012 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1016 // skip the error message below
1021 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1025 // return the value in the spin control if it is there (the text control otherwise)
1026 wxString
wxGridCellNumberEditor::GetValue() const
1033 long value
= Spin()->GetValue();
1034 s
.Printf(wxT("%ld"), value
);
1039 s
= Text()->GetValue();
1045 // ----------------------------------------------------------------------------
1046 // wxGridCellFloatEditor
1047 // ----------------------------------------------------------------------------
1049 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1052 m_precision
= precision
;
1055 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1057 wxEvtHandler
* evtHandler
)
1059 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1061 #if wxUSE_VALIDATORS
1062 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1066 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1068 // first get the value
1069 wxGridTableBase
*table
= grid
->GetTable();
1070 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1072 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1077 wxString sValue
= table
->GetValue(row
, col
);
1078 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.empty())
1080 wxFAIL_MSG( _T("this cell doesn't have float value") );
1085 DoBeginEdit(GetString());
1088 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1092 wxString
text(Text()->GetValue());
1094 if ( (text
.empty() || text
.ToDouble(&value
)) &&
1095 !wxIsSameDouble(value
, m_valueOld
) )
1097 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1098 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1100 grid
->GetTable()->SetValue(row
, col
, text
);
1108 void wxGridCellFloatEditor::Reset()
1110 DoReset(GetString());
1113 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1115 int keycode
= event
.GetKeyCode();
1117 tmpbuf
[0] = (char) keycode
;
1119 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1122 bool is_decimal_point
= ( strbuf
==
1123 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1125 bool is_decimal_point
= ( strbuf
== _T(".") );
1128 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1129 || is_decimal_point
)
1131 wxGridCellTextEditor::StartingKey(event
);
1133 // skip Skip() below
1140 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1151 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1155 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1157 m_precision
= (int)tmp
;
1159 // skip the error message below
1164 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1168 wxString
wxGridCellFloatEditor::GetString() const
1171 if ( m_precision
== -1 && m_width
!= -1)
1173 // default precision
1174 fmt
.Printf(_T("%%%d.f"), m_width
);
1176 else if ( m_precision
!= -1 && m_width
== -1)
1179 fmt
.Printf(_T("%%.%df"), m_precision
);
1181 else if ( m_precision
!= -1 && m_width
!= -1 )
1183 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1187 // default width/precision
1191 return wxString::Format(fmt
, m_valueOld
);
1194 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1196 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1198 const int keycode
= event
.GetKeyCode();
1199 if ( isascii(keycode
) )
1202 tmpbuf
[0] = (char) keycode
;
1204 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1207 const wxString decimalPoint
=
1208 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
1210 const wxString
decimalPoint(_T('.'));
1213 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1214 if ( wxIsdigit(keycode
) ||
1215 tolower(keycode
) == 'e' ||
1216 keycode
== decimalPoint
||
1228 #endif // wxUSE_TEXTCTRL
1232 // ----------------------------------------------------------------------------
1233 // wxGridCellBoolEditor
1234 // ----------------------------------------------------------------------------
1236 // the default values for GetValue()
1237 wxString
wxGridCellBoolEditor::ms_stringValues
[2] = { _T(""), _T("1") };
1239 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1241 wxEvtHandler
* evtHandler
)
1243 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1244 wxDefaultPosition
, wxDefaultSize
,
1247 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1250 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1252 bool resize
= false;
1253 wxSize size
= m_control
->GetSize();
1254 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1256 // check if the checkbox is not too big/small for this cell
1257 wxSize sizeBest
= m_control
->GetBestSize();
1258 if ( !(size
== sizeBest
) )
1260 // reset to default size if it had been made smaller
1266 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1268 // leave 1 pixel margin
1269 size
.x
= size
.y
= minSize
- 2;
1276 m_control
->SetSize(size
);
1279 // position it in the centre of the rectangle (TODO: support alignment?)
1281 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1282 // the checkbox without label still has some space to the right in wxGTK,
1283 // so shift it to the right
1285 #elif defined(__WXMSW__)
1286 // here too, but in other way
1291 int hAlign
= wxALIGN_CENTRE
;
1292 int vAlign
= wxALIGN_CENTRE
;
1294 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1297 if (hAlign
== wxALIGN_LEFT
)
1305 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1307 else if (hAlign
== wxALIGN_RIGHT
)
1309 x
= r
.x
+ r
.width
- size
.x
- 2;
1310 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1312 else if (hAlign
== wxALIGN_CENTRE
)
1314 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1315 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1318 m_control
->Move(x
, y
);
1321 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1323 m_control
->Show(show
);
1327 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1328 CBox()->SetBackgroundColour(colBg
);
1332 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1334 wxASSERT_MSG(m_control
,
1335 wxT("The wxGridCellEditor must be created first!"));
1337 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1339 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1343 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1345 if ( cellval
== ms_stringValues
[false] )
1346 m_startValue
= false;
1347 else if ( cellval
== ms_stringValues
[true] )
1348 m_startValue
= true;
1351 // do not try to be smart here and convert it to true or false
1352 // because we'll still overwrite it with something different and
1353 // this risks to be very surprising for the user code, let them
1355 wxFAIL_MSG( _T("invalid value for a cell with bool editor!") );
1359 CBox()->SetValue(m_startValue
);
1363 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1366 wxASSERT_MSG(m_control
,
1367 wxT("The wxGridCellEditor must be created first!"));
1369 bool changed
= false;
1370 bool value
= CBox()->GetValue();
1371 if ( value
!= m_startValue
)
1376 wxGridTableBase
* const table
= grid
->GetTable();
1377 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1378 table
->SetValueAsBool(row
, col
, value
);
1380 table
->SetValue(row
, col
, GetValue());
1386 void wxGridCellBoolEditor::Reset()
1388 wxASSERT_MSG(m_control
,
1389 wxT("The wxGridCellEditor must be created first!"));
1391 CBox()->SetValue(m_startValue
);
1394 void wxGridCellBoolEditor::StartingClick()
1396 CBox()->SetValue(!CBox()->GetValue());
1399 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1401 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1403 int keycode
= event
.GetKeyCode();
1416 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1418 int keycode
= event
.GetKeyCode();
1422 CBox()->SetValue(!CBox()->GetValue());
1426 CBox()->SetValue(true);
1430 CBox()->SetValue(false);
1435 wxString
wxGridCellBoolEditor::GetValue() const
1437 return ms_stringValues
[CBox()->GetValue()];
1441 wxGridCellBoolEditor::UseStringValues(const wxString
& valueTrue
,
1442 const wxString
& valueFalse
)
1444 ms_stringValues
[false] = valueFalse
;
1445 ms_stringValues
[true] = valueTrue
;
1449 wxGridCellBoolEditor::IsTrueValue(const wxString
& value
)
1451 return value
== ms_stringValues
[true];
1454 #endif // wxUSE_CHECKBOX
1458 // ----------------------------------------------------------------------------
1459 // wxGridCellChoiceEditor
1460 // ----------------------------------------------------------------------------
1462 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1464 : m_choices(choices
),
1465 m_allowOthers(allowOthers
) { }
1467 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1468 const wxString choices
[],
1470 : m_allowOthers(allowOthers
)
1474 m_choices
.Alloc(count
);
1475 for ( size_t n
= 0; n
< count
; n
++ )
1477 m_choices
.Add(choices
[n
]);
1482 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1484 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1485 editor
->m_allowOthers
= m_allowOthers
;
1486 editor
->m_choices
= m_choices
;
1491 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1493 wxEvtHandler
* evtHandler
)
1495 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1496 wxDefaultPosition
, wxDefaultSize
,
1498 m_allowOthers
? 0 : wxCB_READONLY
);
1500 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1503 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1504 wxGridCellAttr
* attr
)
1506 // as we fill the entire client area, don't do anything here to minimize
1509 // TODO: It doesn't actually fill the client area since the height of a
1510 // combo always defaults to the standard. Until someone has time to
1511 // figure out the right rectangle to paint, just do it the normal way.
1512 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1515 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1517 wxASSERT_MSG(m_control
,
1518 wxT("The wxGridCellEditor must be created first!"));
1520 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1522 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1524 // Don't immediately end if we get a kill focus event within BeginEdit
1526 evtHandler
->SetInSetFocus(true);
1528 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1532 Combo()->SetValue(m_startValue
);
1536 // find the right position, or default to the first if not found
1537 int pos
= Combo()->FindString(m_startValue
);
1538 if (pos
== wxNOT_FOUND
)
1540 Combo()->SetSelection(pos
);
1543 Combo()->SetInsertionPointEnd();
1544 Combo()->SetFocus();
1548 // When dropping down the menu, a kill focus event
1549 // happens after this point, so we can't reset the flag yet.
1550 #if !defined(__WXGTK20__)
1551 evtHandler
->SetInSetFocus(false);
1556 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1559 wxString value
= Combo()->GetValue();
1560 if ( value
== m_startValue
)
1563 grid
->GetTable()->SetValue(row
, col
, value
);
1568 void wxGridCellChoiceEditor::Reset()
1570 Combo()->SetValue(m_startValue
);
1571 Combo()->SetInsertionPointEnd();
1574 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1584 wxStringTokenizer
tk(params
, _T(','));
1585 while ( tk
.HasMoreTokens() )
1587 m_choices
.Add(tk
.GetNextToken());
1591 // return the value in the text control
1592 wxString
wxGridCellChoiceEditor::GetValue() const
1594 return Combo()->GetValue();
1597 #endif // wxUSE_COMBOBOX
1599 // ----------------------------------------------------------------------------
1600 // wxGridCellEditorEvtHandler
1601 // ----------------------------------------------------------------------------
1603 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
1605 // Don't disable the cell if we're just starting to edit it
1610 m_grid
->DisableCellEditControl();
1615 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1617 switch ( event
.GetKeyCode() )
1621 m_grid
->DisableCellEditControl();
1625 m_grid
->GetEventHandler()->ProcessEvent( event
);
1629 case WXK_NUMPAD_ENTER
:
1630 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1631 m_editor
->HandleReturn(event
);
1640 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1642 int row
= m_grid
->GetGridCursorRow();
1643 int col
= m_grid
->GetGridCursorCol();
1644 wxRect rect
= m_grid
->CellToRect( row
, col
);
1646 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
1648 // if cell width is smaller than grid client area, cell is wholly visible
1649 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
1651 switch ( event
.GetKeyCode() )
1656 case WXK_NUMPAD_ENTER
:
1661 if ( wholeCellVisible
)
1663 // no special processing needed...
1668 // do special processing for partly visible cell...
1670 // get the widths of all cells previous to this one
1672 for ( int i
= 0; i
< col
; i
++ )
1674 colXPos
+= m_grid
->GetColSize(i
);
1677 int xUnit
= 1, yUnit
= 1;
1678 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1681 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1685 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
1693 if ( wholeCellVisible
)
1695 // no special processing needed...
1700 // do special processing for partly visible cell...
1703 wxString value
= m_grid
->GetCellValue(row
, col
);
1704 if ( wxEmptyString
!= value
)
1706 // get width of cell CONTENTS (text)
1708 wxFont font
= m_grid
->GetCellFont(row
, col
);
1709 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
1711 // try to RIGHT align the text by scrolling
1712 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
1714 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
1715 // otherwise the last part of the cell content might be hidden below the scroll bar
1716 // FIXME: maybe there is a more suitable correction?
1717 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
1718 if ( textWidth
< 0 )
1724 // get the widths of all cells previous to this one
1726 for ( int i
= 0; i
< col
; i
++ )
1728 colXPos
+= m_grid
->GetColSize(i
);
1731 // and add the (modified) text width of the cell contents
1732 // as we'd like to see the last part of the cell contents
1733 colXPos
+= textWidth
;
1735 int xUnit
= 1, yUnit
= 1;
1736 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1737 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1748 // ----------------------------------------------------------------------------
1749 // wxGridCellWorker is an (almost) empty common base class for
1750 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1751 // ----------------------------------------------------------------------------
1753 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1758 wxGridCellWorker::~wxGridCellWorker()
1762 // ============================================================================
1764 // ============================================================================
1766 // ----------------------------------------------------------------------------
1767 // wxGridCellRenderer
1768 // ----------------------------------------------------------------------------
1770 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1771 wxGridCellAttr
& attr
,
1774 int WXUNUSED(row
), int WXUNUSED(col
),
1777 dc
.SetBackgroundMode( wxSOLID
);
1779 // grey out fields if the grid is disabled
1780 if ( grid
.IsEnabled() )
1784 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1788 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1793 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1796 dc
.SetPen( *wxTRANSPARENT_PEN
);
1797 dc
.DrawRectangle(rect
);
1800 // ----------------------------------------------------------------------------
1801 // wxGridCellStringRenderer
1802 // ----------------------------------------------------------------------------
1804 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
1805 const wxGridCellAttr
& attr
,
1809 dc
.SetBackgroundMode( wxTRANSPARENT
);
1811 // TODO some special colours for attr.IsReadOnly() case?
1813 // different coloured text when the grid is disabled
1814 if ( grid
.IsEnabled() )
1818 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1819 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1823 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1824 dc
.SetTextForeground( attr
.GetTextColour() );
1829 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1830 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
1833 dc
.SetFont( attr
.GetFont() );
1836 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
1838 const wxString
& text
)
1840 wxCoord x
= 0, y
= 0, max_x
= 0;
1841 dc
.SetFont(attr
.GetFont());
1842 wxStringTokenizer
tk(text
, _T('\n'));
1843 while ( tk
.HasMoreTokens() )
1845 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1846 max_x
= wxMax(max_x
, x
);
1849 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1851 return wxSize(max_x
, y
);
1854 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1855 wxGridCellAttr
& attr
,
1859 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1862 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1863 wxGridCellAttr
& attr
,
1865 const wxRect
& rectCell
,
1869 wxRect rect
= rectCell
;
1872 // erase only this cells background, overflow cells should have been erased
1873 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1876 attr
.GetAlignment(&hAlign
, &vAlign
);
1878 int overflowCols
= 0;
1880 if (attr
.GetOverflow())
1882 int cols
= grid
.GetNumberCols();
1883 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1884 int cell_rows
, cell_cols
;
1885 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <= 0
1886 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1888 int i
, c_cols
, c_rows
;
1889 for (i
= col
+cell_cols
; i
< cols
; i
++)
1891 bool is_empty
= true;
1892 for (int j
=row
; j
< row
+ cell_rows
; j
++)
1894 // check w/ anchor cell for multicell block
1895 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1898 if (!grid
.GetTable()->IsEmptyCell(j
+ c_rows
, i
))
1907 rect
.width
+= grid
.GetColSize(i
);
1915 if (rect
.width
>= best_width
)
1919 overflowCols
= i
- col
- cell_cols
+ 1;
1920 if (overflowCols
>= cols
)
1921 overflowCols
= cols
- 1;
1924 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1926 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1928 clip
.x
+= rectCell
.width
;
1929 // draw each overflow cell individually
1930 int col_end
= col
+ cell_cols
+ overflowCols
;
1931 if (col_end
>= grid
.GetNumberCols())
1932 col_end
= grid
.GetNumberCols() - 1;
1933 for (int i
= col
+ cell_cols
; i
<= col_end
; i
++)
1935 clip
.width
= grid
.GetColSize(i
) - 1;
1936 dc
.DestroyClippingRegion();
1937 dc
.SetClippingRegion(clip
);
1939 SetTextColoursAndFont(grid
, attr
, dc
,
1940 grid
.IsInSelection(row
,i
));
1942 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1943 rect
, hAlign
, vAlign
);
1944 clip
.x
+= grid
.GetColSize(i
) - 1;
1950 dc
.DestroyClippingRegion();
1954 // now we only have to draw the text
1955 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1957 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1958 rect
, hAlign
, vAlign
);
1961 // ----------------------------------------------------------------------------
1962 // wxGridCellNumberRenderer
1963 // ----------------------------------------------------------------------------
1965 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
1967 wxGridTableBase
*table
= grid
.GetTable();
1969 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1971 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1975 text
= table
->GetValue(row
, col
);
1981 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1982 wxGridCellAttr
& attr
,
1984 const wxRect
& rectCell
,
1988 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1990 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1992 // draw the text right aligned by default
1994 attr
.GetAlignment(&hAlign
, &vAlign
);
1995 hAlign
= wxALIGN_RIGHT
;
1997 wxRect rect
= rectCell
;
2000 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2003 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
2004 wxGridCellAttr
& attr
,
2008 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2011 // ----------------------------------------------------------------------------
2012 // wxGridCellFloatRenderer
2013 // ----------------------------------------------------------------------------
2015 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
2018 SetPrecision(precision
);
2021 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
2023 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
2024 renderer
->m_width
= m_width
;
2025 renderer
->m_precision
= m_precision
;
2026 renderer
->m_format
= m_format
;
2031 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2033 wxGridTableBase
*table
= grid
.GetTable();
2038 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
2040 val
= table
->GetValueAsDouble(row
, col
);
2045 text
= table
->GetValue(row
, col
);
2046 hasDouble
= text
.ToDouble(&val
);
2053 if ( m_width
== -1 )
2055 if ( m_precision
== -1 )
2057 // default width/precision
2058 m_format
= _T("%f");
2062 m_format
.Printf(_T("%%.%df"), m_precision
);
2065 else if ( m_precision
== -1 )
2067 // default precision
2068 m_format
.Printf(_T("%%%d.f"), m_width
);
2072 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
2076 text
.Printf(m_format
, val
);
2079 //else: text already contains the string
2084 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
2085 wxGridCellAttr
& attr
,
2087 const wxRect
& rectCell
,
2091 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2093 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2095 // draw the text right aligned by default
2097 attr
.GetAlignment(&hAlign
, &vAlign
);
2098 hAlign
= wxALIGN_RIGHT
;
2100 wxRect rect
= rectCell
;
2103 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2106 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
2107 wxGridCellAttr
& attr
,
2111 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2114 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
2118 // reset to defaults
2124 wxString tmp
= params
.BeforeFirst(_T(','));
2128 if ( tmp
.ToLong(&width
) )
2130 SetWidth((int)width
);
2134 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
2138 tmp
= params
.AfterFirst(_T(','));
2142 if ( tmp
.ToLong(&precision
) )
2144 SetPrecision((int)precision
);
2148 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
2154 // ----------------------------------------------------------------------------
2155 // wxGridCellBoolRenderer
2156 // ----------------------------------------------------------------------------
2158 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
2160 // FIXME these checkbox size calculations are really ugly...
2162 // between checkmark and box
2163 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2165 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2166 wxGridCellAttr
& WXUNUSED(attr
),
2171 // compute it only once (no locks for MT safeness in GUI thread...)
2172 if ( !ms_sizeCheckMark
.x
)
2174 // get checkbox size
2175 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2176 wxSize size
= checkbox
->GetBestSize();
2177 wxCoord checkSize
= size
.y
+ 2 * wxGRID_CHECKMARK_MARGIN
;
2179 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
2180 #if defined(__WXGTK__) || defined(__WXMOTIF__)
2181 checkSize
-= size
.y
/ 2;
2186 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2189 return ms_sizeCheckMark
;
2192 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2193 wxGridCellAttr
& attr
,
2199 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2201 // draw a check mark in the centre (ignoring alignment - TODO)
2202 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2204 // don't draw outside the cell
2205 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2206 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2208 // and even leave (at least) 1 pixel margin
2209 size
.x
= size
.y
= minSize
- 2;
2212 // draw a border around checkmark
2214 attr
.GetAlignment(&hAlign
, &vAlign
);
2217 if (hAlign
== wxALIGN_CENTRE
)
2219 rectBorder
.x
= rect
.x
+ rect
.width
/ 2 - size
.x
/ 2;
2220 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2221 rectBorder
.width
= size
.x
;
2222 rectBorder
.height
= size
.y
;
2224 else if (hAlign
== wxALIGN_LEFT
)
2226 rectBorder
.x
= rect
.x
+ 2;
2227 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2228 rectBorder
.width
= size
.x
;
2229 rectBorder
.height
= size
.y
;
2231 else if (hAlign
== wxALIGN_RIGHT
)
2233 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2234 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2235 rectBorder
.width
= size
.x
;
2236 rectBorder
.height
= size
.y
;
2240 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2242 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2246 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2247 value
= wxGridCellBoolEditor::IsTrueValue(cellval
);
2252 wxRect rectMark
= rectBorder
;
2255 // MSW DrawCheckMark() is weird (and should probably be changed...)
2256 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/ 2);
2260 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2263 dc
.SetTextForeground(attr
.GetTextColour());
2264 dc
.DrawCheckMark(rectMark
);
2267 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2268 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2269 dc
.DrawRectangle(rectBorder
);
2272 // ----------------------------------------------------------------------------
2274 // ----------------------------------------------------------------------------
2276 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2280 m_isReadOnly
= Unset
;
2285 m_attrkind
= wxGridCellAttr::Cell
;
2287 m_sizeRows
= m_sizeCols
= 1;
2288 m_overflow
= UnsetOverflow
;
2290 SetDefAttr(attrDefault
);
2293 wxGridCellAttr
*wxGridCellAttr::Clone() const
2295 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2297 if ( HasTextColour() )
2298 attr
->SetTextColour(GetTextColour());
2299 if ( HasBackgroundColour() )
2300 attr
->SetBackgroundColour(GetBackgroundColour());
2302 attr
->SetFont(GetFont());
2303 if ( HasAlignment() )
2304 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2306 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2310 attr
->SetRenderer(m_renderer
);
2311 m_renderer
->IncRef();
2315 attr
->SetEditor(m_editor
);
2320 attr
->SetReadOnly();
2322 attr
->SetOverflow( m_overflow
== Overflow
);
2323 attr
->SetKind( m_attrkind
);
2328 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2330 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2331 SetTextColour(mergefrom
->GetTextColour());
2332 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2333 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2334 if ( !HasFont() && mergefrom
->HasFont() )
2335 SetFont(mergefrom
->GetFont());
2336 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2339 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2340 SetAlignment(hAlign
, vAlign
);
2342 if ( !HasSize() && mergefrom
->HasSize() )
2343 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2345 // Directly access member functions as GetRender/Editor don't just return
2346 // m_renderer/m_editor
2348 // Maybe add support for merge of Render and Editor?
2349 if (!HasRenderer() && mergefrom
->HasRenderer() )
2351 m_renderer
= mergefrom
->m_renderer
;
2352 m_renderer
->IncRef();
2354 if ( !HasEditor() && mergefrom
->HasEditor() )
2356 m_editor
= mergefrom
->m_editor
;
2359 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2360 SetReadOnly(mergefrom
->IsReadOnly());
2362 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2363 SetOverflow(mergefrom
->GetOverflow());
2365 SetDefAttr(mergefrom
->m_defGridAttr
);
2368 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2370 // The size of a cell is normally 1,1
2372 // If this cell is larger (2,2) then this is the top left cell
2373 // the other cells that will be covered (lower right cells) must be
2374 // set to negative or zero values such that
2375 // row + num_rows of the covered cell points to the larger cell (this cell)
2376 // same goes for the col + num_cols.
2378 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2380 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
2381 !((num_rows
<= 0) && (num_cols
> 0)) ||
2382 !((num_rows
== 0) && (num_cols
== 0))),
2383 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2385 m_sizeRows
= num_rows
;
2386 m_sizeCols
= num_cols
;
2389 const wxColour
& wxGridCellAttr::GetTextColour() const
2391 if (HasTextColour())
2395 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2397 return m_defGridAttr
->GetTextColour();
2401 wxFAIL_MSG(wxT("Missing default cell attribute"));
2402 return wxNullColour
;
2406 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2408 if (HasBackgroundColour())
2412 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2414 return m_defGridAttr
->GetBackgroundColour();
2418 wxFAIL_MSG(wxT("Missing default cell attribute"));
2419 return wxNullColour
;
2423 const wxFont
& wxGridCellAttr::GetFont() const
2429 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2431 return m_defGridAttr
->GetFont();
2435 wxFAIL_MSG(wxT("Missing default cell attribute"));
2440 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2449 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2451 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2455 wxFAIL_MSG(wxT("Missing default cell attribute"));
2459 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2462 *num_rows
= m_sizeRows
;
2464 *num_cols
= m_sizeCols
;
2467 // GetRenderer and GetEditor use a slightly different decision path about
2468 // which attribute to use. If a non-default attr object has one then it is
2469 // used, otherwise the default editor or renderer is fetched from the grid and
2470 // used. It should be the default for the data type of the cell. If it is
2471 // NULL (because the table has a type that the grid does not have in its
2472 // registry), then the grid's default editor or renderer is used.
2474 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2476 wxGridCellRenderer
*renderer
= NULL
;
2478 if ( m_renderer
&& this != m_defGridAttr
)
2480 // use the cells renderer if it has one
2481 renderer
= m_renderer
;
2484 else // no non-default cell renderer
2486 // get default renderer for the data type
2489 // GetDefaultRendererForCell() will do IncRef() for us
2490 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2493 if ( renderer
== NULL
)
2495 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2497 // if we still don't have one then use the grid default
2498 // (no need for IncRef() here neither)
2499 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2501 else // default grid attr
2503 // use m_renderer which we had decided not to use initially
2504 renderer
= m_renderer
;
2511 // we're supposed to always find something
2512 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2517 // same as above, except for s/renderer/editor/g
2518 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2520 wxGridCellEditor
*editor
= NULL
;
2522 if ( m_editor
&& this != m_defGridAttr
)
2524 // use the cells editor if it has one
2528 else // no non default cell editor
2530 // get default editor for the data type
2533 // GetDefaultEditorForCell() will do IncRef() for us
2534 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2537 if ( editor
== NULL
)
2539 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2541 // if we still don't have one then use the grid default
2542 // (no need for IncRef() here neither)
2543 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2545 else // default grid attr
2547 // use m_editor which we had decided not to use initially
2555 // we're supposed to always find something
2556 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2561 // ----------------------------------------------------------------------------
2562 // wxGridCellAttrData
2563 // ----------------------------------------------------------------------------
2565 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2567 int n
= FindIndex(row
, col
);
2568 if ( n
== wxNOT_FOUND
)
2570 // add the attribute
2571 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2575 // free the old attribute
2576 m_attrs
[(size_t)n
].attr
->DecRef();
2580 // change the attribute
2581 m_attrs
[(size_t)n
].attr
= attr
;
2585 // remove this attribute
2586 m_attrs
.RemoveAt((size_t)n
);
2591 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2593 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2595 int n
= FindIndex(row
, col
);
2596 if ( n
!= wxNOT_FOUND
)
2598 attr
= m_attrs
[(size_t)n
].attr
;
2605 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2607 size_t count
= m_attrs
.GetCount();
2608 for ( size_t n
= 0; n
< count
; n
++ )
2610 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2611 wxCoord row
= coords
.GetRow();
2612 if ((size_t)row
>= pos
)
2616 // If rows inserted, include row counter where necessary
2617 coords
.SetRow(row
+ numRows
);
2619 else if (numRows
< 0)
2621 // If rows deleted ...
2622 if ((size_t)row
>= pos
- numRows
)
2624 // ...either decrement row counter (if row still exists)...
2625 coords
.SetRow(row
+ numRows
);
2629 // ...or remove the attribute
2630 // No need to DecRef the attribute itself since this is
2631 // done be wxGridCellWithAttr's destructor!
2632 m_attrs
.RemoveAt(n
);
2641 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2643 size_t count
= m_attrs
.GetCount();
2644 for ( size_t n
= 0; n
< count
; n
++ )
2646 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2647 wxCoord col
= coords
.GetCol();
2648 if ( (size_t)col
>= pos
)
2652 // If rows inserted, include row counter where necessary
2653 coords
.SetCol(col
+ numCols
);
2655 else if (numCols
< 0)
2657 // If rows deleted ...
2658 if ((size_t)col
>= pos
- numCols
)
2660 // ...either decrement row counter (if row still exists)...
2661 coords
.SetCol(col
+ numCols
);
2665 // ...or remove the attribute
2666 // No need to DecRef the attribute itself since this is
2667 // done be wxGridCellWithAttr's destructor!
2668 m_attrs
.RemoveAt(n
);
2677 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2679 size_t count
= m_attrs
.GetCount();
2680 for ( size_t n
= 0; n
< count
; n
++ )
2682 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2683 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2692 // ----------------------------------------------------------------------------
2693 // wxGridRowOrColAttrData
2694 // ----------------------------------------------------------------------------
2696 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2698 size_t count
= m_attrs
.Count();
2699 for ( size_t n
= 0; n
< count
; n
++ )
2701 m_attrs
[n
]->DecRef();
2705 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2707 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2709 int n
= m_rowsOrCols
.Index(rowOrCol
);
2710 if ( n
!= wxNOT_FOUND
)
2712 attr
= m_attrs
[(size_t)n
];
2719 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2721 int i
= m_rowsOrCols
.Index(rowOrCol
);
2722 if ( i
== wxNOT_FOUND
)
2724 // add the attribute
2725 m_rowsOrCols
.Add(rowOrCol
);
2730 size_t n
= (size_t)i
;
2733 // change the attribute
2734 m_attrs
[n
]->DecRef();
2739 // remove this attribute
2740 m_attrs
[n
]->DecRef();
2741 m_rowsOrCols
.RemoveAt(n
);
2742 m_attrs
.RemoveAt(n
);
2747 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2749 size_t count
= m_attrs
.GetCount();
2750 for ( size_t n
= 0; n
< count
; n
++ )
2752 int & rowOrCol
= m_rowsOrCols
[n
];
2753 if ( (size_t)rowOrCol
>= pos
)
2755 if ( numRowsOrCols
> 0 )
2757 // If rows inserted, include row counter where necessary
2758 rowOrCol
+= numRowsOrCols
;
2760 else if ( numRowsOrCols
< 0)
2762 // If rows deleted, either decrement row counter (if row still exists)
2763 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2764 rowOrCol
+= numRowsOrCols
;
2767 m_rowsOrCols
.RemoveAt(n
);
2768 m_attrs
[n
]->DecRef();
2769 m_attrs
.RemoveAt(n
);
2778 // ----------------------------------------------------------------------------
2779 // wxGridCellAttrProvider
2780 // ----------------------------------------------------------------------------
2782 wxGridCellAttrProvider::wxGridCellAttrProvider()
2784 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2787 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2792 void wxGridCellAttrProvider::InitData()
2794 m_data
= new wxGridCellAttrProviderData
;
2797 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2798 wxGridCellAttr::wxAttrKind kind
) const
2800 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2805 case (wxGridCellAttr::Any
):
2806 // Get cached merge attributes.
2807 // Currently not used as no cache implemented as not mutable
2808 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2811 // Basically implement old version.
2812 // Also check merge cache, so we don't have to re-merge every time..
2813 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2814 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2815 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2817 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
2819 // Two or more are non NULL
2820 attr
= new wxGridCellAttr
;
2821 attr
->SetKind(wxGridCellAttr::Merged
);
2823 // Order is important..
2826 attr
->MergeWith(attrcell
);
2831 attr
->MergeWith(attrcol
);
2836 attr
->MergeWith(attrrow
);
2840 // store merge attr if cache implemented
2842 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2846 // one or none is non null return it or null.
2865 case (wxGridCellAttr::Cell
):
2866 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2869 case (wxGridCellAttr::Col
):
2870 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2873 case (wxGridCellAttr::Row
):
2874 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2879 // (wxGridCellAttr::Default):
2880 // (wxGridCellAttr::Merged):
2888 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2894 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2897 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2902 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2905 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2910 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2913 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2917 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2919 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2923 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2927 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2929 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2933 // ----------------------------------------------------------------------------
2934 // wxGridTypeRegistry
2935 // ----------------------------------------------------------------------------
2937 wxGridTypeRegistry::~wxGridTypeRegistry()
2939 size_t count
= m_typeinfo
.Count();
2940 for ( size_t i
= 0; i
< count
; i
++ )
2941 delete m_typeinfo
[i
];
2944 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2945 wxGridCellRenderer
* renderer
,
2946 wxGridCellEditor
* editor
)
2948 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2950 // is it already registered?
2951 int loc
= FindRegisteredDataType(typeName
);
2952 if ( loc
!= wxNOT_FOUND
)
2954 delete m_typeinfo
[loc
];
2955 m_typeinfo
[loc
] = info
;
2959 m_typeinfo
.Add(info
);
2963 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2965 size_t count
= m_typeinfo
.GetCount();
2966 for ( size_t i
= 0; i
< count
; i
++ )
2968 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2977 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2979 int index
= FindRegisteredDataType(typeName
);
2980 if ( index
== wxNOT_FOUND
)
2982 // check whether this is one of the standard ones, in which case
2983 // register it "on the fly"
2985 if ( typeName
== wxGRID_VALUE_STRING
)
2987 RegisterDataType(wxGRID_VALUE_STRING
,
2988 new wxGridCellStringRenderer
,
2989 new wxGridCellTextEditor
);
2992 #endif // wxUSE_TEXTCTRL
2994 if ( typeName
== wxGRID_VALUE_BOOL
)
2996 RegisterDataType(wxGRID_VALUE_BOOL
,
2997 new wxGridCellBoolRenderer
,
2998 new wxGridCellBoolEditor
);
3001 #endif // wxUSE_CHECKBOX
3003 if ( typeName
== wxGRID_VALUE_NUMBER
)
3005 RegisterDataType(wxGRID_VALUE_NUMBER
,
3006 new wxGridCellNumberRenderer
,
3007 new wxGridCellNumberEditor
);
3009 else if ( typeName
== wxGRID_VALUE_FLOAT
)
3011 RegisterDataType(wxGRID_VALUE_FLOAT
,
3012 new wxGridCellFloatRenderer
,
3013 new wxGridCellFloatEditor
);
3016 #endif // wxUSE_TEXTCTRL
3018 if ( typeName
== wxGRID_VALUE_CHOICE
)
3020 RegisterDataType(wxGRID_VALUE_CHOICE
,
3021 new wxGridCellStringRenderer
,
3022 new wxGridCellChoiceEditor
);
3025 #endif // wxUSE_COMBOBOX
3030 // we get here only if just added the entry for this type, so return
3032 index
= m_typeinfo
.GetCount() - 1;
3038 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
3040 int index
= FindDataType(typeName
);
3041 if ( index
== wxNOT_FOUND
)
3043 // the first part of the typename is the "real" type, anything after ':'
3044 // are the parameters for the renderer
3045 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
3046 if ( index
== wxNOT_FOUND
)
3051 wxGridCellRenderer
*renderer
= GetRenderer(index
);
3052 wxGridCellRenderer
*rendererOld
= renderer
;
3053 renderer
= renderer
->Clone();
3054 rendererOld
->DecRef();
3056 wxGridCellEditor
*editor
= GetEditor(index
);
3057 wxGridCellEditor
*editorOld
= editor
;
3058 editor
= editor
->Clone();
3059 editorOld
->DecRef();
3061 // do it even if there are no parameters to reset them to defaults
3062 wxString params
= typeName
.AfterFirst(_T(':'));
3063 renderer
->SetParameters(params
);
3064 editor
->SetParameters(params
);
3066 // register the new typename
3067 RegisterDataType(typeName
, renderer
, editor
);
3069 // we just registered it, it's the last one
3070 index
= m_typeinfo
.GetCount() - 1;
3076 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
3078 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
3085 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
3087 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
3094 // ----------------------------------------------------------------------------
3096 // ----------------------------------------------------------------------------
3098 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
3100 wxGridTableBase::wxGridTableBase()
3102 m_view
= (wxGrid
*) NULL
;
3103 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
3106 wxGridTableBase::~wxGridTableBase()
3108 delete m_attrProvider
;
3111 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
3113 delete m_attrProvider
;
3114 m_attrProvider
= attrProvider
;
3117 bool wxGridTableBase::CanHaveAttributes()
3119 if ( ! GetAttrProvider() )
3121 // use the default attr provider by default
3122 SetAttrProvider(new wxGridCellAttrProvider
);
3128 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
3130 if ( m_attrProvider
)
3131 return m_attrProvider
->GetAttr(row
, col
, kind
);
3133 return (wxGridCellAttr
*)NULL
;
3136 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
3138 if ( m_attrProvider
)
3140 attr
->SetKind(wxGridCellAttr::Cell
);
3141 m_attrProvider
->SetAttr(attr
, row
, col
);
3145 // as we take ownership of the pointer and don't store it, we must
3151 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3153 if ( m_attrProvider
)
3155 attr
->SetKind(wxGridCellAttr::Row
);
3156 m_attrProvider
->SetRowAttr(attr
, row
);
3160 // as we take ownership of the pointer and don't store it, we must
3166 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3168 if ( m_attrProvider
)
3170 attr
->SetKind(wxGridCellAttr::Col
);
3171 m_attrProvider
->SetColAttr(attr
, col
);
3175 // as we take ownership of the pointer and don't store it, we must
3181 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3182 size_t WXUNUSED(numRows
) )
3184 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3189 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3191 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3196 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3197 size_t WXUNUSED(numRows
) )
3199 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3204 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3205 size_t WXUNUSED(numCols
) )
3207 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3212 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3214 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3219 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3220 size_t WXUNUSED(numCols
) )
3222 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3227 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3231 // RD: Starting the rows at zero confuses users,
3232 // no matter how much it makes sense to us geeks.
3238 wxString
wxGridTableBase::GetColLabelValue( int col
)
3240 // default col labels are:
3241 // cols 0 to 25 : A-Z
3242 // cols 26 to 675 : AA-ZZ
3247 for ( n
= 1; ; n
++ )
3249 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
3255 // reverse the string...
3257 for ( i
= 0; i
< n
; i
++ )
3265 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3267 return wxGRID_VALUE_STRING
;
3270 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3271 const wxString
& typeName
)
3273 return typeName
== wxGRID_VALUE_STRING
;
3276 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3278 return CanGetValueAs(row
, col
, typeName
);
3281 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3286 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3291 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3296 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3297 long WXUNUSED(value
) )
3301 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3302 double WXUNUSED(value
) )
3306 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3307 bool WXUNUSED(value
) )
3311 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3312 const wxString
& WXUNUSED(typeName
) )
3317 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3318 const wxString
& WXUNUSED(typeName
),
3319 void* WXUNUSED(value
) )
3323 //////////////////////////////////////////////////////////////////////
3325 // Message class for the grid table to send requests and notifications
3329 wxGridTableMessage::wxGridTableMessage()
3331 m_table
= (wxGridTableBase
*) NULL
;
3337 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3338 int commandInt1
, int commandInt2
)
3342 m_comInt1
= commandInt1
;
3343 m_comInt2
= commandInt2
;
3346 //////////////////////////////////////////////////////////////////////
3348 // A basic grid table for string data. An object of this class will
3349 // created by wxGrid if you don't specify an alternative table class.
3352 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3354 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3356 wxGridStringTable::wxGridStringTable()
3361 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3364 m_data
.Alloc( numRows
);
3367 sa
.Alloc( numCols
);
3368 sa
.Add( wxEmptyString
, numCols
);
3370 m_data
.Add( sa
, numRows
);
3373 wxGridStringTable::~wxGridStringTable()
3377 int wxGridStringTable::GetNumberRows()
3379 return m_data
.GetCount();
3382 int wxGridStringTable::GetNumberCols()
3384 if ( m_data
.GetCount() > 0 )
3385 return m_data
[0].GetCount();
3390 wxString
wxGridStringTable::GetValue( int row
, int col
)
3392 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3394 _T("invalid row or column index in wxGridStringTable") );
3396 return m_data
[row
][col
];
3399 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3401 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3402 _T("invalid row or column index in wxGridStringTable") );
3404 m_data
[row
][col
] = value
;
3407 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3409 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3411 _T("invalid row or column index in wxGridStringTable") );
3413 return (m_data
[row
][col
] == wxEmptyString
);
3416 void wxGridStringTable::Clear()
3419 int numRows
, numCols
;
3421 numRows
= m_data
.GetCount();
3424 numCols
= m_data
[0].GetCount();
3426 for ( row
= 0; row
< numRows
; row
++ )
3428 for ( col
= 0; col
< numCols
; col
++ )
3430 m_data
[row
][col
] = wxEmptyString
;
3436 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3438 size_t curNumRows
= m_data
.GetCount();
3439 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3440 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3442 if ( pos
>= curNumRows
)
3444 return AppendRows( numRows
);
3448 sa
.Alloc( curNumCols
);
3449 sa
.Add( wxEmptyString
, curNumCols
);
3450 m_data
.Insert( sa
, pos
, numRows
);
3454 wxGridTableMessage
msg( this,
3455 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3459 GetView()->ProcessTableMessage( msg
);
3465 bool wxGridStringTable::AppendRows( size_t numRows
)
3467 size_t curNumRows
= m_data
.GetCount();
3468 size_t curNumCols
= ( curNumRows
> 0
3469 ? m_data
[0].GetCount()
3470 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3473 if ( curNumCols
> 0 )
3475 sa
.Alloc( curNumCols
);
3476 sa
.Add( wxEmptyString
, curNumCols
);
3479 m_data
.Add( sa
, numRows
);
3483 wxGridTableMessage
msg( this,
3484 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3487 GetView()->ProcessTableMessage( msg
);
3493 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3495 size_t curNumRows
= m_data
.GetCount();
3497 if ( pos
>= curNumRows
)
3499 wxFAIL_MSG( wxString::Format
3501 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3503 (unsigned long)numRows
,
3504 (unsigned long)curNumRows
3510 if ( numRows
> curNumRows
- pos
)
3512 numRows
= curNumRows
- pos
;
3515 if ( numRows
>= curNumRows
)
3521 m_data
.RemoveAt( pos
, numRows
);
3526 wxGridTableMessage
msg( this,
3527 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3531 GetView()->ProcessTableMessage( msg
);
3537 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3541 size_t curNumRows
= m_data
.GetCount();
3542 size_t curNumCols
= ( curNumRows
> 0
3543 ? m_data
[0].GetCount()
3544 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3546 if ( pos
>= curNumCols
)
3548 return AppendCols( numCols
);
3551 if ( !m_colLabels
.IsEmpty() )
3553 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
3556 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
3557 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
3560 for ( row
= 0; row
< curNumRows
; row
++ )
3562 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3564 m_data
[row
].Insert( wxEmptyString
, col
);
3570 wxGridTableMessage
msg( this,
3571 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3575 GetView()->ProcessTableMessage( msg
);
3581 bool wxGridStringTable::AppendCols( size_t numCols
)
3585 size_t curNumRows
= m_data
.GetCount();
3590 // TODO: something better than this ?
3592 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3597 for ( row
= 0; row
< curNumRows
; row
++ )
3599 m_data
[row
].Add( wxEmptyString
, numCols
);
3604 wxGridTableMessage
msg( this,
3605 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3608 GetView()->ProcessTableMessage( msg
);
3614 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3618 size_t curNumRows
= m_data
.GetCount();
3619 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3620 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3622 if ( pos
>= curNumCols
)
3624 wxFAIL_MSG( wxString::Format
3626 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3628 (unsigned long)numCols
,
3629 (unsigned long)curNumCols
3636 colID
= GetView()->GetColAt( pos
);
3640 if ( numCols
> curNumCols
- colID
)
3642 numCols
= curNumCols
- colID
;
3645 if ( !m_colLabels
.IsEmpty() )
3647 m_colLabels
.RemoveAt( colID
, numCols
);
3650 for ( row
= 0; row
< curNumRows
; row
++ )
3652 if ( numCols
>= curNumCols
)
3654 m_data
[row
].Clear();
3658 m_data
[row
].RemoveAt( colID
, numCols
);
3664 wxGridTableMessage
msg( this,
3665 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3669 GetView()->ProcessTableMessage( msg
);
3675 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3677 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3679 // using default label
3681 return wxGridTableBase::GetRowLabelValue( row
);
3685 return m_rowLabels
[row
];
3689 wxString
wxGridStringTable::GetColLabelValue( int col
)
3691 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3693 // using default label
3695 return wxGridTableBase::GetColLabelValue( col
);
3699 return m_colLabels
[col
];
3703 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3705 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3707 int n
= m_rowLabels
.GetCount();
3710 for ( i
= n
; i
<= row
; i
++ )
3712 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3716 m_rowLabels
[row
] = value
;
3719 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3721 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3723 int n
= m_colLabels
.GetCount();
3726 for ( i
= n
; i
<= col
; i
++ )
3728 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3732 m_colLabels
[col
] = value
;
3736 //////////////////////////////////////////////////////////////////////
3737 //////////////////////////////////////////////////////////////////////
3739 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3741 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3742 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3743 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3744 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3745 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3746 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3747 EVT_CHAR( wxGridRowLabelWindow::OnChar
)
3750 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3752 const wxPoint
&pos
, const wxSize
&size
)
3753 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxFULL_REPAINT_ON_RESIZE
)
3758 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3762 // NO - don't do this because it will set both the x and y origin
3763 // coords to match the parent scrolled window and we just want to
3764 // set the y coord - MB
3766 // m_owner->PrepareDC( dc );
3769 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3770 wxPoint pt
= dc
.GetDeviceOrigin();
3771 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
3773 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3774 m_owner
->DrawRowLabels( dc
, rows
);
3777 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3779 m_owner
->ProcessRowLabelMouseEvent( event
);
3782 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3784 m_owner
->GetEventHandler()->ProcessEvent( event
);
3787 // This seems to be required for wxMotif otherwise the mouse
3788 // cursor must be in the cell edit control to get key events
3790 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3792 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3796 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3798 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3802 void wxGridRowLabelWindow::OnChar( wxKeyEvent
& event
)
3804 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3808 //////////////////////////////////////////////////////////////////////
3810 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3812 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3813 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3814 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3815 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3816 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3817 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3818 EVT_CHAR( wxGridColLabelWindow::OnChar
)
3821 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3823 const wxPoint
&pos
, const wxSize
&size
)
3824 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxFULL_REPAINT_ON_RESIZE
)
3829 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3833 // NO - don't do this because it will set both the x and y origin
3834 // coords to match the parent scrolled window and we just want to
3835 // set the x coord - MB
3837 // m_owner->PrepareDC( dc );
3840 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3841 wxPoint pt
= dc
.GetDeviceOrigin();
3842 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3843 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
3845 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
3847 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3848 m_owner
->DrawColLabels( dc
, cols
);
3851 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3853 m_owner
->ProcessColLabelMouseEvent( event
);
3856 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3858 m_owner
->GetEventHandler()->ProcessEvent( event
);
3861 // This seems to be required for wxMotif otherwise the mouse
3862 // cursor must be in the cell edit control to get key events
3864 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3866 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3870 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3872 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3876 void wxGridColLabelWindow::OnChar( wxKeyEvent
& event
)
3878 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3882 //////////////////////////////////////////////////////////////////////
3884 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3886 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3887 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3888 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3889 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3890 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3891 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3892 EVT_CHAR( wxGridCornerLabelWindow::OnChar
)
3895 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3897 const wxPoint
&pos
, const wxSize
&size
)
3898 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxFULL_REPAINT_ON_RESIZE
)
3903 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3907 int client_height
= 0;
3908 int client_width
= 0;
3909 GetClientSize( &client_width
, &client_height
);
3911 // VZ: any reason for this ifdef? (FIXME)
3917 rect
.SetWidth( client_width
- 2 );
3918 rect
.SetHeight( client_height
- 2 );
3920 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3922 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
3923 dc
.DrawLine( client_width
- 1, client_height
- 1, client_width
- 1, 0 );
3924 dc
.DrawLine( client_width
- 1, client_height
- 1, 0, client_height
- 1 );
3925 dc
.DrawLine( 0, 0, client_width
, 0 );
3926 dc
.DrawLine( 0, 0, 0, client_height
);
3928 dc
.SetPen( *wxWHITE_PEN
);
3929 dc
.DrawLine( 1, 1, client_width
- 1, 1 );
3930 dc
.DrawLine( 1, 1, 1, client_height
- 1 );
3934 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3936 m_owner
->ProcessCornerLabelMouseEvent( event
);
3939 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3941 m_owner
->GetEventHandler()->ProcessEvent(event
);
3944 // This seems to be required for wxMotif otherwise the mouse
3945 // cursor must be in the cell edit control to get key events
3947 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3949 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3953 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3955 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3959 void wxGridCornerLabelWindow::OnChar( wxKeyEvent
& event
)
3961 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3965 //////////////////////////////////////////////////////////////////////
3967 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3969 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3970 EVT_PAINT( wxGridWindow::OnPaint
)
3971 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3972 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3973 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3974 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3975 EVT_CHAR( wxGridWindow::OnChar
)
3976 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3977 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3978 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3981 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3982 wxGridRowLabelWindow
*rowLblWin
,
3983 wxGridColLabelWindow
*colLblWin
,
3986 const wxSize
&size
)
3988 parent
, id
, pos
, size
,
3989 wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
| wxFULL_REPAINT_ON_RESIZE
,
3990 wxT("grid window") )
3993 m_rowLabelWin
= rowLblWin
;
3994 m_colLabelWin
= colLblWin
;
3997 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3999 wxPaintDC
dc( this );
4000 m_owner
->PrepareDC( dc
);
4001 wxRegion reg
= GetUpdateRegion();
4002 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
4003 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
4005 #if WXGRID_DRAW_LINES
4006 m_owner
->DrawAllGridLines( dc
, reg
);
4009 m_owner
->DrawGridSpace( dc
);
4010 m_owner
->DrawHighlight( dc
, dirtyCells
);
4013 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
4015 wxWindow::ScrollWindow( dx
, dy
, rect
);
4016 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
4017 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
4020 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
4022 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
4025 m_owner
->ProcessGridCellMouseEvent( event
);
4028 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
4030 m_owner
->GetEventHandler()->ProcessEvent( event
);
4033 // This seems to be required for wxMotif/wxGTK otherwise the mouse
4034 // cursor must be in the cell edit control to get key events
4036 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
4038 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4042 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
4044 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4048 void wxGridWindow::OnChar( wxKeyEvent
& event
)
4050 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4054 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
4058 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
4060 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4064 //////////////////////////////////////////////////////////////////////
4066 // Internal Helper function for computing row or column from some
4067 // (unscrolled) coordinate value, using either
4068 // m_defaultRowHeight/m_defaultColWidth or binary search on array
4069 // of m_rowBottoms/m_ColRights to speed up the search!
4071 // Internal helper macros for simpler use of that function
4073 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
4074 const wxArrayInt
& BorderArray
, int nMax
,
4077 #define internalXToCol(x) XToCol(x, true)
4078 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
4079 m_minAcceptableRowHeight, \
4080 m_rowBottoms, m_numRows, true)
4082 /////////////////////////////////////////////////////////////////////
4084 #if wxUSE_EXTENDED_RTTI
4085 WX_DEFINE_FLAGS( wxGridStyle
)
4087 wxBEGIN_FLAGS( wxGridStyle
)
4088 // new style border flags, we put them first to
4089 // use them for streaming out
4090 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4091 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4092 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4093 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4094 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4095 wxFLAGS_MEMBER(wxBORDER_NONE
)
4097 // old style border flags
4098 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4099 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4100 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4101 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4102 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4103 wxFLAGS_MEMBER(wxBORDER
)
4105 // standard window styles
4106 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4107 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4108 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4109 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4110 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4111 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4112 wxFLAGS_MEMBER(wxVSCROLL
)
4113 wxFLAGS_MEMBER(wxHSCROLL
)
4115 wxEND_FLAGS( wxGridStyle
)
4117 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4119 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4120 wxHIDE_PROPERTY( Children
)
4121 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4122 wxEND_PROPERTIES_TABLE()
4124 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4125 wxEND_HANDLERS_TABLE()
4127 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4130 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
4133 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4136 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4137 EVT_PAINT( wxGrid::OnPaint
)
4138 EVT_SIZE( wxGrid::OnSize
)
4139 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
4140 EVT_KEY_UP( wxGrid::OnKeyUp
)
4141 EVT_CHAR ( wxGrid::OnChar
)
4142 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
4147 // in order to make sure that a size event is not
4148 // trigerred in a unfinished state
4149 m_cornerLabelWin
= NULL
;
4150 m_rowLabelWin
= NULL
;
4151 m_colLabelWin
= NULL
;
4155 wxGrid::wxGrid( wxWindow
*parent
,
4160 const wxString
& name
)
4161 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
4162 m_colMinWidths(GRID_HASH_SIZE
),
4163 m_rowMinHeights(GRID_HASH_SIZE
)
4166 SetInitialSize(size
);
4169 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
4170 const wxPoint
& pos
, const wxSize
& size
,
4171 long style
, const wxString
& name
)
4173 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
4174 style
| wxWANTS_CHARS
, name
))
4177 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4178 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4181 SetInitialSize(size
);
4188 // Must do this or ~wxScrollHelper will pop the wrong event handler
4189 SetTargetWindow(this);
4191 wxSafeDecRef(m_defaultCellAttr
);
4193 #ifdef DEBUG_ATTR_CACHE
4194 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4195 wxPrintf(_T("wxGrid attribute cache statistics: "
4196 "total: %u, hits: %u (%u%%)\n"),
4197 total
, gs_nAttrCacheHits
,
4198 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4201 // if we own the table, just delete it, otherwise at least don't leave it
4202 // with dangling view pointer
4205 else if ( m_table
&& m_table
->GetView() == this )
4206 m_table
->SetView(NULL
);
4208 delete m_typeRegistry
;
4213 // ----- internal init and update functions
4216 // NOTE: If using the default visual attributes works everywhere then this can
4217 // be removed as well as the #else cases below.
4218 #define _USE_VISATTR 0
4220 void wxGrid::Create()
4222 // set to true by CreateGrid
4225 // create the type registry
4226 m_typeRegistry
= new wxGridTypeRegistry
;
4229 m_table
= (wxGridTableBase
*) NULL
;
4232 m_cellEditCtrlEnabled
= false;
4234 m_defaultCellAttr
= new wxGridCellAttr();
4236 // Set default cell attributes
4237 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4238 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4239 m_defaultCellAttr
->SetFont(GetFont());
4240 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4241 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4242 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4245 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4246 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4248 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4249 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4252 m_defaultCellAttr
->SetTextColour(
4253 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4254 m_defaultCellAttr
->SetBackgroundColour(
4255 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4260 m_currentCellCoords
= wxGridNoCellCoords
;
4262 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4263 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4265 // subwindow components that make up the wxGrid
4266 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4271 m_colLabelWin
= new wxGridColLabelWindow( this,
4276 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4281 m_gridWin
= new wxGridWindow( this,
4288 SetTargetWindow( m_gridWin
);
4291 wxColour gfg
= gva
.colFg
;
4292 wxColour gbg
= gva
.colBg
;
4293 wxColour lfg
= lva
.colFg
;
4294 wxColour lbg
= lva
.colBg
;
4296 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4297 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4298 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4299 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4302 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4303 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4304 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4305 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4306 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4307 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4309 m_gridWin
->SetOwnForegroundColour(gfg
);
4310 m_gridWin
->SetOwnBackgroundColour(gbg
);
4315 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4316 wxGrid::wxGridSelectionModes selmode
)
4318 wxCHECK_MSG( !m_created
,
4320 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4322 m_numRows
= numRows
;
4323 m_numCols
= numCols
;
4325 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4326 m_table
->SetView( this );
4328 m_selection
= new wxGridSelection( this, selmode
);
4337 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4339 wxCHECK_RET( m_created
,
4340 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4342 m_selection
->SetSelectionMode( selmode
);
4345 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4347 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4348 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4350 return m_selection
->GetSelectionMode();
4353 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4354 wxGrid::wxGridSelectionModes selmode
)
4358 // stop all processing
4363 wxGridTableBase
*t
= m_table
;
4378 m_numRows
= table
->GetNumberRows();
4379 m_numCols
= table
->GetNumberCols();
4382 m_table
->SetView( this );
4383 m_ownTable
= takeOwnership
;
4384 m_selection
= new wxGridSelection( this, selmode
);
4396 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4397 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4399 if ( m_rowLabelWin
)
4401 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4405 m_labelBackgroundColour
= *wxWHITE
;
4408 m_labelTextColour
= *wxBLACK
;
4411 m_attrCache
.row
= -1;
4412 m_attrCache
.col
= -1;
4413 m_attrCache
.attr
= NULL
;
4415 // TODO: something better than this ?
4417 m_labelFont
= this->GetFont();
4418 m_labelFont
.SetWeight( wxBOLD
);
4420 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4421 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4423 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4424 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4425 m_colLabelTextOrientation
= wxHORIZONTAL
;
4427 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4428 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4430 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4431 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4433 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4434 m_defaultRowHeight
+= 8;
4436 m_defaultRowHeight
+= 4;
4439 m_gridLineColour
= wxColour( 192,192,192 );
4440 m_gridLinesEnabled
= true;
4441 m_cellHighlightColour
= *wxBLACK
;
4442 m_cellHighlightPenWidth
= 2;
4443 m_cellHighlightROPenWidth
= 1;
4445 m_canDragColMove
= false;
4447 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4448 m_winCapture
= (wxWindow
*)NULL
;
4449 m_canDragRowSize
= true;
4450 m_canDragColSize
= true;
4451 m_canDragGridSize
= true;
4452 m_canDragCell
= false;
4454 m_dragRowOrCol
= -1;
4455 m_isDragging
= false;
4456 m_startDragPos
= wxDefaultPosition
;
4458 m_waitForSlowClick
= false;
4460 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4461 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4463 m_currentCellCoords
= wxGridNoCellCoords
;
4467 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4468 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4470 m_editable
= true; // default for whole grid
4472 m_inOnKeyDown
= false;
4478 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4479 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4482 // ----------------------------------------------------------------------------
4483 // the idea is to call these functions only when necessary because they create
4484 // quite big arrays which eat memory mostly unnecessary - in particular, if
4485 // default widths/heights are used for all rows/columns, we may not use these
4488 // with some extra code, it should be possible to only store the widths/heights
4489 // different from default ones (resulting in space savings for huge grids) but
4490 // this is not done currently
4491 // ----------------------------------------------------------------------------
4493 void wxGrid::InitRowHeights()
4495 m_rowHeights
.Empty();
4496 m_rowBottoms
.Empty();
4498 m_rowHeights
.Alloc( m_numRows
);
4499 m_rowBottoms
.Alloc( m_numRows
);
4501 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4504 for ( int i
= 0; i
< m_numRows
; i
++ )
4506 rowBottom
+= m_defaultRowHeight
;
4507 m_rowBottoms
.Add( rowBottom
);
4511 void wxGrid::InitColWidths()
4513 m_colWidths
.Empty();
4514 m_colRights
.Empty();
4516 m_colWidths
.Alloc( m_numCols
);
4517 m_colRights
.Alloc( m_numCols
);
4519 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4522 for ( int i
= 0; i
< m_numCols
; i
++ )
4524 colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
4525 m_colRights
.Add( colRight
);
4529 int wxGrid::GetColWidth(int col
) const
4531 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4534 int wxGrid::GetColLeft(int col
) const
4536 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
4537 : m_colRights
[col
] - m_colWidths
[col
];
4540 int wxGrid::GetColRight(int col
) const
4542 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
4546 int wxGrid::GetRowHeight(int row
) const
4548 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4551 int wxGrid::GetRowTop(int row
) const
4553 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4554 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4557 int wxGrid::GetRowBottom(int row
) const
4559 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4560 : m_rowBottoms
[row
];
4563 void wxGrid::CalcDimensions()
4565 // compute the size of the scrollable area
4566 int w
= m_numCols
> 0 ? GetColRight(GetColAt(m_numCols
- 1)) : 0;
4567 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
4572 // take into account editor if shown
4573 if ( IsCellEditControlShown() )
4576 int r
= m_currentCellCoords
.GetRow();
4577 int c
= m_currentCellCoords
.GetCol();
4578 int x
= GetColLeft(c
);
4579 int y
= GetRowTop(r
);
4581 // how big is the editor
4582 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4583 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4584 editor
->GetControl()->GetSize(&w2
, &h2
);
4595 // preserve (more or less) the previous position
4597 GetViewStart( &x
, &y
);
4599 // ensure the position is valid for the new scroll ranges
4601 x
= wxMax( w
- 1, 0 );
4603 y
= wxMax( h
- 1, 0 );
4605 // do set scrollbar parameters
4606 SetScrollbars( m_scrollLineX
, m_scrollLineY
,
4607 GetScrollX(w
), GetScrollY(h
),
4609 GetBatchCount() != 0);
4611 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4612 // still must reposition the children
4616 void wxGrid::CalcWindowSizes()
4618 // escape if the window is has not been fully created yet
4620 if ( m_cornerLabelWin
== NULL
)
4624 GetClientSize( &cw
, &ch
);
4626 // this block of code tries to work around the following problem: the grid
4627 // could have been just resized to have enough space to show the full grid
4628 // window contents without the scrollbars, but its client size could be
4629 // not big enough because the grid has the scrollbars right now and so the
4630 // scrollbars would remain even though we don't need them any more
4632 // to prevent this from happening, check if we have enough space for
4633 // everything without the scrollbars and explicitly disable them then
4634 wxSize size
= GetSize() - GetWindowBorderSize();
4635 if ( size
!= wxSize(cw
, ch
) )
4637 // check if we have enough space for grid window after accounting for
4638 // the fixed size elements
4639 size
.x
-= m_rowLabelWidth
;
4640 size
.y
-= m_colLabelHeight
;
4642 const wxSize vsize
= m_gridWin
->GetVirtualSize();
4644 if ( size
.x
>= vsize
.x
&& size
.y
>= vsize
.y
)
4646 // yes, we do, so remove the scrollbars and use the new client size
4647 // (which should be the same as full window size - borders now)
4648 SetScrollbars(0, 0, 0, 0);
4649 GetClientSize(&cw
, &ch
);
4653 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4654 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4656 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4657 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
- m_rowLabelWidth
, m_colLabelHeight
);
4659 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4660 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
- m_colLabelHeight
);
4662 if ( m_gridWin
&& m_gridWin
->IsShown() )
4663 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
- m_rowLabelWidth
, ch
- m_colLabelHeight
);
4666 // this is called when the grid table sends a message
4667 // to indicate that it has been redimensioned
4669 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4672 bool result
= false;
4674 // Clear the attribute cache as the attribute might refer to a different
4675 // cell than stored in the cache after adding/removing rows/columns.
4678 // By the same reasoning, the editor should be dismissed if columns are
4679 // added or removed. And for consistency, it should IMHO always be
4680 // removed, not only if the cell "underneath" it actually changes.
4681 // For now, I intentionally do not save the editor's content as the
4682 // cell it might want to save that stuff to might no longer exist.
4683 HideCellEditControl();
4686 // if we were using the default widths/heights so far, we must change them
4688 if ( m_colWidths
.IsEmpty() )
4693 if ( m_rowHeights
.IsEmpty() )
4699 switch ( msg
.GetId() )
4701 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4703 size_t pos
= msg
.GetCommandInt();
4704 int numRows
= msg
.GetCommandInt2();
4706 m_numRows
+= numRows
;
4708 if ( !m_rowHeights
.IsEmpty() )
4710 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4711 m_rowBottoms
.Insert( 0, pos
, numRows
);
4715 bottom
= m_rowBottoms
[pos
- 1];
4717 for ( i
= pos
; i
< m_numRows
; i
++ )
4719 bottom
+= m_rowHeights
[i
];
4720 m_rowBottoms
[i
] = bottom
;
4724 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4726 // if we have just inserted cols into an empty grid the current
4727 // cell will be undefined...
4729 SetCurrentCell( 0, 0 );
4733 m_selection
->UpdateRows( pos
, numRows
);
4734 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4736 attrProvider
->UpdateAttrRows( pos
, numRows
);
4738 if ( !GetBatchCount() )
4741 m_rowLabelWin
->Refresh();
4747 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4749 int numRows
= msg
.GetCommandInt();
4750 int oldNumRows
= m_numRows
;
4751 m_numRows
+= numRows
;
4753 if ( !m_rowHeights
.IsEmpty() )
4755 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4756 m_rowBottoms
.Add( 0, numRows
);
4759 if ( oldNumRows
> 0 )
4760 bottom
= m_rowBottoms
[oldNumRows
- 1];
4762 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4764 bottom
+= m_rowHeights
[i
];
4765 m_rowBottoms
[i
] = bottom
;
4769 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4771 // if we have just inserted cols into an empty grid the current
4772 // cell will be undefined...
4774 SetCurrentCell( 0, 0 );
4777 if ( !GetBatchCount() )
4780 m_rowLabelWin
->Refresh();
4786 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4788 size_t pos
= msg
.GetCommandInt();
4789 int numRows
= msg
.GetCommandInt2();
4790 m_numRows
-= numRows
;
4792 if ( !m_rowHeights
.IsEmpty() )
4794 m_rowHeights
.RemoveAt( pos
, numRows
);
4795 m_rowBottoms
.RemoveAt( pos
, numRows
);
4798 for ( i
= 0; i
< m_numRows
; i
++ )
4800 h
+= m_rowHeights
[i
];
4801 m_rowBottoms
[i
] = h
;
4807 m_currentCellCoords
= wxGridNoCellCoords
;
4811 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4812 m_currentCellCoords
.Set( 0, 0 );
4816 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4817 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4820 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4822 // ifdef'd out following patch from Paul Gammans
4824 // No need to touch column attributes, unless we
4825 // removed _all_ rows, in this case, we remove
4826 // all column attributes.
4827 // I hate to do this here, but the
4828 // needed data is not available inside UpdateAttrRows.
4829 if ( !GetNumberRows() )
4830 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4834 if ( !GetBatchCount() )
4837 m_rowLabelWin
->Refresh();
4843 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4845 size_t pos
= msg
.GetCommandInt();
4846 int numCols
= msg
.GetCommandInt2();
4847 m_numCols
+= numCols
;
4849 if ( !m_colAt
.IsEmpty() )
4851 //Shift the column IDs
4853 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
4855 if ( m_colAt
[i
] >= (int)pos
)
4856 m_colAt
[i
] += numCols
;
4859 m_colAt
.Insert( pos
, pos
, numCols
);
4861 //Set the new columns' positions
4862 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
4868 if ( !m_colWidths
.IsEmpty() )
4870 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4871 m_colRights
.Insert( 0, pos
, numCols
);
4875 right
= m_colRights
[GetColAt( pos
- 1 )];
4878 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
4880 i
= GetColAt( colPos
);
4882 right
+= m_colWidths
[i
];
4883 m_colRights
[i
] = right
;
4887 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4889 // if we have just inserted cols into an empty grid the current
4890 // cell will be undefined...
4892 SetCurrentCell( 0, 0 );
4896 m_selection
->UpdateCols( pos
, numCols
);
4897 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4899 attrProvider
->UpdateAttrCols( pos
, numCols
);
4900 if ( !GetBatchCount() )
4903 m_colLabelWin
->Refresh();
4909 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4911 int numCols
= msg
.GetCommandInt();
4912 int oldNumCols
= m_numCols
;
4913 m_numCols
+= numCols
;
4915 if ( !m_colAt
.IsEmpty() )
4917 m_colAt
.Add( 0, numCols
);
4919 //Set the new columns' positions
4921 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4927 if ( !m_colWidths
.IsEmpty() )
4929 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4930 m_colRights
.Add( 0, numCols
);
4933 if ( oldNumCols
> 0 )
4934 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
4937 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
4939 i
= GetColAt( colPos
);
4941 right
+= m_colWidths
[i
];
4942 m_colRights
[i
] = right
;
4946 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4948 // if we have just inserted cols into an empty grid the current
4949 // cell will be undefined...
4951 SetCurrentCell( 0, 0 );
4953 if ( !GetBatchCount() )
4956 m_colLabelWin
->Refresh();
4962 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4964 size_t pos
= msg
.GetCommandInt();
4965 int numCols
= msg
.GetCommandInt2();
4966 m_numCols
-= numCols
;
4968 if ( !m_colAt
.IsEmpty() )
4970 int colID
= GetColAt( pos
);
4972 m_colAt
.RemoveAt( pos
, numCols
);
4974 //Shift the column IDs
4976 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
4978 if ( m_colAt
[colPos
] > colID
)
4979 m_colAt
[colPos
] -= numCols
;
4983 if ( !m_colWidths
.IsEmpty() )
4985 m_colWidths
.RemoveAt( pos
, numCols
);
4986 m_colRights
.RemoveAt( pos
, numCols
);
4990 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
4992 i
= GetColAt( colPos
);
4994 w
+= m_colWidths
[i
];
5001 m_currentCellCoords
= wxGridNoCellCoords
;
5005 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
5006 m_currentCellCoords
.Set( 0, 0 );
5010 m_selection
->UpdateCols( pos
, -((int)numCols
) );
5011 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5014 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
5016 // ifdef'd out following patch from Paul Gammans
5018 // No need to touch row attributes, unless we
5019 // removed _all_ columns, in this case, we remove
5020 // all row attributes.
5021 // I hate to do this here, but the
5022 // needed data is not available inside UpdateAttrCols.
5023 if ( !GetNumberCols() )
5024 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
5028 if ( !GetBatchCount() )
5031 m_colLabelWin
->Refresh();
5038 if (result
&& !GetBatchCount() )
5039 m_gridWin
->Refresh();
5044 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
5046 wxRegionIterator
iter( reg
);
5049 wxArrayInt rowlabels
;
5056 // TODO: remove this when we can...
5057 // There is a bug in wxMotif that gives garbage update
5058 // rectangles if you jump-scroll a long way by clicking the
5059 // scrollbar with middle button. This is a work-around
5061 #if defined(__WXMOTIF__)
5063 m_gridWin
->GetClientSize( &cw
, &ch
);
5064 if ( r
.GetTop() > ch
)
5066 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5069 // logical bounds of update region
5072 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
5073 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
5075 // find the row labels within these bounds
5078 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5080 if ( GetRowBottom(row
) < top
)
5083 if ( GetRowTop(row
) > bottom
)
5086 rowlabels
.Add( row
);
5095 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
5097 wxRegionIterator
iter( reg
);
5100 wxArrayInt colLabels
;
5107 // TODO: remove this when we can...
5108 // There is a bug in wxMotif that gives garbage update
5109 // rectangles if you jump-scroll a long way by clicking the
5110 // scrollbar with middle button. This is a work-around
5112 #if defined(__WXMOTIF__)
5114 m_gridWin
->GetClientSize( &cw
, &ch
);
5115 if ( r
.GetLeft() > cw
)
5117 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5120 // logical bounds of update region
5123 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
5124 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
5126 // find the cells within these bounds
5130 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5132 col
= GetColAt( colPos
);
5134 if ( GetColRight(col
) < left
)
5137 if ( GetColLeft(col
) > right
)
5140 colLabels
.Add( col
);
5149 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
5151 wxRegionIterator
iter( reg
);
5154 wxGridCellCoordsArray cellsExposed
;
5156 int left
, top
, right
, bottom
;
5161 // TODO: remove this when we can...
5162 // There is a bug in wxMotif that gives garbage update
5163 // rectangles if you jump-scroll a long way by clicking the
5164 // scrollbar with middle button. This is a work-around
5166 #if defined(__WXMOTIF__)
5168 m_gridWin
->GetClientSize( &cw
, &ch
);
5169 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5170 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5171 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5172 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5175 // logical bounds of update region
5177 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5178 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5180 // find the cells within these bounds
5183 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5185 if ( GetRowBottom(row
) <= top
)
5188 if ( GetRowTop(row
) > bottom
)
5192 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5194 col
= GetColAt( colPos
);
5196 if ( GetColRight(col
) <= left
)
5199 if ( GetColLeft(col
) > right
)
5202 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
5209 return cellsExposed
;
5213 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5216 wxPoint
pos( event
.GetPosition() );
5217 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5219 if ( event
.Dragging() )
5223 m_isDragging
= true;
5224 m_rowLabelWin
->CaptureMouse();
5227 if ( event
.LeftIsDown() )
5229 switch ( m_cursorMode
)
5231 case WXGRID_CURSOR_RESIZE_ROW
:
5233 int cw
, ch
, left
, dummy
;
5234 m_gridWin
->GetClientSize( &cw
, &ch
);
5235 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5237 wxClientDC
dc( m_gridWin
);
5240 GetRowTop(m_dragRowOrCol
) +
5241 GetRowMinimalHeight(m_dragRowOrCol
) );
5242 dc
.SetLogicalFunction(wxINVERT
);
5243 if ( m_dragLastPos
>= 0 )
5245 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5247 dc
.DrawLine( left
, y
, left
+cw
, y
);
5252 case WXGRID_CURSOR_SELECT_ROW
:
5254 if ( (row
= YToRow( y
)) >= 0 )
5258 m_selection
->SelectRow( row
,
5259 event
.ControlDown(),
5268 // default label to suppress warnings about "enumeration value
5269 // 'xxx' not handled in switch
5277 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5282 if (m_rowLabelWin
->HasCapture())
5283 m_rowLabelWin
->ReleaseMouse();
5284 m_isDragging
= false;
5287 // ------------ Entering or leaving the window
5289 if ( event
.Entering() || event
.Leaving() )
5291 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5294 // ------------ Left button pressed
5296 else if ( event
.LeftDown() )
5298 // don't send a label click event for a hit on the
5299 // edge of the row label - this is probably the user
5300 // wanting to resize the row
5302 if ( YToEdgeOfRow(y
) < 0 )
5306 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5308 if ( !event
.ShiftDown() && !event
.CmdDown() )
5312 if ( event
.ShiftDown() )
5314 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5317 GetNumberCols() - 1,
5318 event
.ControlDown(),
5325 m_selection
->SelectRow( row
,
5326 event
.ControlDown(),
5333 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5338 // starting to drag-resize a row
5339 if ( CanDragRowSize() )
5340 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5344 // ------------ Left double click
5346 else if (event
.LeftDClick() )
5348 row
= YToEdgeOfRow(y
);
5353 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5355 // no default action at the moment
5360 // adjust row height depending on label text
5361 AutoSizeRowLabelSize( row
);
5363 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5368 // ------------ Left button released
5370 else if ( event
.LeftUp() )
5372 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5374 DoEndDragResizeRow();
5376 // Note: we are ending the event *after* doing
5377 // default processing in this case
5379 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5382 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5386 // ------------ Right button down
5388 else if ( event
.RightDown() )
5392 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5394 // no default action at the moment
5398 // ------------ Right double click
5400 else if ( event
.RightDClick() )
5404 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5406 // no default action at the moment
5410 // ------------ No buttons down and mouse moving
5412 else if ( event
.Moving() )
5414 m_dragRowOrCol
= YToEdgeOfRow( y
);
5415 if ( m_dragRowOrCol
>= 0 )
5417 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5419 // don't capture the mouse yet
5420 if ( CanDragRowSize() )
5421 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5424 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5426 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5431 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5434 wxPoint
pos( event
.GetPosition() );
5435 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5437 if ( event
.Dragging() )
5441 m_isDragging
= true;
5442 m_colLabelWin
->CaptureMouse();
5444 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
)
5445 m_dragRowOrCol
= XToCol( x
);
5448 if ( event
.LeftIsDown() )
5450 switch ( m_cursorMode
)
5452 case WXGRID_CURSOR_RESIZE_COL
:
5454 int cw
, ch
, dummy
, top
;
5455 m_gridWin
->GetClientSize( &cw
, &ch
);
5456 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5458 wxClientDC
dc( m_gridWin
);
5461 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5462 GetColMinimalWidth(m_dragRowOrCol
));
5463 dc
.SetLogicalFunction(wxINVERT
);
5464 if ( m_dragLastPos
>= 0 )
5466 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5468 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5473 case WXGRID_CURSOR_SELECT_COL
:
5475 if ( (col
= XToCol( x
)) >= 0 )
5479 m_selection
->SelectCol( col
,
5480 event
.ControlDown(),
5489 case WXGRID_CURSOR_MOVE_COL
:
5492 m_moveToCol
= GetColAt( 0 );
5494 m_moveToCol
= XToCol( x
);
5498 if ( m_moveToCol
< 0 )
5499 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5501 markerX
= GetColLeft( m_moveToCol
);
5503 if ( markerX
!= m_dragLastPos
)
5505 wxClientDC
dc( m_colLabelWin
);
5508 m_colLabelWin
->GetClientSize( &cw
, &ch
);
5512 //Clean up the last indicator
5513 if ( m_dragLastPos
>= 0 )
5515 wxPen
pen( m_colLabelWin
->GetBackgroundColour(), 2 );
5517 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
5518 dc
.SetPen(wxNullPen
);
5520 if ( XToCol( m_dragLastPos
) != -1 )
5521 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
5524 //Moving to the same place? Don't draw a marker
5525 if ( (m_moveToCol
== m_dragRowOrCol
)
5526 || (GetColPos( m_moveToCol
) == GetColPos( m_dragRowOrCol
) + 1)
5527 || (m_moveToCol
< 0 && m_dragRowOrCol
== GetColAt( m_numCols
- 1 )))
5534 wxPen
pen( *wxBLUE
, 2 );
5537 dc
.DrawLine( markerX
, 0, markerX
, ch
);
5539 dc
.SetPen(wxNullPen
);
5541 m_dragLastPos
= markerX
- 1;
5546 // default label to suppress warnings about "enumeration value
5547 // 'xxx' not handled in switch
5555 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5560 if (m_colLabelWin
->HasCapture())
5561 m_colLabelWin
->ReleaseMouse();
5562 m_isDragging
= false;
5565 // ------------ Entering or leaving the window
5567 if ( event
.Entering() || event
.Leaving() )
5569 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5572 // ------------ Left button pressed
5574 else if ( event
.LeftDown() )
5576 // don't send a label click event for a hit on the
5577 // edge of the col label - this is probably the user
5578 // wanting to resize the col
5580 if ( XToEdgeOfCol(x
) < 0 )
5584 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5586 if ( m_canDragColMove
)
5588 //Show button as pressed
5589 wxClientDC
dc( m_colLabelWin
);
5590 int colLeft
= GetColLeft( col
);
5591 int colRight
= GetColRight( col
) - 1;
5592 dc
.SetPen( wxPen( m_colLabelWin
->GetBackgroundColour(), 1 ) );
5593 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
5594 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
5596 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, m_colLabelWin
);
5600 if ( !event
.ShiftDown() && !event
.CmdDown() )
5604 if ( event
.ShiftDown() )
5606 m_selection
->SelectBlock( 0,
5607 m_currentCellCoords
.GetCol(),
5608 GetNumberRows() - 1, col
,
5609 event
.ControlDown(),
5616 m_selection
->SelectCol( col
,
5617 event
.ControlDown(),
5624 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5630 // starting to drag-resize a col
5632 if ( CanDragColSize() )
5633 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5637 // ------------ Left double click
5639 if ( event
.LeftDClick() )
5641 col
= XToEdgeOfCol(x
);
5646 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5648 // no default action at the moment
5653 // adjust column width depending on label text
5654 AutoSizeColLabelSize( col
);
5656 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5661 // ------------ Left button released
5663 else if ( event
.LeftUp() )
5665 switch ( m_cursorMode
)
5667 case WXGRID_CURSOR_RESIZE_COL
:
5668 DoEndDragResizeCol();
5670 // Note: we are ending the event *after* doing
5671 // default processing in this case
5673 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5676 case WXGRID_CURSOR_MOVE_COL
:
5679 SendEvent( wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
, event
);
5682 case WXGRID_CURSOR_SELECT_COL
:
5683 case WXGRID_CURSOR_SELECT_CELL
:
5684 case WXGRID_CURSOR_RESIZE_ROW
:
5685 case WXGRID_CURSOR_SELECT_ROW
:
5686 // nothing to do (?)
5690 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5694 // ------------ Right button down
5696 else if ( event
.RightDown() )
5700 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5702 // no default action at the moment
5706 // ------------ Right double click
5708 else if ( event
.RightDClick() )
5712 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5714 // no default action at the moment
5718 // ------------ No buttons down and mouse moving
5720 else if ( event
.Moving() )
5722 m_dragRowOrCol
= XToEdgeOfCol( x
);
5723 if ( m_dragRowOrCol
>= 0 )
5725 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5727 // don't capture the cursor yet
5728 if ( CanDragColSize() )
5729 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5732 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5734 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5739 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5741 if ( event
.LeftDown() )
5743 // indicate corner label by having both row and
5746 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5751 else if ( event
.LeftDClick() )
5753 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5755 else if ( event
.RightDown() )
5757 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5759 // no default action at the moment
5762 else if ( event
.RightDClick() )
5764 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5766 // no default action at the moment
5771 void wxGrid::ChangeCursorMode(CursorMode mode
,
5776 static const wxChar
*cursorModes
[] =
5786 wxLogTrace(_T("grid"),
5787 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5788 win
== m_colLabelWin
? _T("colLabelWin")
5789 : win
? _T("rowLabelWin")
5791 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5794 if ( mode
== m_cursorMode
&&
5795 win
== m_winCapture
&&
5796 captureMouse
== (m_winCapture
!= NULL
))
5801 // by default use the grid itself
5807 if (m_winCapture
->HasCapture())
5808 m_winCapture
->ReleaseMouse();
5809 m_winCapture
= (wxWindow
*)NULL
;
5812 m_cursorMode
= mode
;
5814 switch ( m_cursorMode
)
5816 case WXGRID_CURSOR_RESIZE_ROW
:
5817 win
->SetCursor( m_rowResizeCursor
);
5820 case WXGRID_CURSOR_RESIZE_COL
:
5821 win
->SetCursor( m_colResizeCursor
);
5824 case WXGRID_CURSOR_MOVE_COL
:
5825 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
5829 win
->SetCursor( *wxSTANDARD_CURSOR
);
5833 // we need to capture mouse when resizing
5834 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5835 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5837 if ( captureMouse
&& resize
)
5839 win
->CaptureMouse();
5844 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5847 wxPoint
pos( event
.GetPosition() );
5848 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5850 wxGridCellCoords coords
;
5851 XYToCell( x
, y
, coords
);
5853 int cell_rows
, cell_cols
;
5854 bool isFirstDrag
= !m_isDragging
;
5855 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5856 if ((cell_rows
< 0) || (cell_cols
< 0))
5858 coords
.SetRow(coords
.GetRow() + cell_rows
);
5859 coords
.SetCol(coords
.GetCol() + cell_cols
);
5862 if ( event
.Dragging() )
5864 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5866 // Don't start doing anything until the mouse has been dragged at
5867 // least 3 pixels in any direction...
5870 if (m_startDragPos
== wxDefaultPosition
)
5872 m_startDragPos
= pos
;
5875 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5879 m_isDragging
= true;
5880 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5882 // Hide the edit control, so it
5883 // won't interfere with drag-shrinking.
5884 if ( IsCellEditControlShown() )
5886 HideCellEditControl();
5887 SaveEditControlValue();
5890 // Have we captured the mouse yet?
5893 m_winCapture
= m_gridWin
;
5894 m_winCapture
->CaptureMouse();
5897 if ( coords
!= wxGridNoCellCoords
)
5899 if ( event
.CmdDown() )
5901 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5902 m_selectingKeyboard
= coords
;
5903 HighlightBlock( m_selectingKeyboard
, coords
);
5905 else if ( CanDragCell() )
5909 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5910 m_selectingKeyboard
= coords
;
5912 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5920 if ( !IsSelection() )
5922 HighlightBlock( coords
, coords
);
5926 HighlightBlock( m_currentCellCoords
, coords
);
5930 if (! IsVisible(coords
))
5932 MakeCellVisible(coords
);
5933 // TODO: need to introduce a delay or something here. The
5934 // scrolling is way to fast, at least on MSW - also on GTK.
5938 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5940 int cw
, ch
, left
, dummy
;
5941 m_gridWin
->GetClientSize( &cw
, &ch
);
5942 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5944 wxClientDC
dc( m_gridWin
);
5946 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5947 GetRowMinimalHeight(m_dragRowOrCol
) );
5948 dc
.SetLogicalFunction(wxINVERT
);
5949 if ( m_dragLastPos
>= 0 )
5951 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5953 dc
.DrawLine( left
, y
, left
+cw
, y
);
5956 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5958 int cw
, ch
, dummy
, top
;
5959 m_gridWin
->GetClientSize( &cw
, &ch
);
5960 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5962 wxClientDC
dc( m_gridWin
);
5964 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5965 GetColMinimalWidth(m_dragRowOrCol
) );
5966 dc
.SetLogicalFunction(wxINVERT
);
5967 if ( m_dragLastPos
>= 0 )
5969 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5971 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5978 m_isDragging
= false;
5979 m_startDragPos
= wxDefaultPosition
;
5981 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5982 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5985 if ( event
.Entering() || event
.Leaving() )
5987 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5988 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5993 // ------------ Left button pressed
5995 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5997 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
6002 if ( !event
.CmdDown() )
6004 if ( event
.ShiftDown() )
6008 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
6009 m_currentCellCoords
.GetCol(),
6012 event
.ControlDown(),
6018 else if ( XToEdgeOfCol(x
) < 0 &&
6019 YToEdgeOfRow(y
) < 0 )
6021 DisableCellEditControl();
6022 MakeCellVisible( coords
);
6024 if ( event
.CmdDown() )
6028 m_selection
->ToggleCellSelection( coords
.GetRow(),
6030 event
.ControlDown(),
6035 m_selectingTopLeft
= wxGridNoCellCoords
;
6036 m_selectingBottomRight
= wxGridNoCellCoords
;
6037 m_selectingKeyboard
= coords
;
6041 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
6042 SetCurrentCell( coords
);
6045 if ( m_selection
->GetSelectionMode() !=
6046 wxGrid::wxGridSelectCells
)
6048 HighlightBlock( coords
, coords
);
6056 // ------------ Left double click
6058 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
6060 DisableCellEditControl();
6062 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
6064 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
6069 // we want double click to select a cell and start editing
6070 // (i.e. to behave in same way as sequence of two slow clicks):
6071 m_waitForSlowClick
= true;
6076 // ------------ Left button released
6078 else if ( event
.LeftUp() )
6080 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6084 if (m_winCapture
->HasCapture())
6085 m_winCapture
->ReleaseMouse();
6086 m_winCapture
= NULL
;
6089 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
6092 EnableCellEditControl();
6094 wxGridCellAttr
*attr
= GetCellAttr(coords
);
6095 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
6096 editor
->StartingClick();
6100 m_waitForSlowClick
= false;
6102 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6103 m_selectingBottomRight
!= wxGridNoCellCoords
)
6107 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6108 m_selectingTopLeft
.GetCol(),
6109 m_selectingBottomRight
.GetRow(),
6110 m_selectingBottomRight
.GetCol(),
6111 event
.ControlDown(),
6117 m_selectingTopLeft
= wxGridNoCellCoords
;
6118 m_selectingBottomRight
= wxGridNoCellCoords
;
6120 // Show the edit control, if it has been hidden for
6122 ShowCellEditControl();
6125 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6127 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6128 DoEndDragResizeRow();
6130 // Note: we are ending the event *after* doing
6131 // default processing in this case
6133 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
6135 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6137 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6138 DoEndDragResizeCol();
6140 // Note: we are ending the event *after* doing
6141 // default processing in this case
6143 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
6149 // ------------ Right button down
6151 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
6153 DisableCellEditControl();
6154 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
6159 // no default action at the moment
6163 // ------------ Right double click
6165 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
6167 DisableCellEditControl();
6168 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
6173 // no default action at the moment
6177 // ------------ Moving and no button action
6179 else if ( event
.Moving() && !event
.IsButton() )
6181 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
6183 // out of grid cell area
6184 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6188 int dragRow
= YToEdgeOfRow( y
);
6189 int dragCol
= XToEdgeOfCol( x
);
6191 // Dragging on the corner of a cell to resize in both
6192 // directions is not implemented yet...
6194 if ( dragRow
>= 0 && dragCol
>= 0 )
6196 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6202 m_dragRowOrCol
= dragRow
;
6204 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6206 if ( CanDragRowSize() && CanDragGridSize() )
6207 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
6210 else if ( dragCol
>= 0 )
6212 m_dragRowOrCol
= dragCol
;
6214 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6216 if ( CanDragColSize() && CanDragGridSize() )
6217 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
6220 else // Neither on a row or col edge
6222 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6224 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6230 void wxGrid::DoEndDragResizeRow()
6232 if ( m_dragLastPos
>= 0 )
6234 // erase the last line and resize the row
6236 int cw
, ch
, left
, dummy
;
6237 m_gridWin
->GetClientSize( &cw
, &ch
);
6238 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6240 wxClientDC
dc( m_gridWin
);
6242 dc
.SetLogicalFunction( wxINVERT
);
6243 dc
.DrawLine( left
, m_dragLastPos
, left
+ cw
, m_dragLastPos
);
6244 HideCellEditControl();
6245 SaveEditControlValue();
6247 int rowTop
= GetRowTop(m_dragRowOrCol
);
6248 SetRowSize( m_dragRowOrCol
,
6249 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
6251 if ( !GetBatchCount() )
6253 // Only needed to get the correct rect.y:
6254 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
6256 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6257 rect
.width
= m_rowLabelWidth
;
6258 rect
.height
= ch
- rect
.y
;
6259 m_rowLabelWin
->Refresh( true, &rect
);
6262 // if there is a multicell block, paint all of it
6265 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
6266 int leftCol
= XToCol(left
);
6267 int rightCol
= internalXToCol(left
+ cw
);
6270 for (i
=leftCol
; i
<rightCol
; i
++)
6272 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
6273 if (cell_rows
< subtract_rows
)
6274 subtract_rows
= cell_rows
;
6276 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
6277 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6278 rect
.height
= ch
- rect
.y
;
6281 m_gridWin
->Refresh( false, &rect
);
6284 ShowCellEditControl();
6289 void wxGrid::DoEndDragResizeCol()
6291 if ( m_dragLastPos
>= 0 )
6293 // erase the last line and resize the col
6295 int cw
, ch
, dummy
, top
;
6296 m_gridWin
->GetClientSize( &cw
, &ch
);
6297 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6299 wxClientDC
dc( m_gridWin
);
6301 dc
.SetLogicalFunction( wxINVERT
);
6302 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6303 HideCellEditControl();
6304 SaveEditControlValue();
6306 int colLeft
= GetColLeft(m_dragRowOrCol
);
6307 SetColSize( m_dragRowOrCol
,
6308 wxMax( m_dragLastPos
- colLeft
,
6309 GetColMinimalWidth(m_dragRowOrCol
) ) );
6311 if ( !GetBatchCount() )
6313 // Only needed to get the correct rect.x:
6314 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
6316 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6317 rect
.width
= cw
- rect
.x
;
6318 rect
.height
= m_colLabelHeight
;
6319 m_colLabelWin
->Refresh( true, &rect
);
6322 // if there is a multicell block, paint all of it
6325 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
6326 int topRow
= YToRow(top
);
6327 int bottomRow
= internalYToRow(top
+ cw
);
6330 for (i
=topRow
; i
<bottomRow
; i
++)
6332 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
6333 if (cell_cols
< subtract_cols
)
6334 subtract_cols
= cell_cols
;
6337 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
6338 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6339 rect
.width
= cw
- rect
.x
;
6343 m_gridWin
->Refresh( false, &rect
);
6346 ShowCellEditControl();
6350 void wxGrid::DoEndDragMoveCol()
6352 //The user clicked on the column but didn't actually drag
6353 if ( m_dragLastPos
< 0 )
6355 m_colLabelWin
->Refresh(); //Do this to "unpress" the column
6360 if ( m_moveToCol
== -1 )
6361 newPos
= m_numCols
- 1;
6364 newPos
= GetColPos( m_moveToCol
);
6365 if ( newPos
> GetColPos( m_dragRowOrCol
) )
6369 SetColPos( m_dragRowOrCol
, newPos
);
6372 void wxGrid::SetColPos( int colID
, int newPos
)
6374 if ( m_colAt
.IsEmpty() )
6376 m_colAt
.Alloc( m_numCols
);
6379 for ( i
= 0; i
< m_numCols
; i
++ )
6385 int oldPos
= GetColPos( colID
);
6387 //Reshuffle the m_colAt array
6388 if ( newPos
> oldPos
)
6391 for ( i
= oldPos
; i
< newPos
; i
++ )
6393 m_colAt
[i
] = m_colAt
[i
+1];
6399 for ( i
= oldPos
; i
> newPos
; i
-- )
6401 m_colAt
[i
] = m_colAt
[i
-1];
6405 m_colAt
[newPos
] = colID
;
6407 //Recalculate the column rights
6408 if ( !m_colWidths
.IsEmpty() )
6412 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6414 int colID
= GetColAt( colPos
);
6416 colRight
+= m_colWidths
[colID
];
6417 m_colRights
[colID
] = colRight
;
6421 m_colLabelWin
->Refresh();
6422 m_gridWin
->Refresh();
6427 void wxGrid::EnableDragColMove( bool enable
)
6429 if ( m_canDragColMove
== enable
)
6432 m_canDragColMove
= enable
;
6434 if ( !m_canDragColMove
)
6438 //Recalculate the column rights
6439 if ( !m_colWidths
.IsEmpty() )
6443 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6445 colRight
+= m_colWidths
[colPos
];
6446 m_colRights
[colPos
] = colRight
;
6450 m_colLabelWin
->Refresh();
6451 m_gridWin
->Refresh();
6457 // ------ interaction with data model
6459 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6461 switch ( msg
.GetId() )
6463 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6464 return GetModelValues();
6466 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6467 return SetModelValues();
6469 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6470 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6471 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6472 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6473 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6474 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6475 return Redimension( msg
);
6482 // The behaviour of this function depends on the grid table class
6483 // Clear() function. For the default wxGridStringTable class the
6484 // behavious is to replace all cell contents with wxEmptyString but
6485 // not to change the number of rows or cols.
6487 void wxGrid::ClearGrid()
6491 if (IsCellEditControlEnabled())
6492 DisableCellEditControl();
6495 if (!GetBatchCount())
6496 m_gridWin
->Refresh();
6500 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6502 // TODO: something with updateLabels flag
6506 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6512 if (IsCellEditControlEnabled())
6513 DisableCellEditControl();
6515 bool done
= m_table
->InsertRows( pos
, numRows
);
6518 // the table will have sent the results of the insert row
6519 // operation to this view object as a grid table message
6525 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6527 // TODO: something with updateLabels flag
6531 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6537 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6540 // the table will have sent the results of the append row
6541 // operation to this view object as a grid table message
6547 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6549 // TODO: something with updateLabels flag
6553 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6559 if (IsCellEditControlEnabled())
6560 DisableCellEditControl();
6562 bool done
= m_table
->DeleteRows( pos
, numRows
);
6564 // the table will have sent the results of the delete row
6565 // operation to this view object as a grid table message
6571 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6573 // TODO: something with updateLabels flag
6577 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6583 if (IsCellEditControlEnabled())
6584 DisableCellEditControl();
6586 bool done
= m_table
->InsertCols( pos
, numCols
);
6588 // the table will have sent the results of the insert col
6589 // operation to this view object as a grid table message
6595 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6597 // TODO: something with updateLabels flag
6601 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6607 bool done
= m_table
->AppendCols( numCols
);
6609 // the table will have sent the results of the append col
6610 // operation to this view object as a grid table message
6616 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6618 // TODO: something with updateLabels flag
6622 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6628 if (IsCellEditControlEnabled())
6629 DisableCellEditControl();
6631 bool done
= m_table
->DeleteCols( pos
, numCols
);
6633 // the table will have sent the results of the delete col
6634 // operation to this view object as a grid table message
6641 // ----- event handlers
6644 // Generate a grid event based on a mouse event and
6645 // return the result of ProcessEvent()
6647 int wxGrid::SendEvent( const wxEventType type
,
6649 wxMouseEvent
& mouseEv
)
6651 bool claimed
, vetoed
;
6653 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6655 int rowOrCol
= (row
== -1 ? col
: row
);
6657 wxGridSizeEvent
gridEvt( GetId(),
6661 mouseEv
.GetX() + GetRowLabelSize(),
6662 mouseEv
.GetY() + GetColLabelSize(),
6663 mouseEv
.ControlDown(),
6664 mouseEv
.ShiftDown(),
6666 mouseEv
.MetaDown() );
6668 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6669 vetoed
= !gridEvt
.IsAllowed();
6671 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6673 // Right now, it should _never_ end up here!
6674 wxGridRangeSelectEvent
gridEvt( GetId(),
6678 m_selectingBottomRight
,
6680 mouseEv
.ControlDown(),
6681 mouseEv
.ShiftDown(),
6683 mouseEv
.MetaDown() );
6685 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6686 vetoed
= !gridEvt
.IsAllowed();
6688 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
6689 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
6690 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
6691 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
6693 wxPoint pos
= mouseEv
.GetPosition();
6695 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
6696 pos
.y
+= GetColLabelSize();
6697 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
6698 pos
.x
+= GetRowLabelSize();
6700 wxGridEvent
gridEvt( GetId(),
6707 mouseEv
.ControlDown(),
6708 mouseEv
.ShiftDown(),
6710 mouseEv
.MetaDown() );
6711 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6712 vetoed
= !gridEvt
.IsAllowed();
6716 wxGridEvent
gridEvt( GetId(),
6720 mouseEv
.GetX() + GetRowLabelSize(),
6721 mouseEv
.GetY() + GetColLabelSize(),
6723 mouseEv
.ControlDown(),
6724 mouseEv
.ShiftDown(),
6726 mouseEv
.MetaDown() );
6727 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6728 vetoed
= !gridEvt
.IsAllowed();
6731 // A Veto'd event may not be `claimed' so test this first
6735 return claimed
? 1 : 0;
6738 // Generate a grid event of specified type and return the result
6739 // of ProcessEvent().
6741 int wxGrid::SendEvent( const wxEventType type
,
6744 bool claimed
, vetoed
;
6746 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6748 int rowOrCol
= (row
== -1 ? col
: row
);
6750 wxGridSizeEvent
gridEvt( GetId(), type
, this, rowOrCol
);
6752 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6753 vetoed
= !gridEvt
.IsAllowed();
6757 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
6759 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6760 vetoed
= !gridEvt
.IsAllowed();
6763 // A Veto'd event may not be `claimed' so test this first
6767 return claimed
? 1 : 0;
6770 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6772 // needed to prevent zillions of paint events on MSW
6776 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6778 // Don't do anything if between Begin/EndBatch...
6779 // EndBatch() will do all this on the last nested one anyway.
6780 if (! GetBatchCount())
6782 // Refresh to get correct scrolled position:
6783 wxScrolledWindow::Refresh(eraseb
, rect
);
6787 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6788 int width_label
, width_cell
, height_label
, height_cell
;
6791 // Copy rectangle can get scroll offsets..
6792 rect_x
= rect
->GetX();
6793 rect_y
= rect
->GetY();
6794 rectWidth
= rect
->GetWidth();
6795 rectHeight
= rect
->GetHeight();
6797 width_label
= m_rowLabelWidth
- rect_x
;
6798 if (width_label
> rectWidth
)
6799 width_label
= rectWidth
;
6801 height_label
= m_colLabelHeight
- rect_y
;
6802 if (height_label
> rectHeight
)
6803 height_label
= rectHeight
;
6805 if (rect_x
> m_rowLabelWidth
)
6807 x
= rect_x
- m_rowLabelWidth
;
6808 width_cell
= rectWidth
;
6813 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6816 if (rect_y
> m_colLabelHeight
)
6818 y
= rect_y
- m_colLabelHeight
;
6819 height_cell
= rectHeight
;
6824 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6827 // Paint corner label part intersecting rect.
6828 if ( width_label
> 0 && height_label
> 0 )
6830 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6831 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6834 // Paint col labels part intersecting rect.
6835 if ( width_cell
> 0 && height_label
> 0 )
6837 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6838 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6841 // Paint row labels part intersecting rect.
6842 if ( width_label
> 0 && height_cell
> 0 )
6844 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6845 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6848 // Paint cell area part intersecting rect.
6849 if ( width_cell
> 0 && height_cell
> 0 )
6851 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6852 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6857 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6858 m_colLabelWin
->Refresh(eraseb
, NULL
);
6859 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6860 m_gridWin
->Refresh(eraseb
, NULL
);
6865 void wxGrid::OnSize( wxSizeEvent
& event
)
6867 // position the child windows
6870 // don't call CalcDimensions() from here, the base class handles the size
6875 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6877 if ( m_inOnKeyDown
)
6879 // shouldn't be here - we are going round in circles...
6881 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6884 m_inOnKeyDown
= true;
6886 // propagate the event up and see if it gets processed
6887 wxWindow
*parent
= GetParent();
6888 wxKeyEvent
keyEvt( event
);
6889 keyEvt
.SetEventObject( parent
);
6891 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6893 if (GetLayoutDirection() == wxLayout_RightToLeft
)
6895 if (event
.GetKeyCode() == WXK_RIGHT
)
6896 event
.m_keyCode
= WXK_LEFT
;
6897 else if (event
.GetKeyCode() == WXK_LEFT
)
6898 event
.m_keyCode
= WXK_RIGHT
;
6901 // try local handlers
6902 switch ( event
.GetKeyCode() )
6905 if ( event
.ControlDown() )
6906 MoveCursorUpBlock( event
.ShiftDown() );
6908 MoveCursorUp( event
.ShiftDown() );
6912 if ( event
.ControlDown() )
6913 MoveCursorDownBlock( event
.ShiftDown() );
6915 MoveCursorDown( event
.ShiftDown() );
6919 if ( event
.ControlDown() )
6920 MoveCursorLeftBlock( event
.ShiftDown() );
6922 MoveCursorLeft( event
.ShiftDown() );
6926 if ( event
.ControlDown() )
6927 MoveCursorRightBlock( event
.ShiftDown() );
6929 MoveCursorRight( event
.ShiftDown() );
6933 case WXK_NUMPAD_ENTER
:
6934 if ( event
.ControlDown() )
6936 event
.Skip(); // to let the edit control have the return
6940 if ( GetGridCursorRow() < GetNumberRows()-1 )
6942 MoveCursorDown( event
.ShiftDown() );
6946 // at the bottom of a column
6947 DisableCellEditControl();
6957 if (event
.ShiftDown())
6959 if ( GetGridCursorCol() > 0 )
6961 MoveCursorLeft( false );
6966 DisableCellEditControl();
6971 if ( GetGridCursorCol() < GetNumberCols() - 1 )
6973 MoveCursorRight( false );
6978 DisableCellEditControl();
6984 if ( event
.ControlDown() )
6986 MakeCellVisible( 0, 0 );
6987 SetCurrentCell( 0, 0 );
6996 if ( event
.ControlDown() )
6998 MakeCellVisible( m_numRows
- 1, m_numCols
- 1 );
6999 SetCurrentCell( m_numRows
- 1, m_numCols
- 1 );
7016 if ( event
.ControlDown() )
7020 m_selection
->ToggleCellSelection(
7021 m_currentCellCoords
.GetRow(),
7022 m_currentCellCoords
.GetCol(),
7023 event
.ControlDown(),
7031 if ( !IsEditable() )
7032 MoveCursorRight( false );
7043 m_inOnKeyDown
= false;
7046 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
7048 // try local handlers
7050 if ( event
.GetKeyCode() == WXK_SHIFT
)
7052 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
7053 m_selectingBottomRight
!= wxGridNoCellCoords
)
7057 m_selection
->SelectBlock(
7058 m_selectingTopLeft
.GetRow(),
7059 m_selectingTopLeft
.GetCol(),
7060 m_selectingBottomRight
.GetRow(),
7061 m_selectingBottomRight
.GetCol(),
7062 event
.ControlDown(),
7069 m_selectingTopLeft
= wxGridNoCellCoords
;
7070 m_selectingBottomRight
= wxGridNoCellCoords
;
7071 m_selectingKeyboard
= wxGridNoCellCoords
;
7075 void wxGrid::OnChar( wxKeyEvent
& event
)
7077 // is it possible to edit the current cell at all?
7078 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7080 // yes, now check whether the cells editor accepts the key
7081 int row
= m_currentCellCoords
.GetRow();
7082 int col
= m_currentCellCoords
.GetCol();
7083 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7084 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7086 // <F2> is special and will always start editing, for
7087 // other keys - ask the editor itself
7088 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
7089 || editor
->IsAcceptedKey(event
) )
7091 // ensure cell is visble
7092 MakeCellVisible(row
, col
);
7093 EnableCellEditControl();
7095 // a problem can arise if the cell is not completely
7096 // visible (even after calling MakeCellVisible the
7097 // control is not created and calling StartingKey will
7099 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
7100 editor
->StartingKey(event
);
7116 void wxGrid::OnEraseBackground(wxEraseEvent
&)
7120 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
7122 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
7124 // the event has been intercepted - do nothing
7128 #if !(defined(__WXMAC__) && wxMAC_USE_CORE_GRAPHICS)
7129 wxClientDC
dc( m_gridWin
);
7133 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
7135 DisableCellEditControl();
7137 if ( IsVisible( m_currentCellCoords
, false ) )
7140 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
7141 if ( !m_gridLinesEnabled
)
7149 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
7151 // Otherwise refresh redraws the highlight!
7152 m_currentCellCoords
= coords
;
7154 #if defined(__WXMAC__) && wxMAC_USE_CORE_GRAPHICS
7155 m_gridWin
->Refresh(true /*, & r */);
7157 DrawGridCellArea( dc
, cells
);
7158 DrawAllGridLines( dc
, r
);
7163 m_currentCellCoords
= coords
;
7165 wxGridCellAttr
*attr
= GetCellAttr( coords
);
7166 #if !(defined(__WXMAC__) && wxMAC_USE_CORE_GRAPHICS)
7167 DrawCellHighlight( dc
, attr
);
7172 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7175 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7179 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
7182 rightCol
= GetNumberCols() - 1;
7184 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
7187 bottomRow
= GetNumberRows() - 1;
7191 if ( topRow
> bottomRow
)
7198 if ( leftCol
> rightCol
)
7205 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7206 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7208 // First the case that we selected a completely new area
7209 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
7210 m_selectingBottomRight
== wxGridNoCellCoords
)
7213 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
7214 wxGridCellCoords ( bottomRow
, rightCol
) );
7215 m_gridWin
->Refresh( false, &rect
);
7218 // Now handle changing an existing selection area.
7219 else if ( m_selectingTopLeft
!= updateTopLeft
||
7220 m_selectingBottomRight
!= updateBottomRight
)
7222 // Compute two optimal update rectangles:
7223 // Either one rectangle is a real subset of the
7224 // other, or they are (almost) disjoint!
7226 bool need_refresh
[4];
7230 need_refresh
[3] = false;
7233 // Store intermediate values
7234 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
7235 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
7236 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
7237 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
7239 // Determine the outer/inner coordinates.
7240 if (oldLeft
> leftCol
)
7246 if (oldTop
> topRow
)
7252 if (oldRight
< rightCol
)
7255 oldRight
= rightCol
;
7258 if (oldBottom
< bottomRow
)
7261 oldBottom
= bottomRow
;
7265 // Now, either the stuff marked old is the outer
7266 // rectangle or we don't have a situation where one
7267 // is contained in the other.
7269 if ( oldLeft
< leftCol
)
7271 // Refresh the newly selected or deselected
7272 // area to the left of the old or new selection.
7273 need_refresh
[0] = true;
7274 rect
[0] = BlockToDeviceRect(
7275 wxGridCellCoords( oldTop
, oldLeft
),
7276 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
7279 if ( oldTop
< topRow
)
7281 // Refresh the newly selected or deselected
7282 // area above the old or new selection.
7283 need_refresh
[1] = true;
7284 rect
[1] = BlockToDeviceRect(
7285 wxGridCellCoords( oldTop
, leftCol
),
7286 wxGridCellCoords( topRow
- 1, rightCol
) );
7289 if ( oldRight
> rightCol
)
7291 // Refresh the newly selected or deselected
7292 // area to the right of the old or new selection.
7293 need_refresh
[2] = true;
7294 rect
[2] = BlockToDeviceRect(
7295 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
7296 wxGridCellCoords( oldBottom
, oldRight
) );
7299 if ( oldBottom
> bottomRow
)
7301 // Refresh the newly selected or deselected
7302 // area below the old or new selection.
7303 need_refresh
[3] = true;
7304 rect
[3] = BlockToDeviceRect(
7305 wxGridCellCoords( bottomRow
+ 1, leftCol
),
7306 wxGridCellCoords( oldBottom
, rightCol
) );
7309 // various Refresh() calls
7310 for (i
= 0; i
< 4; i
++ )
7311 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7312 m_gridWin
->Refresh( false, &(rect
[i
]) );
7316 m_selectingTopLeft
= updateTopLeft
;
7317 m_selectingBottomRight
= updateBottomRight
;
7321 // ------ functions to get/send data (see also public functions)
7324 bool wxGrid::GetModelValues()
7326 // Hide the editor, so it won't hide a changed value.
7327 HideCellEditControl();
7331 // all we need to do is repaint the grid
7333 m_gridWin
->Refresh();
7340 bool wxGrid::SetModelValues()
7344 // Disable the editor, so it won't hide a changed value.
7345 // Do we also want to save the current value of the editor first?
7347 DisableCellEditControl();
7351 for ( row
= 0; row
< m_numRows
; row
++ )
7353 for ( col
= 0; col
< m_numCols
; col
++ )
7355 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
7365 // Note - this function only draws cells that are in the list of
7366 // exposed cells (usually set from the update region by
7367 // CalcExposedCells)
7369 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7371 if ( !m_numRows
|| !m_numCols
)
7374 int i
, numCells
= cells
.GetCount();
7375 int row
, col
, cell_rows
, cell_cols
;
7376 wxGridCellCoordsArray redrawCells
;
7378 for ( i
= numCells
- 1; i
>= 0; i
-- )
7380 row
= cells
[i
].GetRow();
7381 col
= cells
[i
].GetCol();
7382 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7384 // If this cell is part of a multicell block, find owner for repaint
7385 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7387 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
7388 bool marked
= false;
7389 for ( int j
= 0; j
< numCells
; j
++ )
7391 if ( cell
== cells
[j
] )
7400 int count
= redrawCells
.GetCount();
7401 for (int j
= 0; j
< count
; j
++)
7403 if ( cell
== redrawCells
[j
] )
7411 redrawCells
.Add( cell
);
7414 // don't bother drawing this cell
7418 // If this cell is empty, find cell to left that might want to overflow
7419 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7421 for ( int l
= 0; l
< cell_rows
; l
++ )
7423 // find a cell in this row to leave already marked for repaint
7425 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7426 if ((redrawCells
[k
].GetCol() < left
) &&
7427 (redrawCells
[k
].GetRow() == row
))
7429 left
= redrawCells
[k
].GetCol();
7433 left
= 0; // oh well
7435 for (int j
= col
- 1; j
>= left
; j
--)
7437 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7439 if (GetCellOverflow(row
+ l
, j
))
7441 wxGridCellCoords
cell(row
+ l
, j
);
7442 bool marked
= false;
7444 for (int k
= 0; k
< numCells
; k
++)
7446 if ( cell
== cells
[k
] )
7455 int count
= redrawCells
.GetCount();
7456 for (int k
= 0; k
< count
; k
++)
7458 if ( cell
== redrawCells
[k
] )
7465 redrawCells
.Add( cell
);
7474 DrawCell( dc
, cells
[i
] );
7477 numCells
= redrawCells
.GetCount();
7479 for ( i
= numCells
- 1; i
>= 0; i
-- )
7481 DrawCell( dc
, redrawCells
[i
] );
7485 void wxGrid::DrawGridSpace( wxDC
& dc
)
7488 m_gridWin
->GetClientSize( &cw
, &ch
);
7491 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7493 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
7494 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7496 if ( right
> rightCol
|| bottom
> bottomRow
)
7499 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7501 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
7502 dc
.SetPen( *wxTRANSPARENT_PEN
);
7504 if ( right
> rightCol
)
7506 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7509 if ( bottom
> bottomRow
)
7511 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7516 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7518 int row
= coords
.GetRow();
7519 int col
= coords
.GetCol();
7521 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7524 // we draw the cell border ourselves
7525 #if !WXGRID_DRAW_LINES
7526 if ( m_gridLinesEnabled
)
7527 DrawCellBorder( dc
, coords
);
7530 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7532 bool isCurrent
= coords
== m_currentCellCoords
;
7534 wxRect rect
= CellToRect( row
, col
);
7536 // if the editor is shown, we should use it and not the renderer
7537 // Note: However, only if it is really _shown_, i.e. not hidden!
7538 if ( isCurrent
&& IsCellEditControlShown() )
7540 // NB: this "#if..." is temporary and fixes a problem where the
7541 // edit control is erased by this code after being rendered.
7542 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7543 // implicitly, causing this out-of order render.
7544 #if !defined(__WXMAC__)
7545 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7546 editor
->PaintBackground(rect
, attr
);
7552 // but all the rest is drawn by the cell renderer and hence may be customized
7553 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7554 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7561 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7563 int row
= m_currentCellCoords
.GetRow();
7564 int col
= m_currentCellCoords
.GetCol();
7566 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7569 wxRect rect
= CellToRect(row
, col
);
7571 // hmmm... what could we do here to show that the cell is disabled?
7572 // for now, I just draw a thinner border than for the other ones, but
7573 // it doesn't look really good
7575 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7579 // The center of the drawn line is where the position/width/height of
7580 // the rectangle is actually at (on wxMSW at least), so the
7581 // size of the rectangle is reduced to compensate for the thickness of
7582 // the line. If this is too strange on non-wxMSW platforms then
7583 // please #ifdef this appropriately.
7584 rect
.x
+= penWidth
/ 2;
7585 rect
.y
+= penWidth
/ 2;
7586 rect
.width
-= penWidth
- 1;
7587 rect
.height
-= penWidth
- 1;
7589 // Now draw the rectangle
7590 // use the cellHighlightColour if the cell is inside a selection, this
7591 // will ensure the cell is always visible.
7592 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
: m_cellHighlightColour
, penWidth
, wxSOLID
));
7593 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7594 dc
.DrawRectangle(rect
);
7598 // VZ: my experiments with 3D borders...
7600 // how to properly set colours for arbitrary bg?
7601 wxCoord x1
= rect
.x
,
7603 x2
= rect
.x
+ rect
.width
- 1,
7604 y2
= rect
.y
+ rect
.height
- 1;
7606 dc
.SetPen(*wxWHITE_PEN
);
7607 dc
.DrawLine(x1
, y1
, x2
, y1
);
7608 dc
.DrawLine(x1
, y1
, x1
, y2
);
7610 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7611 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7613 dc
.SetPen(*wxBLACK_PEN
);
7614 dc
.DrawLine(x1
, y2
, x2
, y2
);
7615 dc
.DrawLine(x2
, y1
, x2
, y2
+ 1);
7619 wxPen
wxGrid::GetDefaultGridLinePen()
7621 return wxPen(GetGridLineColour(), 1, wxSOLID
);
7624 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
7626 return GetDefaultGridLinePen();
7629 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
7631 return GetDefaultGridLinePen();
7634 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7636 int row
= coords
.GetRow();
7637 int col
= coords
.GetCol();
7638 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7642 wxRect rect
= CellToRect( row
, col
);
7644 // right hand border
7645 dc
.SetPen( GetColGridLinePen(col
) );
7646 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7647 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7650 dc
.SetPen( GetRowGridLinePen(row
) );
7651 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7652 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7655 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7657 // This if block was previously in wxGrid::OnPaint but that doesn't
7658 // seem to get called under wxGTK - MB
7660 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7661 m_numRows
&& m_numCols
)
7663 m_currentCellCoords
.Set(0, 0);
7666 if ( IsCellEditControlShown() )
7668 // don't show highlight when the edit control is shown
7672 // if the active cell was repainted, repaint its highlight too because it
7673 // might have been damaged by the grid lines
7674 size_t count
= cells
.GetCount();
7675 for ( size_t n
= 0; n
< count
; n
++ )
7677 if ( cells
[n
] == m_currentCellCoords
)
7679 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7680 DrawCellHighlight(dc
, attr
);
7688 // TODO: remove this ???
7689 // This is used to redraw all grid lines e.g. when the grid line colour
7692 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7694 #if !WXGRID_DRAW_LINES
7698 if ( !m_gridLinesEnabled
|| !m_numRows
|| !m_numCols
)
7701 int top
, bottom
, left
, right
;
7703 #if 0 //#ifndef __WXGTK__
7707 m_gridWin
->GetClientSize(&cw
, &ch
);
7709 // virtual coords of visible area
7711 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7712 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7717 reg
.GetBox(x
, y
, w
, h
);
7718 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7719 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7723 m_gridWin
->GetClientSize(&cw
, &ch
);
7724 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7725 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7728 // avoid drawing grid lines past the last row and col
7730 right
= wxMin( right
, GetColRight(GetColAt( m_numCols
- 1 )) );
7731 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7733 // no gridlines inside multicells, clip them out
7734 int leftCol
= GetColPos( internalXToCol(left
) );
7735 int topRow
= internalYToRow(top
);
7736 int rightCol
= GetColPos( internalXToCol(right
) );
7737 int bottomRow
= internalYToRow(bottom
);
7739 #if !defined(__WXMAC__) || wxMAC_USE_CORE_GRAPHICS
7740 wxRegion
clippedcells(0, 0, cw
, ch
);
7742 int i
, j
, cell_rows
, cell_cols
;
7745 for (j
=topRow
; j
<=bottomRow
; j
++)
7748 for (colPos
=leftCol
; colPos
<=rightCol
; colPos
++)
7750 i
= GetColAt( colPos
);
7752 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7753 if ((cell_rows
> 1) || (cell_cols
> 1))
7755 rect
= CellToRect(j
,i
);
7756 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7757 clippedcells
.Subtract(rect
);
7759 else if ((cell_rows
< 0) || (cell_cols
< 0))
7761 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7762 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7763 clippedcells
.Subtract(rect
);
7768 wxRegion
clippedcells( left
, top
, right
- left
, bottom
- top
);
7770 int i
, j
, cell_rows
, cell_cols
;
7773 for (j
=topRow
; j
<=bottomRow
; j
++)
7775 for (i
=leftCol
; i
<=rightCol
; i
++)
7777 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7778 if ((cell_rows
> 1) || (cell_cols
> 1))
7780 rect
= CellToRect(j
, i
);
7781 clippedcells
.Subtract(rect
);
7783 else if ((cell_rows
< 0) || (cell_cols
< 0))
7785 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7786 clippedcells
.Subtract(rect
);
7792 dc
.SetClippingRegion( clippedcells
);
7795 // horizontal grid lines
7797 // already declared above - int i;
7798 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7800 int bot
= GetRowBottom(i
) - 1;
7809 dc
.SetPen( GetRowGridLinePen(i
) );
7810 dc
.DrawLine( left
, bot
, right
, bot
);
7814 // vertical grid lines
7817 for ( colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
7819 i
= GetColAt( colPos
);
7821 int colRight
= GetColRight(i
);
7823 if (GetLayoutDirection() != wxLayout_RightToLeft
)
7827 if ( colRight
> right
)
7832 if ( colRight
>= left
)
7834 dc
.SetPen( GetColGridLinePen(i
) );
7835 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7839 dc
.DestroyClippingRegion();
7842 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
7848 size_t numLabels
= rows
.GetCount();
7850 for ( i
= 0; i
< numLabels
; i
++ )
7852 DrawRowLabel( dc
, rows
[i
] );
7856 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7858 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7866 rect
.SetY( GetRowTop(row
) + 1 );
7867 rect
.SetWidth( m_rowLabelWidth
- 2 );
7868 rect
.SetHeight( GetRowHeight(row
) - 2 );
7870 CalcScrolledPosition( 0, rect
.y
, NULL
, &rect
.y
);
7872 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7874 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7876 int rowTop
= GetRowTop(row
),
7877 rowBottom
= GetRowBottom(row
) - 1;
7879 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
7880 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
7881 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7882 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7884 dc
.SetPen( *wxWHITE_PEN
);
7885 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7886 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
7889 dc
.SetBackgroundMode( wxTRANSPARENT
);
7890 dc
.SetTextForeground( GetLabelTextColour() );
7891 dc
.SetFont( GetLabelFont() );
7894 GetRowLabelAlignment( &hAlign
, &vAlign
);
7897 rect
.SetY( GetRowTop(row
) + 2 );
7898 rect
.SetWidth( m_rowLabelWidth
- 4 );
7899 rect
.SetHeight( GetRowHeight(row
) - 4 );
7900 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7903 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7909 size_t numLabels
= cols
.GetCount();
7911 for ( i
= 0; i
< numLabels
; i
++ )
7913 DrawColLabel( dc
, cols
[i
] );
7917 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7919 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7922 int colLeft
= GetColLeft(col
);
7928 rect
.SetX( colLeft
+ 1 );
7930 rect
.SetWidth( GetColWidth(col
) - 2 );
7931 rect
.SetHeight( m_colLabelHeight
- 2 );
7933 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7935 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7937 int colRight
= GetColRight(col
) - 1;
7939 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
7940 dc
.DrawLine( colRight
, 0, colRight
, m_colLabelHeight
- 1 );
7941 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7942 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
7943 colRight
+ 1, m_colLabelHeight
- 1 );
7945 dc
.SetPen( *wxWHITE_PEN
);
7946 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
7947 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7950 dc
.SetBackgroundMode( wxTRANSPARENT
);
7951 dc
.SetTextForeground( GetLabelTextColour() );
7952 dc
.SetFont( GetLabelFont() );
7954 int hAlign
, vAlign
, orient
;
7955 GetColLabelAlignment( &hAlign
, &vAlign
);
7956 orient
= GetColLabelTextOrientation();
7958 rect
.SetX( colLeft
+ 2 );
7960 rect
.SetWidth( GetColWidth(col
) - 4 );
7961 rect
.SetHeight( m_colLabelHeight
- 4 );
7962 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7965 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7966 const wxString
& value
,
7970 int textOrientation
)
7972 wxArrayString lines
;
7974 StringToLines( value
, lines
);
7976 // Forward to new API.
7977 DrawTextRectangle( dc
,
7985 // VZ: this should be replaced with wxDC::DrawLabel() to which we just have to
7986 // add textOrientation support
7987 void wxGrid::DrawTextRectangle(wxDC
& dc
,
7988 const wxArrayString
& lines
,
7992 int textOrientation
)
7994 if ( lines
.empty() )
7997 wxDCClipper
clip(dc
, rect
);
8002 if ( textOrientation
== wxHORIZONTAL
)
8003 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
8005 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
8009 switch ( vertAlign
)
8011 case wxALIGN_BOTTOM
:
8012 if ( textOrientation
== wxHORIZONTAL
)
8013 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
8015 x
= rect
.x
+ rect
.width
- textWidth
;
8018 case wxALIGN_CENTRE
:
8019 if ( textOrientation
== wxHORIZONTAL
)
8020 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
8022 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
8027 if ( textOrientation
== wxHORIZONTAL
)
8034 // Align each line of a multi-line label
8035 size_t nLines
= lines
.GetCount();
8036 for ( size_t l
= 0; l
< nLines
; l
++ )
8038 const wxString
& line
= lines
[l
];
8042 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
8048 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
8050 switch ( horizAlign
)
8053 if ( textOrientation
== wxHORIZONTAL
)
8054 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
8056 y
= rect
.y
+ lineWidth
+ 1;
8059 case wxALIGN_CENTRE
:
8060 if ( textOrientation
== wxHORIZONTAL
)
8061 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
8063 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
8068 if ( textOrientation
== wxHORIZONTAL
)
8071 y
= rect
.y
+ rect
.height
- 1;
8075 if ( textOrientation
== wxHORIZONTAL
)
8077 dc
.DrawText( line
, x
, y
);
8082 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
8088 // Split multi-line text up into an array of strings.
8089 // Any existing contents of the string array are preserved.
8091 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
8095 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
8096 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
8098 while ( startPos
< (int)tVal
.length() )
8100 pos
= tVal
.Mid(startPos
).Find( eol
);
8105 else if ( pos
== 0 )
8107 lines
.Add( wxEmptyString
);
8111 lines
.Add( value
.Mid(startPos
, pos
) );
8114 startPos
+= pos
+ 1;
8117 if ( startPos
< (int)value
.length() )
8119 lines
.Add( value
.Mid( startPos
) );
8123 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
8124 const wxArrayString
& lines
,
8125 long *width
, long *height
)
8129 long lineW
= 0, lineH
= 0;
8132 for ( i
= 0; i
< lines
.GetCount(); i
++ )
8134 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
8135 w
= wxMax( w
, lineW
);
8144 // ------ Batch processing.
8146 void wxGrid::EndBatch()
8148 if ( m_batchCount
> 0 )
8151 if ( !m_batchCount
)
8154 m_rowLabelWin
->Refresh();
8155 m_colLabelWin
->Refresh();
8156 m_cornerLabelWin
->Refresh();
8157 m_gridWin
->Refresh();
8162 // Use this, rather than wxWindow::Refresh(), to force an immediate
8163 // repainting of the grid. Has no effect if you are already inside a
8164 // BeginBatch / EndBatch block.
8166 void wxGrid::ForceRefresh()
8172 bool wxGrid::Enable(bool enable
)
8174 if ( !wxScrolledWindow::Enable(enable
) )
8177 // redraw in the new state
8178 m_gridWin
->Refresh();
8184 // ------ Edit control functions
8187 void wxGrid::EnableEditing( bool edit
)
8189 // TODO: improve this ?
8191 if ( edit
!= m_editable
)
8194 EnableCellEditControl(edit
);
8199 void wxGrid::EnableCellEditControl( bool enable
)
8204 if ( enable
!= m_cellEditCtrlEnabled
)
8208 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
8211 // this should be checked by the caller!
8212 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8214 // do it before ShowCellEditControl()
8215 m_cellEditCtrlEnabled
= enable
;
8217 ShowCellEditControl();
8221 //FIXME:add veto support
8222 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
8224 HideCellEditControl();
8225 SaveEditControlValue();
8227 // do it after HideCellEditControl()
8228 m_cellEditCtrlEnabled
= enable
;
8233 bool wxGrid::IsCurrentCellReadOnly() const
8236 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
8237 bool readonly
= attr
->IsReadOnly();
8243 bool wxGrid::CanEnableCellControl() const
8245 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
8246 !IsCurrentCellReadOnly();
8249 bool wxGrid::IsCellEditControlEnabled() const
8251 // the cell edit control might be disable for all cells or just for the
8252 // current one if it's read only
8253 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
8256 bool wxGrid::IsCellEditControlShown() const
8258 bool isShown
= false;
8260 if ( m_cellEditCtrlEnabled
)
8262 int row
= m_currentCellCoords
.GetRow();
8263 int col
= m_currentCellCoords
.GetCol();
8264 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8265 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
8270 if ( editor
->IsCreated() )
8272 isShown
= editor
->GetControl()->IsShown();
8282 void wxGrid::ShowCellEditControl()
8284 if ( IsCellEditControlEnabled() )
8286 if ( !IsVisible( m_currentCellCoords
, false ) )
8288 m_cellEditCtrlEnabled
= false;
8293 wxRect rect
= CellToRect( m_currentCellCoords
);
8294 int row
= m_currentCellCoords
.GetRow();
8295 int col
= m_currentCellCoords
.GetCol();
8297 // if this is part of a multicell, find owner (topleft)
8298 int cell_rows
, cell_cols
;
8299 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8300 if ( cell_rows
<= 0 || cell_cols
<= 0 )
8304 m_currentCellCoords
.SetRow( row
);
8305 m_currentCellCoords
.SetCol( col
);
8308 // erase the highlight and the cell contents because the editor
8309 // might not cover the entire cell
8310 wxClientDC
dc( m_gridWin
);
8312 dc
.SetBrush(wxBrush(GetCellAttr(row
, col
)->GetBackgroundColour(), wxSOLID
));
8313 dc
.SetPen(*wxTRANSPARENT_PEN
);
8314 dc
.DrawRectangle(rect
);
8316 // convert to scrolled coords
8317 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8323 // cell is shifted by one pixel
8324 // However, don't allow x or y to become negative
8325 // since the SetSize() method interprets that as
8332 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8333 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8334 if ( !editor
->IsCreated() )
8336 editor
->Create(m_gridWin
, wxID_ANY
,
8337 new wxGridCellEditorEvtHandler(this, editor
));
8339 wxGridEditorCreatedEvent
evt(GetId(),
8340 wxEVT_GRID_EDITOR_CREATED
,
8344 editor
->GetControl());
8345 GetEventHandler()->ProcessEvent(evt
);
8348 // resize editor to overflow into righthand cells if allowed
8349 int maxWidth
= rect
.width
;
8350 wxString value
= GetCellValue(row
, col
);
8351 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
8354 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
8355 if (maxWidth
< rect
.width
)
8356 maxWidth
= rect
.width
;
8359 int client_right
= m_gridWin
->GetClientSize().GetWidth();
8360 if (rect
.x
+ maxWidth
> client_right
)
8361 maxWidth
= client_right
- rect
.x
;
8363 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
8365 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8366 // may have changed earlier
8367 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
8370 GetCellSize( row
, i
, &c_rows
, &c_cols
);
8372 // looks weird going over a multicell
8373 if (m_table
->IsEmptyCell( row
, i
) &&
8374 (rect
.width
< maxWidth
) && (c_rows
== 1))
8376 rect
.width
+= GetColWidth( i
);
8382 if (rect
.GetRight() > client_right
)
8383 rect
.SetRight( client_right
- 1 );
8386 editor
->SetCellAttr( attr
);
8387 editor
->SetSize( rect
);
8389 editor
->GetControl()->Move(
8390 editor
->GetControl()->GetPosition().x
+ nXMove
,
8391 editor
->GetControl()->GetPosition().y
);
8392 editor
->Show( true, attr
);
8394 // recalc dimensions in case we need to
8395 // expand the scrolled window to account for editor
8398 editor
->BeginEdit(row
, col
, this);
8399 editor
->SetCellAttr(NULL
);
8407 void wxGrid::HideCellEditControl()
8409 if ( IsCellEditControlEnabled() )
8411 int row
= m_currentCellCoords
.GetRow();
8412 int col
= m_currentCellCoords
.GetCol();
8414 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8415 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
8416 editor
->Show( false );
8420 m_gridWin
->SetFocus();
8422 // refresh whole row to the right
8423 wxRect
rect( CellToRect(row
, col
) );
8424 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8425 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8428 // ensure that the pixels under the focus ring get refreshed as well
8429 rect
.Inflate(10, 10);
8432 m_gridWin
->Refresh( false, &rect
);
8436 void wxGrid::SaveEditControlValue()
8438 if ( IsCellEditControlEnabled() )
8440 int row
= m_currentCellCoords
.GetRow();
8441 int col
= m_currentCellCoords
.GetCol();
8443 wxString oldval
= GetCellValue(row
, col
);
8445 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8446 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8447 bool changed
= editor
->EndEdit(row
, col
, this);
8454 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
8455 m_currentCellCoords
.GetRow(),
8456 m_currentCellCoords
.GetCol() ) < 0 )
8458 // Event has been vetoed, set the data back.
8459 SetCellValue(row
, col
, oldval
);
8466 // ------ Grid location functions
8467 // Note that all of these functions work with the logical coordinates of
8468 // grid cells and labels so you will need to convert from device
8469 // coordinates for mouse events etc.
8472 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
8474 int row
= YToRow(y
);
8475 int col
= XToCol(x
);
8477 if ( row
== -1 || col
== -1 )
8479 coords
= wxGridNoCellCoords
;
8483 coords
.Set( row
, col
);
8487 // Internal Helper function for computing row or column from some
8488 // (unscrolled) coordinate value, using either
8489 // m_defaultRowHeight/m_defaultColWidth or binary search on array
8490 // of m_rowBottoms/m_ColRights to speed up the search!
8492 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
8493 const wxArrayInt
& BorderArray
, int nMax
,
8497 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
8502 size_t i_max
= coord
/ defaultDist
,
8505 if (BorderArray
.IsEmpty())
8507 if ((int) i_max
< nMax
)
8509 return clipToMinMax
? nMax
- 1 : -1;
8512 if ( i_max
>= BorderArray
.GetCount())
8514 i_max
= BorderArray
.GetCount() - 1;
8518 if ( coord
>= BorderArray
[i_max
])
8522 i_max
= coord
/ minDist
;
8524 i_max
= BorderArray
.GetCount() - 1;
8527 if ( i_max
>= BorderArray
.GetCount())
8528 i_max
= BorderArray
.GetCount() - 1;
8531 if ( coord
>= BorderArray
[i_max
])
8532 return clipToMinMax
? (int)i_max
: -1;
8533 if ( coord
< BorderArray
[0] )
8536 while ( i_max
- i_min
> 0 )
8538 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
8539 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8540 if (coord
>= BorderArray
[ i_max
- 1])
8544 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
8545 if (coord
< BorderArray
[median
])
8554 int wxGrid::YToRow( int y
)
8556 return CoordToRowOrCol(y
, m_defaultRowHeight
,
8557 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8560 int wxGrid::XToCol( int x
, bool clipToMinMax
)
8563 return clipToMinMax
&& (m_numCols
> 0) ? GetColAt( 0 ) : -1;
8565 if (!m_defaultColWidth
)
8566 m_defaultColWidth
= 1;
8568 int maxPos
= x
/ m_defaultColWidth
;
8571 if (m_colRights
.IsEmpty())
8573 if(maxPos
< m_numCols
)
8574 return GetColAt( maxPos
);
8575 return clipToMinMax
? GetColAt( m_numCols
- 1 ) : -1;
8578 if ( maxPos
>= m_numCols
)
8579 maxPos
= m_numCols
- 1;
8582 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8585 if (m_minAcceptableColWidth
)
8586 maxPos
= x
/ m_minAcceptableColWidth
;
8588 maxPos
= m_numCols
- 1;
8590 if ( maxPos
>= m_numCols
)
8591 maxPos
= m_numCols
- 1;
8594 //X is beyond the last column
8595 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8596 return clipToMinMax
? GetColAt( maxPos
) : -1;
8598 //X is before the first column
8599 if ( x
< m_colRights
[GetColAt( 0 )] )
8600 return GetColAt( 0 );
8602 //Perform a binary search
8603 while ( maxPos
- minPos
> 0 )
8605 wxCHECK_MSG(m_colRights
[GetColAt( minPos
)] <= x
&& x
< m_colRights
[GetColAt( maxPos
)],
8606 0, _T("wxGrid: internal error in XToCol"));
8608 if (x
>= m_colRights
[GetColAt( maxPos
- 1 )])
8609 return GetColAt( maxPos
);
8612 int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
8613 if (x
< m_colRights
[GetColAt( median
)])
8618 return GetColAt( maxPos
);
8621 // return the row number that that the y coord is near
8622 // the edge of, or -1 if not near an edge.
8623 // coords can only possibly be near an edge if
8624 // (a) the row/column is large enough to still allow for an "inner" area
8625 // that is _not_ nead the edge (i.e., if the height/width is smaller
8626 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8629 // (b) resizing rows/columns (the thing for which edge detection is
8630 // relevant at all) is enabled.
8632 int wxGrid::YToEdgeOfRow( int y
)
8635 i
= internalYToRow(y
);
8637 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragRowSize() )
8639 // We know that we are in row i, test whether we are
8640 // close enough to lower or upper border, respectively.
8641 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8643 else if ( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8650 // return the col number that that the x coord is near the edge of, or
8651 // -1 if not near an edge
8652 // See comment at YToEdgeOfRow for conditions on edge detection.
8654 int wxGrid::XToEdgeOfCol( int x
)
8657 i
= internalXToCol(x
);
8659 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragColSize() )
8661 // We know that we are in column i; test whether we are
8662 // close enough to right or left border, respectively.
8663 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8665 else if ( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8672 wxRect
wxGrid::CellToRect( int row
, int col
)
8674 wxRect
rect( -1, -1, -1, -1 );
8676 if ( row
>= 0 && row
< m_numRows
&&
8677 col
>= 0 && col
< m_numCols
)
8679 int i
, cell_rows
, cell_cols
;
8680 rect
.width
= rect
.height
= 0;
8681 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8682 // if negative then find multicell owner
8687 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8689 rect
.x
= GetColLeft(col
);
8690 rect
.y
= GetRowTop(row
);
8691 for (i
=col
; i
< col
+ cell_cols
; i
++)
8692 rect
.width
+= GetColWidth(i
);
8693 for (i
=row
; i
< row
+ cell_rows
; i
++)
8694 rect
.height
+= GetRowHeight(i
);
8697 // if grid lines are enabled, then the area of the cell is a bit smaller
8698 if (m_gridLinesEnabled
)
8707 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
8709 // get the cell rectangle in logical coords
8711 wxRect
r( CellToRect( row
, col
) );
8713 // convert to device coords
8715 int left
, top
, right
, bottom
;
8716 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8717 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8719 // check against the client area of the grid window
8721 m_gridWin
->GetClientSize( &cw
, &ch
);
8723 if ( wholeCellVisible
)
8725 // is the cell wholly visible ?
8726 return ( left
>= 0 && right
<= cw
&&
8727 top
>= 0 && bottom
<= ch
);
8731 // is the cell partly visible ?
8733 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8734 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8738 // make the specified cell location visible by doing a minimal amount
8741 void wxGrid::MakeCellVisible( int row
, int col
)
8744 int xpos
= -1, ypos
= -1;
8746 if ( row
>= 0 && row
< m_numRows
&&
8747 col
>= 0 && col
< m_numCols
)
8749 // get the cell rectangle in logical coords
8750 wxRect
r( CellToRect( row
, col
) );
8752 // convert to device coords
8753 int left
, top
, right
, bottom
;
8754 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8755 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8758 m_gridWin
->GetClientSize( &cw
, &ch
);
8764 else if ( bottom
> ch
)
8766 int h
= r
.GetHeight();
8768 for ( i
= row
- 1; i
>= 0; i
-- )
8770 int rowHeight
= GetRowHeight(i
);
8771 if ( h
+ rowHeight
> ch
)
8778 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8779 // have rounding errors (this is important, because if we do,
8780 // we might not scroll at all and some cells won't be redrawn)
8782 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
8783 // so just add a full scroll unit...
8784 ypos
+= m_scrollLineY
;
8787 // special handling for wide cells - show always left part of the cell!
8788 // Otherwise, e.g. when stepping from row to row, it would jump between
8789 // left and right part of the cell on every step!
8791 if ( left
< 0 || (right
- left
) >= cw
)
8795 else if ( right
> cw
)
8797 // position the view so that the cell is on the right
8799 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8800 xpos
= x0
+ (right
- cw
);
8802 // see comment for ypos above
8803 xpos
+= m_scrollLineX
;
8806 if ( xpos
!= -1 || ypos
!= -1 )
8809 xpos
/= m_scrollLineX
;
8811 ypos
/= m_scrollLineY
;
8812 Scroll( xpos
, ypos
);
8819 // ------ Grid cursor movement functions
8822 bool wxGrid::MoveCursorUp( bool expandSelection
)
8824 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8825 m_currentCellCoords
.GetRow() >= 0 )
8827 if ( expandSelection
)
8829 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8830 m_selectingKeyboard
= m_currentCellCoords
;
8831 if ( m_selectingKeyboard
.GetRow() > 0 )
8833 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8834 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8835 m_selectingKeyboard
.GetCol() );
8836 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8839 else if ( m_currentCellCoords
.GetRow() > 0 )
8841 int row
= m_currentCellCoords
.GetRow() - 1;
8842 int col
= m_currentCellCoords
.GetCol();
8844 MakeCellVisible( row
, col
);
8845 SetCurrentCell( row
, col
);
8856 bool wxGrid::MoveCursorDown( bool expandSelection
)
8858 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8859 m_currentCellCoords
.GetRow() < m_numRows
)
8861 if ( expandSelection
)
8863 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8864 m_selectingKeyboard
= m_currentCellCoords
;
8865 if ( m_selectingKeyboard
.GetRow() < m_numRows
- 1 )
8867 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8868 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8869 m_selectingKeyboard
.GetCol() );
8870 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8873 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8875 int row
= m_currentCellCoords
.GetRow() + 1;
8876 int col
= m_currentCellCoords
.GetCol();
8878 MakeCellVisible( row
, col
);
8879 SetCurrentCell( row
, col
);
8890 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8892 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8893 m_currentCellCoords
.GetCol() >= 0 )
8895 if ( expandSelection
)
8897 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8898 m_selectingKeyboard
= m_currentCellCoords
;
8899 if ( m_selectingKeyboard
.GetCol() > 0 )
8901 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8902 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8903 m_selectingKeyboard
.GetCol() );
8904 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8907 else if ( GetColPos( m_currentCellCoords
.GetCol() ) > 0 )
8909 int row
= m_currentCellCoords
.GetRow();
8910 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) - 1 );
8913 MakeCellVisible( row
, col
);
8914 SetCurrentCell( row
, col
);
8925 bool wxGrid::MoveCursorRight( bool expandSelection
)
8927 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8928 m_currentCellCoords
.GetCol() < m_numCols
)
8930 if ( expandSelection
)
8932 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8933 m_selectingKeyboard
= m_currentCellCoords
;
8934 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8936 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8937 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8938 m_selectingKeyboard
.GetCol() );
8939 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8942 else if ( GetColPos( m_currentCellCoords
.GetCol() ) < m_numCols
- 1 )
8944 int row
= m_currentCellCoords
.GetRow();
8945 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) + 1 );
8948 MakeCellVisible( row
, col
);
8949 SetCurrentCell( row
, col
);
8960 bool wxGrid::MovePageUp()
8962 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8965 int row
= m_currentCellCoords
.GetRow();
8969 m_gridWin
->GetClientSize( &cw
, &ch
);
8971 int y
= GetRowTop(row
);
8972 int newRow
= internalYToRow( y
- ch
+ 1 );
8974 if ( newRow
== row
)
8976 // row > 0, so newRow can never be less than 0 here.
8980 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8981 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8989 bool wxGrid::MovePageDown()
8991 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8994 int row
= m_currentCellCoords
.GetRow();
8995 if ( (row
+ 1) < m_numRows
)
8998 m_gridWin
->GetClientSize( &cw
, &ch
);
9000 int y
= GetRowTop(row
);
9001 int newRow
= internalYToRow( y
+ ch
);
9002 if ( newRow
== row
)
9004 // row < m_numRows, so newRow can't overflow here.
9008 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
9009 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
9017 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
9020 m_currentCellCoords
!= wxGridNoCellCoords
&&
9021 m_currentCellCoords
.GetRow() > 0 )
9023 int row
= m_currentCellCoords
.GetRow();
9024 int col
= m_currentCellCoords
.GetCol();
9026 if ( m_table
->IsEmptyCell(row
, col
) )
9028 // starting in an empty cell: find the next block of
9034 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9038 else if ( m_table
->IsEmptyCell(row
- 1, col
) )
9040 // starting at the top of a block: find the next block
9046 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9052 // starting within a block: find the top of the block
9057 if ( m_table
->IsEmptyCell(row
, col
) )
9065 MakeCellVisible( row
, col
);
9066 if ( expandSelection
)
9068 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9069 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9074 SetCurrentCell( row
, col
);
9083 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
9086 m_currentCellCoords
!= wxGridNoCellCoords
&&
9087 m_currentCellCoords
.GetRow() < m_numRows
- 1 )
9089 int row
= m_currentCellCoords
.GetRow();
9090 int col
= m_currentCellCoords
.GetCol();
9092 if ( m_table
->IsEmptyCell(row
, col
) )
9094 // starting in an empty cell: find the next block of
9097 while ( row
< m_numRows
- 1 )
9100 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9104 else if ( m_table
->IsEmptyCell(row
+ 1, col
) )
9106 // starting at the bottom of a block: find the next block
9109 while ( row
< m_numRows
- 1 )
9112 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9118 // starting within a block: find the bottom of the block
9120 while ( row
< m_numRows
- 1 )
9123 if ( m_table
->IsEmptyCell(row
, col
) )
9131 MakeCellVisible( row
, col
);
9132 if ( expandSelection
)
9134 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9135 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9140 SetCurrentCell( row
, col
);
9149 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
9152 m_currentCellCoords
!= wxGridNoCellCoords
&&
9153 m_currentCellCoords
.GetCol() > 0 )
9155 int row
= m_currentCellCoords
.GetRow();
9156 int col
= m_currentCellCoords
.GetCol();
9158 if ( m_table
->IsEmptyCell(row
, col
) )
9160 // starting in an empty cell: find the next block of
9166 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9170 else if ( m_table
->IsEmptyCell(row
, col
- 1) )
9172 // starting at the left of a block: find the next block
9178 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9184 // starting within a block: find the left of the block
9189 if ( m_table
->IsEmptyCell(row
, col
) )
9197 MakeCellVisible( row
, col
);
9198 if ( expandSelection
)
9200 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9201 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9206 SetCurrentCell( row
, col
);
9215 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
9218 m_currentCellCoords
!= wxGridNoCellCoords
&&
9219 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
9221 int row
= m_currentCellCoords
.GetRow();
9222 int col
= m_currentCellCoords
.GetCol();
9224 if ( m_table
->IsEmptyCell(row
, col
) )
9226 // starting in an empty cell: find the next block of
9229 while ( col
< m_numCols
- 1 )
9232 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9236 else if ( m_table
->IsEmptyCell(row
, col
+ 1) )
9238 // starting at the right of a block: find the next block
9241 while ( col
< m_numCols
- 1 )
9244 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9250 // starting within a block: find the right of the block
9252 while ( col
< m_numCols
- 1 )
9255 if ( m_table
->IsEmptyCell(row
, col
) )
9263 MakeCellVisible( row
, col
);
9264 if ( expandSelection
)
9266 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9267 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9272 SetCurrentCell( row
, col
);
9282 // ------ Label values and formatting
9285 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
9288 *horiz
= m_rowLabelHorizAlign
;
9290 *vert
= m_rowLabelVertAlign
;
9293 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
9296 *horiz
= m_colLabelHorizAlign
;
9298 *vert
= m_colLabelVertAlign
;
9301 int wxGrid::GetColLabelTextOrientation()
9303 return m_colLabelTextOrientation
;
9306 wxString
wxGrid::GetRowLabelValue( int row
)
9310 return m_table
->GetRowLabelValue( row
);
9320 wxString
wxGrid::GetColLabelValue( int col
)
9324 return m_table
->GetColLabelValue( col
);
9334 void wxGrid::SetRowLabelSize( int width
)
9336 width
= wxMax( width
, 0 );
9337 if ( width
!= m_rowLabelWidth
)
9341 m_rowLabelWin
->Show( false );
9342 m_cornerLabelWin
->Show( false );
9344 else if ( m_rowLabelWidth
== 0 )
9346 m_rowLabelWin
->Show( true );
9347 if ( m_colLabelHeight
> 0 )
9348 m_cornerLabelWin
->Show( true );
9351 m_rowLabelWidth
= width
;
9353 wxScrolledWindow::Refresh( true );
9357 void wxGrid::SetColLabelSize( int height
)
9359 height
= wxMax( height
, 0 );
9360 if ( height
!= m_colLabelHeight
)
9364 m_colLabelWin
->Show( false );
9365 m_cornerLabelWin
->Show( false );
9367 else if ( m_colLabelHeight
== 0 )
9369 m_colLabelWin
->Show( true );
9370 if ( m_rowLabelWidth
> 0 )
9371 m_cornerLabelWin
->Show( true );
9374 m_colLabelHeight
= height
;
9376 wxScrolledWindow::Refresh( true );
9380 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
9382 if ( m_labelBackgroundColour
!= colour
)
9384 m_labelBackgroundColour
= colour
;
9385 m_rowLabelWin
->SetBackgroundColour( colour
);
9386 m_colLabelWin
->SetBackgroundColour( colour
);
9387 m_cornerLabelWin
->SetBackgroundColour( colour
);
9389 if ( !GetBatchCount() )
9391 m_rowLabelWin
->Refresh();
9392 m_colLabelWin
->Refresh();
9393 m_cornerLabelWin
->Refresh();
9398 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
9400 if ( m_labelTextColour
!= colour
)
9402 m_labelTextColour
= colour
;
9403 if ( !GetBatchCount() )
9405 m_rowLabelWin
->Refresh();
9406 m_colLabelWin
->Refresh();
9411 void wxGrid::SetLabelFont( const wxFont
& font
)
9414 if ( !GetBatchCount() )
9416 m_rowLabelWin
->Refresh();
9417 m_colLabelWin
->Refresh();
9421 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
9423 // allow old (incorrect) defs to be used
9426 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9427 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9428 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9433 case wxTOP
: vert
= wxALIGN_TOP
; break;
9434 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9435 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9438 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9440 m_rowLabelHorizAlign
= horiz
;
9443 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9445 m_rowLabelVertAlign
= vert
;
9448 if ( !GetBatchCount() )
9450 m_rowLabelWin
->Refresh();
9454 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
9456 // allow old (incorrect) defs to be used
9459 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9460 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9461 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9466 case wxTOP
: vert
= wxALIGN_TOP
; break;
9467 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9468 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9471 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9473 m_colLabelHorizAlign
= horiz
;
9476 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9478 m_colLabelVertAlign
= vert
;
9481 if ( !GetBatchCount() )
9483 m_colLabelWin
->Refresh();
9487 // Note: under MSW, the default column label font must be changed because it
9488 // does not support vertical printing
9490 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9491 // pGrid->SetLabelFont(font);
9492 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9494 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9496 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9497 m_colLabelTextOrientation
= textOrientation
;
9499 if ( !GetBatchCount() )
9500 m_colLabelWin
->Refresh();
9503 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9507 m_table
->SetRowLabelValue( row
, s
);
9508 if ( !GetBatchCount() )
9510 wxRect rect
= CellToRect( row
, 0 );
9511 if ( rect
.height
> 0 )
9513 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9515 rect
.width
= m_rowLabelWidth
;
9516 m_rowLabelWin
->Refresh( true, &rect
);
9522 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9526 m_table
->SetColLabelValue( col
, s
);
9527 if ( !GetBatchCount() )
9529 wxRect rect
= CellToRect( 0, col
);
9530 if ( rect
.width
> 0 )
9532 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9534 rect
.height
= m_colLabelHeight
;
9535 m_colLabelWin
->Refresh( true, &rect
);
9541 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9543 if ( m_gridLineColour
!= colour
)
9545 m_gridLineColour
= colour
;
9547 wxClientDC
dc( m_gridWin
);
9549 DrawAllGridLines( dc
, wxRegion() );
9553 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9555 if ( m_cellHighlightColour
!= colour
)
9557 m_cellHighlightColour
= colour
;
9559 wxClientDC
dc( m_gridWin
);
9561 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9562 DrawCellHighlight(dc
, attr
);
9567 void wxGrid::SetCellHighlightPenWidth(int width
)
9569 if (m_cellHighlightPenWidth
!= width
)
9571 m_cellHighlightPenWidth
= width
;
9573 // Just redrawing the cell highlight is not enough since that won't
9574 // make any visible change if the the thickness is getting smaller.
9575 int row
= m_currentCellCoords
.GetRow();
9576 int col
= m_currentCellCoords
.GetCol();
9577 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9580 wxRect rect
= CellToRect(row
, col
);
9581 m_gridWin
->Refresh(true, &rect
);
9585 void wxGrid::SetCellHighlightROPenWidth(int width
)
9587 if (m_cellHighlightROPenWidth
!= width
)
9589 m_cellHighlightROPenWidth
= width
;
9591 // Just redrawing the cell highlight is not enough since that won't
9592 // make any visible change if the the thickness is getting smaller.
9593 int row
= m_currentCellCoords
.GetRow();
9594 int col
= m_currentCellCoords
.GetCol();
9595 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9598 wxRect rect
= CellToRect(row
, col
);
9599 m_gridWin
->Refresh(true, &rect
);
9603 void wxGrid::EnableGridLines( bool enable
)
9605 if ( enable
!= m_gridLinesEnabled
)
9607 m_gridLinesEnabled
= enable
;
9609 if ( !GetBatchCount() )
9613 wxClientDC
dc( m_gridWin
);
9615 DrawAllGridLines( dc
, wxRegion() );
9619 m_gridWin
->Refresh();
9625 int wxGrid::GetDefaultRowSize()
9627 return m_defaultRowHeight
;
9630 int wxGrid::GetRowSize( int row
)
9632 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9634 return GetRowHeight(row
);
9637 int wxGrid::GetDefaultColSize()
9639 return m_defaultColWidth
;
9642 int wxGrid::GetColSize( int col
)
9644 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9646 return GetColWidth(col
);
9649 // ============================================================================
9650 // access to the grid attributes: each of them has a default value in the grid
9651 // itself and may be overidden on a per-cell basis
9652 // ============================================================================
9654 // ----------------------------------------------------------------------------
9655 // setting default attributes
9656 // ----------------------------------------------------------------------------
9658 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9660 m_defaultCellAttr
->SetBackgroundColour(col
);
9662 m_gridWin
->SetBackgroundColour(col
);
9666 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9668 m_defaultCellAttr
->SetTextColour(col
);
9671 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9673 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9676 void wxGrid::SetDefaultCellOverflow( bool allow
)
9678 m_defaultCellAttr
->SetOverflow(allow
);
9681 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9683 m_defaultCellAttr
->SetFont(font
);
9686 // For editors and renderers the type registry takes precedence over the
9687 // default attr, so we need to register the new editor/renderer for the string
9688 // data type in order to make setting a default editor/renderer appear to
9691 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9693 RegisterDataType(wxGRID_VALUE_STRING
,
9695 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9698 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9700 RegisterDataType(wxGRID_VALUE_STRING
,
9701 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9705 // ----------------------------------------------------------------------------
9706 // access to the default attrbiutes
9707 // ----------------------------------------------------------------------------
9709 wxColour
wxGrid::GetDefaultCellBackgroundColour()
9711 return m_defaultCellAttr
->GetBackgroundColour();
9714 wxColour
wxGrid::GetDefaultCellTextColour()
9716 return m_defaultCellAttr
->GetTextColour();
9719 wxFont
wxGrid::GetDefaultCellFont()
9721 return m_defaultCellAttr
->GetFont();
9724 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
9726 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9729 bool wxGrid::GetDefaultCellOverflow()
9731 return m_defaultCellAttr
->GetOverflow();
9734 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9736 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9739 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9741 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9744 // ----------------------------------------------------------------------------
9745 // access to cell attributes
9746 // ----------------------------------------------------------------------------
9748 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
9750 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9751 wxColour colour
= attr
->GetBackgroundColour();
9757 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
9759 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9760 wxColour colour
= attr
->GetTextColour();
9766 wxFont
wxGrid::GetCellFont( int row
, int col
)
9768 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9769 wxFont font
= attr
->GetFont();
9775 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
9777 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9778 attr
->GetAlignment(horiz
, vert
);
9782 bool wxGrid::GetCellOverflow( int row
, int col
)
9784 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9785 bool allow
= attr
->GetOverflow();
9791 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
9793 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9794 attr
->GetSize( num_rows
, num_cols
);
9798 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
9800 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9801 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9807 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
9809 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9810 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9816 bool wxGrid::IsReadOnly(int row
, int col
) const
9818 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9819 bool isReadOnly
= attr
->IsReadOnly();
9825 // ----------------------------------------------------------------------------
9826 // attribute support: cache, automatic provider creation, ...
9827 // ----------------------------------------------------------------------------
9829 bool wxGrid::CanHaveAttributes()
9836 return m_table
->CanHaveAttributes();
9839 void wxGrid::ClearAttrCache()
9841 if ( m_attrCache
.row
!= -1 )
9843 wxSafeDecRef(m_attrCache
.attr
);
9844 m_attrCache
.attr
= NULL
;
9845 m_attrCache
.row
= -1;
9849 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9853 wxGrid
*self
= (wxGrid
*)this; // const_cast
9855 self
->ClearAttrCache();
9856 self
->m_attrCache
.row
= row
;
9857 self
->m_attrCache
.col
= col
;
9858 self
->m_attrCache
.attr
= attr
;
9863 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9865 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9867 *attr
= m_attrCache
.attr
;
9868 wxSafeIncRef(m_attrCache
.attr
);
9870 #ifdef DEBUG_ATTR_CACHE
9871 gs_nAttrCacheHits
++;
9878 #ifdef DEBUG_ATTR_CACHE
9879 gs_nAttrCacheMisses
++;
9886 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9888 wxGridCellAttr
*attr
= NULL
;
9889 // Additional test to avoid looking at the cache e.g. for
9890 // wxNoCellCoords, as this will confuse memory management.
9893 if ( !LookupAttr(row
, col
, &attr
) )
9895 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9896 : (wxGridCellAttr
*)NULL
;
9897 CacheAttr(row
, col
, attr
);
9903 attr
->SetDefAttr(m_defaultCellAttr
);
9907 attr
= m_defaultCellAttr
;
9914 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9916 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9917 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9919 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9920 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9922 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9925 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9927 // artificially inc the ref count to match DecRef() in caller
9929 m_table
->SetAttr(attr
, row
, col
);
9935 // ----------------------------------------------------------------------------
9936 // setting column attributes (wrappers around SetColAttr)
9937 // ----------------------------------------------------------------------------
9939 void wxGrid::SetColFormatBool(int col
)
9941 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9944 void wxGrid::SetColFormatNumber(int col
)
9946 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9949 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9951 wxString typeName
= wxGRID_VALUE_FLOAT
;
9952 if ( (width
!= -1) || (precision
!= -1) )
9954 typeName
<< _T(':') << width
<< _T(',') << precision
;
9957 SetColFormatCustom(col
, typeName
);
9960 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9962 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9964 attr
= new wxGridCellAttr
;
9965 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9966 attr
->SetRenderer(renderer
);
9968 SetColAttr(col
, attr
);
9972 // ----------------------------------------------------------------------------
9973 // setting cell attributes: this is forwarded to the table
9974 // ----------------------------------------------------------------------------
9976 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9978 if ( CanHaveAttributes() )
9980 m_table
->SetAttr(attr
, row
, col
);
9989 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9991 if ( CanHaveAttributes() )
9993 m_table
->SetRowAttr(attr
, row
);
10002 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
10004 if ( CanHaveAttributes() )
10006 m_table
->SetColAttr(attr
, col
);
10011 wxSafeDecRef(attr
);
10015 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
10017 if ( CanHaveAttributes() )
10019 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10020 attr
->SetBackgroundColour(colour
);
10025 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
10027 if ( CanHaveAttributes() )
10029 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10030 attr
->SetTextColour(colour
);
10035 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
10037 if ( CanHaveAttributes() )
10039 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10040 attr
->SetFont(font
);
10045 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
10047 if ( CanHaveAttributes() )
10049 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10050 attr
->SetAlignment(horiz
, vert
);
10055 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
10057 if ( CanHaveAttributes() )
10059 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10060 attr
->SetOverflow(allow
);
10065 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
10067 if ( CanHaveAttributes() )
10069 int cell_rows
, cell_cols
;
10071 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10072 attr
->GetSize(&cell_rows
, &cell_cols
);
10073 attr
->SetSize(num_rows
, num_cols
);
10076 // Cannot set the size of a cell to 0 or negative values
10077 // While it is perfectly legal to do that, this function cannot
10078 // handle all the possibilies, do it by hand by getting the CellAttr.
10079 // You can only set the size of a cell to 1,1 or greater with this fn
10080 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
10081 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10082 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
10083 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10085 // if this was already a multicell then "turn off" the other cells first
10086 if ((cell_rows
> 1) || (cell_rows
> 1))
10089 for (j
=row
; j
< row
+ cell_rows
; j
++)
10091 for (i
=col
; i
< col
+ cell_cols
; i
++)
10093 if ((i
!= col
) || (j
!= row
))
10095 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10096 attr_stub
->SetSize( 1, 1 );
10097 attr_stub
->DecRef();
10103 // mark the cells that will be covered by this cell to
10104 // negative or zero values to point back at this cell
10105 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
10108 for (j
=row
; j
< row
+ num_rows
; j
++)
10110 for (i
=col
; i
< col
+ num_cols
; i
++)
10112 if ((i
!= col
) || (j
!= row
))
10114 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10115 attr_stub
->SetSize( row
- j
, col
- i
);
10116 attr_stub
->DecRef();
10124 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
10126 if ( CanHaveAttributes() )
10128 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10129 attr
->SetRenderer(renderer
);
10134 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
10136 if ( CanHaveAttributes() )
10138 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10139 attr
->SetEditor(editor
);
10144 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
10146 if ( CanHaveAttributes() )
10148 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10149 attr
->SetReadOnly(isReadOnly
);
10154 // ----------------------------------------------------------------------------
10155 // Data type registration
10156 // ----------------------------------------------------------------------------
10158 void wxGrid::RegisterDataType(const wxString
& typeName
,
10159 wxGridCellRenderer
* renderer
,
10160 wxGridCellEditor
* editor
)
10162 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
10166 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
10168 wxString typeName
= m_table
->GetTypeName(row
, col
);
10169 return GetDefaultEditorForType(typeName
);
10172 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
10174 wxString typeName
= m_table
->GetTypeName(row
, col
);
10175 return GetDefaultRendererForType(typeName
);
10178 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
10180 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10181 if ( index
== wxNOT_FOUND
)
10185 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
10186 wxFAIL_MSG(errStr
.c_str());
10191 return m_typeRegistry
->GetEditor(index
);
10194 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
10196 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10197 if ( index
== wxNOT_FOUND
)
10201 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
10202 wxFAIL_MSG(errStr
.c_str());
10207 return m_typeRegistry
->GetRenderer(index
);
10210 // ----------------------------------------------------------------------------
10212 // ----------------------------------------------------------------------------
10214 void wxGrid::EnableDragRowSize( bool enable
)
10216 m_canDragRowSize
= enable
;
10219 void wxGrid::EnableDragColSize( bool enable
)
10221 m_canDragColSize
= enable
;
10224 void wxGrid::EnableDragGridSize( bool enable
)
10226 m_canDragGridSize
= enable
;
10229 void wxGrid::EnableDragCell( bool enable
)
10231 m_canDragCell
= enable
;
10234 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
10236 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
10238 if ( resizeExistingRows
)
10240 // since we are resizing all rows to the default row size,
10241 // we can simply clear the row heights and row bottoms
10242 // arrays (which also allows us to take advantage of
10243 // some speed optimisations)
10244 m_rowHeights
.Empty();
10245 m_rowBottoms
.Empty();
10246 if ( !GetBatchCount() )
10251 void wxGrid::SetRowSize( int row
, int height
)
10253 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
10255 // See comment in SetColSize
10256 if ( height
< GetRowMinimalAcceptableHeight())
10259 if ( m_rowHeights
.IsEmpty() )
10261 // need to really create the array
10265 int h
= wxMax( 0, height
);
10266 int diff
= h
- m_rowHeights
[row
];
10268 m_rowHeights
[row
] = h
;
10269 for ( int i
= row
; i
< m_numRows
; i
++ )
10271 m_rowBottoms
[i
] += diff
;
10274 if ( !GetBatchCount() )
10278 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
10280 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
10282 if ( resizeExistingCols
)
10284 // since we are resizing all columns to the default column size,
10285 // we can simply clear the col widths and col rights
10286 // arrays (which also allows us to take advantage of
10287 // some speed optimisations)
10288 m_colWidths
.Empty();
10289 m_colRights
.Empty();
10290 if ( !GetBatchCount() )
10295 void wxGrid::SetColSize( int col
, int width
)
10297 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
10299 // should we check that it's bigger than GetColMinimalWidth(col) here?
10301 // No, because it is reasonable to assume the library user know's
10302 // what he is doing. However we should test against the weaker
10303 // constraint of minimalAcceptableWidth, as this breaks rendering
10305 // This test then fixes sf.net bug #645734
10307 if ( width
< GetColMinimalAcceptableWidth() )
10310 if ( m_colWidths
.IsEmpty() )
10312 // need to really create the array
10316 // if < 0 then calculate new width from label
10320 wxArrayString lines
;
10321 wxClientDC
dc(m_colLabelWin
);
10322 dc
.SetFont(GetLabelFont());
10323 StringToLines(GetColLabelValue(col
), lines
);
10324 GetTextBoxSize(dc
, lines
, &w
, &h
);
10328 int w
= wxMax( 0, width
);
10329 int diff
= w
- m_colWidths
[col
];
10330 m_colWidths
[col
] = w
;
10332 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
10334 m_colRights
[GetColAt(colPos
)] += diff
;
10337 if ( !GetBatchCount() )
10341 void wxGrid::SetColMinimalWidth( int col
, int width
)
10343 if (width
> GetColMinimalAcceptableWidth())
10345 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10346 m_colMinWidths
[key
] = width
;
10350 void wxGrid::SetRowMinimalHeight( int row
, int width
)
10352 if (width
> GetRowMinimalAcceptableHeight())
10354 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10355 m_rowMinHeights
[key
] = width
;
10359 int wxGrid::GetColMinimalWidth(int col
) const
10361 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10362 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
10364 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
10367 int wxGrid::GetRowMinimalHeight(int row
) const
10369 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10370 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
10372 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
10375 void wxGrid::SetColMinimalAcceptableWidth( int width
)
10377 // We do allow a width of 0 since this gives us
10378 // an easy way to temporarily hiding columns.
10380 m_minAcceptableColWidth
= width
;
10383 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
10385 // We do allow a height of 0 since this gives us
10386 // an easy way to temporarily hiding rows.
10388 m_minAcceptableRowHeight
= height
;
10391 int wxGrid::GetColMinimalAcceptableWidth() const
10393 return m_minAcceptableColWidth
;
10396 int wxGrid::GetRowMinimalAcceptableHeight() const
10398 return m_minAcceptableRowHeight
;
10401 // ----------------------------------------------------------------------------
10403 // ----------------------------------------------------------------------------
10405 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
10407 wxClientDC
dc(m_gridWin
);
10409 // cancel editing of cell
10410 HideCellEditControl();
10411 SaveEditControlValue();
10413 // init both of them to avoid compiler warnings, even if we only need one
10421 wxCoord extent
, extentMax
= 0;
10422 int max
= column
? m_numRows
: m_numCols
;
10423 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
10430 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
10431 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
10434 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
10435 extent
= column
? size
.x
: size
.y
;
10436 if ( extent
> extentMax
)
10437 extentMax
= extent
;
10439 renderer
->DecRef();
10445 // now also compare with the column label extent
10447 dc
.SetFont( GetLabelFont() );
10451 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
10452 if ( GetColLabelTextOrientation() == wxVERTICAL
)
10456 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
10458 extent
= column
? w
: h
;
10459 if ( extent
> extentMax
)
10460 extentMax
= extent
;
10464 // empty column - give default extent (notice that if extentMax is less
10465 // than default extent but != 0, it's OK)
10466 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
10471 // leave some space around text
10479 SetColSize( col
, extentMax
);
10480 if ( !GetBatchCount() )
10483 m_gridWin
->GetClientSize( &cw
, &ch
);
10484 wxRect
rect ( CellToRect( 0, col
) );
10486 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10487 rect
.width
= cw
- rect
.x
;
10488 rect
.height
= m_colLabelHeight
;
10489 m_colLabelWin
->Refresh( true, &rect
);
10494 SetRowSize(row
, extentMax
);
10495 if ( !GetBatchCount() )
10498 m_gridWin
->GetClientSize( &cw
, &ch
);
10499 wxRect
rect( CellToRect( row
, 0 ) );
10501 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10502 rect
.width
= m_rowLabelWidth
;
10503 rect
.height
= ch
- rect
.y
;
10504 m_rowLabelWin
->Refresh( true, &rect
);
10511 SetColMinimalWidth(col
, extentMax
);
10513 SetRowMinimalHeight(row
, extentMax
);
10517 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10519 int width
= m_rowLabelWidth
;
10524 for ( int col
= 0; col
< m_numCols
; col
++ )
10527 AutoSizeColumn(col
, setAsMin
);
10529 width
+= GetColWidth(col
);
10538 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10540 int height
= m_colLabelHeight
;
10545 for ( int row
= 0; row
< m_numRows
; row
++ )
10548 AutoSizeRow(row
, setAsMin
);
10550 height
+= GetRowHeight(row
);
10559 void wxGrid::AutoSize()
10563 // we need to round up the size of the scrollable area to a multiple of
10564 // scroll step to ensure that we don't get the scrollbars when we're sized
10565 // exactly to fit our contents
10566 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
10567 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
10568 wxSize
sizeFit(GetScrollX(size
.x
) * GetScrollLineX(),
10569 GetScrollY(size
.y
) * GetScrollLineY());
10571 // distribute the extra space between the columns/rows to avoid having
10572 // extra white space
10573 wxCoord diff
= sizeFit
.x
- size
.x
;
10574 if ( diff
&& m_numCols
)
10576 // try to resize the columns uniformly
10577 wxCoord diffPerCol
= diff
/ m_numCols
;
10580 for ( int col
= 0; col
< m_numCols
; col
++ )
10582 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
10586 // add remaining amount to the last columns
10587 diff
-= diffPerCol
* m_numCols
;
10590 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
10592 SetColSize(col
, GetColWidth(col
) + 1);
10598 diff
= sizeFit
.y
- size
.y
;
10599 if ( diff
&& m_numRows
)
10601 // try to resize the columns uniformly
10602 wxCoord diffPerRow
= diff
/ m_numRows
;
10605 for ( int row
= 0; row
< m_numRows
; row
++ )
10607 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
10611 // add remaining amount to the last rows
10612 diff
-= diffPerRow
* m_numRows
;
10615 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
10617 SetRowSize(row
, GetRowHeight(row
) + 1);
10622 // we know that we're not going to have scrollbars so disable them now to
10623 // avoid trouble in SetClientSize() which can otherwise set the correct
10624 // client size but also leave space for (not needed any more) scrollbars
10625 SetScrollbars(0, 0, 0, 0, 0, 0, true);
10626 SetClientSize(sizeFit
.x
+ m_rowLabelWidth
, sizeFit
.y
+ m_colLabelHeight
);
10631 void wxGrid::AutoSizeRowLabelSize( int row
)
10633 wxArrayString lines
;
10636 // Hide the edit control, so it
10637 // won't interfere with drag-shrinking.
10638 if ( IsCellEditControlShown() )
10640 HideCellEditControl();
10641 SaveEditControlValue();
10644 // autosize row height depending on label text
10645 StringToLines( GetRowLabelValue( row
), lines
);
10646 wxClientDC
dc( m_rowLabelWin
);
10647 GetTextBoxSize( dc
, lines
, &w
, &h
);
10648 if ( h
< m_defaultRowHeight
)
10649 h
= m_defaultRowHeight
;
10650 SetRowSize(row
, h
);
10654 void wxGrid::AutoSizeColLabelSize( int col
)
10656 wxArrayString lines
;
10659 // Hide the edit control, so it
10660 // won't interfere with drag-shrinking.
10661 if ( IsCellEditControlShown() )
10663 HideCellEditControl();
10664 SaveEditControlValue();
10667 // autosize column width depending on label text
10668 StringToLines( GetColLabelValue( col
), lines
);
10669 wxClientDC
dc( m_colLabelWin
);
10670 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10671 GetTextBoxSize( dc
, lines
, &w
, &h
);
10673 GetTextBoxSize( dc
, lines
, &h
, &w
);
10674 if ( w
< m_defaultColWidth
)
10675 w
= m_defaultColWidth
;
10676 SetColSize(col
, w
);
10680 wxSize
wxGrid::DoGetBestSize() const
10682 wxGrid
*self
= (wxGrid
*)this; // const_cast
10684 // we do the same as in AutoSize() here with the exception that we don't
10685 // change the column/row sizes, only calculate them
10686 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
10687 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
10688 wxSize
sizeFit(GetScrollX(size
.x
) * GetScrollLineX(),
10689 GetScrollY(size
.y
) * GetScrollLineY());
10691 // NOTE: This size should be cached, but first we need to add calls to
10692 // InvalidateBestSize everywhere that could change the results of this
10694 // CacheBestSize(size);
10696 return wxSize(sizeFit
.x
+ m_rowLabelWidth
, sizeFit
.y
+ m_colLabelHeight
)
10697 + GetWindowBorderSize();
10705 wxPen
& wxGrid::GetDividerPen() const
10710 // ----------------------------------------------------------------------------
10711 // cell value accessor functions
10712 // ----------------------------------------------------------------------------
10714 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10718 m_table
->SetValue( row
, col
, s
);
10719 if ( !GetBatchCount() )
10722 wxRect
rect( CellToRect( row
, col
) );
10724 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10725 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10726 m_gridWin
->Refresh( false, &rect
);
10729 if ( m_currentCellCoords
.GetRow() == row
&&
10730 m_currentCellCoords
.GetCol() == col
&&
10731 IsCellEditControlShown())
10732 // Note: If we are using IsCellEditControlEnabled,
10733 // this interacts badly with calling SetCellValue from
10734 // an EVT_GRID_CELL_CHANGE handler.
10736 HideCellEditControl();
10737 ShowCellEditControl(); // will reread data from table
10742 // ----------------------------------------------------------------------------
10743 // block, row and column selection
10744 // ----------------------------------------------------------------------------
10746 void wxGrid::SelectRow( int row
, bool addToSelected
)
10748 if ( IsSelection() && !addToSelected
)
10752 m_selection
->SelectRow( row
, false, addToSelected
);
10755 void wxGrid::SelectCol( int col
, bool addToSelected
)
10757 if ( IsSelection() && !addToSelected
)
10761 m_selection
->SelectCol( col
, false, addToSelected
);
10764 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10765 bool addToSelected
)
10767 if ( IsSelection() && !addToSelected
)
10771 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10772 false, addToSelected
);
10775 void wxGrid::SelectAll()
10777 if ( m_numRows
> 0 && m_numCols
> 0 )
10780 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10784 // ----------------------------------------------------------------------------
10785 // cell, row and col deselection
10786 // ----------------------------------------------------------------------------
10788 void wxGrid::DeselectRow( int row
)
10790 if ( !m_selection
)
10793 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10795 if ( m_selection
->IsInSelection(row
, 0 ) )
10796 m_selection
->ToggleCellSelection(row
, 0);
10800 int nCols
= GetNumberCols();
10801 for ( int i
= 0; i
< nCols
; i
++ )
10803 if ( m_selection
->IsInSelection(row
, i
) )
10804 m_selection
->ToggleCellSelection(row
, i
);
10809 void wxGrid::DeselectCol( int col
)
10811 if ( !m_selection
)
10814 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10816 if ( m_selection
->IsInSelection(0, col
) )
10817 m_selection
->ToggleCellSelection(0, col
);
10821 int nRows
= GetNumberRows();
10822 for ( int i
= 0; i
< nRows
; i
++ )
10824 if ( m_selection
->IsInSelection(i
, col
) )
10825 m_selection
->ToggleCellSelection(i
, col
);
10830 void wxGrid::DeselectCell( int row
, int col
)
10832 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10833 m_selection
->ToggleCellSelection(row
, col
);
10836 bool wxGrid::IsSelection()
10838 return ( m_selection
&& (m_selection
->IsSelection() ||
10839 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10840 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10843 bool wxGrid::IsInSelection( int row
, int col
) const
10845 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10846 ( row
>= m_selectingTopLeft
.GetRow() &&
10847 col
>= m_selectingTopLeft
.GetCol() &&
10848 row
<= m_selectingBottomRight
.GetRow() &&
10849 col
<= m_selectingBottomRight
.GetCol() )) );
10852 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10856 wxGridCellCoordsArray a
;
10860 return m_selection
->m_cellSelection
;
10863 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10867 wxGridCellCoordsArray a
;
10871 return m_selection
->m_blockSelectionTopLeft
;
10874 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10878 wxGridCellCoordsArray a
;
10882 return m_selection
->m_blockSelectionBottomRight
;
10885 wxArrayInt
wxGrid::GetSelectedRows() const
10893 return m_selection
->m_rowSelection
;
10896 wxArrayInt
wxGrid::GetSelectedCols() const
10904 return m_selection
->m_colSelection
;
10907 void wxGrid::ClearSelection()
10909 m_selectingTopLeft
=
10910 m_selectingBottomRight
=
10911 m_selectingKeyboard
= wxGridNoCellCoords
;
10913 m_selection
->ClearSelection();
10916 // This function returns the rectangle that encloses the given block
10917 // in device coords clipped to the client size of the grid window.
10919 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10920 const wxGridCellCoords
&bottomRight
)
10922 wxRect
rect( wxGridNoCellRect
);
10925 cellRect
= CellToRect( topLeft
);
10926 if ( cellRect
!= wxGridNoCellRect
)
10932 rect
= wxRect(0, 0, 0, 0);
10935 cellRect
= CellToRect( bottomRight
);
10936 if ( cellRect
!= wxGridNoCellRect
)
10942 return wxGridNoCellRect
;
10946 int left
= rect
.GetLeft();
10947 int top
= rect
.GetTop();
10948 int right
= rect
.GetRight();
10949 int bottom
= rect
.GetBottom();
10951 int leftCol
= topLeft
.GetCol();
10952 int topRow
= topLeft
.GetRow();
10953 int rightCol
= bottomRight
.GetCol();
10954 int bottomRow
= bottomRight
.GetRow();
10962 leftCol
= rightCol
;
10972 topRow
= bottomRow
;
10976 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10978 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10980 if ((j
== topRow
) || (j
== bottomRow
) || (i
== leftCol
) || (i
== rightCol
))
10982 cellRect
= CellToRect( j
, i
);
10984 if (cellRect
.x
< left
)
10986 if (cellRect
.y
< top
)
10988 if (cellRect
.x
+ cellRect
.width
> right
)
10989 right
= cellRect
.x
+ cellRect
.width
;
10990 if (cellRect
.y
+ cellRect
.height
> bottom
)
10991 bottom
= cellRect
.y
+ cellRect
.height
;
10995 i
= rightCol
; // jump over inner cells.
11000 // convert to scrolled coords
11002 CalcScrolledPosition( left
, top
, &left
, &top
);
11003 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
11006 m_gridWin
->GetClientSize( &cw
, &ch
);
11008 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
11009 return wxRect(0,0,0,0);
11011 rect
.SetLeft( wxMax(0, left
) );
11012 rect
.SetTop( wxMax(0, top
) );
11013 rect
.SetRight( wxMin(cw
, right
) );
11014 rect
.SetBottom( wxMin(ch
, bottom
) );
11019 // ----------------------------------------------------------------------------
11020 // grid event classes
11021 // ----------------------------------------------------------------------------
11023 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
11025 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
11026 int row
, int col
, int x
, int y
, bool sel
,
11027 bool control
, bool shift
, bool alt
, bool meta
)
11028 : wxNotifyEvent( type
, id
)
11035 m_control
= control
;
11040 SetEventObject(obj
);
11044 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
11046 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
11047 int rowOrCol
, int x
, int y
,
11048 bool control
, bool shift
, bool alt
, bool meta
)
11049 : wxNotifyEvent( type
, id
)
11051 m_rowOrCol
= rowOrCol
;
11054 m_control
= control
;
11059 SetEventObject(obj
);
11063 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
11065 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
11066 const wxGridCellCoords
& topLeft
,
11067 const wxGridCellCoords
& bottomRight
,
11068 bool sel
, bool control
,
11069 bool shift
, bool alt
, bool meta
)
11070 : wxNotifyEvent( type
, id
)
11072 m_topLeft
= topLeft
;
11073 m_bottomRight
= bottomRight
;
11075 m_control
= control
;
11080 SetEventObject(obj
);
11084 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
11086 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
11087 wxObject
* obj
, int row
,
11088 int col
, wxControl
* ctrl
)
11089 : wxCommandEvent(type
, id
)
11091 SetEventObject(obj
);
11097 #endif // wxUSE_GRID