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("1"), _T("") };
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
) );
1344 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1347 CBox()->SetValue(m_startValue
);
1351 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1354 wxASSERT_MSG(m_control
,
1355 wxT("The wxGridCellEditor must be created first!"));
1357 bool changed
= false;
1358 bool value
= CBox()->GetValue();
1359 if ( value
!= m_startValue
)
1364 wxGridTableBase
* const table
= grid
->GetTable();
1365 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1366 table
->SetValueAsBool(row
, col
, value
);
1368 table
->SetValue(row
, col
, GetValue());
1374 void wxGridCellBoolEditor::Reset()
1376 wxASSERT_MSG(m_control
,
1377 wxT("The wxGridCellEditor must be created first!"));
1379 CBox()->SetValue(m_startValue
);
1382 void wxGridCellBoolEditor::StartingClick()
1384 CBox()->SetValue(!CBox()->GetValue());
1387 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1389 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1391 int keycode
= event
.GetKeyCode();
1404 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1406 int keycode
= event
.GetKeyCode();
1410 CBox()->SetValue(!CBox()->GetValue());
1414 CBox()->SetValue(true);
1418 CBox()->SetValue(false);
1423 wxString
wxGridCellBoolEditor::GetValue() const
1425 return ms_stringValues
[CBox()->GetValue()];
1429 wxGridCellBoolEditor::UseStringValues(const wxString
& valueTrue
,
1430 const wxString
& valueFalse
)
1432 ms_stringValues
[false] = valueFalse
;
1433 ms_stringValues
[true] = valueTrue
;
1437 wxGridCellBoolEditor::IsTrueValue(const wxString
& value
)
1439 return value
== ms_stringValues
[true];
1442 #endif // wxUSE_CHECKBOX
1446 // ----------------------------------------------------------------------------
1447 // wxGridCellChoiceEditor
1448 // ----------------------------------------------------------------------------
1450 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1452 : m_choices(choices
),
1453 m_allowOthers(allowOthers
) { }
1455 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1456 const wxString choices
[],
1458 : m_allowOthers(allowOthers
)
1462 m_choices
.Alloc(count
);
1463 for ( size_t n
= 0; n
< count
; n
++ )
1465 m_choices
.Add(choices
[n
]);
1470 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1472 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1473 editor
->m_allowOthers
= m_allowOthers
;
1474 editor
->m_choices
= m_choices
;
1479 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1481 wxEvtHandler
* evtHandler
)
1483 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1484 wxDefaultPosition
, wxDefaultSize
,
1486 m_allowOthers
? 0 : wxCB_READONLY
);
1488 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1491 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1492 wxGridCellAttr
* attr
)
1494 // as we fill the entire client area, don't do anything here to minimize
1497 // TODO: It doesn't actually fill the client area since the height of a
1498 // combo always defaults to the standard. Until someone has time to
1499 // figure out the right rectangle to paint, just do it the normal way.
1500 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1503 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1505 wxASSERT_MSG(m_control
,
1506 wxT("The wxGridCellEditor must be created first!"));
1508 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1510 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1512 // Don't immediately end if we get a kill focus event within BeginEdit
1514 evtHandler
->SetInSetFocus(true);
1516 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1520 Combo()->SetValue(m_startValue
);
1524 // find the right position, or default to the first if not found
1525 int pos
= Combo()->FindString(m_startValue
);
1526 if (pos
== wxNOT_FOUND
)
1528 Combo()->SetSelection(pos
);
1531 Combo()->SetInsertionPointEnd();
1532 Combo()->SetFocus();
1536 // When dropping down the menu, a kill focus event
1537 // happens after this point, so we can't reset the flag yet.
1538 #if !defined(__WXGTK20__)
1539 evtHandler
->SetInSetFocus(false);
1544 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1547 wxString value
= Combo()->GetValue();
1548 if ( value
== m_startValue
)
1551 grid
->GetTable()->SetValue(row
, col
, value
);
1556 void wxGridCellChoiceEditor::Reset()
1558 Combo()->SetValue(m_startValue
);
1559 Combo()->SetInsertionPointEnd();
1562 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1572 wxStringTokenizer
tk(params
, _T(','));
1573 while ( tk
.HasMoreTokens() )
1575 m_choices
.Add(tk
.GetNextToken());
1579 // return the value in the text control
1580 wxString
wxGridCellChoiceEditor::GetValue() const
1582 return Combo()->GetValue();
1585 #endif // wxUSE_COMBOBOX
1587 // ----------------------------------------------------------------------------
1588 // wxGridCellEditorEvtHandler
1589 // ----------------------------------------------------------------------------
1591 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
1593 // Don't disable the cell if we're just starting to edit it
1598 m_grid
->DisableCellEditControl();
1603 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1605 switch ( event
.GetKeyCode() )
1609 m_grid
->DisableCellEditControl();
1613 m_grid
->GetEventHandler()->ProcessEvent( event
);
1617 case WXK_NUMPAD_ENTER
:
1618 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1619 m_editor
->HandleReturn(event
);
1628 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1630 int row
= m_grid
->GetGridCursorRow();
1631 int col
= m_grid
->GetGridCursorCol();
1632 wxRect rect
= m_grid
->CellToRect( row
, col
);
1634 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
1636 // if cell width is smaller than grid client area, cell is wholly visible
1637 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
1639 switch ( event
.GetKeyCode() )
1644 case WXK_NUMPAD_ENTER
:
1649 if ( wholeCellVisible
)
1651 // no special processing needed...
1656 // do special processing for partly visible cell...
1658 // get the widths of all cells previous to this one
1660 for ( int i
= 0; i
< col
; i
++ )
1662 colXPos
+= m_grid
->GetColSize(i
);
1665 int xUnit
= 1, yUnit
= 1;
1666 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1669 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1673 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
1681 if ( wholeCellVisible
)
1683 // no special processing needed...
1688 // do special processing for partly visible cell...
1691 wxString value
= m_grid
->GetCellValue(row
, col
);
1692 if ( wxEmptyString
!= value
)
1694 // get width of cell CONTENTS (text)
1696 wxFont font
= m_grid
->GetCellFont(row
, col
);
1697 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
1699 // try to RIGHT align the text by scrolling
1700 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
1702 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
1703 // otherwise the last part of the cell content might be hidden below the scroll bar
1704 // FIXME: maybe there is a more suitable correction?
1705 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
1706 if ( textWidth
< 0 )
1712 // get the widths of all cells previous to this one
1714 for ( int i
= 0; i
< col
; i
++ )
1716 colXPos
+= m_grid
->GetColSize(i
);
1719 // and add the (modified) text width of the cell contents
1720 // as we'd like to see the last part of the cell contents
1721 colXPos
+= textWidth
;
1723 int xUnit
= 1, yUnit
= 1;
1724 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1725 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1736 // ----------------------------------------------------------------------------
1737 // wxGridCellWorker is an (almost) empty common base class for
1738 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1739 // ----------------------------------------------------------------------------
1741 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1746 wxGridCellWorker::~wxGridCellWorker()
1750 // ============================================================================
1752 // ============================================================================
1754 // ----------------------------------------------------------------------------
1755 // wxGridCellRenderer
1756 // ----------------------------------------------------------------------------
1758 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1759 wxGridCellAttr
& attr
,
1762 int WXUNUSED(row
), int WXUNUSED(col
),
1765 dc
.SetBackgroundMode( wxSOLID
);
1767 // grey out fields if the grid is disabled
1768 if ( grid
.IsEnabled() )
1772 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1776 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1781 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1784 dc
.SetPen( *wxTRANSPARENT_PEN
);
1785 dc
.DrawRectangle(rect
);
1788 // ----------------------------------------------------------------------------
1789 // wxGridCellStringRenderer
1790 // ----------------------------------------------------------------------------
1792 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
1793 const wxGridCellAttr
& attr
,
1797 dc
.SetBackgroundMode( wxTRANSPARENT
);
1799 // TODO some special colours for attr.IsReadOnly() case?
1801 // different coloured text when the grid is disabled
1802 if ( grid
.IsEnabled() )
1806 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1807 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1811 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1812 dc
.SetTextForeground( attr
.GetTextColour() );
1817 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1818 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
1821 dc
.SetFont( attr
.GetFont() );
1824 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
1826 const wxString
& text
)
1828 wxCoord x
= 0, y
= 0, max_x
= 0;
1829 dc
.SetFont(attr
.GetFont());
1830 wxStringTokenizer
tk(text
, _T('\n'));
1831 while ( tk
.HasMoreTokens() )
1833 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1834 max_x
= wxMax(max_x
, x
);
1837 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1839 return wxSize(max_x
, y
);
1842 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1843 wxGridCellAttr
& attr
,
1847 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1850 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1851 wxGridCellAttr
& attr
,
1853 const wxRect
& rectCell
,
1857 wxRect rect
= rectCell
;
1860 // erase only this cells background, overflow cells should have been erased
1861 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1864 attr
.GetAlignment(&hAlign
, &vAlign
);
1866 int overflowCols
= 0;
1868 if (attr
.GetOverflow())
1870 int cols
= grid
.GetNumberCols();
1871 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1872 int cell_rows
, cell_cols
;
1873 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <= 0
1874 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1876 int i
, c_cols
, c_rows
;
1877 for (i
= col
+cell_cols
; i
< cols
; i
++)
1879 bool is_empty
= true;
1880 for (int j
=row
; j
< row
+ cell_rows
; j
++)
1882 // check w/ anchor cell for multicell block
1883 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1886 if (!grid
.GetTable()->IsEmptyCell(j
+ c_rows
, i
))
1895 rect
.width
+= grid
.GetColSize(i
);
1903 if (rect
.width
>= best_width
)
1907 overflowCols
= i
- col
- cell_cols
+ 1;
1908 if (overflowCols
>= cols
)
1909 overflowCols
= cols
- 1;
1912 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1914 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1916 clip
.x
+= rectCell
.width
;
1917 // draw each overflow cell individually
1918 int col_end
= col
+ cell_cols
+ overflowCols
;
1919 if (col_end
>= grid
.GetNumberCols())
1920 col_end
= grid
.GetNumberCols() - 1;
1921 for (int i
= col
+ cell_cols
; i
<= col_end
; i
++)
1923 clip
.width
= grid
.GetColSize(i
) - 1;
1924 dc
.DestroyClippingRegion();
1925 dc
.SetClippingRegion(clip
);
1927 SetTextColoursAndFont(grid
, attr
, dc
,
1928 grid
.IsInSelection(row
,i
));
1930 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1931 rect
, hAlign
, vAlign
);
1932 clip
.x
+= grid
.GetColSize(i
) - 1;
1938 dc
.DestroyClippingRegion();
1942 // now we only have to draw the text
1943 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1945 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1946 rect
, hAlign
, vAlign
);
1949 // ----------------------------------------------------------------------------
1950 // wxGridCellNumberRenderer
1951 // ----------------------------------------------------------------------------
1953 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
1955 wxGridTableBase
*table
= grid
.GetTable();
1957 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1959 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1963 text
= table
->GetValue(row
, col
);
1969 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1970 wxGridCellAttr
& attr
,
1972 const wxRect
& rectCell
,
1976 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1978 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1980 // draw the text right aligned by default
1982 attr
.GetAlignment(&hAlign
, &vAlign
);
1983 hAlign
= wxALIGN_RIGHT
;
1985 wxRect rect
= rectCell
;
1988 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1991 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1992 wxGridCellAttr
& attr
,
1996 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1999 // ----------------------------------------------------------------------------
2000 // wxGridCellFloatRenderer
2001 // ----------------------------------------------------------------------------
2003 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
2006 SetPrecision(precision
);
2009 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
2011 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
2012 renderer
->m_width
= m_width
;
2013 renderer
->m_precision
= m_precision
;
2014 renderer
->m_format
= m_format
;
2019 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2021 wxGridTableBase
*table
= grid
.GetTable();
2026 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
2028 val
= table
->GetValueAsDouble(row
, col
);
2033 text
= table
->GetValue(row
, col
);
2034 hasDouble
= text
.ToDouble(&val
);
2041 if ( m_width
== -1 )
2043 if ( m_precision
== -1 )
2045 // default width/precision
2046 m_format
= _T("%f");
2050 m_format
.Printf(_T("%%.%df"), m_precision
);
2053 else if ( m_precision
== -1 )
2055 // default precision
2056 m_format
.Printf(_T("%%%d.f"), m_width
);
2060 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
2064 text
.Printf(m_format
, val
);
2067 //else: text already contains the string
2072 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
2073 wxGridCellAttr
& attr
,
2075 const wxRect
& rectCell
,
2079 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2081 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2083 // draw the text right aligned by default
2085 attr
.GetAlignment(&hAlign
, &vAlign
);
2086 hAlign
= wxALIGN_RIGHT
;
2088 wxRect rect
= rectCell
;
2091 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2094 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
2095 wxGridCellAttr
& attr
,
2099 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2102 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
2106 // reset to defaults
2112 wxString tmp
= params
.BeforeFirst(_T(','));
2116 if ( tmp
.ToLong(&width
) )
2118 SetWidth((int)width
);
2122 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
2126 tmp
= params
.AfterFirst(_T(','));
2130 if ( tmp
.ToLong(&precision
) )
2132 SetPrecision((int)precision
);
2136 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
2142 // ----------------------------------------------------------------------------
2143 // wxGridCellBoolRenderer
2144 // ----------------------------------------------------------------------------
2146 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
2148 // FIXME these checkbox size calculations are really ugly...
2150 // between checkmark and box
2151 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2153 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2154 wxGridCellAttr
& WXUNUSED(attr
),
2159 // compute it only once (no locks for MT safeness in GUI thread...)
2160 if ( !ms_sizeCheckMark
.x
)
2162 // get checkbox size
2163 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2164 wxSize size
= checkbox
->GetBestSize();
2165 wxCoord checkSize
= size
.y
+ 2 * wxGRID_CHECKMARK_MARGIN
;
2167 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
2168 #if defined(__WXGTK__) || defined(__WXMOTIF__)
2169 checkSize
-= size
.y
/ 2;
2174 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2177 return ms_sizeCheckMark
;
2180 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2181 wxGridCellAttr
& attr
,
2187 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2189 // draw a check mark in the centre (ignoring alignment - TODO)
2190 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2192 // don't draw outside the cell
2193 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2194 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2196 // and even leave (at least) 1 pixel margin
2197 size
.x
= size
.y
= minSize
- 2;
2200 // draw a border around checkmark
2202 attr
.GetAlignment(&hAlign
, &vAlign
);
2205 if (hAlign
== wxALIGN_CENTRE
)
2207 rectBorder
.x
= rect
.x
+ rect
.width
/ 2 - size
.x
/ 2;
2208 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2209 rectBorder
.width
= size
.x
;
2210 rectBorder
.height
= size
.y
;
2212 else if (hAlign
== wxALIGN_LEFT
)
2214 rectBorder
.x
= rect
.x
+ 2;
2215 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2216 rectBorder
.width
= size
.x
;
2217 rectBorder
.height
= size
.y
;
2219 else if (hAlign
== wxALIGN_RIGHT
)
2221 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2222 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2223 rectBorder
.width
= size
.x
;
2224 rectBorder
.height
= size
.y
;
2228 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2230 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2234 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2235 value
= wxGridCellBoolEditor::IsTrueValue(cellval
);
2240 wxRect rectMark
= rectBorder
;
2243 // MSW DrawCheckMark() is weird (and should probably be changed...)
2244 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/ 2);
2248 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2251 dc
.SetTextForeground(attr
.GetTextColour());
2252 dc
.DrawCheckMark(rectMark
);
2255 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2256 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2257 dc
.DrawRectangle(rectBorder
);
2260 // ----------------------------------------------------------------------------
2262 // ----------------------------------------------------------------------------
2264 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2268 m_isReadOnly
= Unset
;
2273 m_attrkind
= wxGridCellAttr::Cell
;
2275 m_sizeRows
= m_sizeCols
= 1;
2276 m_overflow
= UnsetOverflow
;
2278 SetDefAttr(attrDefault
);
2281 wxGridCellAttr
*wxGridCellAttr::Clone() const
2283 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2285 if ( HasTextColour() )
2286 attr
->SetTextColour(GetTextColour());
2287 if ( HasBackgroundColour() )
2288 attr
->SetBackgroundColour(GetBackgroundColour());
2290 attr
->SetFont(GetFont());
2291 if ( HasAlignment() )
2292 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2294 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2298 attr
->SetRenderer(m_renderer
);
2299 m_renderer
->IncRef();
2303 attr
->SetEditor(m_editor
);
2308 attr
->SetReadOnly();
2310 attr
->SetOverflow( m_overflow
== Overflow
);
2311 attr
->SetKind( m_attrkind
);
2316 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2318 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2319 SetTextColour(mergefrom
->GetTextColour());
2320 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2321 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2322 if ( !HasFont() && mergefrom
->HasFont() )
2323 SetFont(mergefrom
->GetFont());
2324 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2327 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2328 SetAlignment(hAlign
, vAlign
);
2330 if ( !HasSize() && mergefrom
->HasSize() )
2331 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2333 // Directly access member functions as GetRender/Editor don't just return
2334 // m_renderer/m_editor
2336 // Maybe add support for merge of Render and Editor?
2337 if (!HasRenderer() && mergefrom
->HasRenderer() )
2339 m_renderer
= mergefrom
->m_renderer
;
2340 m_renderer
->IncRef();
2342 if ( !HasEditor() && mergefrom
->HasEditor() )
2344 m_editor
= mergefrom
->m_editor
;
2347 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2348 SetReadOnly(mergefrom
->IsReadOnly());
2350 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2351 SetOverflow(mergefrom
->GetOverflow());
2353 SetDefAttr(mergefrom
->m_defGridAttr
);
2356 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2358 // The size of a cell is normally 1,1
2360 // If this cell is larger (2,2) then this is the top left cell
2361 // the other cells that will be covered (lower right cells) must be
2362 // set to negative or zero values such that
2363 // row + num_rows of the covered cell points to the larger cell (this cell)
2364 // same goes for the col + num_cols.
2366 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2368 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
2369 !((num_rows
<= 0) && (num_cols
> 0)) ||
2370 !((num_rows
== 0) && (num_cols
== 0))),
2371 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2373 m_sizeRows
= num_rows
;
2374 m_sizeCols
= num_cols
;
2377 const wxColour
& wxGridCellAttr::GetTextColour() const
2379 if (HasTextColour())
2383 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2385 return m_defGridAttr
->GetTextColour();
2389 wxFAIL_MSG(wxT("Missing default cell attribute"));
2390 return wxNullColour
;
2394 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2396 if (HasBackgroundColour())
2400 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2402 return m_defGridAttr
->GetBackgroundColour();
2406 wxFAIL_MSG(wxT("Missing default cell attribute"));
2407 return wxNullColour
;
2411 const wxFont
& wxGridCellAttr::GetFont() const
2417 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2419 return m_defGridAttr
->GetFont();
2423 wxFAIL_MSG(wxT("Missing default cell attribute"));
2428 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2437 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2439 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2443 wxFAIL_MSG(wxT("Missing default cell attribute"));
2447 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2450 *num_rows
= m_sizeRows
;
2452 *num_cols
= m_sizeCols
;
2455 // GetRenderer and GetEditor use a slightly different decision path about
2456 // which attribute to use. If a non-default attr object has one then it is
2457 // used, otherwise the default editor or renderer is fetched from the grid and
2458 // used. It should be the default for the data type of the cell. If it is
2459 // NULL (because the table has a type that the grid does not have in its
2460 // registry), then the grid's default editor or renderer is used.
2462 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2464 wxGridCellRenderer
*renderer
= NULL
;
2466 if ( m_renderer
&& this != m_defGridAttr
)
2468 // use the cells renderer if it has one
2469 renderer
= m_renderer
;
2472 else // no non-default cell renderer
2474 // get default renderer for the data type
2477 // GetDefaultRendererForCell() will do IncRef() for us
2478 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2481 if ( renderer
== NULL
)
2483 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2485 // if we still don't have one then use the grid default
2486 // (no need for IncRef() here neither)
2487 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2489 else // default grid attr
2491 // use m_renderer which we had decided not to use initially
2492 renderer
= m_renderer
;
2499 // we're supposed to always find something
2500 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2505 // same as above, except for s/renderer/editor/g
2506 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2508 wxGridCellEditor
*editor
= NULL
;
2510 if ( m_editor
&& this != m_defGridAttr
)
2512 // use the cells editor if it has one
2516 else // no non default cell editor
2518 // get default editor for the data type
2521 // GetDefaultEditorForCell() will do IncRef() for us
2522 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2525 if ( editor
== NULL
)
2527 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2529 // if we still don't have one then use the grid default
2530 // (no need for IncRef() here neither)
2531 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2533 else // default grid attr
2535 // use m_editor which we had decided not to use initially
2543 // we're supposed to always find something
2544 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2549 // ----------------------------------------------------------------------------
2550 // wxGridCellAttrData
2551 // ----------------------------------------------------------------------------
2553 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2555 int n
= FindIndex(row
, col
);
2556 if ( n
== wxNOT_FOUND
)
2558 // add the attribute
2559 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2563 // free the old attribute
2564 m_attrs
[(size_t)n
].attr
->DecRef();
2568 // change the attribute
2569 m_attrs
[(size_t)n
].attr
= attr
;
2573 // remove this attribute
2574 m_attrs
.RemoveAt((size_t)n
);
2579 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2581 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2583 int n
= FindIndex(row
, col
);
2584 if ( n
!= wxNOT_FOUND
)
2586 attr
= m_attrs
[(size_t)n
].attr
;
2593 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2595 size_t count
= m_attrs
.GetCount();
2596 for ( size_t n
= 0; n
< count
; n
++ )
2598 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2599 wxCoord row
= coords
.GetRow();
2600 if ((size_t)row
>= pos
)
2604 // If rows inserted, include row counter where necessary
2605 coords
.SetRow(row
+ numRows
);
2607 else if (numRows
< 0)
2609 // If rows deleted ...
2610 if ((size_t)row
>= pos
- numRows
)
2612 // ...either decrement row counter (if row still exists)...
2613 coords
.SetRow(row
+ numRows
);
2617 // ...or remove the attribute
2618 // No need to DecRef the attribute itself since this is
2619 // done be wxGridCellWithAttr's destructor!
2620 m_attrs
.RemoveAt(n
);
2629 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2631 size_t count
= m_attrs
.GetCount();
2632 for ( size_t n
= 0; n
< count
; n
++ )
2634 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2635 wxCoord col
= coords
.GetCol();
2636 if ( (size_t)col
>= pos
)
2640 // If rows inserted, include row counter where necessary
2641 coords
.SetCol(col
+ numCols
);
2643 else if (numCols
< 0)
2645 // If rows deleted ...
2646 if ((size_t)col
>= pos
- numCols
)
2648 // ...either decrement row counter (if row still exists)...
2649 coords
.SetCol(col
+ numCols
);
2653 // ...or remove the attribute
2654 // No need to DecRef the attribute itself since this is
2655 // done be wxGridCellWithAttr's destructor!
2656 m_attrs
.RemoveAt(n
);
2665 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2667 size_t count
= m_attrs
.GetCount();
2668 for ( size_t n
= 0; n
< count
; n
++ )
2670 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2671 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2680 // ----------------------------------------------------------------------------
2681 // wxGridRowOrColAttrData
2682 // ----------------------------------------------------------------------------
2684 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2686 size_t count
= m_attrs
.Count();
2687 for ( size_t n
= 0; n
< count
; n
++ )
2689 m_attrs
[n
]->DecRef();
2693 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2695 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2697 int n
= m_rowsOrCols
.Index(rowOrCol
);
2698 if ( n
!= wxNOT_FOUND
)
2700 attr
= m_attrs
[(size_t)n
];
2707 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2709 int i
= m_rowsOrCols
.Index(rowOrCol
);
2710 if ( i
== wxNOT_FOUND
)
2712 // add the attribute
2713 m_rowsOrCols
.Add(rowOrCol
);
2718 size_t n
= (size_t)i
;
2721 // change the attribute
2722 m_attrs
[n
]->DecRef();
2727 // remove this attribute
2728 m_attrs
[n
]->DecRef();
2729 m_rowsOrCols
.RemoveAt(n
);
2730 m_attrs
.RemoveAt(n
);
2735 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2737 size_t count
= m_attrs
.GetCount();
2738 for ( size_t n
= 0; n
< count
; n
++ )
2740 int & rowOrCol
= m_rowsOrCols
[n
];
2741 if ( (size_t)rowOrCol
>= pos
)
2743 if ( numRowsOrCols
> 0 )
2745 // If rows inserted, include row counter where necessary
2746 rowOrCol
+= numRowsOrCols
;
2748 else if ( numRowsOrCols
< 0)
2750 // If rows deleted, either decrement row counter (if row still exists)
2751 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2752 rowOrCol
+= numRowsOrCols
;
2755 m_rowsOrCols
.RemoveAt(n
);
2756 m_attrs
[n
]->DecRef();
2757 m_attrs
.RemoveAt(n
);
2766 // ----------------------------------------------------------------------------
2767 // wxGridCellAttrProvider
2768 // ----------------------------------------------------------------------------
2770 wxGridCellAttrProvider::wxGridCellAttrProvider()
2772 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2775 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2780 void wxGridCellAttrProvider::InitData()
2782 m_data
= new wxGridCellAttrProviderData
;
2785 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2786 wxGridCellAttr::wxAttrKind kind
) const
2788 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2793 case (wxGridCellAttr::Any
):
2794 // Get cached merge attributes.
2795 // Currently not used as no cache implemented as not mutable
2796 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2799 // Basically implement old version.
2800 // Also check merge cache, so we don't have to re-merge every time..
2801 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2802 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2803 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2805 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
2807 // Two or more are non NULL
2808 attr
= new wxGridCellAttr
;
2809 attr
->SetKind(wxGridCellAttr::Merged
);
2811 // Order is important..
2814 attr
->MergeWith(attrcell
);
2819 attr
->MergeWith(attrcol
);
2824 attr
->MergeWith(attrrow
);
2828 // store merge attr if cache implemented
2830 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2834 // one or none is non null return it or null.
2853 case (wxGridCellAttr::Cell
):
2854 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2857 case (wxGridCellAttr::Col
):
2858 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2861 case (wxGridCellAttr::Row
):
2862 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2867 // (wxGridCellAttr::Default):
2868 // (wxGridCellAttr::Merged):
2876 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2882 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2885 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2890 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2893 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2898 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2901 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2905 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2907 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2911 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2915 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2917 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2921 // ----------------------------------------------------------------------------
2922 // wxGridTypeRegistry
2923 // ----------------------------------------------------------------------------
2925 wxGridTypeRegistry::~wxGridTypeRegistry()
2927 size_t count
= m_typeinfo
.Count();
2928 for ( size_t i
= 0; i
< count
; i
++ )
2929 delete m_typeinfo
[i
];
2932 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2933 wxGridCellRenderer
* renderer
,
2934 wxGridCellEditor
* editor
)
2936 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2938 // is it already registered?
2939 int loc
= FindRegisteredDataType(typeName
);
2940 if ( loc
!= wxNOT_FOUND
)
2942 delete m_typeinfo
[loc
];
2943 m_typeinfo
[loc
] = info
;
2947 m_typeinfo
.Add(info
);
2951 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2953 size_t count
= m_typeinfo
.GetCount();
2954 for ( size_t i
= 0; i
< count
; i
++ )
2956 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2965 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2967 int index
= FindRegisteredDataType(typeName
);
2968 if ( index
== wxNOT_FOUND
)
2970 // check whether this is one of the standard ones, in which case
2971 // register it "on the fly"
2973 if ( typeName
== wxGRID_VALUE_STRING
)
2975 RegisterDataType(wxGRID_VALUE_STRING
,
2976 new wxGridCellStringRenderer
,
2977 new wxGridCellTextEditor
);
2980 #endif // wxUSE_TEXTCTRL
2982 if ( typeName
== wxGRID_VALUE_BOOL
)
2984 RegisterDataType(wxGRID_VALUE_BOOL
,
2985 new wxGridCellBoolRenderer
,
2986 new wxGridCellBoolEditor
);
2989 #endif // wxUSE_CHECKBOX
2991 if ( typeName
== wxGRID_VALUE_NUMBER
)
2993 RegisterDataType(wxGRID_VALUE_NUMBER
,
2994 new wxGridCellNumberRenderer
,
2995 new wxGridCellNumberEditor
);
2997 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2999 RegisterDataType(wxGRID_VALUE_FLOAT
,
3000 new wxGridCellFloatRenderer
,
3001 new wxGridCellFloatEditor
);
3004 #endif // wxUSE_TEXTCTRL
3006 if ( typeName
== wxGRID_VALUE_CHOICE
)
3008 RegisterDataType(wxGRID_VALUE_CHOICE
,
3009 new wxGridCellStringRenderer
,
3010 new wxGridCellChoiceEditor
);
3013 #endif // wxUSE_COMBOBOX
3018 // we get here only if just added the entry for this type, so return
3020 index
= m_typeinfo
.GetCount() - 1;
3026 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
3028 int index
= FindDataType(typeName
);
3029 if ( index
== wxNOT_FOUND
)
3031 // the first part of the typename is the "real" type, anything after ':'
3032 // are the parameters for the renderer
3033 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
3034 if ( index
== wxNOT_FOUND
)
3039 wxGridCellRenderer
*renderer
= GetRenderer(index
);
3040 wxGridCellRenderer
*rendererOld
= renderer
;
3041 renderer
= renderer
->Clone();
3042 rendererOld
->DecRef();
3044 wxGridCellEditor
*editor
= GetEditor(index
);
3045 wxGridCellEditor
*editorOld
= editor
;
3046 editor
= editor
->Clone();
3047 editorOld
->DecRef();
3049 // do it even if there are no parameters to reset them to defaults
3050 wxString params
= typeName
.AfterFirst(_T(':'));
3051 renderer
->SetParameters(params
);
3052 editor
->SetParameters(params
);
3054 // register the new typename
3055 RegisterDataType(typeName
, renderer
, editor
);
3057 // we just registered it, it's the last one
3058 index
= m_typeinfo
.GetCount() - 1;
3064 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
3066 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
3073 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
3075 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
3082 // ----------------------------------------------------------------------------
3084 // ----------------------------------------------------------------------------
3086 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
3088 wxGridTableBase::wxGridTableBase()
3090 m_view
= (wxGrid
*) NULL
;
3091 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
3094 wxGridTableBase::~wxGridTableBase()
3096 delete m_attrProvider
;
3099 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
3101 delete m_attrProvider
;
3102 m_attrProvider
= attrProvider
;
3105 bool wxGridTableBase::CanHaveAttributes()
3107 if ( ! GetAttrProvider() )
3109 // use the default attr provider by default
3110 SetAttrProvider(new wxGridCellAttrProvider
);
3116 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
3118 if ( m_attrProvider
)
3119 return m_attrProvider
->GetAttr(row
, col
, kind
);
3121 return (wxGridCellAttr
*)NULL
;
3124 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
3126 if ( m_attrProvider
)
3128 attr
->SetKind(wxGridCellAttr::Cell
);
3129 m_attrProvider
->SetAttr(attr
, row
, col
);
3133 // as we take ownership of the pointer and don't store it, we must
3139 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3141 if ( m_attrProvider
)
3143 attr
->SetKind(wxGridCellAttr::Row
);
3144 m_attrProvider
->SetRowAttr(attr
, row
);
3148 // as we take ownership of the pointer and don't store it, we must
3154 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3156 if ( m_attrProvider
)
3158 attr
->SetKind(wxGridCellAttr::Col
);
3159 m_attrProvider
->SetColAttr(attr
, col
);
3163 // as we take ownership of the pointer and don't store it, we must
3169 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3170 size_t WXUNUSED(numRows
) )
3172 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3177 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3179 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3184 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3185 size_t WXUNUSED(numRows
) )
3187 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3192 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3193 size_t WXUNUSED(numCols
) )
3195 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3200 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3202 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3207 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3208 size_t WXUNUSED(numCols
) )
3210 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3215 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3219 // RD: Starting the rows at zero confuses users,
3220 // no matter how much it makes sense to us geeks.
3226 wxString
wxGridTableBase::GetColLabelValue( int col
)
3228 // default col labels are:
3229 // cols 0 to 25 : A-Z
3230 // cols 26 to 675 : AA-ZZ
3235 for ( n
= 1; ; n
++ )
3237 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
3243 // reverse the string...
3245 for ( i
= 0; i
< n
; i
++ )
3253 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3255 return wxGRID_VALUE_STRING
;
3258 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3259 const wxString
& typeName
)
3261 return typeName
== wxGRID_VALUE_STRING
;
3264 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3266 return CanGetValueAs(row
, col
, typeName
);
3269 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3274 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3279 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3284 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3285 long WXUNUSED(value
) )
3289 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3290 double WXUNUSED(value
) )
3294 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3295 bool WXUNUSED(value
) )
3299 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3300 const wxString
& WXUNUSED(typeName
) )
3305 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3306 const wxString
& WXUNUSED(typeName
),
3307 void* WXUNUSED(value
) )
3311 //////////////////////////////////////////////////////////////////////
3313 // Message class for the grid table to send requests and notifications
3317 wxGridTableMessage::wxGridTableMessage()
3319 m_table
= (wxGridTableBase
*) NULL
;
3325 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3326 int commandInt1
, int commandInt2
)
3330 m_comInt1
= commandInt1
;
3331 m_comInt2
= commandInt2
;
3334 //////////////////////////////////////////////////////////////////////
3336 // A basic grid table for string data. An object of this class will
3337 // created by wxGrid if you don't specify an alternative table class.
3340 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3342 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3344 wxGridStringTable::wxGridStringTable()
3349 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3352 m_data
.Alloc( numRows
);
3355 sa
.Alloc( numCols
);
3356 sa
.Add( wxEmptyString
, numCols
);
3358 m_data
.Add( sa
, numRows
);
3361 wxGridStringTable::~wxGridStringTable()
3365 int wxGridStringTable::GetNumberRows()
3367 return m_data
.GetCount();
3370 int wxGridStringTable::GetNumberCols()
3372 if ( m_data
.GetCount() > 0 )
3373 return m_data
[0].GetCount();
3378 wxString
wxGridStringTable::GetValue( int row
, int col
)
3380 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3382 _T("invalid row or column index in wxGridStringTable") );
3384 return m_data
[row
][col
];
3387 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3389 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3390 _T("invalid row or column index in wxGridStringTable") );
3392 m_data
[row
][col
] = value
;
3395 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3397 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3399 _T("invalid row or column index in wxGridStringTable") );
3401 return (m_data
[row
][col
] == wxEmptyString
);
3404 void wxGridStringTable::Clear()
3407 int numRows
, numCols
;
3409 numRows
= m_data
.GetCount();
3412 numCols
= m_data
[0].GetCount();
3414 for ( row
= 0; row
< numRows
; row
++ )
3416 for ( col
= 0; col
< numCols
; col
++ )
3418 m_data
[row
][col
] = wxEmptyString
;
3424 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3426 size_t curNumRows
= m_data
.GetCount();
3427 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3428 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3430 if ( pos
>= curNumRows
)
3432 return AppendRows( numRows
);
3436 sa
.Alloc( curNumCols
);
3437 sa
.Add( wxEmptyString
, curNumCols
);
3438 m_data
.Insert( sa
, pos
, numRows
);
3442 wxGridTableMessage
msg( this,
3443 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3447 GetView()->ProcessTableMessage( msg
);
3453 bool wxGridStringTable::AppendRows( size_t numRows
)
3455 size_t curNumRows
= m_data
.GetCount();
3456 size_t curNumCols
= ( curNumRows
> 0
3457 ? m_data
[0].GetCount()
3458 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3461 if ( curNumCols
> 0 )
3463 sa
.Alloc( curNumCols
);
3464 sa
.Add( wxEmptyString
, curNumCols
);
3467 m_data
.Add( sa
, numRows
);
3471 wxGridTableMessage
msg( this,
3472 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3475 GetView()->ProcessTableMessage( msg
);
3481 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3483 size_t curNumRows
= m_data
.GetCount();
3485 if ( pos
>= curNumRows
)
3487 wxFAIL_MSG( wxString::Format
3489 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3491 (unsigned long)numRows
,
3492 (unsigned long)curNumRows
3498 if ( numRows
> curNumRows
- pos
)
3500 numRows
= curNumRows
- pos
;
3503 if ( numRows
>= curNumRows
)
3509 m_data
.RemoveAt( pos
, numRows
);
3514 wxGridTableMessage
msg( this,
3515 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3519 GetView()->ProcessTableMessage( msg
);
3525 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3529 size_t curNumRows
= m_data
.GetCount();
3530 size_t curNumCols
= ( curNumRows
> 0
3531 ? m_data
[0].GetCount()
3532 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3534 if ( pos
>= curNumCols
)
3536 return AppendCols( numCols
);
3539 if ( !m_colLabels
.IsEmpty() )
3541 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
3544 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
3545 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
3548 for ( row
= 0; row
< curNumRows
; row
++ )
3550 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3552 m_data
[row
].Insert( wxEmptyString
, col
);
3558 wxGridTableMessage
msg( this,
3559 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3563 GetView()->ProcessTableMessage( msg
);
3569 bool wxGridStringTable::AppendCols( size_t numCols
)
3573 size_t curNumRows
= m_data
.GetCount();
3578 // TODO: something better than this ?
3580 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3585 for ( row
= 0; row
< curNumRows
; row
++ )
3587 m_data
[row
].Add( wxEmptyString
, numCols
);
3592 wxGridTableMessage
msg( this,
3593 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3596 GetView()->ProcessTableMessage( msg
);
3602 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3606 size_t curNumRows
= m_data
.GetCount();
3607 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3608 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3610 if ( pos
>= curNumCols
)
3612 wxFAIL_MSG( wxString::Format
3614 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3616 (unsigned long)numCols
,
3617 (unsigned long)curNumCols
3624 colID
= GetView()->GetColAt( pos
);
3628 if ( numCols
> curNumCols
- colID
)
3630 numCols
= curNumCols
- colID
;
3633 if ( !m_colLabels
.IsEmpty() )
3635 m_colLabels
.RemoveAt( colID
, numCols
);
3638 for ( row
= 0; row
< curNumRows
; row
++ )
3640 if ( numCols
>= curNumCols
)
3642 m_data
[row
].Clear();
3646 m_data
[row
].RemoveAt( colID
, numCols
);
3652 wxGridTableMessage
msg( this,
3653 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3657 GetView()->ProcessTableMessage( msg
);
3663 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3665 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3667 // using default label
3669 return wxGridTableBase::GetRowLabelValue( row
);
3673 return m_rowLabels
[row
];
3677 wxString
wxGridStringTable::GetColLabelValue( int col
)
3679 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3681 // using default label
3683 return wxGridTableBase::GetColLabelValue( col
);
3687 return m_colLabels
[col
];
3691 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3693 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3695 int n
= m_rowLabels
.GetCount();
3698 for ( i
= n
; i
<= row
; i
++ )
3700 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3704 m_rowLabels
[row
] = value
;
3707 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3709 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3711 int n
= m_colLabels
.GetCount();
3714 for ( i
= n
; i
<= col
; i
++ )
3716 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3720 m_colLabels
[col
] = value
;
3724 //////////////////////////////////////////////////////////////////////
3725 //////////////////////////////////////////////////////////////////////
3727 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3729 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3730 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3731 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3732 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3733 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3734 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3735 EVT_CHAR( wxGridRowLabelWindow::OnChar
)
3738 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3740 const wxPoint
&pos
, const wxSize
&size
)
3741 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxFULL_REPAINT_ON_RESIZE
)
3746 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3750 // NO - don't do this because it will set both the x and y origin
3751 // coords to match the parent scrolled window and we just want to
3752 // set the y coord - MB
3754 // m_owner->PrepareDC( dc );
3757 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3758 wxPoint pt
= dc
.GetDeviceOrigin();
3759 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
3761 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3762 m_owner
->DrawRowLabels( dc
, rows
);
3765 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3767 m_owner
->ProcessRowLabelMouseEvent( event
);
3770 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3772 m_owner
->GetEventHandler()->ProcessEvent( event
);
3775 // This seems to be required for wxMotif otherwise the mouse
3776 // cursor must be in the cell edit control to get key events
3778 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3780 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3784 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3786 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3790 void wxGridRowLabelWindow::OnChar( wxKeyEvent
& event
)
3792 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3796 //////////////////////////////////////////////////////////////////////
3798 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3800 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3801 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3802 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3803 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3804 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3805 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3806 EVT_CHAR( wxGridColLabelWindow::OnChar
)
3809 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3811 const wxPoint
&pos
, const wxSize
&size
)
3812 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxFULL_REPAINT_ON_RESIZE
)
3817 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3821 // NO - don't do this because it will set both the x and y origin
3822 // coords to match the parent scrolled window and we just want to
3823 // set the x coord - MB
3825 // m_owner->PrepareDC( dc );
3828 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3829 wxPoint pt
= dc
.GetDeviceOrigin();
3830 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3831 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
3833 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
3835 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3836 m_owner
->DrawColLabels( dc
, cols
);
3839 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3841 m_owner
->ProcessColLabelMouseEvent( event
);
3844 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3846 m_owner
->GetEventHandler()->ProcessEvent( event
);
3849 // This seems to be required for wxMotif otherwise the mouse
3850 // cursor must be in the cell edit control to get key events
3852 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3854 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3858 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3860 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3864 void wxGridColLabelWindow::OnChar( wxKeyEvent
& event
)
3866 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3870 //////////////////////////////////////////////////////////////////////
3872 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3874 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3875 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3876 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3877 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3878 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3879 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3880 EVT_CHAR( wxGridCornerLabelWindow::OnChar
)
3883 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3885 const wxPoint
&pos
, const wxSize
&size
)
3886 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxFULL_REPAINT_ON_RESIZE
)
3891 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3895 int client_height
= 0;
3896 int client_width
= 0;
3897 GetClientSize( &client_width
, &client_height
);
3899 // VZ: any reason for this ifdef? (FIXME)
3905 rect
.SetWidth( client_width
- 2 );
3906 rect
.SetHeight( client_height
- 2 );
3908 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3910 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
3911 dc
.DrawLine( client_width
- 1, client_height
- 1, client_width
- 1, 0 );
3912 dc
.DrawLine( client_width
- 1, client_height
- 1, 0, client_height
- 1 );
3913 dc
.DrawLine( 0, 0, client_width
, 0 );
3914 dc
.DrawLine( 0, 0, 0, client_height
);
3916 dc
.SetPen( *wxWHITE_PEN
);
3917 dc
.DrawLine( 1, 1, client_width
- 1, 1 );
3918 dc
.DrawLine( 1, 1, 1, client_height
- 1 );
3922 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3924 m_owner
->ProcessCornerLabelMouseEvent( event
);
3927 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3929 m_owner
->GetEventHandler()->ProcessEvent(event
);
3932 // This seems to be required for wxMotif otherwise the mouse
3933 // cursor must be in the cell edit control to get key events
3935 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3937 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3941 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3943 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3947 void wxGridCornerLabelWindow::OnChar( wxKeyEvent
& event
)
3949 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3953 //////////////////////////////////////////////////////////////////////
3955 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3957 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3958 EVT_PAINT( wxGridWindow::OnPaint
)
3959 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3960 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3961 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3962 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3963 EVT_CHAR( wxGridWindow::OnChar
)
3964 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3965 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3966 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3969 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3970 wxGridRowLabelWindow
*rowLblWin
,
3971 wxGridColLabelWindow
*colLblWin
,
3974 const wxSize
&size
)
3976 parent
, id
, pos
, size
,
3977 wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
| wxFULL_REPAINT_ON_RESIZE
,
3978 wxT("grid window") )
3981 m_rowLabelWin
= rowLblWin
;
3982 m_colLabelWin
= colLblWin
;
3985 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3987 wxPaintDC
dc( this );
3988 m_owner
->PrepareDC( dc
);
3989 wxRegion reg
= GetUpdateRegion();
3990 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
3991 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
3993 #if WXGRID_DRAW_LINES
3994 m_owner
->DrawAllGridLines( dc
, reg
);
3997 m_owner
->DrawGridSpace( dc
);
3998 m_owner
->DrawHighlight( dc
, dirtyCells
);
4001 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
4003 wxWindow::ScrollWindow( dx
, dy
, rect
);
4004 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
4005 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
4008 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
4010 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
4013 m_owner
->ProcessGridCellMouseEvent( event
);
4016 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
4018 m_owner
->GetEventHandler()->ProcessEvent( event
);
4021 // This seems to be required for wxMotif/wxGTK otherwise the mouse
4022 // cursor must be in the cell edit control to get key events
4024 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
4026 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4030 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
4032 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4036 void wxGridWindow::OnChar( wxKeyEvent
& event
)
4038 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4042 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
4046 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
4048 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4052 //////////////////////////////////////////////////////////////////////
4054 // Internal Helper function for computing row or column from some
4055 // (unscrolled) coordinate value, using either
4056 // m_defaultRowHeight/m_defaultColWidth or binary search on array
4057 // of m_rowBottoms/m_ColRights to speed up the search!
4059 // Internal helper macros for simpler use of that function
4061 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
4062 const wxArrayInt
& BorderArray
, int nMax
,
4065 #define internalXToCol(x) XToCol(x, true)
4066 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
4067 m_minAcceptableRowHeight, \
4068 m_rowBottoms, m_numRows, true)
4070 /////////////////////////////////////////////////////////////////////
4072 #if wxUSE_EXTENDED_RTTI
4073 WX_DEFINE_FLAGS( wxGridStyle
)
4075 wxBEGIN_FLAGS( wxGridStyle
)
4076 // new style border flags, we put them first to
4077 // use them for streaming out
4078 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4079 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4080 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4081 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4082 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4083 wxFLAGS_MEMBER(wxBORDER_NONE
)
4085 // old style border flags
4086 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4087 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4088 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4089 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4090 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4091 wxFLAGS_MEMBER(wxBORDER
)
4093 // standard window styles
4094 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4095 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4096 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4097 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4098 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4099 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4100 wxFLAGS_MEMBER(wxVSCROLL
)
4101 wxFLAGS_MEMBER(wxHSCROLL
)
4103 wxEND_FLAGS( wxGridStyle
)
4105 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4107 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4108 wxHIDE_PROPERTY( Children
)
4109 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4110 wxEND_PROPERTIES_TABLE()
4112 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4113 wxEND_HANDLERS_TABLE()
4115 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4118 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
4121 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4124 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4125 EVT_PAINT( wxGrid::OnPaint
)
4126 EVT_SIZE( wxGrid::OnSize
)
4127 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
4128 EVT_KEY_UP( wxGrid::OnKeyUp
)
4129 EVT_CHAR ( wxGrid::OnChar
)
4130 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
4135 // in order to make sure that a size event is not
4136 // trigerred in a unfinished state
4137 m_cornerLabelWin
= NULL
;
4138 m_rowLabelWin
= NULL
;
4139 m_colLabelWin
= NULL
;
4143 wxGrid::wxGrid( wxWindow
*parent
,
4148 const wxString
& name
)
4149 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
4150 m_colMinWidths(GRID_HASH_SIZE
),
4151 m_rowMinHeights(GRID_HASH_SIZE
)
4154 SetBestFittingSize(size
);
4157 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
4158 const wxPoint
& pos
, const wxSize
& size
,
4159 long style
, const wxString
& name
)
4161 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
4162 style
| wxWANTS_CHARS
, name
))
4165 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4166 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4169 SetBestFittingSize(size
);
4176 // Must do this or ~wxScrollHelper will pop the wrong event handler
4177 SetTargetWindow(this);
4179 wxSafeDecRef(m_defaultCellAttr
);
4181 #ifdef DEBUG_ATTR_CACHE
4182 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4183 wxPrintf(_T("wxGrid attribute cache statistics: "
4184 "total: %u, hits: %u (%u%%)\n"),
4185 total
, gs_nAttrCacheHits
,
4186 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4192 delete m_typeRegistry
;
4197 // ----- internal init and update functions
4200 // NOTE: If using the default visual attributes works everywhere then this can
4201 // be removed as well as the #else cases below.
4202 #define _USE_VISATTR 0
4204 void wxGrid::Create()
4206 // set to true by CreateGrid
4209 // create the type registry
4210 m_typeRegistry
= new wxGridTypeRegistry
;
4213 m_table
= (wxGridTableBase
*) NULL
;
4216 m_cellEditCtrlEnabled
= false;
4218 m_defaultCellAttr
= new wxGridCellAttr();
4220 // Set default cell attributes
4221 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4222 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4223 m_defaultCellAttr
->SetFont(GetFont());
4224 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4225 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4226 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4229 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4230 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4232 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4233 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4236 m_defaultCellAttr
->SetTextColour(
4237 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4238 m_defaultCellAttr
->SetBackgroundColour(
4239 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4244 m_currentCellCoords
= wxGridNoCellCoords
;
4246 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4247 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4249 // subwindow components that make up the wxGrid
4250 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4255 m_colLabelWin
= new wxGridColLabelWindow( this,
4260 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4265 m_gridWin
= new wxGridWindow( this,
4272 SetTargetWindow( m_gridWin
);
4275 wxColour gfg
= gva
.colFg
;
4276 wxColour gbg
= gva
.colBg
;
4277 wxColour lfg
= lva
.colFg
;
4278 wxColour lbg
= lva
.colBg
;
4280 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4281 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4282 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4283 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4286 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4287 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4288 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4289 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4290 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4291 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4293 m_gridWin
->SetOwnForegroundColour(gfg
);
4294 m_gridWin
->SetOwnBackgroundColour(gbg
);
4299 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4300 wxGrid::wxGridSelectionModes selmode
)
4302 wxCHECK_MSG( !m_created
,
4304 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4306 m_numRows
= numRows
;
4307 m_numCols
= numCols
;
4309 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4310 m_table
->SetView( this );
4312 m_selection
= new wxGridSelection( this, selmode
);
4321 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4323 wxCHECK_RET( m_created
,
4324 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4326 m_selection
->SetSelectionMode( selmode
);
4329 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4331 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4332 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4334 return m_selection
->GetSelectionMode();
4337 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4338 wxGrid::wxGridSelectionModes selmode
)
4342 // stop all processing
4347 wxGridTableBase
*t
= m_table
;
4362 m_numRows
= table
->GetNumberRows();
4363 m_numCols
= table
->GetNumberCols();
4366 m_table
->SetView( this );
4367 m_ownTable
= takeOwnership
;
4368 m_selection
= new wxGridSelection( this, selmode
);
4380 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4381 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4383 if ( m_rowLabelWin
)
4385 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4389 m_labelBackgroundColour
= *wxWHITE
;
4392 m_labelTextColour
= *wxBLACK
;
4395 m_attrCache
.row
= -1;
4396 m_attrCache
.col
= -1;
4397 m_attrCache
.attr
= NULL
;
4399 // TODO: something better than this ?
4401 m_labelFont
= this->GetFont();
4402 m_labelFont
.SetWeight( wxBOLD
);
4404 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4405 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4407 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4408 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4409 m_colLabelTextOrientation
= wxHORIZONTAL
;
4411 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4412 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4414 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4415 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4417 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4418 m_defaultRowHeight
+= 8;
4420 m_defaultRowHeight
+= 4;
4423 m_gridLineColour
= wxColour( 192,192,192 );
4424 m_gridLinesEnabled
= true;
4425 m_cellHighlightColour
= *wxBLACK
;
4426 m_cellHighlightPenWidth
= 2;
4427 m_cellHighlightROPenWidth
= 1;
4429 m_canDragColMove
= false;
4431 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4432 m_winCapture
= (wxWindow
*)NULL
;
4433 m_canDragRowSize
= true;
4434 m_canDragColSize
= true;
4435 m_canDragGridSize
= true;
4436 m_canDragCell
= false;
4438 m_dragRowOrCol
= -1;
4439 m_isDragging
= false;
4440 m_startDragPos
= wxDefaultPosition
;
4442 m_waitForSlowClick
= false;
4444 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4445 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4447 m_currentCellCoords
= wxGridNoCellCoords
;
4451 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4452 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4454 m_editable
= true; // default for whole grid
4456 m_inOnKeyDown
= false;
4462 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4463 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4466 // ----------------------------------------------------------------------------
4467 // the idea is to call these functions only when necessary because they create
4468 // quite big arrays which eat memory mostly unnecessary - in particular, if
4469 // default widths/heights are used for all rows/columns, we may not use these
4472 // with some extra code, it should be possible to only store the
4473 // widths/heights different from default ones but this will be done later...
4474 // ----------------------------------------------------------------------------
4476 void wxGrid::InitRowHeights()
4478 m_rowHeights
.Empty();
4479 m_rowBottoms
.Empty();
4481 m_rowHeights
.Alloc( m_numRows
);
4482 m_rowBottoms
.Alloc( m_numRows
);
4486 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4488 for ( int i
= 0; i
< m_numRows
; i
++ )
4490 rowBottom
+= m_defaultRowHeight
;
4491 m_rowBottoms
.Add( rowBottom
);
4495 void wxGrid::InitColWidths()
4497 m_colWidths
.Empty();
4498 m_colRights
.Empty();
4500 m_colWidths
.Alloc( m_numCols
);
4501 m_colRights
.Alloc( m_numCols
);
4504 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4506 for ( int i
= 0; i
< m_numCols
; i
++ )
4508 colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
4509 m_colRights
.Add( colRight
);
4513 int wxGrid::GetColWidth(int col
) const
4515 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4518 int wxGrid::GetColLeft(int col
) const
4520 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
4521 : m_colRights
[col
] - m_colWidths
[col
];
4524 int wxGrid::GetColRight(int col
) const
4526 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
4530 int wxGrid::GetRowHeight(int row
) const
4532 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4535 int wxGrid::GetRowTop(int row
) const
4537 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4538 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4541 int wxGrid::GetRowBottom(int row
) const
4543 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4544 : m_rowBottoms
[row
];
4547 void wxGrid::CalcDimensions()
4550 GetClientSize( &cw
, &ch
);
4552 if ( m_rowLabelWin
->IsShown() )
4553 cw
-= m_rowLabelWidth
;
4554 if ( m_colLabelWin
->IsShown() )
4555 ch
-= m_colLabelHeight
;
4558 int w
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) + m_extraWidth
+ 1 : 0;
4559 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4561 // take into account editor if shown
4562 if ( IsCellEditControlShown() )
4565 int r
= m_currentCellCoords
.GetRow();
4566 int c
= m_currentCellCoords
.GetCol();
4567 int x
= GetColLeft(c
);
4568 int y
= GetRowTop(r
);
4570 // how big is the editor
4571 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4572 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4573 editor
->GetControl()->GetSize(&w2
, &h2
);
4584 // preserve (more or less) the previous position
4586 GetViewStart( &x
, &y
);
4588 // ensure the position is valid for the new scroll ranges
4590 x
= wxMax( w
- 1, 0 );
4592 y
= wxMax( h
- 1, 0 );
4594 // do set scrollbar parameters
4595 SetScrollbars( m_scrollLineX
, m_scrollLineY
,
4596 GetScrollX(w
), GetScrollY(h
), x
, y
,
4597 GetBatchCount() != 0);
4599 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4600 // still must reposition the children
4604 void wxGrid::CalcWindowSizes()
4606 // escape if the window is has not been fully created yet
4608 if ( m_cornerLabelWin
== NULL
)
4612 GetClientSize( &cw
, &ch
);
4614 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4615 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4617 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4618 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
- m_rowLabelWidth
, m_colLabelHeight
);
4620 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4621 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
- m_colLabelHeight
);
4623 if ( m_gridWin
&& m_gridWin
->IsShown() )
4624 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
- m_rowLabelWidth
, ch
- m_colLabelHeight
);
4627 // this is called when the grid table sends a message
4628 // to indicate that it has been redimensioned
4630 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4633 bool result
= false;
4635 // Clear the attribute cache as the attribute might refer to a different
4636 // cell than stored in the cache after adding/removing rows/columns.
4639 // By the same reasoning, the editor should be dismissed if columns are
4640 // added or removed. And for consistency, it should IMHO always be
4641 // removed, not only if the cell "underneath" it actually changes.
4642 // For now, I intentionally do not save the editor's content as the
4643 // cell it might want to save that stuff to might no longer exist.
4644 HideCellEditControl();
4647 // if we were using the default widths/heights so far, we must change them
4649 if ( m_colWidths
.IsEmpty() )
4654 if ( m_rowHeights
.IsEmpty() )
4660 switch ( msg
.GetId() )
4662 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4664 size_t pos
= msg
.GetCommandInt();
4665 int numRows
= msg
.GetCommandInt2();
4667 m_numRows
+= numRows
;
4669 if ( !m_rowHeights
.IsEmpty() )
4671 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4672 m_rowBottoms
.Insert( 0, pos
, numRows
);
4676 bottom
= m_rowBottoms
[pos
- 1];
4678 for ( i
= pos
; i
< m_numRows
; i
++ )
4680 bottom
+= m_rowHeights
[i
];
4681 m_rowBottoms
[i
] = bottom
;
4685 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4687 // if we have just inserted cols into an empty grid the current
4688 // cell will be undefined...
4690 SetCurrentCell( 0, 0 );
4694 m_selection
->UpdateRows( pos
, numRows
);
4695 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4697 attrProvider
->UpdateAttrRows( pos
, numRows
);
4699 if ( !GetBatchCount() )
4702 m_rowLabelWin
->Refresh();
4708 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4710 int numRows
= msg
.GetCommandInt();
4711 int oldNumRows
= m_numRows
;
4712 m_numRows
+= numRows
;
4714 if ( !m_rowHeights
.IsEmpty() )
4716 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4717 m_rowBottoms
.Add( 0, numRows
);
4720 if ( oldNumRows
> 0 )
4721 bottom
= m_rowBottoms
[oldNumRows
- 1];
4723 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4725 bottom
+= m_rowHeights
[i
];
4726 m_rowBottoms
[i
] = bottom
;
4730 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4732 // if we have just inserted cols into an empty grid the current
4733 // cell will be undefined...
4735 SetCurrentCell( 0, 0 );
4738 if ( !GetBatchCount() )
4741 m_rowLabelWin
->Refresh();
4747 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4749 size_t pos
= msg
.GetCommandInt();
4750 int numRows
= msg
.GetCommandInt2();
4751 m_numRows
-= numRows
;
4753 if ( !m_rowHeights
.IsEmpty() )
4755 m_rowHeights
.RemoveAt( pos
, numRows
);
4756 m_rowBottoms
.RemoveAt( pos
, numRows
);
4759 for ( i
= 0; i
< m_numRows
; i
++ )
4761 h
+= m_rowHeights
[i
];
4762 m_rowBottoms
[i
] = h
;
4768 m_currentCellCoords
= wxGridNoCellCoords
;
4772 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4773 m_currentCellCoords
.Set( 0, 0 );
4777 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4778 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4781 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4783 // ifdef'd out following patch from Paul Gammans
4785 // No need to touch column attributes, unless we
4786 // removed _all_ rows, in this case, we remove
4787 // all column attributes.
4788 // I hate to do this here, but the
4789 // needed data is not available inside UpdateAttrRows.
4790 if ( !GetNumberRows() )
4791 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4795 if ( !GetBatchCount() )
4798 m_rowLabelWin
->Refresh();
4804 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4806 size_t pos
= msg
.GetCommandInt();
4807 int numCols
= msg
.GetCommandInt2();
4808 m_numCols
+= numCols
;
4810 if ( !m_colAt
.IsEmpty() )
4812 //Shift the column IDs
4814 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
4816 if ( m_colAt
[i
] >= (int)pos
)
4817 m_colAt
[i
] += numCols
;
4820 m_colAt
.Insert( pos
, pos
, numCols
);
4822 //Set the new columns' positions
4823 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
4829 if ( !m_colWidths
.IsEmpty() )
4831 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4832 m_colRights
.Insert( 0, pos
, numCols
);
4836 right
= m_colRights
[GetColAt( pos
- 1 )];
4839 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
4841 i
= GetColAt( colPos
);
4843 right
+= m_colWidths
[i
];
4844 m_colRights
[i
] = right
;
4848 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4850 // if we have just inserted cols into an empty grid the current
4851 // cell will be undefined...
4853 SetCurrentCell( 0, 0 );
4857 m_selection
->UpdateCols( pos
, numCols
);
4858 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4860 attrProvider
->UpdateAttrCols( pos
, numCols
);
4861 if ( !GetBatchCount() )
4864 m_colLabelWin
->Refresh();
4870 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4872 int numCols
= msg
.GetCommandInt();
4873 int oldNumCols
= m_numCols
;
4874 m_numCols
+= numCols
;
4876 if ( !m_colAt
.IsEmpty() )
4878 m_colAt
.Add( 0, numCols
);
4880 //Set the new columns' positions
4882 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4888 if ( !m_colWidths
.IsEmpty() )
4890 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4891 m_colRights
.Add( 0, numCols
);
4894 if ( oldNumCols
> 0 )
4895 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
4898 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
4900 i
= GetColAt( colPos
);
4902 right
+= m_colWidths
[i
];
4903 m_colRights
[i
] = right
;
4907 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4909 // if we have just inserted cols into an empty grid the current
4910 // cell will be undefined...
4912 SetCurrentCell( 0, 0 );
4914 if ( !GetBatchCount() )
4917 m_colLabelWin
->Refresh();
4923 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4925 size_t pos
= msg
.GetCommandInt();
4926 int numCols
= msg
.GetCommandInt2();
4927 m_numCols
-= numCols
;
4929 if ( !m_colAt
.IsEmpty() )
4931 int colID
= GetColAt( pos
);
4933 m_colAt
.RemoveAt( pos
, numCols
);
4935 //Shift the column IDs
4937 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
4939 if ( m_colAt
[colPos
] > colID
)
4940 m_colAt
[colPos
] -= numCols
;
4944 if ( !m_colWidths
.IsEmpty() )
4946 m_colWidths
.RemoveAt( pos
, numCols
);
4947 m_colRights
.RemoveAt( pos
, numCols
);
4951 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
4953 i
= GetColAt( colPos
);
4955 w
+= m_colWidths
[i
];
4962 m_currentCellCoords
= wxGridNoCellCoords
;
4966 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4967 m_currentCellCoords
.Set( 0, 0 );
4971 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4972 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4975 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4977 // ifdef'd out following patch from Paul Gammans
4979 // No need to touch row attributes, unless we
4980 // removed _all_ columns, in this case, we remove
4981 // all row attributes.
4982 // I hate to do this here, but the
4983 // needed data is not available inside UpdateAttrCols.
4984 if ( !GetNumberCols() )
4985 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4989 if ( !GetBatchCount() )
4992 m_colLabelWin
->Refresh();
4999 if (result
&& !GetBatchCount() )
5000 m_gridWin
->Refresh();
5005 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
5007 wxRegionIterator
iter( reg
);
5010 wxArrayInt rowlabels
;
5017 // TODO: remove this when we can...
5018 // There is a bug in wxMotif that gives garbage update
5019 // rectangles if you jump-scroll a long way by clicking the
5020 // scrollbar with middle button. This is a work-around
5022 #if defined(__WXMOTIF__)
5024 m_gridWin
->GetClientSize( &cw
, &ch
);
5025 if ( r
.GetTop() > ch
)
5027 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5030 // logical bounds of update region
5033 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
5034 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
5036 // find the row labels within these bounds
5039 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5041 if ( GetRowBottom(row
) < top
)
5044 if ( GetRowTop(row
) > bottom
)
5047 rowlabels
.Add( row
);
5056 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
5058 wxRegionIterator
iter( reg
);
5061 wxArrayInt colLabels
;
5068 // TODO: remove this when we can...
5069 // There is a bug in wxMotif that gives garbage update
5070 // rectangles if you jump-scroll a long way by clicking the
5071 // scrollbar with middle button. This is a work-around
5073 #if defined(__WXMOTIF__)
5075 m_gridWin
->GetClientSize( &cw
, &ch
);
5076 if ( r
.GetLeft() > cw
)
5078 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5081 // logical bounds of update region
5084 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
5085 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
5087 // find the cells within these bounds
5091 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5093 col
= GetColAt( colPos
);
5095 if ( GetColRight(col
) < left
)
5098 if ( GetColLeft(col
) > right
)
5101 colLabels
.Add( col
);
5110 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
5112 wxRegionIterator
iter( reg
);
5115 wxGridCellCoordsArray cellsExposed
;
5117 int left
, top
, right
, bottom
;
5122 // TODO: remove this when we can...
5123 // There is a bug in wxMotif that gives garbage update
5124 // rectangles if you jump-scroll a long way by clicking the
5125 // scrollbar with middle button. This is a work-around
5127 #if defined(__WXMOTIF__)
5129 m_gridWin
->GetClientSize( &cw
, &ch
);
5130 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5131 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5132 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5133 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5136 // logical bounds of update region
5138 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5139 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5141 // find the cells within these bounds
5144 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5146 if ( GetRowBottom(row
) <= top
)
5149 if ( GetRowTop(row
) > bottom
)
5153 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5155 col
= GetColAt( colPos
);
5157 if ( GetColRight(col
) <= left
)
5160 if ( GetColLeft(col
) > right
)
5163 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
5170 return cellsExposed
;
5174 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5177 wxPoint
pos( event
.GetPosition() );
5178 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5180 if ( event
.Dragging() )
5184 m_isDragging
= true;
5185 m_rowLabelWin
->CaptureMouse();
5188 if ( event
.LeftIsDown() )
5190 switch ( m_cursorMode
)
5192 case WXGRID_CURSOR_RESIZE_ROW
:
5194 int cw
, ch
, left
, dummy
;
5195 m_gridWin
->GetClientSize( &cw
, &ch
);
5196 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5198 wxClientDC
dc( m_gridWin
);
5201 GetRowTop(m_dragRowOrCol
) +
5202 GetRowMinimalHeight(m_dragRowOrCol
) );
5203 dc
.SetLogicalFunction(wxINVERT
);
5204 if ( m_dragLastPos
>= 0 )
5206 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5208 dc
.DrawLine( left
, y
, left
+cw
, y
);
5213 case WXGRID_CURSOR_SELECT_ROW
:
5215 if ( (row
= YToRow( y
)) >= 0 )
5219 m_selection
->SelectRow( row
,
5220 event
.ControlDown(),
5229 // default label to suppress warnings about "enumeration value
5230 // 'xxx' not handled in switch
5238 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5243 if (m_rowLabelWin
->HasCapture())
5244 m_rowLabelWin
->ReleaseMouse();
5245 m_isDragging
= false;
5248 // ------------ Entering or leaving the window
5250 if ( event
.Entering() || event
.Leaving() )
5252 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5255 // ------------ Left button pressed
5257 else if ( event
.LeftDown() )
5259 // don't send a label click event for a hit on the
5260 // edge of the row label - this is probably the user
5261 // wanting to resize the row
5263 if ( YToEdgeOfRow(y
) < 0 )
5267 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5269 if ( !event
.ShiftDown() && !event
.CmdDown() )
5273 if ( event
.ShiftDown() )
5275 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5278 GetNumberCols() - 1,
5279 event
.ControlDown(),
5286 m_selection
->SelectRow( row
,
5287 event
.ControlDown(),
5294 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5299 // starting to drag-resize a row
5300 if ( CanDragRowSize() )
5301 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5305 // ------------ Left double click
5307 else if (event
.LeftDClick() )
5309 row
= YToEdgeOfRow(y
);
5314 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5316 // no default action at the moment
5321 // adjust row height depending on label text
5322 AutoSizeRowLabelSize( row
);
5324 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5329 // ------------ Left button released
5331 else if ( event
.LeftUp() )
5333 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5335 DoEndDragResizeRow();
5337 // Note: we are ending the event *after* doing
5338 // default processing in this case
5340 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5343 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5347 // ------------ Right button down
5349 else if ( event
.RightDown() )
5353 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5355 // no default action at the moment
5359 // ------------ Right double click
5361 else if ( event
.RightDClick() )
5365 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5367 // no default action at the moment
5371 // ------------ No buttons down and mouse moving
5373 else if ( event
.Moving() )
5375 m_dragRowOrCol
= YToEdgeOfRow( y
);
5376 if ( m_dragRowOrCol
>= 0 )
5378 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5380 // don't capture the mouse yet
5381 if ( CanDragRowSize() )
5382 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5385 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5387 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5392 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5395 wxPoint
pos( event
.GetPosition() );
5396 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5398 if ( event
.Dragging() )
5402 m_isDragging
= true;
5403 m_colLabelWin
->CaptureMouse();
5405 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
)
5406 m_dragRowOrCol
= XToCol( x
);
5409 if ( event
.LeftIsDown() )
5411 switch ( m_cursorMode
)
5413 case WXGRID_CURSOR_RESIZE_COL
:
5415 int cw
, ch
, dummy
, top
;
5416 m_gridWin
->GetClientSize( &cw
, &ch
);
5417 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5419 wxClientDC
dc( m_gridWin
);
5422 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5423 GetColMinimalWidth(m_dragRowOrCol
));
5424 dc
.SetLogicalFunction(wxINVERT
);
5425 if ( m_dragLastPos
>= 0 )
5427 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5429 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5434 case WXGRID_CURSOR_SELECT_COL
:
5436 if ( (col
= XToCol( x
)) >= 0 )
5440 m_selection
->SelectCol( col
,
5441 event
.ControlDown(),
5450 case WXGRID_CURSOR_MOVE_COL
:
5453 m_moveToCol
= GetColAt( 0 );
5455 m_moveToCol
= XToCol( x
);
5459 if ( m_moveToCol
< 0 )
5460 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5462 markerX
= GetColLeft( m_moveToCol
);
5464 if ( markerX
!= m_dragLastPos
)
5466 wxClientDC
dc( m_colLabelWin
);
5469 m_colLabelWin
->GetClientSize( &cw
, &ch
);
5473 //Clean up the last indicator
5474 if ( m_dragLastPos
>= 0 )
5476 wxPen
pen( m_colLabelWin
->GetBackgroundColour(), 2 );
5478 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
5479 dc
.SetPen(wxNullPen
);
5481 if ( XToCol( m_dragLastPos
) != -1 )
5482 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
5485 //Moving to the same place? Don't draw a marker
5486 if ( (m_moveToCol
== m_dragRowOrCol
)
5487 || (GetColPos( m_moveToCol
) == GetColPos( m_dragRowOrCol
) + 1)
5488 || (m_moveToCol
< 0 && m_dragRowOrCol
== GetColAt( m_numCols
- 1 )))
5495 wxPen
pen( *wxBLUE
, 2 );
5498 dc
.DrawLine( markerX
, 0, markerX
, ch
);
5500 dc
.SetPen(wxNullPen
);
5502 m_dragLastPos
= markerX
- 1;
5507 // default label to suppress warnings about "enumeration value
5508 // 'xxx' not handled in switch
5516 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5521 if (m_colLabelWin
->HasCapture())
5522 m_colLabelWin
->ReleaseMouse();
5523 m_isDragging
= false;
5526 // ------------ Entering or leaving the window
5528 if ( event
.Entering() || event
.Leaving() )
5530 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5533 // ------------ Left button pressed
5535 else if ( event
.LeftDown() )
5537 // don't send a label click event for a hit on the
5538 // edge of the col label - this is probably the user
5539 // wanting to resize the col
5541 if ( XToEdgeOfCol(x
) < 0 )
5545 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5547 if ( m_canDragColMove
)
5549 //Show button as pressed
5550 wxClientDC
dc( m_colLabelWin
);
5551 int colLeft
= GetColLeft( col
);
5552 int colRight
= GetColRight( col
) - 1;
5553 dc
.SetPen( wxPen( m_colLabelWin
->GetBackgroundColour(), 1 ) );
5554 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
5555 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
5557 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, m_colLabelWin
);
5561 if ( !event
.ShiftDown() && !event
.CmdDown() )
5565 if ( event
.ShiftDown() )
5567 m_selection
->SelectBlock( 0,
5568 m_currentCellCoords
.GetCol(),
5569 GetNumberRows() - 1, col
,
5570 event
.ControlDown(),
5577 m_selection
->SelectCol( col
,
5578 event
.ControlDown(),
5585 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5591 // starting to drag-resize a col
5593 if ( CanDragColSize() )
5594 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5598 // ------------ Left double click
5600 if ( event
.LeftDClick() )
5602 col
= XToEdgeOfCol(x
);
5607 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5609 // no default action at the moment
5614 // adjust column width depending on label text
5615 AutoSizeColLabelSize( col
);
5617 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5622 // ------------ Left button released
5624 else if ( event
.LeftUp() )
5626 switch ( m_cursorMode
)
5628 case WXGRID_CURSOR_RESIZE_COL
:
5629 DoEndDragResizeCol();
5631 // Note: we are ending the event *after* doing
5632 // default processing in this case
5634 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5637 case WXGRID_CURSOR_MOVE_COL
:
5640 SendEvent( wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
, event
);
5643 case WXGRID_CURSOR_SELECT_COL
:
5644 case WXGRID_CURSOR_SELECT_CELL
:
5645 case WXGRID_CURSOR_RESIZE_ROW
:
5646 case WXGRID_CURSOR_SELECT_ROW
:
5647 // nothing to do (?)
5651 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5655 // ------------ Right button down
5657 else if ( event
.RightDown() )
5661 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5663 // no default action at the moment
5667 // ------------ Right double click
5669 else if ( event
.RightDClick() )
5673 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5675 // no default action at the moment
5679 // ------------ No buttons down and mouse moving
5681 else if ( event
.Moving() )
5683 m_dragRowOrCol
= XToEdgeOfCol( x
);
5684 if ( m_dragRowOrCol
>= 0 )
5686 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5688 // don't capture the cursor yet
5689 if ( CanDragColSize() )
5690 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5693 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5695 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5700 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5702 if ( event
.LeftDown() )
5704 // indicate corner label by having both row and
5707 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5712 else if ( event
.LeftDClick() )
5714 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5716 else if ( event
.RightDown() )
5718 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5720 // no default action at the moment
5723 else if ( event
.RightDClick() )
5725 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5727 // no default action at the moment
5732 void wxGrid::ChangeCursorMode(CursorMode mode
,
5737 static const wxChar
*cursorModes
[] =
5747 wxLogTrace(_T("grid"),
5748 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5749 win
== m_colLabelWin
? _T("colLabelWin")
5750 : win
? _T("rowLabelWin")
5752 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5755 if ( mode
== m_cursorMode
&&
5756 win
== m_winCapture
&&
5757 captureMouse
== (m_winCapture
!= NULL
))
5762 // by default use the grid itself
5768 if (m_winCapture
->HasCapture())
5769 m_winCapture
->ReleaseMouse();
5770 m_winCapture
= (wxWindow
*)NULL
;
5773 m_cursorMode
= mode
;
5775 switch ( m_cursorMode
)
5777 case WXGRID_CURSOR_RESIZE_ROW
:
5778 win
->SetCursor( m_rowResizeCursor
);
5781 case WXGRID_CURSOR_RESIZE_COL
:
5782 win
->SetCursor( m_colResizeCursor
);
5785 case WXGRID_CURSOR_MOVE_COL
:
5786 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
5790 win
->SetCursor( *wxSTANDARD_CURSOR
);
5794 // we need to capture mouse when resizing
5795 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5796 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5798 if ( captureMouse
&& resize
)
5800 win
->CaptureMouse();
5805 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5808 wxPoint
pos( event
.GetPosition() );
5809 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5811 wxGridCellCoords coords
;
5812 XYToCell( x
, y
, coords
);
5814 int cell_rows
, cell_cols
;
5815 bool isFirstDrag
= !m_isDragging
;
5816 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5817 if ((cell_rows
< 0) || (cell_cols
< 0))
5819 coords
.SetRow(coords
.GetRow() + cell_rows
);
5820 coords
.SetCol(coords
.GetCol() + cell_cols
);
5823 if ( event
.Dragging() )
5825 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5827 // Don't start doing anything until the mouse has been dragged at
5828 // least 3 pixels in any direction...
5831 if (m_startDragPos
== wxDefaultPosition
)
5833 m_startDragPos
= pos
;
5836 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5840 m_isDragging
= true;
5841 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5843 // Hide the edit control, so it
5844 // won't interfere with drag-shrinking.
5845 if ( IsCellEditControlShown() )
5847 HideCellEditControl();
5848 SaveEditControlValue();
5851 // Have we captured the mouse yet?
5854 m_winCapture
= m_gridWin
;
5855 m_winCapture
->CaptureMouse();
5858 if ( coords
!= wxGridNoCellCoords
)
5860 if ( event
.CmdDown() )
5862 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5863 m_selectingKeyboard
= coords
;
5864 HighlightBlock( m_selectingKeyboard
, coords
);
5866 else if ( CanDragCell() )
5870 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5871 m_selectingKeyboard
= coords
;
5873 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5881 if ( !IsSelection() )
5883 HighlightBlock( coords
, coords
);
5887 HighlightBlock( m_currentCellCoords
, coords
);
5891 if (! IsVisible(coords
))
5893 MakeCellVisible(coords
);
5894 // TODO: need to introduce a delay or something here. The
5895 // scrolling is way to fast, at least on MSW - also on GTK.
5899 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5901 int cw
, ch
, left
, dummy
;
5902 m_gridWin
->GetClientSize( &cw
, &ch
);
5903 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5905 wxClientDC
dc( m_gridWin
);
5907 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5908 GetRowMinimalHeight(m_dragRowOrCol
) );
5909 dc
.SetLogicalFunction(wxINVERT
);
5910 if ( m_dragLastPos
>= 0 )
5912 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5914 dc
.DrawLine( left
, y
, left
+cw
, y
);
5917 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5919 int cw
, ch
, dummy
, top
;
5920 m_gridWin
->GetClientSize( &cw
, &ch
);
5921 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5923 wxClientDC
dc( m_gridWin
);
5925 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5926 GetColMinimalWidth(m_dragRowOrCol
) );
5927 dc
.SetLogicalFunction(wxINVERT
);
5928 if ( m_dragLastPos
>= 0 )
5930 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5932 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5939 m_isDragging
= false;
5940 m_startDragPos
= wxDefaultPosition
;
5942 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5943 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5946 if ( event
.Entering() || event
.Leaving() )
5948 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5949 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5954 // ------------ Left button pressed
5956 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5958 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5963 if ( !event
.CmdDown() )
5965 if ( event
.ShiftDown() )
5969 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5970 m_currentCellCoords
.GetCol(),
5973 event
.ControlDown(),
5979 else if ( XToEdgeOfCol(x
) < 0 &&
5980 YToEdgeOfRow(y
) < 0 )
5982 DisableCellEditControl();
5983 MakeCellVisible( coords
);
5985 if ( event
.CmdDown() )
5989 m_selection
->ToggleCellSelection( coords
.GetRow(),
5991 event
.ControlDown(),
5996 m_selectingTopLeft
= wxGridNoCellCoords
;
5997 m_selectingBottomRight
= wxGridNoCellCoords
;
5998 m_selectingKeyboard
= coords
;
6002 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
6003 SetCurrentCell( coords
);
6006 if ( m_selection
->GetSelectionMode() !=
6007 wxGrid::wxGridSelectCells
)
6009 HighlightBlock( coords
, coords
);
6017 // ------------ Left double click
6019 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
6021 DisableCellEditControl();
6023 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
6025 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
6030 // we want double click to select a cell and start editing
6031 // (i.e. to behave in same way as sequence of two slow clicks):
6032 m_waitForSlowClick
= true;
6037 // ------------ Left button released
6039 else if ( event
.LeftUp() )
6041 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6045 if (m_winCapture
->HasCapture())
6046 m_winCapture
->ReleaseMouse();
6047 m_winCapture
= NULL
;
6050 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
6053 EnableCellEditControl();
6055 wxGridCellAttr
*attr
= GetCellAttr(coords
);
6056 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
6057 editor
->StartingClick();
6061 m_waitForSlowClick
= false;
6063 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6064 m_selectingBottomRight
!= wxGridNoCellCoords
)
6068 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6069 m_selectingTopLeft
.GetCol(),
6070 m_selectingBottomRight
.GetRow(),
6071 m_selectingBottomRight
.GetCol(),
6072 event
.ControlDown(),
6078 m_selectingTopLeft
= wxGridNoCellCoords
;
6079 m_selectingBottomRight
= wxGridNoCellCoords
;
6081 // Show the edit control, if it has been hidden for
6083 ShowCellEditControl();
6086 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6088 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6089 DoEndDragResizeRow();
6091 // Note: we are ending the event *after* doing
6092 // default processing in this case
6094 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
6096 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6098 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6099 DoEndDragResizeCol();
6101 // Note: we are ending the event *after* doing
6102 // default processing in this case
6104 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
6110 // ------------ Right button down
6112 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
6114 DisableCellEditControl();
6115 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
6120 // no default action at the moment
6124 // ------------ Right double click
6126 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
6128 DisableCellEditControl();
6129 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
6134 // no default action at the moment
6138 // ------------ Moving and no button action
6140 else if ( event
.Moving() && !event
.IsButton() )
6142 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
6144 // out of grid cell area
6145 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6149 int dragRow
= YToEdgeOfRow( y
);
6150 int dragCol
= XToEdgeOfCol( x
);
6152 // Dragging on the corner of a cell to resize in both
6153 // directions is not implemented yet...
6155 if ( dragRow
>= 0 && dragCol
>= 0 )
6157 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6163 m_dragRowOrCol
= dragRow
;
6165 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6167 if ( CanDragRowSize() && CanDragGridSize() )
6168 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
6171 else if ( dragCol
>= 0 )
6173 m_dragRowOrCol
= dragCol
;
6175 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6177 if ( CanDragColSize() && CanDragGridSize() )
6178 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
6181 else // Neither on a row or col edge
6183 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6185 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6191 void wxGrid::DoEndDragResizeRow()
6193 if ( m_dragLastPos
>= 0 )
6195 // erase the last line and resize the row
6197 int cw
, ch
, left
, dummy
;
6198 m_gridWin
->GetClientSize( &cw
, &ch
);
6199 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6201 wxClientDC
dc( m_gridWin
);
6203 dc
.SetLogicalFunction( wxINVERT
);
6204 dc
.DrawLine( left
, m_dragLastPos
, left
+ cw
, m_dragLastPos
);
6205 HideCellEditControl();
6206 SaveEditControlValue();
6208 int rowTop
= GetRowTop(m_dragRowOrCol
);
6209 SetRowSize( m_dragRowOrCol
,
6210 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
6212 if ( !GetBatchCount() )
6214 // Only needed to get the correct rect.y:
6215 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
6217 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6218 rect
.width
= m_rowLabelWidth
;
6219 rect
.height
= ch
- rect
.y
;
6220 m_rowLabelWin
->Refresh( true, &rect
);
6223 // if there is a multicell block, paint all of it
6226 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
6227 int leftCol
= XToCol(left
);
6228 int rightCol
= internalXToCol(left
+ cw
);
6231 for (i
=leftCol
; i
<rightCol
; i
++)
6233 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
6234 if (cell_rows
< subtract_rows
)
6235 subtract_rows
= cell_rows
;
6237 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
6238 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6239 rect
.height
= ch
- rect
.y
;
6242 m_gridWin
->Refresh( false, &rect
);
6245 ShowCellEditControl();
6250 void wxGrid::DoEndDragResizeCol()
6252 if ( m_dragLastPos
>= 0 )
6254 // erase the last line and resize the col
6256 int cw
, ch
, dummy
, top
;
6257 m_gridWin
->GetClientSize( &cw
, &ch
);
6258 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6260 wxClientDC
dc( m_gridWin
);
6262 dc
.SetLogicalFunction( wxINVERT
);
6263 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6264 HideCellEditControl();
6265 SaveEditControlValue();
6267 int colLeft
= GetColLeft(m_dragRowOrCol
);
6268 SetColSize( m_dragRowOrCol
,
6269 wxMax( m_dragLastPos
- colLeft
,
6270 GetColMinimalWidth(m_dragRowOrCol
) ) );
6272 if ( !GetBatchCount() )
6274 // Only needed to get the correct rect.x:
6275 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
6277 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6278 rect
.width
= cw
- rect
.x
;
6279 rect
.height
= m_colLabelHeight
;
6280 m_colLabelWin
->Refresh( true, &rect
);
6283 // if there is a multicell block, paint all of it
6286 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
6287 int topRow
= YToRow(top
);
6288 int bottomRow
= internalYToRow(top
+ cw
);
6291 for (i
=topRow
; i
<bottomRow
; i
++)
6293 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
6294 if (cell_cols
< subtract_cols
)
6295 subtract_cols
= cell_cols
;
6298 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
6299 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6300 rect
.width
= cw
- rect
.x
;
6304 m_gridWin
->Refresh( false, &rect
);
6307 ShowCellEditControl();
6311 void wxGrid::DoEndDragMoveCol()
6313 //The user clicked on the column but didn't actually drag
6314 if ( m_dragLastPos
< 0 )
6316 m_colLabelWin
->Refresh(); //Do this to "unpress" the column
6321 if ( m_moveToCol
== -1 )
6322 newPos
= m_numCols
- 1;
6325 newPos
= GetColPos( m_moveToCol
);
6326 if ( newPos
> GetColPos( m_dragRowOrCol
) )
6330 SetColPos( m_dragRowOrCol
, newPos
);
6333 void wxGrid::SetColPos( int colID
, int newPos
)
6335 if ( m_colAt
.IsEmpty() )
6337 m_colAt
.Alloc( m_numCols
);
6340 for ( i
= 0; i
< m_numCols
; i
++ )
6346 int oldPos
= GetColPos( colID
);
6348 //Reshuffle the m_colAt array
6349 if ( newPos
> oldPos
)
6352 for ( i
= oldPos
; i
< newPos
; i
++ )
6354 m_colAt
[i
] = m_colAt
[i
+1];
6360 for ( i
= oldPos
; i
> newPos
; i
-- )
6362 m_colAt
[i
] = m_colAt
[i
-1];
6366 m_colAt
[newPos
] = colID
;
6368 //Recalculate the column rights
6369 if ( !m_colWidths
.IsEmpty() )
6373 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6375 int colID
= GetColAt( colPos
);
6377 colRight
+= m_colWidths
[colID
];
6378 m_colRights
[colID
] = colRight
;
6382 m_colLabelWin
->Refresh();
6383 m_gridWin
->Refresh();
6388 void wxGrid::EnableDragColMove( bool enable
)
6390 if ( m_canDragColMove
== enable
)
6393 m_canDragColMove
= enable
;
6395 if ( !m_canDragColMove
)
6399 //Recalculate the column rights
6400 if ( !m_colWidths
.IsEmpty() )
6404 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6406 colRight
+= m_colWidths
[colPos
];
6407 m_colRights
[colPos
] = colRight
;
6411 m_colLabelWin
->Refresh();
6412 m_gridWin
->Refresh();
6418 // ------ interaction with data model
6420 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6422 switch ( msg
.GetId() )
6424 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6425 return GetModelValues();
6427 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6428 return SetModelValues();
6430 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6431 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6432 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6433 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6434 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6435 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6436 return Redimension( msg
);
6443 // The behaviour of this function depends on the grid table class
6444 // Clear() function. For the default wxGridStringTable class the
6445 // behavious is to replace all cell contents with wxEmptyString but
6446 // not to change the number of rows or cols.
6448 void wxGrid::ClearGrid()
6452 if (IsCellEditControlEnabled())
6453 DisableCellEditControl();
6456 if (!GetBatchCount())
6457 m_gridWin
->Refresh();
6461 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6463 // TODO: something with updateLabels flag
6467 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6473 if (IsCellEditControlEnabled())
6474 DisableCellEditControl();
6476 bool done
= m_table
->InsertRows( pos
, numRows
);
6479 // the table will have sent the results of the insert row
6480 // operation to this view object as a grid table message
6486 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6488 // TODO: something with updateLabels flag
6492 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6498 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6501 // the table will have sent the results of the append row
6502 // operation to this view object as a grid table message
6508 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6510 // TODO: something with updateLabels flag
6514 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6520 if (IsCellEditControlEnabled())
6521 DisableCellEditControl();
6523 bool done
= m_table
->DeleteRows( pos
, numRows
);
6525 // the table will have sent the results of the delete row
6526 // operation to this view object as a grid table message
6532 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6534 // TODO: something with updateLabels flag
6538 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6544 if (IsCellEditControlEnabled())
6545 DisableCellEditControl();
6547 bool done
= m_table
->InsertCols( pos
, numCols
);
6549 // the table will have sent the results of the insert col
6550 // operation to this view object as a grid table message
6556 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6558 // TODO: something with updateLabels flag
6562 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6568 bool done
= m_table
->AppendCols( numCols
);
6570 // the table will have sent the results of the append col
6571 // operation to this view object as a grid table message
6577 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6579 // TODO: something with updateLabels flag
6583 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6589 if (IsCellEditControlEnabled())
6590 DisableCellEditControl();
6592 bool done
= m_table
->DeleteCols( pos
, numCols
);
6594 // the table will have sent the results of the delete col
6595 // operation to this view object as a grid table message
6602 // ----- event handlers
6605 // Generate a grid event based on a mouse event and
6606 // return the result of ProcessEvent()
6608 int wxGrid::SendEvent( const wxEventType type
,
6610 wxMouseEvent
& mouseEv
)
6612 bool claimed
, vetoed
;
6614 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6616 int rowOrCol
= (row
== -1 ? col
: row
);
6618 wxGridSizeEvent
gridEvt( GetId(),
6622 mouseEv
.GetX() + GetRowLabelSize(),
6623 mouseEv
.GetY() + GetColLabelSize(),
6624 mouseEv
.ControlDown(),
6625 mouseEv
.ShiftDown(),
6627 mouseEv
.MetaDown() );
6629 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6630 vetoed
= !gridEvt
.IsAllowed();
6632 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6634 // Right now, it should _never_ end up here!
6635 wxGridRangeSelectEvent
gridEvt( GetId(),
6639 m_selectingBottomRight
,
6641 mouseEv
.ControlDown(),
6642 mouseEv
.ShiftDown(),
6644 mouseEv
.MetaDown() );
6646 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6647 vetoed
= !gridEvt
.IsAllowed();
6649 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
6650 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
6651 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
6652 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
6654 wxPoint pos
= mouseEv
.GetPosition();
6656 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
6657 pos
.y
+= GetColLabelSize();
6658 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
6659 pos
.x
+= GetRowLabelSize();
6661 wxGridEvent
gridEvt( GetId(),
6668 mouseEv
.ControlDown(),
6669 mouseEv
.ShiftDown(),
6671 mouseEv
.MetaDown() );
6672 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6673 vetoed
= !gridEvt
.IsAllowed();
6677 wxGridEvent
gridEvt( GetId(),
6681 mouseEv
.GetX() + GetRowLabelSize(),
6682 mouseEv
.GetY() + GetColLabelSize(),
6684 mouseEv
.ControlDown(),
6685 mouseEv
.ShiftDown(),
6687 mouseEv
.MetaDown() );
6688 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6689 vetoed
= !gridEvt
.IsAllowed();
6692 // A Veto'd event may not be `claimed' so test this first
6696 return claimed
? 1 : 0;
6699 // Generate a grid event of specified type and return the result
6700 // of ProcessEvent().
6702 int wxGrid::SendEvent( const wxEventType type
,
6705 bool claimed
, vetoed
;
6707 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6709 int rowOrCol
= (row
== -1 ? col
: row
);
6711 wxGridSizeEvent
gridEvt( GetId(), type
, this, rowOrCol
);
6713 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6714 vetoed
= !gridEvt
.IsAllowed();
6718 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
6720 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6721 vetoed
= !gridEvt
.IsAllowed();
6724 // A Veto'd event may not be `claimed' so test this first
6728 return claimed
? 1 : 0;
6731 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6733 // needed to prevent zillions of paint events on MSW
6737 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6739 // Don't do anything if between Begin/EndBatch...
6740 // EndBatch() will do all this on the last nested one anyway.
6741 if (! GetBatchCount())
6743 // Refresh to get correct scrolled position:
6744 wxScrolledWindow::Refresh(eraseb
, rect
);
6748 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6749 int width_label
, width_cell
, height_label
, height_cell
;
6752 // Copy rectangle can get scroll offsets..
6753 rect_x
= rect
->GetX();
6754 rect_y
= rect
->GetY();
6755 rectWidth
= rect
->GetWidth();
6756 rectHeight
= rect
->GetHeight();
6758 width_label
= m_rowLabelWidth
- rect_x
;
6759 if (width_label
> rectWidth
)
6760 width_label
= rectWidth
;
6762 height_label
= m_colLabelHeight
- rect_y
;
6763 if (height_label
> rectHeight
)
6764 height_label
= rectHeight
;
6766 if (rect_x
> m_rowLabelWidth
)
6768 x
= rect_x
- m_rowLabelWidth
;
6769 width_cell
= rectWidth
;
6774 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6777 if (rect_y
> m_colLabelHeight
)
6779 y
= rect_y
- m_colLabelHeight
;
6780 height_cell
= rectHeight
;
6785 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6788 // Paint corner label part intersecting rect.
6789 if ( width_label
> 0 && height_label
> 0 )
6791 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6792 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6795 // Paint col labels part intersecting rect.
6796 if ( width_cell
> 0 && height_label
> 0 )
6798 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6799 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6802 // Paint row labels part intersecting rect.
6803 if ( width_label
> 0 && height_cell
> 0 )
6805 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6806 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6809 // Paint cell area part intersecting rect.
6810 if ( width_cell
> 0 && height_cell
> 0 )
6812 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6813 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6818 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6819 m_colLabelWin
->Refresh(eraseb
, NULL
);
6820 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6821 m_gridWin
->Refresh(eraseb
, NULL
);
6826 void wxGrid::OnSize( wxSizeEvent
& event
)
6828 // position the child windows
6831 // don't call CalcDimensions() from here, the base class handles the size
6836 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6838 if ( m_inOnKeyDown
)
6840 // shouldn't be here - we are going round in circles...
6842 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6845 m_inOnKeyDown
= true;
6847 // propagate the event up and see if it gets processed
6848 wxWindow
*parent
= GetParent();
6849 wxKeyEvent
keyEvt( event
);
6850 keyEvt
.SetEventObject( parent
);
6852 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6854 if (GetLayoutDirection() == wxLayout_RightToLeft
)
6856 if (event
.GetKeyCode() == WXK_RIGHT
)
6857 event
.m_keyCode
= WXK_LEFT
;
6858 else if (event
.GetKeyCode() == WXK_LEFT
)
6859 event
.m_keyCode
= WXK_RIGHT
;
6862 // try local handlers
6863 switch ( event
.GetKeyCode() )
6866 if ( event
.ControlDown() )
6867 MoveCursorUpBlock( event
.ShiftDown() );
6869 MoveCursorUp( event
.ShiftDown() );
6873 if ( event
.ControlDown() )
6874 MoveCursorDownBlock( event
.ShiftDown() );
6876 MoveCursorDown( event
.ShiftDown() );
6880 if ( event
.ControlDown() )
6881 MoveCursorLeftBlock( event
.ShiftDown() );
6883 MoveCursorLeft( event
.ShiftDown() );
6887 if ( event
.ControlDown() )
6888 MoveCursorRightBlock( event
.ShiftDown() );
6890 MoveCursorRight( event
.ShiftDown() );
6894 case WXK_NUMPAD_ENTER
:
6895 if ( event
.ControlDown() )
6897 event
.Skip(); // to let the edit control have the return
6901 if ( GetGridCursorRow() < GetNumberRows()-1 )
6903 MoveCursorDown( event
.ShiftDown() );
6907 // at the bottom of a column
6908 DisableCellEditControl();
6918 if (event
.ShiftDown())
6920 if ( GetGridCursorCol() > 0 )
6922 MoveCursorLeft( false );
6927 DisableCellEditControl();
6932 if ( GetGridCursorCol() < GetNumberCols() - 1 )
6934 MoveCursorRight( false );
6939 DisableCellEditControl();
6945 if ( event
.ControlDown() )
6947 MakeCellVisible( 0, 0 );
6948 SetCurrentCell( 0, 0 );
6957 if ( event
.ControlDown() )
6959 MakeCellVisible( m_numRows
- 1, m_numCols
- 1 );
6960 SetCurrentCell( m_numRows
- 1, m_numCols
- 1 );
6977 if ( event
.ControlDown() )
6981 m_selection
->ToggleCellSelection(
6982 m_currentCellCoords
.GetRow(),
6983 m_currentCellCoords
.GetCol(),
6984 event
.ControlDown(),
6992 if ( !IsEditable() )
6993 MoveCursorRight( false );
7004 m_inOnKeyDown
= false;
7007 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
7009 // try local handlers
7011 if ( event
.GetKeyCode() == WXK_SHIFT
)
7013 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
7014 m_selectingBottomRight
!= wxGridNoCellCoords
)
7018 m_selection
->SelectBlock(
7019 m_selectingTopLeft
.GetRow(),
7020 m_selectingTopLeft
.GetCol(),
7021 m_selectingBottomRight
.GetRow(),
7022 m_selectingBottomRight
.GetCol(),
7023 event
.ControlDown(),
7030 m_selectingTopLeft
= wxGridNoCellCoords
;
7031 m_selectingBottomRight
= wxGridNoCellCoords
;
7032 m_selectingKeyboard
= wxGridNoCellCoords
;
7036 void wxGrid::OnChar( wxKeyEvent
& event
)
7038 // is it possible to edit the current cell at all?
7039 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7041 // yes, now check whether the cells editor accepts the key
7042 int row
= m_currentCellCoords
.GetRow();
7043 int col
= m_currentCellCoords
.GetCol();
7044 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7045 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7047 // <F2> is special and will always start editing, for
7048 // other keys - ask the editor itself
7049 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
7050 || editor
->IsAcceptedKey(event
) )
7052 // ensure cell is visble
7053 MakeCellVisible(row
, col
);
7054 EnableCellEditControl();
7056 // a problem can arise if the cell is not completely
7057 // visible (even after calling MakeCellVisible the
7058 // control is not created and calling StartingKey will
7060 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
7061 editor
->StartingKey(event
);
7077 void wxGrid::OnEraseBackground(wxEraseEvent
&)
7081 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
7083 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
7085 // the event has been intercepted - do nothing
7089 wxClientDC
dc( m_gridWin
);
7092 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
7094 DisableCellEditControl();
7096 if ( IsVisible( m_currentCellCoords
, false ) )
7099 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
7100 if ( !m_gridLinesEnabled
)
7108 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
7110 // Otherwise refresh redraws the highlight!
7111 m_currentCellCoords
= coords
;
7113 DrawGridCellArea( dc
, cells
);
7114 DrawAllGridLines( dc
, r
);
7118 m_currentCellCoords
= coords
;
7120 wxGridCellAttr
*attr
= GetCellAttr( coords
);
7121 DrawCellHighlight( dc
, attr
);
7125 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7128 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7132 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
7135 rightCol
= GetNumberCols() - 1;
7137 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
7140 bottomRow
= GetNumberRows() - 1;
7144 if ( topRow
> bottomRow
)
7151 if ( leftCol
> rightCol
)
7158 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7159 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7161 // First the case that we selected a completely new area
7162 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
7163 m_selectingBottomRight
== wxGridNoCellCoords
)
7166 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
7167 wxGridCellCoords ( bottomRow
, rightCol
) );
7168 m_gridWin
->Refresh( false, &rect
);
7171 // Now handle changing an existing selection area.
7172 else if ( m_selectingTopLeft
!= updateTopLeft
||
7173 m_selectingBottomRight
!= updateBottomRight
)
7175 // Compute two optimal update rectangles:
7176 // Either one rectangle is a real subset of the
7177 // other, or they are (almost) disjoint!
7179 bool need_refresh
[4];
7183 need_refresh
[3] = false;
7186 // Store intermediate values
7187 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
7188 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
7189 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
7190 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
7192 // Determine the outer/inner coordinates.
7193 if (oldLeft
> leftCol
)
7199 if (oldTop
> topRow
)
7205 if (oldRight
< rightCol
)
7208 oldRight
= rightCol
;
7211 if (oldBottom
< bottomRow
)
7214 oldBottom
= bottomRow
;
7218 // Now, either the stuff marked old is the outer
7219 // rectangle or we don't have a situation where one
7220 // is contained in the other.
7222 if ( oldLeft
< leftCol
)
7224 // Refresh the newly selected or deselected
7225 // area to the left of the old or new selection.
7226 need_refresh
[0] = true;
7227 rect
[0] = BlockToDeviceRect(
7228 wxGridCellCoords( oldTop
, oldLeft
),
7229 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
7232 if ( oldTop
< topRow
)
7234 // Refresh the newly selected or deselected
7235 // area above the old or new selection.
7236 need_refresh
[1] = true;
7237 rect
[1] = BlockToDeviceRect(
7238 wxGridCellCoords( oldTop
, leftCol
),
7239 wxGridCellCoords( topRow
- 1, rightCol
) );
7242 if ( oldRight
> rightCol
)
7244 // Refresh the newly selected or deselected
7245 // area to the right of the old or new selection.
7246 need_refresh
[2] = true;
7247 rect
[2] = BlockToDeviceRect(
7248 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
7249 wxGridCellCoords( oldBottom
, oldRight
) );
7252 if ( oldBottom
> bottomRow
)
7254 // Refresh the newly selected or deselected
7255 // area below the old or new selection.
7256 need_refresh
[3] = true;
7257 rect
[3] = BlockToDeviceRect(
7258 wxGridCellCoords( bottomRow
+ 1, leftCol
),
7259 wxGridCellCoords( oldBottom
, rightCol
) );
7262 // various Refresh() calls
7263 for (i
= 0; i
< 4; i
++ )
7264 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7265 m_gridWin
->Refresh( false, &(rect
[i
]) );
7269 m_selectingTopLeft
= updateTopLeft
;
7270 m_selectingBottomRight
= updateBottomRight
;
7274 // ------ functions to get/send data (see also public functions)
7277 bool wxGrid::GetModelValues()
7279 // Hide the editor, so it won't hide a changed value.
7280 HideCellEditControl();
7284 // all we need to do is repaint the grid
7286 m_gridWin
->Refresh();
7293 bool wxGrid::SetModelValues()
7297 // Disable the editor, so it won't hide a changed value.
7298 // Do we also want to save the current value of the editor first?
7300 DisableCellEditControl();
7304 for ( row
= 0; row
< m_numRows
; row
++ )
7306 for ( col
= 0; col
< m_numCols
; col
++ )
7308 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
7318 // Note - this function only draws cells that are in the list of
7319 // exposed cells (usually set from the update region by
7320 // CalcExposedCells)
7322 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7324 if ( !m_numRows
|| !m_numCols
)
7327 int i
, numCells
= cells
.GetCount();
7328 int row
, col
, cell_rows
, cell_cols
;
7329 wxGridCellCoordsArray redrawCells
;
7331 for ( i
= numCells
- 1; i
>= 0; i
-- )
7333 row
= cells
[i
].GetRow();
7334 col
= cells
[i
].GetCol();
7335 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7337 // If this cell is part of a multicell block, find owner for repaint
7338 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7340 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
7341 bool marked
= false;
7342 for ( int j
= 0; j
< numCells
; j
++ )
7344 if ( cell
== cells
[j
] )
7353 int count
= redrawCells
.GetCount();
7354 for (int j
= 0; j
< count
; j
++)
7356 if ( cell
== redrawCells
[j
] )
7364 redrawCells
.Add( cell
);
7367 // don't bother drawing this cell
7371 // If this cell is empty, find cell to left that might want to overflow
7372 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7374 for ( int l
= 0; l
< cell_rows
; l
++ )
7376 // find a cell in this row to leave already marked for repaint
7378 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7379 if ((redrawCells
[k
].GetCol() < left
) &&
7380 (redrawCells
[k
].GetRow() == row
))
7382 left
= redrawCells
[k
].GetCol();
7386 left
= 0; // oh well
7388 for (int j
= col
- 1; j
>= left
; j
--)
7390 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7392 if (GetCellOverflow(row
+ l
, j
))
7394 wxGridCellCoords
cell(row
+ l
, j
);
7395 bool marked
= false;
7397 for (int k
= 0; k
< numCells
; k
++)
7399 if ( cell
== cells
[k
] )
7408 int count
= redrawCells
.GetCount();
7409 for (int k
= 0; k
< count
; k
++)
7411 if ( cell
== redrawCells
[k
] )
7418 redrawCells
.Add( cell
);
7427 DrawCell( dc
, cells
[i
] );
7430 numCells
= redrawCells
.GetCount();
7432 for ( i
= numCells
- 1; i
>= 0; i
-- )
7434 DrawCell( dc
, redrawCells
[i
] );
7438 void wxGrid::DrawGridSpace( wxDC
& dc
)
7441 m_gridWin
->GetClientSize( &cw
, &ch
);
7444 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7446 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
7447 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7449 if ( right
> rightCol
|| bottom
> bottomRow
)
7452 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7454 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
7455 dc
.SetPen( *wxTRANSPARENT_PEN
);
7457 if ( right
> rightCol
)
7459 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7462 if ( bottom
> bottomRow
)
7464 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7469 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7471 int row
= coords
.GetRow();
7472 int col
= coords
.GetCol();
7474 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7477 // we draw the cell border ourselves
7478 #if !WXGRID_DRAW_LINES
7479 if ( m_gridLinesEnabled
)
7480 DrawCellBorder( dc
, coords
);
7483 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7485 bool isCurrent
= coords
== m_currentCellCoords
;
7487 wxRect rect
= CellToRect( row
, col
);
7489 // if the editor is shown, we should use it and not the renderer
7490 // Note: However, only if it is really _shown_, i.e. not hidden!
7491 if ( isCurrent
&& IsCellEditControlShown() )
7493 // NB: this "#if..." is temporary and fixes a problem where the
7494 // edit control is erased by this code after being rendered.
7495 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7496 // implicitly, causing this out-of order render.
7497 #if !defined(__WXMAC__) || wxMAC_USE_CORE_GRAPHICS
7498 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7499 editor
->PaintBackground(rect
, attr
);
7505 // but all the rest is drawn by the cell renderer and hence may be customized
7506 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7507 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7514 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7516 int row
= m_currentCellCoords
.GetRow();
7517 int col
= m_currentCellCoords
.GetCol();
7519 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7522 wxRect rect
= CellToRect(row
, col
);
7524 // hmmm... what could we do here to show that the cell is disabled?
7525 // for now, I just draw a thinner border than for the other ones, but
7526 // it doesn't look really good
7528 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7532 // The center of the drawn line is where the position/width/height of
7533 // the rectangle is actually at (on wxMSW at least), so the
7534 // size of the rectangle is reduced to compensate for the thickness of
7535 // the line. If this is too strange on non-wxMSW platforms then
7536 // please #ifdef this appropriately.
7537 rect
.x
+= penWidth
/ 2;
7538 rect
.y
+= penWidth
/ 2;
7539 rect
.width
-= penWidth
- 1;
7540 rect
.height
-= penWidth
- 1;
7542 // Now draw the rectangle
7543 // use the cellHighlightColour if the cell is inside a selection, this
7544 // will ensure the cell is always visible.
7545 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
: m_cellHighlightColour
, penWidth
, wxSOLID
));
7546 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7547 dc
.DrawRectangle(rect
);
7551 // VZ: my experiments with 3D borders...
7553 // how to properly set colours for arbitrary bg?
7554 wxCoord x1
= rect
.x
,
7556 x2
= rect
.x
+ rect
.width
- 1,
7557 y2
= rect
.y
+ rect
.height
- 1;
7559 dc
.SetPen(*wxWHITE_PEN
);
7560 dc
.DrawLine(x1
, y1
, x2
, y1
);
7561 dc
.DrawLine(x1
, y1
, x1
, y2
);
7563 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7564 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7566 dc
.SetPen(*wxBLACK_PEN
);
7567 dc
.DrawLine(x1
, y2
, x2
, y2
);
7568 dc
.DrawLine(x2
, y1
, x2
, y2
+ 1);
7572 wxPen
wxGrid::GetDefaultGridLinePen()
7574 return wxPen(GetGridLineColour(), 1, wxSOLID
);
7577 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
7579 return GetDefaultGridLinePen();
7582 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
7584 return GetDefaultGridLinePen();
7587 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7589 int row
= coords
.GetRow();
7590 int col
= coords
.GetCol();
7591 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7595 wxRect rect
= CellToRect( row
, col
);
7597 // right hand border
7598 dc
.SetPen( GetColGridLinePen(col
) );
7599 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7600 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7603 dc
.SetPen( GetRowGridLinePen(row
) );
7604 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7605 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7608 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7610 // This if block was previously in wxGrid::OnPaint but that doesn't
7611 // seem to get called under wxGTK - MB
7613 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7614 m_numRows
&& m_numCols
)
7616 m_currentCellCoords
.Set(0, 0);
7619 if ( IsCellEditControlShown() )
7621 // don't show highlight when the edit control is shown
7625 // if the active cell was repainted, repaint its highlight too because it
7626 // might have been damaged by the grid lines
7627 size_t count
= cells
.GetCount();
7628 for ( size_t n
= 0; n
< count
; n
++ )
7630 if ( cells
[n
] == m_currentCellCoords
)
7632 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7633 DrawCellHighlight(dc
, attr
);
7641 // TODO: remove this ???
7642 // This is used to redraw all grid lines e.g. when the grid line colour
7645 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7647 #if !WXGRID_DRAW_LINES
7651 if ( !m_gridLinesEnabled
|| !m_numRows
|| !m_numCols
)
7654 int top
, bottom
, left
, right
;
7656 #if 0 //#ifndef __WXGTK__
7660 m_gridWin
->GetClientSize(&cw
, &ch
);
7662 // virtual coords of visible area
7664 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7665 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7670 reg
.GetBox(x
, y
, w
, h
);
7671 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7672 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7676 m_gridWin
->GetClientSize(&cw
, &ch
);
7677 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7678 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7681 // avoid drawing grid lines past the last row and col
7683 right
= wxMin( right
, GetColRight(GetColAt( m_numCols
- 1 )) );
7684 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7686 // no gridlines inside multicells, clip them out
7687 int leftCol
= GetColPos( internalXToCol(left
) );
7688 int topRow
= internalYToRow(top
);
7689 int rightCol
= GetColPos( internalXToCol(right
) );
7690 int bottomRow
= internalYToRow(bottom
);
7693 // CS: I don't know why suddenly unscrolled coordinates are used for clipping
7694 wxRegion
clippedcells(0, 0, cw
, ch
);
7696 int i
, j
, cell_rows
, cell_cols
;
7699 for (j
=topRow
; j
<bottomRow
; j
++)
7702 for (colPos
=leftCol
; colPos
<rightCol
; colPos
++)
7704 i
= GetColAt( colPos
);
7706 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7707 if ((cell_rows
> 1) || (cell_cols
> 1))
7709 rect
= CellToRect(j
,i
);
7710 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7711 clippedcells
.Subtract(rect
);
7713 else if ((cell_rows
< 0) || (cell_cols
< 0))
7715 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7716 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7717 clippedcells
.Subtract(rect
);
7722 wxRegion
clippedcells( left
, top
, right
- left
, bottom
- top
);
7724 int i
, j
, cell_rows
, cell_cols
;
7727 for (j
=topRow
; j
<bottomRow
; j
++)
7729 for (i
=leftCol
; i
<rightCol
; i
++)
7731 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7732 if ((cell_rows
> 1) || (cell_cols
> 1))
7734 rect
= CellToRect(j
, i
);
7735 clippedcells
.Subtract(rect
);
7737 else if ((cell_rows
< 0) || (cell_cols
< 0))
7739 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7740 clippedcells
.Subtract(rect
);
7746 dc
.SetClippingRegion( clippedcells
);
7749 // horizontal grid lines
7751 // already declared above - int i;
7752 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7754 int bot
= GetRowBottom(i
) - 1;
7763 dc
.SetPen( GetRowGridLinePen(i
) );
7764 dc
.DrawLine( left
, bot
, right
, bot
);
7768 // vertical grid lines
7771 for ( colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
7773 i
= GetColAt( colPos
);
7775 int colRight
= GetColRight(i
);
7777 if (GetLayoutDirection() != wxLayout_RightToLeft
)
7781 if ( colRight
> right
)
7786 if ( colRight
>= left
)
7788 dc
.SetPen( GetColGridLinePen(i
) );
7789 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7793 dc
.DestroyClippingRegion();
7796 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
7802 size_t numLabels
= rows
.GetCount();
7804 for ( i
= 0; i
< numLabels
; i
++ )
7806 DrawRowLabel( dc
, rows
[i
] );
7810 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7812 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7820 rect
.SetY( GetRowTop(row
) + 1 );
7821 rect
.SetWidth( m_rowLabelWidth
- 2 );
7822 rect
.SetHeight( GetRowHeight(row
) - 2 );
7824 CalcScrolledPosition( 0, rect
.y
, NULL
, &rect
.y
);
7826 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7828 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7830 int rowTop
= GetRowTop(row
),
7831 rowBottom
= GetRowBottom(row
) - 1;
7833 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
7834 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
7835 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7836 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7838 dc
.SetPen( *wxWHITE_PEN
);
7839 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7840 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
7843 dc
.SetBackgroundMode( wxTRANSPARENT
);
7844 dc
.SetTextForeground( GetLabelTextColour() );
7845 dc
.SetFont( GetLabelFont() );
7848 GetRowLabelAlignment( &hAlign
, &vAlign
);
7851 rect
.SetY( GetRowTop(row
) + 2 );
7852 rect
.SetWidth( m_rowLabelWidth
- 4 );
7853 rect
.SetHeight( GetRowHeight(row
) - 4 );
7854 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7857 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7863 size_t numLabels
= cols
.GetCount();
7865 for ( i
= 0; i
< numLabels
; i
++ )
7867 DrawColLabel( dc
, cols
[i
] );
7871 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7873 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7876 int colLeft
= GetColLeft(col
);
7882 rect
.SetX( colLeft
+ 1 );
7884 rect
.SetWidth( GetColWidth(col
) - 2 );
7885 rect
.SetHeight( m_colLabelHeight
- 2 );
7887 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7889 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7891 int colRight
= GetColRight(col
) - 1;
7893 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
7894 dc
.DrawLine( colRight
, 0, colRight
, m_colLabelHeight
- 1 );
7895 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7896 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
7897 colRight
+ 1, m_colLabelHeight
- 1 );
7899 dc
.SetPen( *wxWHITE_PEN
);
7900 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
7901 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7904 dc
.SetBackgroundMode( wxTRANSPARENT
);
7905 dc
.SetTextForeground( GetLabelTextColour() );
7906 dc
.SetFont( GetLabelFont() );
7908 int hAlign
, vAlign
, orient
;
7909 GetColLabelAlignment( &hAlign
, &vAlign
);
7910 orient
= GetColLabelTextOrientation();
7912 rect
.SetX( colLeft
+ 2 );
7914 rect
.SetWidth( GetColWidth(col
) - 4 );
7915 rect
.SetHeight( m_colLabelHeight
- 4 );
7916 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7919 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7920 const wxString
& value
,
7924 int textOrientation
)
7926 wxArrayString lines
;
7928 StringToLines( value
, lines
);
7930 // Forward to new API.
7931 DrawTextRectangle( dc
,
7939 // VZ: this should be replaced with wxDC::DrawLabel() to which we just have to
7940 // add textOrientation support
7941 void wxGrid::DrawTextRectangle(wxDC
& dc
,
7942 const wxArrayString
& lines
,
7946 int textOrientation
)
7948 if ( lines
.empty() )
7951 wxDCClipper
clip(dc
, rect
);
7956 if ( textOrientation
== wxHORIZONTAL
)
7957 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
7959 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7963 switch ( vertAlign
)
7965 case wxALIGN_BOTTOM
:
7966 if ( textOrientation
== wxHORIZONTAL
)
7967 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7969 x
= rect
.x
+ rect
.width
- textWidth
;
7972 case wxALIGN_CENTRE
:
7973 if ( textOrientation
== wxHORIZONTAL
)
7974 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
7976 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
7981 if ( textOrientation
== wxHORIZONTAL
)
7988 // Align each line of a multi-line label
7989 size_t nLines
= lines
.GetCount();
7990 for ( size_t l
= 0; l
< nLines
; l
++ )
7992 const wxString
& line
= lines
[l
];
7996 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
8002 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
8004 switch ( horizAlign
)
8007 if ( textOrientation
== wxHORIZONTAL
)
8008 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
8010 y
= rect
.y
+ lineWidth
+ 1;
8013 case wxALIGN_CENTRE
:
8014 if ( textOrientation
== wxHORIZONTAL
)
8015 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
8017 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
8022 if ( textOrientation
== wxHORIZONTAL
)
8025 y
= rect
.y
+ rect
.height
- 1;
8029 if ( textOrientation
== wxHORIZONTAL
)
8031 dc
.DrawText( line
, x
, y
);
8036 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
8042 // Split multi-line text up into an array of strings.
8043 // Any existing contents of the string array are preserved.
8045 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
8049 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
8050 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
8052 while ( startPos
< (int)tVal
.length() )
8054 pos
= tVal
.Mid(startPos
).Find( eol
);
8059 else if ( pos
== 0 )
8061 lines
.Add( wxEmptyString
);
8065 lines
.Add( value
.Mid(startPos
, pos
) );
8068 startPos
+= pos
+ 1;
8071 if ( startPos
< (int)value
.length() )
8073 lines
.Add( value
.Mid( startPos
) );
8077 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
8078 const wxArrayString
& lines
,
8079 long *width
, long *height
)
8083 long lineW
= 0, lineH
= 0;
8086 for ( i
= 0; i
< lines
.GetCount(); i
++ )
8088 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
8089 w
= wxMax( w
, lineW
);
8098 // ------ Batch processing.
8100 void wxGrid::EndBatch()
8102 if ( m_batchCount
> 0 )
8105 if ( !m_batchCount
)
8108 m_rowLabelWin
->Refresh();
8109 m_colLabelWin
->Refresh();
8110 m_cornerLabelWin
->Refresh();
8111 m_gridWin
->Refresh();
8116 // Use this, rather than wxWindow::Refresh(), to force an immediate
8117 // repainting of the grid. Has no effect if you are already inside a
8118 // BeginBatch / EndBatch block.
8120 void wxGrid::ForceRefresh()
8126 bool wxGrid::Enable(bool enable
)
8128 if ( !wxScrolledWindow::Enable(enable
) )
8131 // redraw in the new state
8132 m_gridWin
->Refresh();
8138 // ------ Edit control functions
8141 void wxGrid::EnableEditing( bool edit
)
8143 // TODO: improve this ?
8145 if ( edit
!= m_editable
)
8148 EnableCellEditControl(edit
);
8153 void wxGrid::EnableCellEditControl( bool enable
)
8158 if ( enable
!= m_cellEditCtrlEnabled
)
8162 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
8165 // this should be checked by the caller!
8166 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8168 // do it before ShowCellEditControl()
8169 m_cellEditCtrlEnabled
= enable
;
8171 ShowCellEditControl();
8175 //FIXME:add veto support
8176 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
8178 HideCellEditControl();
8179 SaveEditControlValue();
8181 // do it after HideCellEditControl()
8182 m_cellEditCtrlEnabled
= enable
;
8187 bool wxGrid::IsCurrentCellReadOnly() const
8190 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
8191 bool readonly
= attr
->IsReadOnly();
8197 bool wxGrid::CanEnableCellControl() const
8199 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
8200 !IsCurrentCellReadOnly();
8203 bool wxGrid::IsCellEditControlEnabled() const
8205 // the cell edit control might be disable for all cells or just for the
8206 // current one if it's read only
8207 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
8210 bool wxGrid::IsCellEditControlShown() const
8212 bool isShown
= false;
8214 if ( m_cellEditCtrlEnabled
)
8216 int row
= m_currentCellCoords
.GetRow();
8217 int col
= m_currentCellCoords
.GetCol();
8218 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8219 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
8224 if ( editor
->IsCreated() )
8226 isShown
= editor
->GetControl()->IsShown();
8236 void wxGrid::ShowCellEditControl()
8238 if ( IsCellEditControlEnabled() )
8240 if ( !IsVisible( m_currentCellCoords
, false ) )
8242 m_cellEditCtrlEnabled
= false;
8247 wxRect rect
= CellToRect( m_currentCellCoords
);
8248 int row
= m_currentCellCoords
.GetRow();
8249 int col
= m_currentCellCoords
.GetCol();
8251 // if this is part of a multicell, find owner (topleft)
8252 int cell_rows
, cell_cols
;
8253 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8254 if ( cell_rows
<= 0 || cell_cols
<= 0 )
8258 m_currentCellCoords
.SetRow( row
);
8259 m_currentCellCoords
.SetCol( col
);
8262 // erase the highlight and the cell contents because the editor
8263 // might not cover the entire cell
8264 wxClientDC
dc( m_gridWin
);
8266 dc
.SetBrush(wxBrush(GetCellAttr(row
, col
)->GetBackgroundColour(), wxSOLID
));
8267 dc
.SetPen(*wxTRANSPARENT_PEN
);
8268 dc
.DrawRectangle(rect
);
8270 // convert to scrolled coords
8271 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8277 // cell is shifted by one pixel
8278 // However, don't allow x or y to become negative
8279 // since the SetSize() method interprets that as
8286 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8287 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8288 if ( !editor
->IsCreated() )
8290 editor
->Create(m_gridWin
, wxID_ANY
,
8291 new wxGridCellEditorEvtHandler(this, editor
));
8293 wxGridEditorCreatedEvent
evt(GetId(),
8294 wxEVT_GRID_EDITOR_CREATED
,
8298 editor
->GetControl());
8299 GetEventHandler()->ProcessEvent(evt
);
8302 // resize editor to overflow into righthand cells if allowed
8303 int maxWidth
= rect
.width
;
8304 wxString value
= GetCellValue(row
, col
);
8305 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
8308 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
8309 if (maxWidth
< rect
.width
)
8310 maxWidth
= rect
.width
;
8313 int client_right
= m_gridWin
->GetClientSize().GetWidth();
8314 if (rect
.x
+ maxWidth
> client_right
)
8315 maxWidth
= client_right
- rect
.x
;
8317 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
8319 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8320 // may have changed earlier
8321 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
8324 GetCellSize( row
, i
, &c_rows
, &c_cols
);
8326 // looks weird going over a multicell
8327 if (m_table
->IsEmptyCell( row
, i
) &&
8328 (rect
.width
< maxWidth
) && (c_rows
== 1))
8330 rect
.width
+= GetColWidth( i
);
8336 if (rect
.GetRight() > client_right
)
8337 rect
.SetRight( client_right
- 1 );
8340 editor
->SetCellAttr( attr
);
8341 editor
->SetSize( rect
);
8343 editor
->GetControl()->Move(
8344 editor
->GetControl()->GetPosition().x
+ nXMove
,
8345 editor
->GetControl()->GetPosition().y
);
8346 editor
->Show( true, attr
);
8348 // recalc dimensions in case we need to
8349 // expand the scrolled window to account for editor
8352 editor
->BeginEdit(row
, col
, this);
8353 editor
->SetCellAttr(NULL
);
8361 void wxGrid::HideCellEditControl()
8363 if ( IsCellEditControlEnabled() )
8365 int row
= m_currentCellCoords
.GetRow();
8366 int col
= m_currentCellCoords
.GetCol();
8368 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8369 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
8370 editor
->Show( false );
8374 m_gridWin
->SetFocus();
8376 // refresh whole row to the right
8377 wxRect
rect( CellToRect(row
, col
) );
8378 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8379 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8382 // ensure that the pixels under the focus ring get refreshed as well
8383 rect
.Inflate(10, 10);
8386 m_gridWin
->Refresh( false, &rect
);
8390 void wxGrid::SaveEditControlValue()
8392 if ( IsCellEditControlEnabled() )
8394 int row
= m_currentCellCoords
.GetRow();
8395 int col
= m_currentCellCoords
.GetCol();
8397 wxString oldval
= GetCellValue(row
, col
);
8399 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8400 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8401 bool changed
= editor
->EndEdit(row
, col
, this);
8408 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
8409 m_currentCellCoords
.GetRow(),
8410 m_currentCellCoords
.GetCol() ) < 0 )
8412 // Event has been vetoed, set the data back.
8413 SetCellValue(row
, col
, oldval
);
8420 // ------ Grid location functions
8421 // Note that all of these functions work with the logical coordinates of
8422 // grid cells and labels so you will need to convert from device
8423 // coordinates for mouse events etc.
8426 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
8428 int row
= YToRow(y
);
8429 int col
= XToCol(x
);
8431 if ( row
== -1 || col
== -1 )
8433 coords
= wxGridNoCellCoords
;
8437 coords
.Set( row
, col
);
8441 // Internal Helper function for computing row or column from some
8442 // (unscrolled) coordinate value, using either
8443 // m_defaultRowHeight/m_defaultColWidth or binary search on array
8444 // of m_rowBottoms/m_ColRights to speed up the search!
8446 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
8447 const wxArrayInt
& BorderArray
, int nMax
,
8451 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
8456 size_t i_max
= coord
/ defaultDist
,
8459 if (BorderArray
.IsEmpty())
8461 if ((int) i_max
< nMax
)
8463 return clipToMinMax
? nMax
- 1 : -1;
8466 if ( i_max
>= BorderArray
.GetCount())
8468 i_max
= BorderArray
.GetCount() - 1;
8472 if ( coord
>= BorderArray
[i_max
])
8476 i_max
= coord
/ minDist
;
8478 i_max
= BorderArray
.GetCount() - 1;
8481 if ( i_max
>= BorderArray
.GetCount())
8482 i_max
= BorderArray
.GetCount() - 1;
8485 if ( coord
>= BorderArray
[i_max
])
8486 return clipToMinMax
? (int)i_max
: -1;
8487 if ( coord
< BorderArray
[0] )
8490 while ( i_max
- i_min
> 0 )
8492 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
8493 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8494 if (coord
>= BorderArray
[ i_max
- 1])
8498 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
8499 if (coord
< BorderArray
[median
])
8508 int wxGrid::YToRow( int y
)
8510 return CoordToRowOrCol(y
, m_defaultRowHeight
,
8511 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8514 int wxGrid::XToCol( int x
, bool clipToMinMax
)
8517 return clipToMinMax
&& (m_numCols
> 0) ? GetColAt( 0 ) : -1;
8519 if (!m_defaultColWidth
)
8520 m_defaultColWidth
= 1;
8522 int maxPos
= x
/ m_defaultColWidth
;
8525 if (m_colRights
.IsEmpty())
8527 if(maxPos
< m_numCols
)
8528 return GetColAt( maxPos
);
8529 return clipToMinMax
? GetColAt( m_numCols
- 1 ) : -1;
8532 if ( maxPos
>= m_numCols
)
8533 maxPos
= m_numCols
- 1;
8536 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8539 if (m_minAcceptableColWidth
)
8540 maxPos
= x
/ m_minAcceptableColWidth
;
8542 maxPos
= m_numCols
- 1;
8544 if ( maxPos
>= m_numCols
)
8545 maxPos
= m_numCols
- 1;
8548 //X is beyond the last column
8549 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8550 return clipToMinMax
? GetColAt( maxPos
) : -1;
8552 //X is before the first column
8553 if ( x
< m_colRights
[GetColAt( 0 )] )
8554 return GetColAt( 0 );
8556 //Perform a binary search
8557 while ( maxPos
- minPos
> 0 )
8559 wxCHECK_MSG(m_colRights
[GetColAt( minPos
)] <= x
&& x
< m_colRights
[GetColAt( maxPos
)],
8560 0, _T("wxGrid: internal error in XToCol"));
8562 if (x
>= m_colRights
[GetColAt( maxPos
- 1 )])
8563 return GetColAt( maxPos
);
8566 int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
8567 if (x
< m_colRights
[GetColAt( median
)])
8572 return GetColAt( maxPos
);
8575 // return the row number that that the y coord is near
8576 // the edge of, or -1 if not near an edge.
8577 // coords can only possibly be near an edge if
8578 // (a) the row/column is large enough to still allow for an "inner" area
8579 // that is _not_ nead the edge (i.e., if the height/width is smaller
8580 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8583 // (b) resizing rows/columns (the thing for which edge detection is
8584 // relevant at all) is enabled.
8586 int wxGrid::YToEdgeOfRow( int y
)
8589 i
= internalYToRow(y
);
8591 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragRowSize() )
8593 // We know that we are in row i, test whether we are
8594 // close enough to lower or upper border, respectively.
8595 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8597 else if ( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8604 // return the col number that that the x coord is near the edge of, or
8605 // -1 if not near an edge
8606 // See comment at YToEdgeOfRow for conditions on edge detection.
8608 int wxGrid::XToEdgeOfCol( int x
)
8611 i
= internalXToCol(x
);
8613 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragColSize() )
8615 // We know that we are in column i; test whether we are
8616 // close enough to right or left border, respectively.
8617 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8619 else if ( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8626 wxRect
wxGrid::CellToRect( int row
, int col
)
8628 wxRect
rect( -1, -1, -1, -1 );
8630 if ( row
>= 0 && row
< m_numRows
&&
8631 col
>= 0 && col
< m_numCols
)
8633 int i
, cell_rows
, cell_cols
;
8634 rect
.width
= rect
.height
= 0;
8635 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8636 // if negative then find multicell owner
8641 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8643 rect
.x
= GetColLeft(col
);
8644 rect
.y
= GetRowTop(row
);
8645 for (i
=col
; i
< col
+ cell_cols
; i
++)
8646 rect
.width
+= GetColWidth(i
);
8647 for (i
=row
; i
< row
+ cell_rows
; i
++)
8648 rect
.height
+= GetRowHeight(i
);
8651 // if grid lines are enabled, then the area of the cell is a bit smaller
8652 if (m_gridLinesEnabled
)
8661 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
8663 // get the cell rectangle in logical coords
8665 wxRect
r( CellToRect( row
, col
) );
8667 // convert to device coords
8669 int left
, top
, right
, bottom
;
8670 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8671 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8673 // check against the client area of the grid window
8675 m_gridWin
->GetClientSize( &cw
, &ch
);
8677 if ( wholeCellVisible
)
8679 // is the cell wholly visible ?
8680 return ( left
>= 0 && right
<= cw
&&
8681 top
>= 0 && bottom
<= ch
);
8685 // is the cell partly visible ?
8687 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8688 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8692 // make the specified cell location visible by doing a minimal amount
8695 void wxGrid::MakeCellVisible( int row
, int col
)
8698 int xpos
= -1, ypos
= -1;
8700 if ( row
>= 0 && row
< m_numRows
&&
8701 col
>= 0 && col
< m_numCols
)
8703 // get the cell rectangle in logical coords
8704 wxRect
r( CellToRect( row
, col
) );
8706 // convert to device coords
8707 int left
, top
, right
, bottom
;
8708 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8709 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8712 m_gridWin
->GetClientSize( &cw
, &ch
);
8718 else if ( bottom
> ch
)
8720 int h
= r
.GetHeight();
8722 for ( i
= row
- 1; i
>= 0; i
-- )
8724 int rowHeight
= GetRowHeight(i
);
8725 if ( h
+ rowHeight
> ch
)
8732 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8733 // have rounding errors (this is important, because if we do,
8734 // we might not scroll at all and some cells won't be redrawn)
8736 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
8737 // so just add a full scroll unit...
8738 ypos
+= m_scrollLineY
;
8741 // special handling for wide cells - show always left part of the cell!
8742 // Otherwise, e.g. when stepping from row to row, it would jump between
8743 // left and right part of the cell on every step!
8745 if ( left
< 0 || (right
- left
) >= cw
)
8749 else if ( right
> cw
)
8751 // position the view so that the cell is on the right
8753 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8754 xpos
= x0
+ (right
- cw
);
8756 // see comment for ypos above
8757 xpos
+= m_scrollLineX
;
8760 if ( xpos
!= -1 || ypos
!= -1 )
8763 xpos
/= m_scrollLineX
;
8765 ypos
/= m_scrollLineY
;
8766 Scroll( xpos
, ypos
);
8773 // ------ Grid cursor movement functions
8776 bool wxGrid::MoveCursorUp( bool expandSelection
)
8778 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8779 m_currentCellCoords
.GetRow() >= 0 )
8781 if ( expandSelection
)
8783 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8784 m_selectingKeyboard
= m_currentCellCoords
;
8785 if ( m_selectingKeyboard
.GetRow() > 0 )
8787 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8788 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8789 m_selectingKeyboard
.GetCol() );
8790 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8793 else if ( m_currentCellCoords
.GetRow() > 0 )
8795 int row
= m_currentCellCoords
.GetRow() - 1;
8796 int col
= m_currentCellCoords
.GetCol();
8798 MakeCellVisible( row
, col
);
8799 SetCurrentCell( row
, col
);
8810 bool wxGrid::MoveCursorDown( bool expandSelection
)
8812 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8813 m_currentCellCoords
.GetRow() < m_numRows
)
8815 if ( expandSelection
)
8817 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8818 m_selectingKeyboard
= m_currentCellCoords
;
8819 if ( m_selectingKeyboard
.GetRow() < m_numRows
- 1 )
8821 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8822 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8823 m_selectingKeyboard
.GetCol() );
8824 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8827 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8829 int row
= m_currentCellCoords
.GetRow() + 1;
8830 int col
= m_currentCellCoords
.GetCol();
8832 MakeCellVisible( row
, col
);
8833 SetCurrentCell( row
, col
);
8844 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8846 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8847 m_currentCellCoords
.GetCol() >= 0 )
8849 if ( expandSelection
)
8851 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8852 m_selectingKeyboard
= m_currentCellCoords
;
8853 if ( m_selectingKeyboard
.GetCol() > 0 )
8855 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8856 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8857 m_selectingKeyboard
.GetCol() );
8858 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8861 else if ( GetColPos( m_currentCellCoords
.GetCol() ) > 0 )
8863 int row
= m_currentCellCoords
.GetRow();
8864 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) - 1 );
8867 MakeCellVisible( row
, col
);
8868 SetCurrentCell( row
, col
);
8879 bool wxGrid::MoveCursorRight( bool expandSelection
)
8881 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8882 m_currentCellCoords
.GetCol() < m_numCols
)
8884 if ( expandSelection
)
8886 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8887 m_selectingKeyboard
= m_currentCellCoords
;
8888 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8890 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8891 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8892 m_selectingKeyboard
.GetCol() );
8893 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8896 else if ( GetColPos( m_currentCellCoords
.GetCol() ) < m_numCols
- 1 )
8898 int row
= m_currentCellCoords
.GetRow();
8899 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) + 1 );
8902 MakeCellVisible( row
, col
);
8903 SetCurrentCell( row
, col
);
8914 bool wxGrid::MovePageUp()
8916 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8919 int row
= m_currentCellCoords
.GetRow();
8923 m_gridWin
->GetClientSize( &cw
, &ch
);
8925 int y
= GetRowTop(row
);
8926 int newRow
= internalYToRow( y
- ch
+ 1 );
8928 if ( newRow
== row
)
8930 // row > 0, so newRow can never be less than 0 here.
8934 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8935 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8943 bool wxGrid::MovePageDown()
8945 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8948 int row
= m_currentCellCoords
.GetRow();
8949 if ( (row
+ 1) < m_numRows
)
8952 m_gridWin
->GetClientSize( &cw
, &ch
);
8954 int y
= GetRowTop(row
);
8955 int newRow
= internalYToRow( y
+ ch
);
8956 if ( newRow
== row
)
8958 // row < m_numRows, so newRow can't overflow here.
8962 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8963 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8971 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8974 m_currentCellCoords
!= wxGridNoCellCoords
&&
8975 m_currentCellCoords
.GetRow() > 0 )
8977 int row
= m_currentCellCoords
.GetRow();
8978 int col
= m_currentCellCoords
.GetCol();
8980 if ( m_table
->IsEmptyCell(row
, col
) )
8982 // starting in an empty cell: find the next block of
8988 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8992 else if ( m_table
->IsEmptyCell(row
- 1, col
) )
8994 // starting at the top of a block: find the next block
9000 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9006 // starting within a block: find the top of the block
9011 if ( m_table
->IsEmptyCell(row
, col
) )
9019 MakeCellVisible( row
, col
);
9020 if ( expandSelection
)
9022 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9023 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9028 SetCurrentCell( row
, col
);
9037 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
9040 m_currentCellCoords
!= wxGridNoCellCoords
&&
9041 m_currentCellCoords
.GetRow() < m_numRows
- 1 )
9043 int row
= m_currentCellCoords
.GetRow();
9044 int col
= m_currentCellCoords
.GetCol();
9046 if ( m_table
->IsEmptyCell(row
, col
) )
9048 // starting in an empty cell: find the next block of
9051 while ( row
< m_numRows
- 1 )
9054 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9058 else if ( m_table
->IsEmptyCell(row
+ 1, col
) )
9060 // starting at the bottom of a block: find the next block
9063 while ( row
< m_numRows
- 1 )
9066 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9072 // starting within a block: find the bottom of the block
9074 while ( row
< m_numRows
- 1 )
9077 if ( m_table
->IsEmptyCell(row
, col
) )
9085 MakeCellVisible( row
, col
);
9086 if ( expandSelection
)
9088 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9089 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9094 SetCurrentCell( row
, col
);
9103 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
9106 m_currentCellCoords
!= wxGridNoCellCoords
&&
9107 m_currentCellCoords
.GetCol() > 0 )
9109 int row
= m_currentCellCoords
.GetRow();
9110 int col
= m_currentCellCoords
.GetCol();
9112 if ( m_table
->IsEmptyCell(row
, col
) )
9114 // starting in an empty cell: find the next block of
9120 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9124 else if ( m_table
->IsEmptyCell(row
, col
- 1) )
9126 // starting at the left of a block: find the next block
9132 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9138 // starting within a block: find the left of the block
9143 if ( m_table
->IsEmptyCell(row
, col
) )
9151 MakeCellVisible( row
, col
);
9152 if ( expandSelection
)
9154 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9155 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9160 SetCurrentCell( row
, col
);
9169 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
9172 m_currentCellCoords
!= wxGridNoCellCoords
&&
9173 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
9175 int row
= m_currentCellCoords
.GetRow();
9176 int col
= m_currentCellCoords
.GetCol();
9178 if ( m_table
->IsEmptyCell(row
, col
) )
9180 // starting in an empty cell: find the next block of
9183 while ( col
< m_numCols
- 1 )
9186 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9190 else if ( m_table
->IsEmptyCell(row
, col
+ 1) )
9192 // starting at the right of a block: find the next block
9195 while ( col
< m_numCols
- 1 )
9198 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9204 // starting within a block: find the right of the block
9206 while ( col
< m_numCols
- 1 )
9209 if ( m_table
->IsEmptyCell(row
, col
) )
9217 MakeCellVisible( row
, col
);
9218 if ( expandSelection
)
9220 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9221 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9226 SetCurrentCell( row
, col
);
9236 // ------ Label values and formatting
9239 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
9242 *horiz
= m_rowLabelHorizAlign
;
9244 *vert
= m_rowLabelVertAlign
;
9247 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
9250 *horiz
= m_colLabelHorizAlign
;
9252 *vert
= m_colLabelVertAlign
;
9255 int wxGrid::GetColLabelTextOrientation()
9257 return m_colLabelTextOrientation
;
9260 wxString
wxGrid::GetRowLabelValue( int row
)
9264 return m_table
->GetRowLabelValue( row
);
9274 wxString
wxGrid::GetColLabelValue( int col
)
9278 return m_table
->GetColLabelValue( col
);
9288 void wxGrid::SetRowLabelSize( int width
)
9290 width
= wxMax( width
, 0 );
9291 if ( width
!= m_rowLabelWidth
)
9295 m_rowLabelWin
->Show( false );
9296 m_cornerLabelWin
->Show( false );
9298 else if ( m_rowLabelWidth
== 0 )
9300 m_rowLabelWin
->Show( true );
9301 if ( m_colLabelHeight
> 0 )
9302 m_cornerLabelWin
->Show( true );
9305 m_rowLabelWidth
= width
;
9307 wxScrolledWindow::Refresh( true );
9311 void wxGrid::SetColLabelSize( int height
)
9313 height
= wxMax( height
, 0 );
9314 if ( height
!= m_colLabelHeight
)
9318 m_colLabelWin
->Show( false );
9319 m_cornerLabelWin
->Show( false );
9321 else if ( m_colLabelHeight
== 0 )
9323 m_colLabelWin
->Show( true );
9324 if ( m_rowLabelWidth
> 0 )
9325 m_cornerLabelWin
->Show( true );
9328 m_colLabelHeight
= height
;
9330 wxScrolledWindow::Refresh( true );
9334 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
9336 if ( m_labelBackgroundColour
!= colour
)
9338 m_labelBackgroundColour
= colour
;
9339 m_rowLabelWin
->SetBackgroundColour( colour
);
9340 m_colLabelWin
->SetBackgroundColour( colour
);
9341 m_cornerLabelWin
->SetBackgroundColour( colour
);
9343 if ( !GetBatchCount() )
9345 m_rowLabelWin
->Refresh();
9346 m_colLabelWin
->Refresh();
9347 m_cornerLabelWin
->Refresh();
9352 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
9354 if ( m_labelTextColour
!= colour
)
9356 m_labelTextColour
= colour
;
9357 if ( !GetBatchCount() )
9359 m_rowLabelWin
->Refresh();
9360 m_colLabelWin
->Refresh();
9365 void wxGrid::SetLabelFont( const wxFont
& font
)
9368 if ( !GetBatchCount() )
9370 m_rowLabelWin
->Refresh();
9371 m_colLabelWin
->Refresh();
9375 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
9377 // allow old (incorrect) defs to be used
9380 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9381 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9382 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9387 case wxTOP
: vert
= wxALIGN_TOP
; break;
9388 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9389 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9392 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9394 m_rowLabelHorizAlign
= horiz
;
9397 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9399 m_rowLabelVertAlign
= vert
;
9402 if ( !GetBatchCount() )
9404 m_rowLabelWin
->Refresh();
9408 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
9410 // allow old (incorrect) defs to be used
9413 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9414 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9415 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9420 case wxTOP
: vert
= wxALIGN_TOP
; break;
9421 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9422 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9425 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9427 m_colLabelHorizAlign
= horiz
;
9430 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9432 m_colLabelVertAlign
= vert
;
9435 if ( !GetBatchCount() )
9437 m_colLabelWin
->Refresh();
9441 // Note: under MSW, the default column label font must be changed because it
9442 // does not support vertical printing
9444 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9445 // pGrid->SetLabelFont(font);
9446 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9448 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9450 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9451 m_colLabelTextOrientation
= textOrientation
;
9453 if ( !GetBatchCount() )
9454 m_colLabelWin
->Refresh();
9457 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9461 m_table
->SetRowLabelValue( row
, s
);
9462 if ( !GetBatchCount() )
9464 wxRect rect
= CellToRect( row
, 0 );
9465 if ( rect
.height
> 0 )
9467 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9469 rect
.width
= m_rowLabelWidth
;
9470 m_rowLabelWin
->Refresh( true, &rect
);
9476 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9480 m_table
->SetColLabelValue( col
, s
);
9481 if ( !GetBatchCount() )
9483 wxRect rect
= CellToRect( 0, col
);
9484 if ( rect
.width
> 0 )
9486 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9488 rect
.height
= m_colLabelHeight
;
9489 m_colLabelWin
->Refresh( true, &rect
);
9495 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9497 if ( m_gridLineColour
!= colour
)
9499 m_gridLineColour
= colour
;
9501 wxClientDC
dc( m_gridWin
);
9503 DrawAllGridLines( dc
, wxRegion() );
9507 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9509 if ( m_cellHighlightColour
!= colour
)
9511 m_cellHighlightColour
= colour
;
9513 wxClientDC
dc( m_gridWin
);
9515 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9516 DrawCellHighlight(dc
, attr
);
9521 void wxGrid::SetCellHighlightPenWidth(int width
)
9523 if (m_cellHighlightPenWidth
!= width
)
9525 m_cellHighlightPenWidth
= width
;
9527 // Just redrawing the cell highlight is not enough since that won't
9528 // make any visible change if the the thickness is getting smaller.
9529 int row
= m_currentCellCoords
.GetRow();
9530 int col
= m_currentCellCoords
.GetCol();
9531 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9534 wxRect rect
= CellToRect(row
, col
);
9535 m_gridWin
->Refresh(true, &rect
);
9539 void wxGrid::SetCellHighlightROPenWidth(int width
)
9541 if (m_cellHighlightROPenWidth
!= width
)
9543 m_cellHighlightROPenWidth
= width
;
9545 // Just redrawing the cell highlight is not enough since that won't
9546 // make any visible change if the the thickness is getting smaller.
9547 int row
= m_currentCellCoords
.GetRow();
9548 int col
= m_currentCellCoords
.GetCol();
9549 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9552 wxRect rect
= CellToRect(row
, col
);
9553 m_gridWin
->Refresh(true, &rect
);
9557 void wxGrid::EnableGridLines( bool enable
)
9559 if ( enable
!= m_gridLinesEnabled
)
9561 m_gridLinesEnabled
= enable
;
9563 if ( !GetBatchCount() )
9567 wxClientDC
dc( m_gridWin
);
9569 DrawAllGridLines( dc
, wxRegion() );
9573 m_gridWin
->Refresh();
9579 int wxGrid::GetDefaultRowSize()
9581 return m_defaultRowHeight
;
9584 int wxGrid::GetRowSize( int row
)
9586 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9588 return GetRowHeight(row
);
9591 int wxGrid::GetDefaultColSize()
9593 return m_defaultColWidth
;
9596 int wxGrid::GetColSize( int col
)
9598 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9600 return GetColWidth(col
);
9603 // ============================================================================
9604 // access to the grid attributes: each of them has a default value in the grid
9605 // itself and may be overidden on a per-cell basis
9606 // ============================================================================
9608 // ----------------------------------------------------------------------------
9609 // setting default attributes
9610 // ----------------------------------------------------------------------------
9612 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9614 m_defaultCellAttr
->SetBackgroundColour(col
);
9616 m_gridWin
->SetBackgroundColour(col
);
9620 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9622 m_defaultCellAttr
->SetTextColour(col
);
9625 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9627 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9630 void wxGrid::SetDefaultCellOverflow( bool allow
)
9632 m_defaultCellAttr
->SetOverflow(allow
);
9635 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9637 m_defaultCellAttr
->SetFont(font
);
9640 // For editors and renderers the type registry takes precedence over the
9641 // default attr, so we need to register the new editor/renderer for the string
9642 // data type in order to make setting a default editor/renderer appear to
9645 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9647 RegisterDataType(wxGRID_VALUE_STRING
,
9649 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9652 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9654 RegisterDataType(wxGRID_VALUE_STRING
,
9655 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9659 // ----------------------------------------------------------------------------
9660 // access to the default attrbiutes
9661 // ----------------------------------------------------------------------------
9663 wxColour
wxGrid::GetDefaultCellBackgroundColour()
9665 return m_defaultCellAttr
->GetBackgroundColour();
9668 wxColour
wxGrid::GetDefaultCellTextColour()
9670 return m_defaultCellAttr
->GetTextColour();
9673 wxFont
wxGrid::GetDefaultCellFont()
9675 return m_defaultCellAttr
->GetFont();
9678 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
9680 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9683 bool wxGrid::GetDefaultCellOverflow()
9685 return m_defaultCellAttr
->GetOverflow();
9688 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9690 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9693 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9695 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9698 // ----------------------------------------------------------------------------
9699 // access to cell attributes
9700 // ----------------------------------------------------------------------------
9702 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
9704 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9705 wxColour colour
= attr
->GetBackgroundColour();
9711 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
9713 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9714 wxColour colour
= attr
->GetTextColour();
9720 wxFont
wxGrid::GetCellFont( int row
, int col
)
9722 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9723 wxFont font
= attr
->GetFont();
9729 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
9731 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9732 attr
->GetAlignment(horiz
, vert
);
9736 bool wxGrid::GetCellOverflow( int row
, int col
)
9738 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9739 bool allow
= attr
->GetOverflow();
9745 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
9747 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9748 attr
->GetSize( num_rows
, num_cols
);
9752 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
9754 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9755 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9761 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
9763 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9764 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9770 bool wxGrid::IsReadOnly(int row
, int col
) const
9772 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9773 bool isReadOnly
= attr
->IsReadOnly();
9779 // ----------------------------------------------------------------------------
9780 // attribute support: cache, automatic provider creation, ...
9781 // ----------------------------------------------------------------------------
9783 bool wxGrid::CanHaveAttributes()
9790 return m_table
->CanHaveAttributes();
9793 void wxGrid::ClearAttrCache()
9795 if ( m_attrCache
.row
!= -1 )
9797 wxSafeDecRef(m_attrCache
.attr
);
9798 m_attrCache
.attr
= NULL
;
9799 m_attrCache
.row
= -1;
9803 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9807 wxGrid
*self
= (wxGrid
*)this; // const_cast
9809 self
->ClearAttrCache();
9810 self
->m_attrCache
.row
= row
;
9811 self
->m_attrCache
.col
= col
;
9812 self
->m_attrCache
.attr
= attr
;
9817 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9819 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9821 *attr
= m_attrCache
.attr
;
9822 wxSafeIncRef(m_attrCache
.attr
);
9824 #ifdef DEBUG_ATTR_CACHE
9825 gs_nAttrCacheHits
++;
9832 #ifdef DEBUG_ATTR_CACHE
9833 gs_nAttrCacheMisses
++;
9840 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9842 wxGridCellAttr
*attr
= NULL
;
9843 // Additional test to avoid looking at the cache e.g. for
9844 // wxNoCellCoords, as this will confuse memory management.
9847 if ( !LookupAttr(row
, col
, &attr
) )
9849 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9850 : (wxGridCellAttr
*)NULL
;
9851 CacheAttr(row
, col
, attr
);
9857 attr
->SetDefAttr(m_defaultCellAttr
);
9861 attr
= m_defaultCellAttr
;
9868 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9870 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9871 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9873 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9874 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9876 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9879 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9881 // artificially inc the ref count to match DecRef() in caller
9883 m_table
->SetAttr(attr
, row
, col
);
9889 // ----------------------------------------------------------------------------
9890 // setting column attributes (wrappers around SetColAttr)
9891 // ----------------------------------------------------------------------------
9893 void wxGrid::SetColFormatBool(int col
)
9895 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9898 void wxGrid::SetColFormatNumber(int col
)
9900 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9903 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9905 wxString typeName
= wxGRID_VALUE_FLOAT
;
9906 if ( (width
!= -1) || (precision
!= -1) )
9908 typeName
<< _T(':') << width
<< _T(',') << precision
;
9911 SetColFormatCustom(col
, typeName
);
9914 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9916 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9918 attr
= new wxGridCellAttr
;
9919 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9920 attr
->SetRenderer(renderer
);
9922 SetColAttr(col
, attr
);
9926 // ----------------------------------------------------------------------------
9927 // setting cell attributes: this is forwarded to the table
9928 // ----------------------------------------------------------------------------
9930 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9932 if ( CanHaveAttributes() )
9934 m_table
->SetAttr(attr
, row
, col
);
9943 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9945 if ( CanHaveAttributes() )
9947 m_table
->SetRowAttr(attr
, row
);
9956 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9958 if ( CanHaveAttributes() )
9960 m_table
->SetColAttr(attr
, col
);
9969 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9971 if ( CanHaveAttributes() )
9973 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9974 attr
->SetBackgroundColour(colour
);
9979 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9981 if ( CanHaveAttributes() )
9983 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9984 attr
->SetTextColour(colour
);
9989 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9991 if ( CanHaveAttributes() )
9993 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9994 attr
->SetFont(font
);
9999 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
10001 if ( CanHaveAttributes() )
10003 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10004 attr
->SetAlignment(horiz
, vert
);
10009 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
10011 if ( CanHaveAttributes() )
10013 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10014 attr
->SetOverflow(allow
);
10019 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
10021 if ( CanHaveAttributes() )
10023 int cell_rows
, cell_cols
;
10025 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10026 attr
->GetSize(&cell_rows
, &cell_cols
);
10027 attr
->SetSize(num_rows
, num_cols
);
10030 // Cannot set the size of a cell to 0 or negative values
10031 // While it is perfectly legal to do that, this function cannot
10032 // handle all the possibilies, do it by hand by getting the CellAttr.
10033 // You can only set the size of a cell to 1,1 or greater with this fn
10034 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
10035 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10036 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
10037 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10039 // if this was already a multicell then "turn off" the other cells first
10040 if ((cell_rows
> 1) || (cell_rows
> 1))
10043 for (j
=row
; j
< row
+ cell_rows
; j
++)
10045 for (i
=col
; i
< col
+ cell_cols
; i
++)
10047 if ((i
!= col
) || (j
!= row
))
10049 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10050 attr_stub
->SetSize( 1, 1 );
10051 attr_stub
->DecRef();
10057 // mark the cells that will be covered by this cell to
10058 // negative or zero values to point back at this cell
10059 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
10062 for (j
=row
; j
< row
+ num_rows
; j
++)
10064 for (i
=col
; i
< col
+ num_cols
; i
++)
10066 if ((i
!= col
) || (j
!= row
))
10068 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10069 attr_stub
->SetSize( row
- j
, col
- i
);
10070 attr_stub
->DecRef();
10078 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
10080 if ( CanHaveAttributes() )
10082 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10083 attr
->SetRenderer(renderer
);
10088 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
10090 if ( CanHaveAttributes() )
10092 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10093 attr
->SetEditor(editor
);
10098 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
10100 if ( CanHaveAttributes() )
10102 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10103 attr
->SetReadOnly(isReadOnly
);
10108 // ----------------------------------------------------------------------------
10109 // Data type registration
10110 // ----------------------------------------------------------------------------
10112 void wxGrid::RegisterDataType(const wxString
& typeName
,
10113 wxGridCellRenderer
* renderer
,
10114 wxGridCellEditor
* editor
)
10116 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
10120 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
10122 wxString typeName
= m_table
->GetTypeName(row
, col
);
10123 return GetDefaultEditorForType(typeName
);
10126 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
10128 wxString typeName
= m_table
->GetTypeName(row
, col
);
10129 return GetDefaultRendererForType(typeName
);
10132 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
10134 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10135 if ( index
== wxNOT_FOUND
)
10139 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
10140 wxFAIL_MSG(errStr
.c_str());
10145 return m_typeRegistry
->GetEditor(index
);
10148 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
10150 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10151 if ( index
== wxNOT_FOUND
)
10155 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
10156 wxFAIL_MSG(errStr
.c_str());
10161 return m_typeRegistry
->GetRenderer(index
);
10164 // ----------------------------------------------------------------------------
10166 // ----------------------------------------------------------------------------
10168 void wxGrid::EnableDragRowSize( bool enable
)
10170 m_canDragRowSize
= enable
;
10173 void wxGrid::EnableDragColSize( bool enable
)
10175 m_canDragColSize
= enable
;
10178 void wxGrid::EnableDragGridSize( bool enable
)
10180 m_canDragGridSize
= enable
;
10183 void wxGrid::EnableDragCell( bool enable
)
10185 m_canDragCell
= enable
;
10188 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
10190 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
10192 if ( resizeExistingRows
)
10194 // since we are resizing all rows to the default row size,
10195 // we can simply clear the row heights and row bottoms
10196 // arrays (which also allows us to take advantage of
10197 // some speed optimisations)
10198 m_rowHeights
.Empty();
10199 m_rowBottoms
.Empty();
10200 if ( !GetBatchCount() )
10205 void wxGrid::SetRowSize( int row
, int height
)
10207 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
10209 // See comment in SetColSize
10210 if ( height
< GetRowMinimalAcceptableHeight())
10213 if ( m_rowHeights
.IsEmpty() )
10215 // need to really create the array
10219 int h
= wxMax( 0, height
);
10220 int diff
= h
- m_rowHeights
[row
];
10222 m_rowHeights
[row
] = h
;
10224 for ( i
= row
; i
< m_numRows
; i
++ )
10226 m_rowBottoms
[i
] += diff
;
10229 if ( !GetBatchCount() )
10233 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
10235 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
10237 if ( resizeExistingCols
)
10239 // since we are resizing all columns to the default column size,
10240 // we can simply clear the col widths and col rights
10241 // arrays (which also allows us to take advantage of
10242 // some speed optimisations)
10243 m_colWidths
.Empty();
10244 m_colRights
.Empty();
10245 if ( !GetBatchCount() )
10250 void wxGrid::SetColSize( int col
, int width
)
10252 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
10254 // should we check that it's bigger than GetColMinimalWidth(col) here?
10256 // No, because it is reasonable to assume the library user know's
10257 // what he is doing. However we should test against the weaker
10258 // constraint of minimalAcceptableWidth, as this breaks rendering
10260 // This test then fixes sf.net bug #645734
10262 if ( width
< GetColMinimalAcceptableWidth() )
10265 if ( m_colWidths
.IsEmpty() )
10267 // need to really create the array
10271 // if < 0 then calculate new width from label
10275 wxArrayString lines
;
10276 wxClientDC
dc(m_colLabelWin
);
10277 dc
.SetFont(GetLabelFont());
10278 StringToLines(GetColLabelValue(col
), lines
);
10279 GetTextBoxSize(dc
, lines
, &w
, &h
);
10283 int w
= wxMax( 0, width
);
10284 int diff
= w
- m_colWidths
[col
];
10285 m_colWidths
[col
] = w
;
10289 for ( colPos
= GetColPos( col
); colPos
< m_numCols
; colPos
++ )
10291 i
= GetColAt( colPos
);
10292 m_colRights
[i
] += diff
;
10295 if ( !GetBatchCount() )
10299 void wxGrid::SetColMinimalWidth( int col
, int width
)
10301 if (width
> GetColMinimalAcceptableWidth())
10303 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10304 m_colMinWidths
[key
] = width
;
10308 void wxGrid::SetRowMinimalHeight( int row
, int width
)
10310 if (width
> GetRowMinimalAcceptableHeight())
10312 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10313 m_rowMinHeights
[key
] = width
;
10317 int wxGrid::GetColMinimalWidth(int col
) const
10319 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10320 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
10322 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
10325 int wxGrid::GetRowMinimalHeight(int row
) const
10327 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10328 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
10330 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
10333 void wxGrid::SetColMinimalAcceptableWidth( int width
)
10335 // We do allow a width of 0 since this gives us
10336 // an easy way to temporarily hiding columns.
10338 m_minAcceptableColWidth
= width
;
10341 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
10343 // We do allow a height of 0 since this gives us
10344 // an easy way to temporarily hiding rows.
10346 m_minAcceptableRowHeight
= height
;
10349 int wxGrid::GetColMinimalAcceptableWidth() const
10351 return m_minAcceptableColWidth
;
10354 int wxGrid::GetRowMinimalAcceptableHeight() const
10356 return m_minAcceptableRowHeight
;
10359 // ----------------------------------------------------------------------------
10361 // ----------------------------------------------------------------------------
10363 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
10365 wxClientDC
dc(m_gridWin
);
10367 // cancel editing of cell
10368 HideCellEditControl();
10369 SaveEditControlValue();
10371 // init both of them to avoid compiler warnings, even if we only need one
10379 wxCoord extent
, extentMax
= 0;
10380 int max
= column
? m_numRows
: m_numCols
;
10381 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
10388 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
10389 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
10392 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
10393 extent
= column
? size
.x
: size
.y
;
10394 if ( extent
> extentMax
)
10395 extentMax
= extent
;
10397 renderer
->DecRef();
10403 // now also compare with the column label extent
10405 dc
.SetFont( GetLabelFont() );
10409 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
10410 if ( GetColLabelTextOrientation() == wxVERTICAL
)
10414 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
10416 extent
= column
? w
: h
;
10417 if ( extent
> extentMax
)
10418 extentMax
= extent
;
10422 // empty column - give default extent (notice that if extentMax is less
10423 // than default extent but != 0, it's OK)
10424 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
10429 // leave some space around text
10437 SetColSize( col
, extentMax
);
10438 if ( !GetBatchCount() )
10441 m_gridWin
->GetClientSize( &cw
, &ch
);
10442 wxRect
rect ( CellToRect( 0, col
) );
10444 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10445 rect
.width
= cw
- rect
.x
;
10446 rect
.height
= m_colLabelHeight
;
10447 m_colLabelWin
->Refresh( true, &rect
);
10452 SetRowSize(row
, extentMax
);
10453 if ( !GetBatchCount() )
10456 m_gridWin
->GetClientSize( &cw
, &ch
);
10457 wxRect
rect( CellToRect( row
, 0 ) );
10459 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10460 rect
.width
= m_rowLabelWidth
;
10461 rect
.height
= ch
- rect
.y
;
10462 m_rowLabelWin
->Refresh( true, &rect
);
10469 SetColMinimalWidth(col
, extentMax
);
10471 SetRowMinimalHeight(row
, extentMax
);
10475 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10477 int width
= m_rowLabelWidth
;
10482 for ( int col
= 0; col
< m_numCols
; col
++ )
10485 AutoSizeColumn(col
, setAsMin
);
10487 width
+= GetColWidth(col
);
10496 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10498 int height
= m_colLabelHeight
;
10503 for ( int row
= 0; row
< m_numRows
; row
++ )
10506 AutoSizeRow(row
, setAsMin
);
10508 height
+= GetRowHeight(row
);
10517 void wxGrid::AutoSize()
10521 wxSize
size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
10523 // round up the size to a multiple of scroll step - this ensures that we
10524 // won't get the scrollbars if we're sized exactly to this width
10525 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
10528 GetScrollX(size
.x
+ m_extraWidth
+ 1) * m_scrollLineX
,
10529 GetScrollY(size
.y
+ m_extraHeight
+ 1) * m_scrollLineY
);
10531 // distribute the extra space between the columns/rows to avoid having
10532 // extra white space
10534 // Remove the extra m_extraWidth + 1 added above
10535 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
10536 if ( diff
&& m_numCols
)
10538 // try to resize the columns uniformly
10539 wxCoord diffPerCol
= diff
/ m_numCols
;
10542 for ( int col
= 0; col
< m_numCols
; col
++ )
10544 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
10548 // add remaining amount to the last columns
10549 diff
-= diffPerCol
* m_numCols
;
10552 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
10554 SetColSize(col
, GetColWidth(col
) + 1);
10560 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
10561 if ( diff
&& m_numRows
)
10563 // try to resize the columns uniformly
10564 wxCoord diffPerRow
= diff
/ m_numRows
;
10567 for ( int row
= 0; row
< m_numRows
; row
++ )
10569 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
10573 // add remaining amount to the last rows
10574 diff
-= diffPerRow
* m_numRows
;
10577 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
10579 SetRowSize(row
, GetRowHeight(row
) + 1);
10586 SetClientSize(sizeFit
);
10589 void wxGrid::AutoSizeRowLabelSize( int row
)
10591 wxArrayString lines
;
10594 // Hide the edit control, so it
10595 // won't interfere with drag-shrinking.
10596 if ( IsCellEditControlShown() )
10598 HideCellEditControl();
10599 SaveEditControlValue();
10602 // autosize row height depending on label text
10603 StringToLines( GetRowLabelValue( row
), lines
);
10604 wxClientDC
dc( m_rowLabelWin
);
10605 GetTextBoxSize( dc
, lines
, &w
, &h
);
10606 if ( h
< m_defaultRowHeight
)
10607 h
= m_defaultRowHeight
;
10608 SetRowSize(row
, h
);
10612 void wxGrid::AutoSizeColLabelSize( int col
)
10614 wxArrayString lines
;
10617 // Hide the edit control, so it
10618 // won't interfere with drag-shrinking.
10619 if ( IsCellEditControlShown() )
10621 HideCellEditControl();
10622 SaveEditControlValue();
10625 // autosize column width depending on label text
10626 StringToLines( GetColLabelValue( col
), lines
);
10627 wxClientDC
dc( m_colLabelWin
);
10628 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10629 GetTextBoxSize( dc
, lines
, &w
, &h
);
10631 GetTextBoxSize( dc
, lines
, &h
, &w
);
10632 if ( w
< m_defaultColWidth
)
10633 w
= m_defaultColWidth
;
10634 SetColSize(col
, w
);
10638 wxSize
wxGrid::DoGetBestSize() const
10640 // don't set sizes, only calculate them
10641 wxGrid
*self
= (wxGrid
*)this; // const_cast
10644 width
= self
->SetOrCalcColumnSizes(true);
10645 height
= self
->SetOrCalcRowSizes(true);
10652 // Round up to a multiple the scroll rate
10653 // NOTE: this still doesn't get rid of the scrollbars;
10654 // is there any magic incantation for that?
10656 GetScrollPixelsPerUnit(&xpu
, &ypu
);
10658 width
+= 1 + xpu
- (width
% xpu
);
10660 height
+= 1 + ypu
- (height
% ypu
);
10662 // limit to 1/4 of the screen size
10663 int maxwidth
, maxheight
;
10664 wxDisplaySize( &maxwidth
, &maxheight
);
10667 if ( width
> maxwidth
)
10669 if ( height
> maxheight
)
10670 height
= maxheight
;
10672 wxSize
best(width
, height
);
10674 // NOTE: This size should be cached, but first we need to add calls to
10675 // InvalidateBestSize everywhere that could change the results of this
10677 // CacheBestSize(size);
10687 wxPen
& wxGrid::GetDividerPen() const
10692 // ----------------------------------------------------------------------------
10693 // cell value accessor functions
10694 // ----------------------------------------------------------------------------
10696 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10700 m_table
->SetValue( row
, col
, s
);
10701 if ( !GetBatchCount() )
10704 wxRect
rect( CellToRect( row
, col
) );
10706 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10707 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10708 m_gridWin
->Refresh( false, &rect
);
10711 if ( m_currentCellCoords
.GetRow() == row
&&
10712 m_currentCellCoords
.GetCol() == col
&&
10713 IsCellEditControlShown())
10714 // Note: If we are using IsCellEditControlEnabled,
10715 // this interacts badly with calling SetCellValue from
10716 // an EVT_GRID_CELL_CHANGE handler.
10718 HideCellEditControl();
10719 ShowCellEditControl(); // will reread data from table
10724 // ----------------------------------------------------------------------------
10725 // block, row and column selection
10726 // ----------------------------------------------------------------------------
10728 void wxGrid::SelectRow( int row
, bool addToSelected
)
10730 if ( IsSelection() && !addToSelected
)
10734 m_selection
->SelectRow( row
, false, addToSelected
);
10737 void wxGrid::SelectCol( int col
, bool addToSelected
)
10739 if ( IsSelection() && !addToSelected
)
10743 m_selection
->SelectCol( col
, false, addToSelected
);
10746 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10747 bool addToSelected
)
10749 if ( IsSelection() && !addToSelected
)
10753 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10754 false, addToSelected
);
10757 void wxGrid::SelectAll()
10759 if ( m_numRows
> 0 && m_numCols
> 0 )
10762 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10766 // ----------------------------------------------------------------------------
10767 // cell, row and col deselection
10768 // ----------------------------------------------------------------------------
10770 void wxGrid::DeselectRow( int row
)
10772 if ( !m_selection
)
10775 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10777 if ( m_selection
->IsInSelection(row
, 0 ) )
10778 m_selection
->ToggleCellSelection(row
, 0);
10782 int nCols
= GetNumberCols();
10783 for ( int i
= 0; i
< nCols
; i
++ )
10785 if ( m_selection
->IsInSelection(row
, i
) )
10786 m_selection
->ToggleCellSelection(row
, i
);
10791 void wxGrid::DeselectCol( int col
)
10793 if ( !m_selection
)
10796 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10798 if ( m_selection
->IsInSelection(0, col
) )
10799 m_selection
->ToggleCellSelection(0, col
);
10803 int nRows
= GetNumberRows();
10804 for ( int i
= 0; i
< nRows
; i
++ )
10806 if ( m_selection
->IsInSelection(i
, col
) )
10807 m_selection
->ToggleCellSelection(i
, col
);
10812 void wxGrid::DeselectCell( int row
, int col
)
10814 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10815 m_selection
->ToggleCellSelection(row
, col
);
10818 bool wxGrid::IsSelection()
10820 return ( m_selection
&& (m_selection
->IsSelection() ||
10821 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10822 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10825 bool wxGrid::IsInSelection( int row
, int col
) const
10827 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10828 ( row
>= m_selectingTopLeft
.GetRow() &&
10829 col
>= m_selectingTopLeft
.GetCol() &&
10830 row
<= m_selectingBottomRight
.GetRow() &&
10831 col
<= m_selectingBottomRight
.GetCol() )) );
10834 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10838 wxGridCellCoordsArray a
;
10842 return m_selection
->m_cellSelection
;
10845 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10849 wxGridCellCoordsArray a
;
10853 return m_selection
->m_blockSelectionTopLeft
;
10856 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10860 wxGridCellCoordsArray a
;
10864 return m_selection
->m_blockSelectionBottomRight
;
10867 wxArrayInt
wxGrid::GetSelectedRows() const
10875 return m_selection
->m_rowSelection
;
10878 wxArrayInt
wxGrid::GetSelectedCols() const
10886 return m_selection
->m_colSelection
;
10889 void wxGrid::ClearSelection()
10891 m_selectingTopLeft
=
10892 m_selectingBottomRight
=
10893 m_selectingKeyboard
= wxGridNoCellCoords
;
10895 m_selection
->ClearSelection();
10898 // This function returns the rectangle that encloses the given block
10899 // in device coords clipped to the client size of the grid window.
10901 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10902 const wxGridCellCoords
&bottomRight
)
10904 wxRect
rect( wxGridNoCellRect
);
10907 cellRect
= CellToRect( topLeft
);
10908 if ( cellRect
!= wxGridNoCellRect
)
10914 rect
= wxRect(0, 0, 0, 0);
10917 cellRect
= CellToRect( bottomRight
);
10918 if ( cellRect
!= wxGridNoCellRect
)
10924 return wxGridNoCellRect
;
10928 int left
= rect
.GetLeft();
10929 int top
= rect
.GetTop();
10930 int right
= rect
.GetRight();
10931 int bottom
= rect
.GetBottom();
10933 int leftCol
= topLeft
.GetCol();
10934 int topRow
= topLeft
.GetRow();
10935 int rightCol
= bottomRight
.GetCol();
10936 int bottomRow
= bottomRight
.GetRow();
10944 leftCol
= rightCol
;
10954 topRow
= bottomRow
;
10958 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10960 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10962 if ((j
== topRow
) || (j
== bottomRow
) || (i
== leftCol
) || (i
== rightCol
))
10964 cellRect
= CellToRect( j
, i
);
10966 if (cellRect
.x
< left
)
10968 if (cellRect
.y
< top
)
10970 if (cellRect
.x
+ cellRect
.width
> right
)
10971 right
= cellRect
.x
+ cellRect
.width
;
10972 if (cellRect
.y
+ cellRect
.height
> bottom
)
10973 bottom
= cellRect
.y
+ cellRect
.height
;
10977 i
= rightCol
; // jump over inner cells.
10982 // convert to scrolled coords
10984 CalcScrolledPosition( left
, top
, &left
, &top
);
10985 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10988 m_gridWin
->GetClientSize( &cw
, &ch
);
10990 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10991 return wxRect(0,0,0,0);
10993 rect
.SetLeft( wxMax(0, left
) );
10994 rect
.SetTop( wxMax(0, top
) );
10995 rect
.SetRight( wxMin(cw
, right
) );
10996 rect
.SetBottom( wxMin(ch
, bottom
) );
11001 // ----------------------------------------------------------------------------
11002 // grid event classes
11003 // ----------------------------------------------------------------------------
11005 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
11007 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
11008 int row
, int col
, int x
, int y
, bool sel
,
11009 bool control
, bool shift
, bool alt
, bool meta
)
11010 : wxNotifyEvent( type
, id
)
11017 m_control
= control
;
11022 SetEventObject(obj
);
11026 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
11028 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
11029 int rowOrCol
, int x
, int y
,
11030 bool control
, bool shift
, bool alt
, bool meta
)
11031 : wxNotifyEvent( type
, id
)
11033 m_rowOrCol
= rowOrCol
;
11036 m_control
= control
;
11041 SetEventObject(obj
);
11045 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
11047 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
11048 const wxGridCellCoords
& topLeft
,
11049 const wxGridCellCoords
& bottomRight
,
11050 bool sel
, bool control
,
11051 bool shift
, bool alt
, bool meta
)
11052 : wxNotifyEvent( type
, id
)
11054 m_topLeft
= topLeft
;
11055 m_bottomRight
= bottomRight
;
11057 m_control
= control
;
11062 SetEventObject(obj
);
11066 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
11068 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
11069 wxObject
* obj
, int row
,
11070 int col
, wxControl
* ctrl
)
11071 : wxCommandEvent(type
, id
)
11073 SetEventObject(obj
);
11079 #endif // wxUSE_GRID