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 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1238 wxEvtHandler
* evtHandler
)
1240 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1241 wxDefaultPosition
, wxDefaultSize
,
1244 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1247 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1249 bool resize
= false;
1250 wxSize size
= m_control
->GetSize();
1251 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1253 // check if the checkbox is not too big/small for this cell
1254 wxSize sizeBest
= m_control
->GetBestSize();
1255 if ( !(size
== sizeBest
) )
1257 // reset to default size if it had been made smaller
1263 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1265 // leave 1 pixel margin
1266 size
.x
= size
.y
= minSize
- 2;
1273 m_control
->SetSize(size
);
1276 // position it in the centre of the rectangle (TODO: support alignment?)
1278 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1279 // the checkbox without label still has some space to the right in wxGTK,
1280 // so shift it to the right
1282 #elif defined(__WXMSW__)
1283 // here too, but in other way
1288 int hAlign
= wxALIGN_CENTRE
;
1289 int vAlign
= wxALIGN_CENTRE
;
1291 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1294 if (hAlign
== wxALIGN_LEFT
)
1302 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1304 else if (hAlign
== wxALIGN_RIGHT
)
1306 x
= r
.x
+ r
.width
- size
.x
- 2;
1307 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1309 else if (hAlign
== wxALIGN_CENTRE
)
1311 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1312 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1315 m_control
->Move(x
, y
);
1318 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1320 m_control
->Show(show
);
1324 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1325 CBox()->SetBackgroundColour(colBg
);
1329 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1331 wxASSERT_MSG(m_control
,
1332 wxT("The wxGridCellEditor must be created first!"));
1334 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1336 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1340 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1341 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1344 CBox()->SetValue(m_startValue
);
1348 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1351 wxASSERT_MSG(m_control
,
1352 wxT("The wxGridCellEditor must be created first!"));
1354 bool changed
= false;
1355 bool value
= CBox()->GetValue();
1356 if ( value
!= m_startValue
)
1361 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1362 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1364 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1370 void wxGridCellBoolEditor::Reset()
1372 wxASSERT_MSG(m_control
,
1373 wxT("The wxGridCellEditor must be created first!"));
1375 CBox()->SetValue(m_startValue
);
1378 void wxGridCellBoolEditor::StartingClick()
1380 CBox()->SetValue(!CBox()->GetValue());
1383 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1385 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1387 int keycode
= event
.GetKeyCode();
1400 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1402 int keycode
= event
.GetKeyCode();
1406 CBox()->SetValue(!CBox()->GetValue());
1410 CBox()->SetValue(true);
1414 CBox()->SetValue(false);
1420 // return the value as "1" for true and the empty string for false
1421 wxString
wxGridCellBoolEditor::GetValue() const
1423 bool bSet
= CBox()->GetValue();
1424 return bSet
? _T("1") : wxEmptyString
;
1427 #endif // wxUSE_CHECKBOX
1431 // ----------------------------------------------------------------------------
1432 // wxGridCellChoiceEditor
1433 // ----------------------------------------------------------------------------
1435 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1437 : m_choices(choices
),
1438 m_allowOthers(allowOthers
) { }
1440 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1441 const wxString choices
[],
1443 : m_allowOthers(allowOthers
)
1447 m_choices
.Alloc(count
);
1448 for ( size_t n
= 0; n
< count
; n
++ )
1450 m_choices
.Add(choices
[n
]);
1455 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1457 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1458 editor
->m_allowOthers
= m_allowOthers
;
1459 editor
->m_choices
= m_choices
;
1464 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1466 wxEvtHandler
* evtHandler
)
1468 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1469 wxDefaultPosition
, wxDefaultSize
,
1471 m_allowOthers
? 0 : wxCB_READONLY
);
1473 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1476 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1477 wxGridCellAttr
* attr
)
1479 // as we fill the entire client area, don't do anything here to minimize
1482 // TODO: It doesn't actually fill the client area since the height of a
1483 // combo always defaults to the standard. Until someone has time to
1484 // figure out the right rectangle to paint, just do it the normal way.
1485 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1488 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1490 wxASSERT_MSG(m_control
,
1491 wxT("The wxGridCellEditor must be created first!"));
1493 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1495 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1497 // Don't immediately end if we get a kill focus event within BeginEdit
1499 evtHandler
->SetInSetFocus(true);
1501 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1505 Combo()->SetValue(m_startValue
);
1509 // find the right position, or default to the first if not found
1510 int pos
= Combo()->FindString(m_startValue
);
1511 if (pos
== wxNOT_FOUND
)
1513 Combo()->SetSelection(pos
);
1516 Combo()->SetInsertionPointEnd();
1517 Combo()->SetFocus();
1521 // When dropping down the menu, a kill focus event
1522 // happens after this point, so we can't reset the flag yet.
1523 #if !defined(__WXGTK20__)
1524 evtHandler
->SetInSetFocus(false);
1529 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1532 wxString value
= Combo()->GetValue();
1533 if ( value
== m_startValue
)
1536 grid
->GetTable()->SetValue(row
, col
, value
);
1541 void wxGridCellChoiceEditor::Reset()
1543 Combo()->SetValue(m_startValue
);
1544 Combo()->SetInsertionPointEnd();
1547 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1557 wxStringTokenizer
tk(params
, _T(','));
1558 while ( tk
.HasMoreTokens() )
1560 m_choices
.Add(tk
.GetNextToken());
1564 // return the value in the text control
1565 wxString
wxGridCellChoiceEditor::GetValue() const
1567 return Combo()->GetValue();
1570 #endif // wxUSE_COMBOBOX
1572 // ----------------------------------------------------------------------------
1573 // wxGridCellEditorEvtHandler
1574 // ----------------------------------------------------------------------------
1576 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
1578 // Don't disable the cell if we're just starting to edit it
1583 m_grid
->DisableCellEditControl();
1588 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1590 switch ( event
.GetKeyCode() )
1594 m_grid
->DisableCellEditControl();
1598 m_grid
->GetEventHandler()->ProcessEvent( event
);
1602 case WXK_NUMPAD_ENTER
:
1603 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1604 m_editor
->HandleReturn(event
);
1613 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1615 int row
= m_grid
->GetGridCursorRow();
1616 int col
= m_grid
->GetGridCursorCol();
1617 wxRect rect
= m_grid
->CellToRect( row
, col
);
1619 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
1621 // if cell width is smaller than grid client area, cell is wholly visible
1622 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
1624 switch ( event
.GetKeyCode() )
1629 case WXK_NUMPAD_ENTER
:
1634 if ( wholeCellVisible
)
1636 // no special processing needed...
1641 // do special processing for partly visible cell...
1643 // get the widths of all cells previous to this one
1645 for ( int i
= 0; i
< col
; i
++ )
1647 colXPos
+= m_grid
->GetColSize(i
);
1650 int xUnit
= 1, yUnit
= 1;
1651 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1654 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1658 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
1666 if ( wholeCellVisible
)
1668 // no special processing needed...
1673 // do special processing for partly visible cell...
1676 wxString value
= m_grid
->GetCellValue(row
, col
);
1677 if ( wxEmptyString
!= value
)
1679 // get width of cell CONTENTS (text)
1681 wxFont font
= m_grid
->GetCellFont(row
, col
);
1682 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
1684 // try to RIGHT align the text by scrolling
1685 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
1687 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
1688 // otherwise the last part of the cell content might be hidden below the scroll bar
1689 // FIXME: maybe there is a more suitable correction?
1690 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
1691 if ( textWidth
< 0 )
1697 // get the widths of all cells previous to this one
1699 for ( int i
= 0; i
< col
; i
++ )
1701 colXPos
+= m_grid
->GetColSize(i
);
1704 // and add the (modified) text width of the cell contents
1705 // as we'd like to see the last part of the cell contents
1706 colXPos
+= textWidth
;
1708 int xUnit
= 1, yUnit
= 1;
1709 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1710 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1721 // ----------------------------------------------------------------------------
1722 // wxGridCellWorker is an (almost) empty common base class for
1723 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1724 // ----------------------------------------------------------------------------
1726 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1731 wxGridCellWorker::~wxGridCellWorker()
1735 // ============================================================================
1737 // ============================================================================
1739 // ----------------------------------------------------------------------------
1740 // wxGridCellRenderer
1741 // ----------------------------------------------------------------------------
1743 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1744 wxGridCellAttr
& attr
,
1747 int WXUNUSED(row
), int WXUNUSED(col
),
1750 dc
.SetBackgroundMode( wxSOLID
);
1752 // grey out fields if the grid is disabled
1753 if ( grid
.IsEnabled() )
1757 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1761 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1766 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1769 dc
.SetPen( *wxTRANSPARENT_PEN
);
1770 dc
.DrawRectangle(rect
);
1773 // ----------------------------------------------------------------------------
1774 // wxGridCellStringRenderer
1775 // ----------------------------------------------------------------------------
1777 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
1778 const wxGridCellAttr
& attr
,
1782 dc
.SetBackgroundMode( wxTRANSPARENT
);
1784 // TODO some special colours for attr.IsReadOnly() case?
1786 // different coloured text when the grid is disabled
1787 if ( grid
.IsEnabled() )
1791 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1792 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1796 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1797 dc
.SetTextForeground( attr
.GetTextColour() );
1802 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1803 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
1806 dc
.SetFont( attr
.GetFont() );
1809 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
1811 const wxString
& text
)
1813 wxCoord x
= 0, y
= 0, max_x
= 0;
1814 dc
.SetFont(attr
.GetFont());
1815 wxStringTokenizer
tk(text
, _T('\n'));
1816 while ( tk
.HasMoreTokens() )
1818 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1819 max_x
= wxMax(max_x
, x
);
1822 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1824 return wxSize(max_x
, y
);
1827 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1828 wxGridCellAttr
& attr
,
1832 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1835 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1836 wxGridCellAttr
& attr
,
1838 const wxRect
& rectCell
,
1842 wxRect rect
= rectCell
;
1845 // erase only this cells background, overflow cells should have been erased
1846 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1849 attr
.GetAlignment(&hAlign
, &vAlign
);
1851 int overflowCols
= 0;
1853 if (attr
.GetOverflow())
1855 int cols
= grid
.GetNumberCols();
1856 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1857 int cell_rows
, cell_cols
;
1858 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <= 0
1859 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1861 int i
, c_cols
, c_rows
;
1862 for (i
= col
+cell_cols
; i
< cols
; i
++)
1864 bool is_empty
= true;
1865 for (int j
=row
; j
< row
+ cell_rows
; j
++)
1867 // check w/ anchor cell for multicell block
1868 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1871 if (!grid
.GetTable()->IsEmptyCell(j
+ c_rows
, i
))
1880 rect
.width
+= grid
.GetColSize(i
);
1888 if (rect
.width
>= best_width
)
1892 overflowCols
= i
- col
- cell_cols
+ 1;
1893 if (overflowCols
>= cols
)
1894 overflowCols
= cols
- 1;
1897 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1899 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1901 clip
.x
+= rectCell
.width
;
1902 // draw each overflow cell individually
1903 int col_end
= col
+ cell_cols
+ overflowCols
;
1904 if (col_end
>= grid
.GetNumberCols())
1905 col_end
= grid
.GetNumberCols() - 1;
1906 for (int i
= col
+ cell_cols
; i
<= col_end
; i
++)
1908 clip
.width
= grid
.GetColSize(i
) - 1;
1909 dc
.DestroyClippingRegion();
1910 dc
.SetClippingRegion(clip
);
1912 SetTextColoursAndFont(grid
, attr
, dc
,
1913 grid
.IsInSelection(row
,i
));
1915 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1916 rect
, hAlign
, vAlign
);
1917 clip
.x
+= grid
.GetColSize(i
) - 1;
1923 dc
.DestroyClippingRegion();
1927 // now we only have to draw the text
1928 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1930 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1931 rect
, hAlign
, vAlign
);
1934 // ----------------------------------------------------------------------------
1935 // wxGridCellNumberRenderer
1936 // ----------------------------------------------------------------------------
1938 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
1940 wxGridTableBase
*table
= grid
.GetTable();
1942 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1944 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1948 text
= table
->GetValue(row
, col
);
1954 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1955 wxGridCellAttr
& attr
,
1957 const wxRect
& rectCell
,
1961 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1963 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1965 // draw the text right aligned by default
1967 attr
.GetAlignment(&hAlign
, &vAlign
);
1968 hAlign
= wxALIGN_RIGHT
;
1970 wxRect rect
= rectCell
;
1973 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1976 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1977 wxGridCellAttr
& attr
,
1981 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1984 // ----------------------------------------------------------------------------
1985 // wxGridCellFloatRenderer
1986 // ----------------------------------------------------------------------------
1988 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1991 SetPrecision(precision
);
1994 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1996 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1997 renderer
->m_width
= m_width
;
1998 renderer
->m_precision
= m_precision
;
1999 renderer
->m_format
= m_format
;
2004 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2006 wxGridTableBase
*table
= grid
.GetTable();
2011 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
2013 val
= table
->GetValueAsDouble(row
, col
);
2018 text
= table
->GetValue(row
, col
);
2019 hasDouble
= text
.ToDouble(&val
);
2026 if ( m_width
== -1 )
2028 if ( m_precision
== -1 )
2030 // default width/precision
2031 m_format
= _T("%f");
2035 m_format
.Printf(_T("%%.%df"), m_precision
);
2038 else if ( m_precision
== -1 )
2040 // default precision
2041 m_format
.Printf(_T("%%%d.f"), m_width
);
2045 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
2049 text
.Printf(m_format
, val
);
2052 //else: text already contains the string
2057 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
2058 wxGridCellAttr
& attr
,
2060 const wxRect
& rectCell
,
2064 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2066 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2068 // draw the text right aligned by default
2070 attr
.GetAlignment(&hAlign
, &vAlign
);
2071 hAlign
= wxALIGN_RIGHT
;
2073 wxRect rect
= rectCell
;
2076 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2079 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
2080 wxGridCellAttr
& attr
,
2084 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2087 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
2091 // reset to defaults
2097 wxString tmp
= params
.BeforeFirst(_T(','));
2101 if ( tmp
.ToLong(&width
) )
2103 SetWidth((int)width
);
2107 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
2111 tmp
= params
.AfterFirst(_T(','));
2115 if ( tmp
.ToLong(&precision
) )
2117 SetPrecision((int)precision
);
2121 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
2127 // ----------------------------------------------------------------------------
2128 // wxGridCellBoolRenderer
2129 // ----------------------------------------------------------------------------
2131 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
2133 // FIXME these checkbox size calculations are really ugly...
2135 // between checkmark and box
2136 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2138 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2139 wxGridCellAttr
& WXUNUSED(attr
),
2144 // compute it only once (no locks for MT safeness in GUI thread...)
2145 if ( !ms_sizeCheckMark
.x
)
2147 // get checkbox size
2148 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2149 wxSize size
= checkbox
->GetBestSize();
2150 wxCoord checkSize
= size
.y
+ 2 * wxGRID_CHECKMARK_MARGIN
;
2152 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
2153 #if defined(__WXGTK__) || defined(__WXMOTIF__)
2154 checkSize
-= size
.y
/ 2;
2159 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2162 return ms_sizeCheckMark
;
2165 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2166 wxGridCellAttr
& attr
,
2172 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2174 // draw a check mark in the centre (ignoring alignment - TODO)
2175 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2177 // don't draw outside the cell
2178 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2179 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2181 // and even leave (at least) 1 pixel margin
2182 size
.x
= size
.y
= minSize
- 2;
2185 // draw a border around checkmark
2187 attr
.GetAlignment(&hAlign
, &vAlign
);
2190 if (hAlign
== wxALIGN_CENTRE
)
2192 rectBorder
.x
= rect
.x
+ rect
.width
/ 2 - size
.x
/ 2;
2193 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2194 rectBorder
.width
= size
.x
;
2195 rectBorder
.height
= size
.y
;
2197 else if (hAlign
== wxALIGN_LEFT
)
2199 rectBorder
.x
= rect
.x
+ 2;
2200 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2201 rectBorder
.width
= size
.x
;
2202 rectBorder
.height
= size
.y
;
2204 else if (hAlign
== wxALIGN_RIGHT
)
2206 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2207 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2208 rectBorder
.width
= size
.x
;
2209 rectBorder
.height
= size
.y
;
2213 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2215 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2219 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2220 value
= !( !cellval
|| (cellval
== wxT("0")) );
2225 wxRect rectMark
= rectBorder
;
2228 // MSW DrawCheckMark() is weird (and should probably be changed...)
2229 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/ 2);
2233 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2236 dc
.SetTextForeground(attr
.GetTextColour());
2237 dc
.DrawCheckMark(rectMark
);
2240 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2241 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2242 dc
.DrawRectangle(rectBorder
);
2245 // ----------------------------------------------------------------------------
2247 // ----------------------------------------------------------------------------
2249 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2253 m_isReadOnly
= Unset
;
2258 m_attrkind
= wxGridCellAttr::Cell
;
2260 m_sizeRows
= m_sizeCols
= 1;
2261 m_overflow
= UnsetOverflow
;
2263 SetDefAttr(attrDefault
);
2266 wxGridCellAttr
*wxGridCellAttr::Clone() const
2268 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2270 if ( HasTextColour() )
2271 attr
->SetTextColour(GetTextColour());
2272 if ( HasBackgroundColour() )
2273 attr
->SetBackgroundColour(GetBackgroundColour());
2275 attr
->SetFont(GetFont());
2276 if ( HasAlignment() )
2277 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2279 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2283 attr
->SetRenderer(m_renderer
);
2284 m_renderer
->IncRef();
2288 attr
->SetEditor(m_editor
);
2293 attr
->SetReadOnly();
2295 attr
->SetKind( m_attrkind
);
2300 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2302 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2303 SetTextColour(mergefrom
->GetTextColour());
2304 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2305 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2306 if ( !HasFont() && mergefrom
->HasFont() )
2307 SetFont(mergefrom
->GetFont());
2308 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2311 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2312 SetAlignment(hAlign
, vAlign
);
2314 if ( !HasSize() && mergefrom
->HasSize() )
2315 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2317 // Directly access member functions as GetRender/Editor don't just return
2318 // m_renderer/m_editor
2320 // Maybe add support for merge of Render and Editor?
2321 if (!HasRenderer() && mergefrom
->HasRenderer() )
2323 m_renderer
= mergefrom
->m_renderer
;
2324 m_renderer
->IncRef();
2326 if ( !HasEditor() && mergefrom
->HasEditor() )
2328 m_editor
= mergefrom
->m_editor
;
2331 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2332 SetReadOnly(mergefrom
->IsReadOnly());
2334 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2335 SetOverflow(mergefrom
->GetOverflow());
2337 SetDefAttr(mergefrom
->m_defGridAttr
);
2340 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2342 // The size of a cell is normally 1,1
2344 // If this cell is larger (2,2) then this is the top left cell
2345 // the other cells that will be covered (lower right cells) must be
2346 // set to negative or zero values such that
2347 // row + num_rows of the covered cell points to the larger cell (this cell)
2348 // same goes for the col + num_cols.
2350 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2352 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
2353 !((num_rows
<= 0) && (num_cols
> 0)) ||
2354 !((num_rows
== 0) && (num_cols
== 0))),
2355 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2357 m_sizeRows
= num_rows
;
2358 m_sizeCols
= num_cols
;
2361 const wxColour
& wxGridCellAttr::GetTextColour() const
2363 if (HasTextColour())
2367 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2369 return m_defGridAttr
->GetTextColour();
2373 wxFAIL_MSG(wxT("Missing default cell attribute"));
2374 return wxNullColour
;
2378 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2380 if (HasBackgroundColour())
2384 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2386 return m_defGridAttr
->GetBackgroundColour();
2390 wxFAIL_MSG(wxT("Missing default cell attribute"));
2391 return wxNullColour
;
2395 const wxFont
& wxGridCellAttr::GetFont() const
2401 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2403 return m_defGridAttr
->GetFont();
2407 wxFAIL_MSG(wxT("Missing default cell attribute"));
2412 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2421 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2423 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2427 wxFAIL_MSG(wxT("Missing default cell attribute"));
2431 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2434 *num_rows
= m_sizeRows
;
2436 *num_cols
= m_sizeCols
;
2439 // GetRenderer and GetEditor use a slightly different decision path about
2440 // which attribute to use. If a non-default attr object has one then it is
2441 // used, otherwise the default editor or renderer is fetched from the grid and
2442 // used. It should be the default for the data type of the cell. If it is
2443 // NULL (because the table has a type that the grid does not have in its
2444 // registry), then the grid's default editor or renderer is used.
2446 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2448 wxGridCellRenderer
*renderer
= NULL
;
2450 if ( m_renderer
&& this != m_defGridAttr
)
2452 // use the cells renderer if it has one
2453 renderer
= m_renderer
;
2456 else // no non-default cell renderer
2458 // get default renderer for the data type
2461 // GetDefaultRendererForCell() will do IncRef() for us
2462 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2465 if ( renderer
== NULL
)
2467 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2469 // if we still don't have one then use the grid default
2470 // (no need for IncRef() here neither)
2471 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2473 else // default grid attr
2475 // use m_renderer which we had decided not to use initially
2476 renderer
= m_renderer
;
2483 // we're supposed to always find something
2484 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2489 // same as above, except for s/renderer/editor/g
2490 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2492 wxGridCellEditor
*editor
= NULL
;
2494 if ( m_editor
&& this != m_defGridAttr
)
2496 // use the cells editor if it has one
2500 else // no non default cell editor
2502 // get default editor for the data type
2505 // GetDefaultEditorForCell() will do IncRef() for us
2506 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2509 if ( editor
== NULL
)
2511 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2513 // if we still don't have one then use the grid default
2514 // (no need for IncRef() here neither)
2515 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2517 else // default grid attr
2519 // use m_editor which we had decided not to use initially
2527 // we're supposed to always find something
2528 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2533 // ----------------------------------------------------------------------------
2534 // wxGridCellAttrData
2535 // ----------------------------------------------------------------------------
2537 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2539 int n
= FindIndex(row
, col
);
2540 if ( n
== wxNOT_FOUND
)
2542 // add the attribute
2543 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2547 // free the old attribute
2548 m_attrs
[(size_t)n
].attr
->DecRef();
2552 // change the attribute
2553 m_attrs
[(size_t)n
].attr
= attr
;
2557 // remove this attribute
2558 m_attrs
.RemoveAt((size_t)n
);
2563 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2565 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2567 int n
= FindIndex(row
, col
);
2568 if ( n
!= wxNOT_FOUND
)
2570 attr
= m_attrs
[(size_t)n
].attr
;
2577 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2579 size_t count
= m_attrs
.GetCount();
2580 for ( size_t n
= 0; n
< count
; n
++ )
2582 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2583 wxCoord row
= coords
.GetRow();
2584 if ((size_t)row
>= pos
)
2588 // If rows inserted, include row counter where necessary
2589 coords
.SetRow(row
+ numRows
);
2591 else if (numRows
< 0)
2593 // If rows deleted ...
2594 if ((size_t)row
>= pos
- numRows
)
2596 // ...either decrement row counter (if row still exists)...
2597 coords
.SetRow(row
+ numRows
);
2601 // ...or remove the attribute
2602 // No need to DecRef the attribute itself since this is
2603 // done be wxGridCellWithAttr's destructor!
2604 m_attrs
.RemoveAt(n
);
2613 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2615 size_t count
= m_attrs
.GetCount();
2616 for ( size_t n
= 0; n
< count
; n
++ )
2618 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2619 wxCoord col
= coords
.GetCol();
2620 if ( (size_t)col
>= pos
)
2624 // If rows inserted, include row counter where necessary
2625 coords
.SetCol(col
+ numCols
);
2627 else if (numCols
< 0)
2629 // If rows deleted ...
2630 if ((size_t)col
>= pos
- numCols
)
2632 // ...either decrement row counter (if row still exists)...
2633 coords
.SetCol(col
+ numCols
);
2637 // ...or remove the attribute
2638 // No need to DecRef the attribute itself since this is
2639 // done be wxGridCellWithAttr's destructor!
2640 m_attrs
.RemoveAt(n
);
2649 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2651 size_t count
= m_attrs
.GetCount();
2652 for ( size_t n
= 0; n
< count
; n
++ )
2654 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2655 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2664 // ----------------------------------------------------------------------------
2665 // wxGridRowOrColAttrData
2666 // ----------------------------------------------------------------------------
2668 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2670 size_t count
= m_attrs
.Count();
2671 for ( size_t n
= 0; n
< count
; n
++ )
2673 m_attrs
[n
]->DecRef();
2677 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2679 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2681 int n
= m_rowsOrCols
.Index(rowOrCol
);
2682 if ( n
!= wxNOT_FOUND
)
2684 attr
= m_attrs
[(size_t)n
];
2691 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2693 int i
= m_rowsOrCols
.Index(rowOrCol
);
2694 if ( i
== wxNOT_FOUND
)
2696 // add the attribute
2697 m_rowsOrCols
.Add(rowOrCol
);
2702 size_t n
= (size_t)i
;
2705 // change the attribute
2706 m_attrs
[n
]->DecRef();
2711 // remove this attribute
2712 m_attrs
[n
]->DecRef();
2713 m_rowsOrCols
.RemoveAt(n
);
2714 m_attrs
.RemoveAt(n
);
2719 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2721 size_t count
= m_attrs
.GetCount();
2722 for ( size_t n
= 0; n
< count
; n
++ )
2724 int & rowOrCol
= m_rowsOrCols
[n
];
2725 if ( (size_t)rowOrCol
>= pos
)
2727 if ( numRowsOrCols
> 0 )
2729 // If rows inserted, include row counter where necessary
2730 rowOrCol
+= numRowsOrCols
;
2732 else if ( numRowsOrCols
< 0)
2734 // If rows deleted, either decrement row counter (if row still exists)
2735 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2736 rowOrCol
+= numRowsOrCols
;
2739 m_rowsOrCols
.RemoveAt(n
);
2740 m_attrs
[n
]->DecRef();
2741 m_attrs
.RemoveAt(n
);
2750 // ----------------------------------------------------------------------------
2751 // wxGridCellAttrProvider
2752 // ----------------------------------------------------------------------------
2754 wxGridCellAttrProvider::wxGridCellAttrProvider()
2756 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2759 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2764 void wxGridCellAttrProvider::InitData()
2766 m_data
= new wxGridCellAttrProviderData
;
2769 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2770 wxGridCellAttr::wxAttrKind kind
) const
2772 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2777 case (wxGridCellAttr::Any
):
2778 // Get cached merge attributes.
2779 // Currently not used as no cache implemented as not mutable
2780 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2783 // Basically implement old version.
2784 // Also check merge cache, so we don't have to re-merge every time..
2785 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2786 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2787 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2789 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
2791 // Two or more are non NULL
2792 attr
= new wxGridCellAttr
;
2793 attr
->SetKind(wxGridCellAttr::Merged
);
2795 // Order is important..
2798 attr
->MergeWith(attrcell
);
2803 attr
->MergeWith(attrcol
);
2808 attr
->MergeWith(attrrow
);
2812 // store merge attr if cache implemented
2814 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2818 // one or none is non null return it or null.
2837 case (wxGridCellAttr::Cell
):
2838 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2841 case (wxGridCellAttr::Col
):
2842 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2845 case (wxGridCellAttr::Row
):
2846 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2851 // (wxGridCellAttr::Default):
2852 // (wxGridCellAttr::Merged):
2860 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2866 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2869 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2874 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2877 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2882 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2885 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2889 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2891 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2895 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2899 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2901 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2905 // ----------------------------------------------------------------------------
2906 // wxGridTypeRegistry
2907 // ----------------------------------------------------------------------------
2909 wxGridTypeRegistry::~wxGridTypeRegistry()
2911 size_t count
= m_typeinfo
.Count();
2912 for ( size_t i
= 0; i
< count
; i
++ )
2913 delete m_typeinfo
[i
];
2916 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2917 wxGridCellRenderer
* renderer
,
2918 wxGridCellEditor
* editor
)
2920 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2922 // is it already registered?
2923 int loc
= FindRegisteredDataType(typeName
);
2924 if ( loc
!= wxNOT_FOUND
)
2926 delete m_typeinfo
[loc
];
2927 m_typeinfo
[loc
] = info
;
2931 m_typeinfo
.Add(info
);
2935 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2937 size_t count
= m_typeinfo
.GetCount();
2938 for ( size_t i
= 0; i
< count
; i
++ )
2940 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2949 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2951 int index
= FindRegisteredDataType(typeName
);
2952 if ( index
== wxNOT_FOUND
)
2954 // check whether this is one of the standard ones, in which case
2955 // register it "on the fly"
2957 if ( typeName
== wxGRID_VALUE_STRING
)
2959 RegisterDataType(wxGRID_VALUE_STRING
,
2960 new wxGridCellStringRenderer
,
2961 new wxGridCellTextEditor
);
2964 #endif // wxUSE_TEXTCTRL
2966 if ( typeName
== wxGRID_VALUE_BOOL
)
2968 RegisterDataType(wxGRID_VALUE_BOOL
,
2969 new wxGridCellBoolRenderer
,
2970 new wxGridCellBoolEditor
);
2973 #endif // wxUSE_CHECKBOX
2975 if ( typeName
== wxGRID_VALUE_NUMBER
)
2977 RegisterDataType(wxGRID_VALUE_NUMBER
,
2978 new wxGridCellNumberRenderer
,
2979 new wxGridCellNumberEditor
);
2981 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2983 RegisterDataType(wxGRID_VALUE_FLOAT
,
2984 new wxGridCellFloatRenderer
,
2985 new wxGridCellFloatEditor
);
2988 #endif // wxUSE_TEXTCTRL
2990 if ( typeName
== wxGRID_VALUE_CHOICE
)
2992 RegisterDataType(wxGRID_VALUE_CHOICE
,
2993 new wxGridCellStringRenderer
,
2994 new wxGridCellChoiceEditor
);
2997 #endif // wxUSE_COMBOBOX
3002 // we get here only if just added the entry for this type, so return
3004 index
= m_typeinfo
.GetCount() - 1;
3010 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
3012 int index
= FindDataType(typeName
);
3013 if ( index
== wxNOT_FOUND
)
3015 // the first part of the typename is the "real" type, anything after ':'
3016 // are the parameters for the renderer
3017 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
3018 if ( index
== wxNOT_FOUND
)
3023 wxGridCellRenderer
*renderer
= GetRenderer(index
);
3024 wxGridCellRenderer
*rendererOld
= renderer
;
3025 renderer
= renderer
->Clone();
3026 rendererOld
->DecRef();
3028 wxGridCellEditor
*editor
= GetEditor(index
);
3029 wxGridCellEditor
*editorOld
= editor
;
3030 editor
= editor
->Clone();
3031 editorOld
->DecRef();
3033 // do it even if there are no parameters to reset them to defaults
3034 wxString params
= typeName
.AfterFirst(_T(':'));
3035 renderer
->SetParameters(params
);
3036 editor
->SetParameters(params
);
3038 // register the new typename
3039 RegisterDataType(typeName
, renderer
, editor
);
3041 // we just registered it, it's the last one
3042 index
= m_typeinfo
.GetCount() - 1;
3048 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
3050 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
3057 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
3059 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
3066 // ----------------------------------------------------------------------------
3068 // ----------------------------------------------------------------------------
3070 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
3072 wxGridTableBase::wxGridTableBase()
3074 m_view
= (wxGrid
*) NULL
;
3075 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
3078 wxGridTableBase::~wxGridTableBase()
3080 delete m_attrProvider
;
3083 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
3085 delete m_attrProvider
;
3086 m_attrProvider
= attrProvider
;
3089 bool wxGridTableBase::CanHaveAttributes()
3091 if ( ! GetAttrProvider() )
3093 // use the default attr provider by default
3094 SetAttrProvider(new wxGridCellAttrProvider
);
3100 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
3102 if ( m_attrProvider
)
3103 return m_attrProvider
->GetAttr(row
, col
, kind
);
3105 return (wxGridCellAttr
*)NULL
;
3108 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
3110 if ( m_attrProvider
)
3112 attr
->SetKind(wxGridCellAttr::Cell
);
3113 m_attrProvider
->SetAttr(attr
, row
, col
);
3117 // as we take ownership of the pointer and don't store it, we must
3123 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3125 if ( m_attrProvider
)
3127 attr
->SetKind(wxGridCellAttr::Row
);
3128 m_attrProvider
->SetRowAttr(attr
, row
);
3132 // as we take ownership of the pointer and don't store it, we must
3138 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3140 if ( m_attrProvider
)
3142 attr
->SetKind(wxGridCellAttr::Col
);
3143 m_attrProvider
->SetColAttr(attr
, col
);
3147 // as we take ownership of the pointer and don't store it, we must
3153 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3154 size_t WXUNUSED(numRows
) )
3156 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3161 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3163 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3168 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3169 size_t WXUNUSED(numRows
) )
3171 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3176 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3177 size_t WXUNUSED(numCols
) )
3179 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3184 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3186 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3191 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3192 size_t WXUNUSED(numCols
) )
3194 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3199 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3203 // RD: Starting the rows at zero confuses users,
3204 // no matter how much it makes sense to us geeks.
3210 wxString
wxGridTableBase::GetColLabelValue( int col
)
3212 // default col labels are:
3213 // cols 0 to 25 : A-Z
3214 // cols 26 to 675 : AA-ZZ
3219 for ( n
= 1; ; n
++ )
3221 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
3227 // reverse the string...
3229 for ( i
= 0; i
< n
; i
++ )
3237 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3239 return wxGRID_VALUE_STRING
;
3242 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3243 const wxString
& typeName
)
3245 return typeName
== wxGRID_VALUE_STRING
;
3248 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3250 return CanGetValueAs(row
, col
, typeName
);
3253 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3258 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3263 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3268 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3269 long WXUNUSED(value
) )
3273 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3274 double WXUNUSED(value
) )
3278 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3279 bool WXUNUSED(value
) )
3283 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3284 const wxString
& WXUNUSED(typeName
) )
3289 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3290 const wxString
& WXUNUSED(typeName
),
3291 void* WXUNUSED(value
) )
3295 //////////////////////////////////////////////////////////////////////
3297 // Message class for the grid table to send requests and notifications
3301 wxGridTableMessage::wxGridTableMessage()
3303 m_table
= (wxGridTableBase
*) NULL
;
3309 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3310 int commandInt1
, int commandInt2
)
3314 m_comInt1
= commandInt1
;
3315 m_comInt2
= commandInt2
;
3318 //////////////////////////////////////////////////////////////////////
3320 // A basic grid table for string data. An object of this class will
3321 // created by wxGrid if you don't specify an alternative table class.
3324 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3326 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3328 wxGridStringTable::wxGridStringTable()
3333 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3336 m_data
.Alloc( numRows
);
3339 sa
.Alloc( numCols
);
3340 sa
.Add( wxEmptyString
, numCols
);
3342 m_data
.Add( sa
, numRows
);
3345 wxGridStringTable::~wxGridStringTable()
3349 int wxGridStringTable::GetNumberRows()
3351 return m_data
.GetCount();
3354 int wxGridStringTable::GetNumberCols()
3356 if ( m_data
.GetCount() > 0 )
3357 return m_data
[0].GetCount();
3362 wxString
wxGridStringTable::GetValue( int row
, int col
)
3364 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3366 _T("invalid row or column index in wxGridStringTable") );
3368 return m_data
[row
][col
];
3371 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3373 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3374 _T("invalid row or column index in wxGridStringTable") );
3376 m_data
[row
][col
] = value
;
3379 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3381 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3383 _T("invalid row or column index in wxGridStringTable") );
3385 return (m_data
[row
][col
] == wxEmptyString
);
3388 void wxGridStringTable::Clear()
3391 int numRows
, numCols
;
3393 numRows
= m_data
.GetCount();
3396 numCols
= m_data
[0].GetCount();
3398 for ( row
= 0; row
< numRows
; row
++ )
3400 for ( col
= 0; col
< numCols
; col
++ )
3402 m_data
[row
][col
] = wxEmptyString
;
3408 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3410 size_t curNumRows
= m_data
.GetCount();
3411 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3412 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3414 if ( pos
>= curNumRows
)
3416 return AppendRows( numRows
);
3420 sa
.Alloc( curNumCols
);
3421 sa
.Add( wxEmptyString
, curNumCols
);
3422 m_data
.Insert( sa
, pos
, numRows
);
3426 wxGridTableMessage
msg( this,
3427 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3431 GetView()->ProcessTableMessage( msg
);
3437 bool wxGridStringTable::AppendRows( size_t numRows
)
3439 size_t curNumRows
= m_data
.GetCount();
3440 size_t curNumCols
= ( curNumRows
> 0
3441 ? m_data
[0].GetCount()
3442 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3445 if ( curNumCols
> 0 )
3447 sa
.Alloc( curNumCols
);
3448 sa
.Add( wxEmptyString
, curNumCols
);
3451 m_data
.Add( sa
, numRows
);
3455 wxGridTableMessage
msg( this,
3456 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3459 GetView()->ProcessTableMessage( msg
);
3465 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3467 size_t curNumRows
= m_data
.GetCount();
3469 if ( pos
>= curNumRows
)
3471 wxFAIL_MSG( wxString::Format
3473 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3475 (unsigned long)numRows
,
3476 (unsigned long)curNumRows
3482 if ( numRows
> curNumRows
- pos
)
3484 numRows
= curNumRows
- pos
;
3487 if ( numRows
>= curNumRows
)
3493 m_data
.RemoveAt( pos
, numRows
);
3498 wxGridTableMessage
msg( this,
3499 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3503 GetView()->ProcessTableMessage( msg
);
3509 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3513 size_t curNumRows
= m_data
.GetCount();
3514 size_t curNumCols
= ( curNumRows
> 0
3515 ? m_data
[0].GetCount()
3516 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3518 if ( pos
>= curNumCols
)
3520 return AppendCols( numCols
);
3523 if ( !m_colLabels
.IsEmpty() )
3525 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
3528 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
3529 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
3532 for ( row
= 0; row
< curNumRows
; row
++ )
3534 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3536 m_data
[row
].Insert( wxEmptyString
, col
);
3542 wxGridTableMessage
msg( this,
3543 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3547 GetView()->ProcessTableMessage( msg
);
3553 bool wxGridStringTable::AppendCols( size_t numCols
)
3557 size_t curNumRows
= m_data
.GetCount();
3562 // TODO: something better than this ?
3564 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3569 for ( row
= 0; row
< curNumRows
; row
++ )
3571 m_data
[row
].Add( wxEmptyString
, numCols
);
3576 wxGridTableMessage
msg( this,
3577 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3580 GetView()->ProcessTableMessage( msg
);
3586 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3590 size_t curNumRows
= m_data
.GetCount();
3591 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3592 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3594 if ( pos
>= curNumCols
)
3596 wxFAIL_MSG( wxString::Format
3598 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3600 (unsigned long)numCols
,
3601 (unsigned long)curNumCols
3608 colID
= GetView()->GetColAt( pos
);
3612 if ( numCols
> curNumCols
- colID
)
3614 numCols
= curNumCols
- colID
;
3617 if ( !m_colLabels
.IsEmpty() )
3619 m_colLabels
.RemoveAt( colID
, numCols
);
3622 for ( row
= 0; row
< curNumRows
; row
++ )
3624 if ( numCols
>= curNumCols
)
3626 m_data
[row
].Clear();
3630 m_data
[row
].RemoveAt( colID
, numCols
);
3636 wxGridTableMessage
msg( this,
3637 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3641 GetView()->ProcessTableMessage( msg
);
3647 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3649 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3651 // using default label
3653 return wxGridTableBase::GetRowLabelValue( row
);
3657 return m_rowLabels
[row
];
3661 wxString
wxGridStringTable::GetColLabelValue( int col
)
3663 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3665 // using default label
3667 return wxGridTableBase::GetColLabelValue( col
);
3671 return m_colLabels
[col
];
3675 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3677 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3679 int n
= m_rowLabels
.GetCount();
3682 for ( i
= n
; i
<= row
; i
++ )
3684 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3688 m_rowLabels
[row
] = value
;
3691 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3693 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3695 int n
= m_colLabels
.GetCount();
3698 for ( i
= n
; i
<= col
; i
++ )
3700 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3704 m_colLabels
[col
] = value
;
3708 //////////////////////////////////////////////////////////////////////
3709 //////////////////////////////////////////////////////////////////////
3711 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3713 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3714 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3715 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3716 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3717 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3718 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3719 EVT_CHAR( wxGridRowLabelWindow::OnChar
)
3722 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3724 const wxPoint
&pos
, const wxSize
&size
)
3725 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxFULL_REPAINT_ON_RESIZE
)
3730 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3734 // NO - don't do this because it will set both the x and y origin
3735 // coords to match the parent scrolled window and we just want to
3736 // set the y coord - MB
3738 // m_owner->PrepareDC( dc );
3741 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3742 wxPoint pt
= dc
.GetDeviceOrigin();
3743 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
3745 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3746 m_owner
->DrawRowLabels( dc
, rows
);
3749 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3751 m_owner
->ProcessRowLabelMouseEvent( event
);
3754 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3756 m_owner
->GetEventHandler()->ProcessEvent( event
);
3759 // This seems to be required for wxMotif otherwise the mouse
3760 // cursor must be in the cell edit control to get key events
3762 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3764 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3768 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3770 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3774 void wxGridRowLabelWindow::OnChar( wxKeyEvent
& event
)
3776 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3780 //////////////////////////////////////////////////////////////////////
3782 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3784 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3785 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3786 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3787 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3788 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3789 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3790 EVT_CHAR( wxGridColLabelWindow::OnChar
)
3793 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3795 const wxPoint
&pos
, const wxSize
&size
)
3796 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxFULL_REPAINT_ON_RESIZE
)
3801 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3805 // NO - don't do this because it will set both the x and y origin
3806 // coords to match the parent scrolled window and we just want to
3807 // set the x coord - MB
3809 // m_owner->PrepareDC( dc );
3812 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3813 wxPoint pt
= dc
.GetDeviceOrigin();
3814 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3815 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
3817 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
3819 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3820 m_owner
->DrawColLabels( dc
, cols
);
3823 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3825 m_owner
->ProcessColLabelMouseEvent( event
);
3828 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3830 m_owner
->GetEventHandler()->ProcessEvent( event
);
3833 // This seems to be required for wxMotif otherwise the mouse
3834 // cursor must be in the cell edit control to get key events
3836 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3838 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3842 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3844 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3848 void wxGridColLabelWindow::OnChar( wxKeyEvent
& event
)
3850 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3854 //////////////////////////////////////////////////////////////////////
3856 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3858 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3859 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3860 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3861 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3862 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3863 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3864 EVT_CHAR( wxGridCornerLabelWindow::OnChar
)
3867 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3869 const wxPoint
&pos
, const wxSize
&size
)
3870 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxFULL_REPAINT_ON_RESIZE
)
3875 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3879 int client_height
= 0;
3880 int client_width
= 0;
3881 GetClientSize( &client_width
, &client_height
);
3883 // VZ: any reason for this ifdef? (FIXME)
3888 rect
.SetWidth( client_width
- 2 );
3889 rect
.SetHeight( client_height
- 2 );
3891 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3893 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
3894 dc
.DrawLine( client_width
- 1, client_height
- 1, client_width
- 1, 0 );
3895 dc
.DrawLine( client_width
- 1, client_height
- 1, 0, client_height
- 1 );
3896 dc
.DrawLine( 0, 0, client_width
, 0 );
3897 dc
.DrawLine( 0, 0, 0, client_height
);
3899 dc
.SetPen( *wxWHITE_PEN
);
3900 dc
.DrawLine( 1, 1, client_width
- 1, 1 );
3901 dc
.DrawLine( 1, 1, 1, client_height
- 1 );
3905 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3907 m_owner
->ProcessCornerLabelMouseEvent( event
);
3910 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3912 m_owner
->GetEventHandler()->ProcessEvent(event
);
3915 // This seems to be required for wxMotif otherwise the mouse
3916 // cursor must be in the cell edit control to get key events
3918 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3920 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3924 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3926 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3930 void wxGridCornerLabelWindow::OnChar( wxKeyEvent
& event
)
3932 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3936 //////////////////////////////////////////////////////////////////////
3938 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3940 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3941 EVT_PAINT( wxGridWindow::OnPaint
)
3942 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3943 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3944 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3945 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3946 EVT_CHAR( wxGridWindow::OnChar
)
3947 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3948 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3949 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3952 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3953 wxGridRowLabelWindow
*rowLblWin
,
3954 wxGridColLabelWindow
*colLblWin
,
3957 const wxSize
&size
)
3959 parent
, id
, pos
, size
,
3960 wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
| wxFULL_REPAINT_ON_RESIZE
,
3961 wxT("grid window") )
3964 m_rowLabelWin
= rowLblWin
;
3965 m_colLabelWin
= colLblWin
;
3968 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3970 wxPaintDC
dc( this );
3971 m_owner
->PrepareDC( dc
);
3972 wxRegion reg
= GetUpdateRegion();
3973 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
3974 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
3976 #if WXGRID_DRAW_LINES
3977 m_owner
->DrawAllGridLines( dc
, reg
);
3980 m_owner
->DrawGridSpace( dc
);
3981 m_owner
->DrawHighlight( dc
, dirtyCells
);
3984 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3986 wxWindow::ScrollWindow( dx
, dy
, rect
);
3987 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3988 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3991 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3993 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
3996 m_owner
->ProcessGridCellMouseEvent( event
);
3999 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
4001 m_owner
->GetEventHandler()->ProcessEvent( event
);
4004 // This seems to be required for wxMotif/wxGTK otherwise the mouse
4005 // cursor must be in the cell edit control to get key events
4007 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
4009 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4013 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
4015 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4019 void wxGridWindow::OnChar( wxKeyEvent
& event
)
4021 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4025 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
4029 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
4031 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4035 //////////////////////////////////////////////////////////////////////
4037 // Internal Helper function for computing row or column from some
4038 // (unscrolled) coordinate value, using either
4039 // m_defaultRowHeight/m_defaultColWidth or binary search on array
4040 // of m_rowBottoms/m_ColRights to speed up the search!
4042 // Internal helper macros for simpler use of that function
4044 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
4045 const wxArrayInt
& BorderArray
, int nMax
,
4048 #define internalXToCol(x) XToCol(x, true)
4049 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
4050 m_minAcceptableRowHeight, \
4051 m_rowBottoms, m_numRows, true)
4053 /////////////////////////////////////////////////////////////////////
4055 #if wxUSE_EXTENDED_RTTI
4056 WX_DEFINE_FLAGS( wxGridStyle
)
4058 wxBEGIN_FLAGS( wxGridStyle
)
4059 // new style border flags, we put them first to
4060 // use them for streaming out
4061 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4062 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4063 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4064 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4065 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4066 wxFLAGS_MEMBER(wxBORDER_NONE
)
4068 // old style border flags
4069 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4070 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4071 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4072 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4073 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4074 wxFLAGS_MEMBER(wxBORDER
)
4076 // standard window styles
4077 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4078 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4079 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4080 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4081 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4082 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4083 wxFLAGS_MEMBER(wxVSCROLL
)
4084 wxFLAGS_MEMBER(wxHSCROLL
)
4086 wxEND_FLAGS( wxGridStyle
)
4088 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4090 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4091 wxHIDE_PROPERTY( Children
)
4092 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4093 wxEND_PROPERTIES_TABLE()
4095 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4096 wxEND_HANDLERS_TABLE()
4098 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4101 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
4104 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4107 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4108 EVT_PAINT( wxGrid::OnPaint
)
4109 EVT_SIZE( wxGrid::OnSize
)
4110 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
4111 EVT_KEY_UP( wxGrid::OnKeyUp
)
4112 EVT_CHAR ( wxGrid::OnChar
)
4113 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
4118 // in order to make sure that a size event is not
4119 // trigerred in a unfinished state
4120 m_cornerLabelWin
= NULL
;
4121 m_rowLabelWin
= NULL
;
4122 m_colLabelWin
= NULL
;
4126 wxGrid::wxGrid( wxWindow
*parent
,
4131 const wxString
& name
)
4132 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
4133 m_colMinWidths(GRID_HASH_SIZE
),
4134 m_rowMinHeights(GRID_HASH_SIZE
)
4137 SetBestFittingSize(size
);
4140 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
4141 const wxPoint
& pos
, const wxSize
& size
,
4142 long style
, const wxString
& name
)
4144 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
4145 style
| wxWANTS_CHARS
, name
))
4148 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4149 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4152 SetBestFittingSize(size
);
4159 // Must do this or ~wxScrollHelper will pop the wrong event handler
4160 SetTargetWindow(this);
4162 wxSafeDecRef(m_defaultCellAttr
);
4164 #ifdef DEBUG_ATTR_CACHE
4165 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4166 wxPrintf(_T("wxGrid attribute cache statistics: "
4167 "total: %u, hits: %u (%u%%)\n"),
4168 total
, gs_nAttrCacheHits
,
4169 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4175 delete m_typeRegistry
;
4180 // ----- internal init and update functions
4183 // NOTE: If using the default visual attributes works everywhere then this can
4184 // be removed as well as the #else cases below.
4185 #define _USE_VISATTR 0
4187 void wxGrid::Create()
4189 // set to true by CreateGrid
4192 // create the type registry
4193 m_typeRegistry
= new wxGridTypeRegistry
;
4196 m_table
= (wxGridTableBase
*) NULL
;
4199 m_cellEditCtrlEnabled
= false;
4201 m_defaultCellAttr
= new wxGridCellAttr();
4203 // Set default cell attributes
4204 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4205 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4206 m_defaultCellAttr
->SetFont(GetFont());
4207 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4208 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4209 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4212 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4213 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4215 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4216 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4219 m_defaultCellAttr
->SetTextColour(
4220 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4221 m_defaultCellAttr
->SetBackgroundColour(
4222 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4227 m_currentCellCoords
= wxGridNoCellCoords
;
4229 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4230 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4232 // subwindow components that make up the wxGrid
4233 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4238 m_colLabelWin
= new wxGridColLabelWindow( this,
4243 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4248 m_gridWin
= new wxGridWindow( this,
4255 SetTargetWindow( m_gridWin
);
4258 wxColour gfg
= gva
.colFg
;
4259 wxColour gbg
= gva
.colBg
;
4260 wxColour lfg
= lva
.colFg
;
4261 wxColour lbg
= lva
.colBg
;
4263 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4264 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4265 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4266 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4269 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4270 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4271 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4272 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4273 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4274 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4276 m_gridWin
->SetOwnForegroundColour(gfg
);
4277 m_gridWin
->SetOwnBackgroundColour(gbg
);
4282 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4283 wxGrid::wxGridSelectionModes selmode
)
4285 wxCHECK_MSG( !m_created
,
4287 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4289 m_numRows
= numRows
;
4290 m_numCols
= numCols
;
4292 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4293 m_table
->SetView( this );
4295 m_selection
= new wxGridSelection( this, selmode
);
4304 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4306 wxCHECK_RET( m_created
,
4307 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4309 m_selection
->SetSelectionMode( selmode
);
4312 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4314 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4315 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4317 return m_selection
->GetSelectionMode();
4320 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4321 wxGrid::wxGridSelectionModes selmode
)
4325 // stop all processing
4330 wxGridTableBase
*t
= m_table
;
4345 m_numRows
= table
->GetNumberRows();
4346 m_numCols
= table
->GetNumberCols();
4349 m_table
->SetView( this );
4350 m_ownTable
= takeOwnership
;
4351 m_selection
= new wxGridSelection( this, selmode
);
4363 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4364 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4366 if ( m_rowLabelWin
)
4368 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4372 m_labelBackgroundColour
= *wxWHITE
;
4375 m_labelTextColour
= *wxBLACK
;
4378 m_attrCache
.row
= -1;
4379 m_attrCache
.col
= -1;
4380 m_attrCache
.attr
= NULL
;
4382 // TODO: something better than this ?
4384 m_labelFont
= this->GetFont();
4385 m_labelFont
.SetWeight( wxBOLD
);
4387 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4388 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4390 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4391 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4392 m_colLabelTextOrientation
= wxHORIZONTAL
;
4394 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4395 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4397 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4398 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4400 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4401 m_defaultRowHeight
+= 8;
4403 m_defaultRowHeight
+= 4;
4406 m_gridLineColour
= wxColour( 192,192,192 );
4407 m_gridLinesEnabled
= true;
4408 m_cellHighlightColour
= *wxBLACK
;
4409 m_cellHighlightPenWidth
= 2;
4410 m_cellHighlightROPenWidth
= 1;
4412 m_canDragColMove
= false;
4414 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4415 m_winCapture
= (wxWindow
*)NULL
;
4416 m_canDragRowSize
= true;
4417 m_canDragColSize
= true;
4418 m_canDragGridSize
= true;
4419 m_canDragCell
= false;
4421 m_dragRowOrCol
= -1;
4422 m_isDragging
= false;
4423 m_startDragPos
= wxDefaultPosition
;
4425 m_waitForSlowClick
= false;
4427 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4428 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4430 m_currentCellCoords
= wxGridNoCellCoords
;
4434 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4435 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4437 m_editable
= true; // default for whole grid
4439 m_inOnKeyDown
= false;
4445 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4446 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4449 // ----------------------------------------------------------------------------
4450 // the idea is to call these functions only when necessary because they create
4451 // quite big arrays which eat memory mostly unnecessary - in particular, if
4452 // default widths/heights are used for all rows/columns, we may not use these
4455 // with some extra code, it should be possible to only store the
4456 // widths/heights different from default ones but this will be done later...
4457 // ----------------------------------------------------------------------------
4459 void wxGrid::InitRowHeights()
4461 m_rowHeights
.Empty();
4462 m_rowBottoms
.Empty();
4464 m_rowHeights
.Alloc( m_numRows
);
4465 m_rowBottoms
.Alloc( m_numRows
);
4469 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4471 for ( int i
= 0; i
< m_numRows
; i
++ )
4473 rowBottom
+= m_defaultRowHeight
;
4474 m_rowBottoms
.Add( rowBottom
);
4478 void wxGrid::InitColWidths()
4480 m_colWidths
.Empty();
4481 m_colRights
.Empty();
4483 m_colWidths
.Alloc( m_numCols
);
4484 m_colRights
.Alloc( m_numCols
);
4487 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4489 for ( int i
= 0; i
< m_numCols
; i
++ )
4491 colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
4492 m_colRights
.Add( colRight
);
4496 int wxGrid::GetColWidth(int col
) const
4498 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4501 int wxGrid::GetColLeft(int col
) const
4503 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
4504 : m_colRights
[col
] - m_colWidths
[col
];
4507 int wxGrid::GetColRight(int col
) const
4509 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
4513 int wxGrid::GetRowHeight(int row
) const
4515 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4518 int wxGrid::GetRowTop(int row
) const
4520 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4521 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4524 int wxGrid::GetRowBottom(int row
) const
4526 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4527 : m_rowBottoms
[row
];
4530 void wxGrid::CalcDimensions()
4533 GetClientSize( &cw
, &ch
);
4535 if ( m_rowLabelWin
->IsShown() )
4536 cw
-= m_rowLabelWidth
;
4537 if ( m_colLabelWin
->IsShown() )
4538 ch
-= m_colLabelHeight
;
4541 int w
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) + m_extraWidth
+ 1 : 0;
4542 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4544 // take into account editor if shown
4545 if ( IsCellEditControlShown() )
4548 int r
= m_currentCellCoords
.GetRow();
4549 int c
= m_currentCellCoords
.GetCol();
4550 int x
= GetColLeft(c
);
4551 int y
= GetRowTop(r
);
4553 // how big is the editor
4554 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4555 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4556 editor
->GetControl()->GetSize(&w2
, &h2
);
4567 // preserve (more or less) the previous position
4569 GetViewStart( &x
, &y
);
4571 // ensure the position is valid for the new scroll ranges
4573 x
= wxMax( w
- 1, 0 );
4575 y
= wxMax( h
- 1, 0 );
4577 // do set scrollbar parameters
4578 SetScrollbars( m_scrollLineX
, m_scrollLineY
,
4579 GetScrollX(w
), GetScrollY(h
), x
, y
,
4580 GetBatchCount() != 0);
4582 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4583 // still must reposition the children
4587 void wxGrid::CalcWindowSizes()
4589 // escape if the window is has not been fully created yet
4591 if ( m_cornerLabelWin
== NULL
)
4595 GetClientSize( &cw
, &ch
);
4597 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4598 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4600 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4601 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
- m_rowLabelWidth
, m_colLabelHeight
);
4603 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4604 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
- m_colLabelHeight
);
4606 if ( m_gridWin
&& m_gridWin
->IsShown() )
4607 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
- m_rowLabelWidth
, ch
- m_colLabelHeight
);
4610 // this is called when the grid table sends a message
4611 // to indicate that it has been redimensioned
4613 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4616 bool result
= false;
4618 // Clear the attribute cache as the attribute might refer to a different
4619 // cell than stored in the cache after adding/removing rows/columns.
4622 // By the same reasoning, the editor should be dismissed if columns are
4623 // added or removed. And for consistency, it should IMHO always be
4624 // removed, not only if the cell "underneath" it actually changes.
4625 // For now, I intentionally do not save the editor's content as the
4626 // cell it might want to save that stuff to might no longer exist.
4627 HideCellEditControl();
4630 // if we were using the default widths/heights so far, we must change them
4632 if ( m_colWidths
.IsEmpty() )
4637 if ( m_rowHeights
.IsEmpty() )
4643 switch ( msg
.GetId() )
4645 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4647 size_t pos
= msg
.GetCommandInt();
4648 int numRows
= msg
.GetCommandInt2();
4650 m_numRows
+= numRows
;
4652 if ( !m_rowHeights
.IsEmpty() )
4654 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4655 m_rowBottoms
.Insert( 0, pos
, numRows
);
4659 bottom
= m_rowBottoms
[pos
- 1];
4661 for ( i
= pos
; i
< m_numRows
; i
++ )
4663 bottom
+= m_rowHeights
[i
];
4664 m_rowBottoms
[i
] = bottom
;
4668 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4670 // if we have just inserted cols into an empty grid the current
4671 // cell will be undefined...
4673 SetCurrentCell( 0, 0 );
4677 m_selection
->UpdateRows( pos
, numRows
);
4678 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4680 attrProvider
->UpdateAttrRows( pos
, numRows
);
4682 if ( !GetBatchCount() )
4685 m_rowLabelWin
->Refresh();
4691 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4693 int numRows
= msg
.GetCommandInt();
4694 int oldNumRows
= m_numRows
;
4695 m_numRows
+= numRows
;
4697 if ( !m_rowHeights
.IsEmpty() )
4699 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4700 m_rowBottoms
.Add( 0, numRows
);
4703 if ( oldNumRows
> 0 )
4704 bottom
= m_rowBottoms
[oldNumRows
- 1];
4706 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4708 bottom
+= m_rowHeights
[i
];
4709 m_rowBottoms
[i
] = bottom
;
4713 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4715 // if we have just inserted cols into an empty grid the current
4716 // cell will be undefined...
4718 SetCurrentCell( 0, 0 );
4721 if ( !GetBatchCount() )
4724 m_rowLabelWin
->Refresh();
4730 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4732 size_t pos
= msg
.GetCommandInt();
4733 int numRows
= msg
.GetCommandInt2();
4734 m_numRows
-= numRows
;
4736 if ( !m_rowHeights
.IsEmpty() )
4738 m_rowHeights
.RemoveAt( pos
, numRows
);
4739 m_rowBottoms
.RemoveAt( pos
, numRows
);
4742 for ( i
= 0; i
< m_numRows
; i
++ )
4744 h
+= m_rowHeights
[i
];
4745 m_rowBottoms
[i
] = h
;
4751 m_currentCellCoords
= wxGridNoCellCoords
;
4755 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4756 m_currentCellCoords
.Set( 0, 0 );
4760 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4761 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4764 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4766 // ifdef'd out following patch from Paul Gammans
4768 // No need to touch column attributes, unless we
4769 // removed _all_ rows, in this case, we remove
4770 // all column attributes.
4771 // I hate to do this here, but the
4772 // needed data is not available inside UpdateAttrRows.
4773 if ( !GetNumberRows() )
4774 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4778 if ( !GetBatchCount() )
4781 m_rowLabelWin
->Refresh();
4787 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4789 size_t pos
= msg
.GetCommandInt();
4790 int numCols
= msg
.GetCommandInt2();
4791 m_numCols
+= numCols
;
4793 if ( !m_colAt
.IsEmpty() )
4795 //Shift the column IDs
4797 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
4799 if ( m_colAt
[i
] >= (int)pos
)
4800 m_colAt
[i
] += numCols
;
4803 m_colAt
.Insert( pos
, pos
, numCols
);
4805 //Set the new columns' positions
4806 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
4812 if ( !m_colWidths
.IsEmpty() )
4814 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4815 m_colRights
.Insert( 0, pos
, numCols
);
4819 right
= m_colRights
[GetColAt( pos
- 1 )];
4822 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
4824 i
= GetColAt( colPos
);
4826 right
+= m_colWidths
[i
];
4827 m_colRights
[i
] = right
;
4831 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4833 // if we have just inserted cols into an empty grid the current
4834 // cell will be undefined...
4836 SetCurrentCell( 0, 0 );
4840 m_selection
->UpdateCols( pos
, numCols
);
4841 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4843 attrProvider
->UpdateAttrCols( pos
, numCols
);
4844 if ( !GetBatchCount() )
4847 m_colLabelWin
->Refresh();
4853 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4855 int numCols
= msg
.GetCommandInt();
4856 int oldNumCols
= m_numCols
;
4857 m_numCols
+= numCols
;
4859 if ( !m_colAt
.IsEmpty() )
4861 m_colAt
.Add( 0, numCols
);
4863 //Set the new columns' positions
4865 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4871 if ( !m_colWidths
.IsEmpty() )
4873 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4874 m_colRights
.Add( 0, numCols
);
4877 if ( oldNumCols
> 0 )
4878 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
4881 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
4883 i
= GetColAt( colPos
);
4885 right
+= m_colWidths
[i
];
4886 m_colRights
[i
] = right
;
4890 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4892 // if we have just inserted cols into an empty grid the current
4893 // cell will be undefined...
4895 SetCurrentCell( 0, 0 );
4897 if ( !GetBatchCount() )
4900 m_colLabelWin
->Refresh();
4906 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4908 size_t pos
= msg
.GetCommandInt();
4909 int numCols
= msg
.GetCommandInt2();
4910 m_numCols
-= numCols
;
4912 if ( !m_colAt
.IsEmpty() )
4914 int colID
= GetColAt( pos
);
4916 m_colAt
.RemoveAt( pos
, numCols
);
4918 //Shift the column IDs
4920 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
4922 if ( m_colAt
[colPos
] > colID
)
4923 m_colAt
[colPos
] -= numCols
;
4927 if ( !m_colWidths
.IsEmpty() )
4929 m_colWidths
.RemoveAt( pos
, numCols
);
4930 m_colRights
.RemoveAt( pos
, numCols
);
4934 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
4936 i
= GetColAt( colPos
);
4938 w
+= m_colWidths
[i
];
4945 m_currentCellCoords
= wxGridNoCellCoords
;
4949 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4950 m_currentCellCoords
.Set( 0, 0 );
4954 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4955 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4958 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4960 // ifdef'd out following patch from Paul Gammans
4962 // No need to touch row attributes, unless we
4963 // removed _all_ columns, in this case, we remove
4964 // all row attributes.
4965 // I hate to do this here, but the
4966 // needed data is not available inside UpdateAttrCols.
4967 if ( !GetNumberCols() )
4968 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4972 if ( !GetBatchCount() )
4975 m_colLabelWin
->Refresh();
4982 if (result
&& !GetBatchCount() )
4983 m_gridWin
->Refresh();
4988 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4990 wxRegionIterator
iter( reg
);
4993 wxArrayInt rowlabels
;
5000 // TODO: remove this when we can...
5001 // There is a bug in wxMotif that gives garbage update
5002 // rectangles if you jump-scroll a long way by clicking the
5003 // scrollbar with middle button. This is a work-around
5005 #if defined(__WXMOTIF__)
5007 m_gridWin
->GetClientSize( &cw
, &ch
);
5008 if ( r
.GetTop() > ch
)
5010 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5013 // logical bounds of update region
5016 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
5017 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
5019 // find the row labels within these bounds
5022 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5024 if ( GetRowBottom(row
) < top
)
5027 if ( GetRowTop(row
) > bottom
)
5030 rowlabels
.Add( row
);
5039 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
5041 wxRegionIterator
iter( reg
);
5044 wxArrayInt colLabels
;
5051 // TODO: remove this when we can...
5052 // There is a bug in wxMotif that gives garbage update
5053 // rectangles if you jump-scroll a long way by clicking the
5054 // scrollbar with middle button. This is a work-around
5056 #if defined(__WXMOTIF__)
5058 m_gridWin
->GetClientSize( &cw
, &ch
);
5059 if ( r
.GetLeft() > cw
)
5061 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5064 // logical bounds of update region
5067 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
5068 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
5070 // find the cells within these bounds
5074 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5076 col
= GetColAt( colPos
);
5078 if ( GetColRight(col
) < left
)
5081 if ( GetColLeft(col
) > right
)
5084 colLabels
.Add( col
);
5093 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
5095 wxRegionIterator
iter( reg
);
5098 wxGridCellCoordsArray cellsExposed
;
5100 int left
, top
, right
, bottom
;
5105 // TODO: remove this when we can...
5106 // There is a bug in wxMotif that gives garbage update
5107 // rectangles if you jump-scroll a long way by clicking the
5108 // scrollbar with middle button. This is a work-around
5110 #if defined(__WXMOTIF__)
5112 m_gridWin
->GetClientSize( &cw
, &ch
);
5113 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5114 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5115 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5116 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5119 // logical bounds of update region
5121 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5122 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5124 // find the cells within these bounds
5127 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5129 if ( GetRowBottom(row
) <= top
)
5132 if ( GetRowTop(row
) > bottom
)
5136 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5138 col
= GetColAt( colPos
);
5140 if ( GetColRight(col
) <= left
)
5143 if ( GetColLeft(col
) > right
)
5146 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
5153 return cellsExposed
;
5157 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5160 wxPoint
pos( event
.GetPosition() );
5161 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5163 if ( event
.Dragging() )
5167 m_isDragging
= true;
5168 m_rowLabelWin
->CaptureMouse();
5171 if ( event
.LeftIsDown() )
5173 switch ( m_cursorMode
)
5175 case WXGRID_CURSOR_RESIZE_ROW
:
5177 int cw
, ch
, left
, dummy
;
5178 m_gridWin
->GetClientSize( &cw
, &ch
);
5179 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5181 wxClientDC
dc( m_gridWin
);
5184 GetRowTop(m_dragRowOrCol
) +
5185 GetRowMinimalHeight(m_dragRowOrCol
) );
5186 dc
.SetLogicalFunction(wxINVERT
);
5187 if ( m_dragLastPos
>= 0 )
5189 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5191 dc
.DrawLine( left
, y
, left
+cw
, y
);
5196 case WXGRID_CURSOR_SELECT_ROW
:
5198 if ( (row
= YToRow( y
)) >= 0 )
5202 m_selection
->SelectRow( row
,
5203 event
.ControlDown(),
5212 // default label to suppress warnings about "enumeration value
5213 // 'xxx' not handled in switch
5221 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5226 if (m_rowLabelWin
->HasCapture())
5227 m_rowLabelWin
->ReleaseMouse();
5228 m_isDragging
= false;
5231 // ------------ Entering or leaving the window
5233 if ( event
.Entering() || event
.Leaving() )
5235 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5238 // ------------ Left button pressed
5240 else if ( event
.LeftDown() )
5242 // don't send a label click event for a hit on the
5243 // edge of the row label - this is probably the user
5244 // wanting to resize the row
5246 if ( YToEdgeOfRow(y
) < 0 )
5250 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5252 if ( !event
.ShiftDown() && !event
.CmdDown() )
5256 if ( event
.ShiftDown() )
5258 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5261 GetNumberCols() - 1,
5262 event
.ControlDown(),
5269 m_selection
->SelectRow( row
,
5270 event
.ControlDown(),
5277 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5282 // starting to drag-resize a row
5283 if ( CanDragRowSize() )
5284 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5288 // ------------ Left double click
5290 else if (event
.LeftDClick() )
5292 row
= YToEdgeOfRow(y
);
5297 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5299 // no default action at the moment
5304 // adjust row height depending on label text
5305 AutoSizeRowLabelSize( row
);
5307 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5312 // ------------ Left button released
5314 else if ( event
.LeftUp() )
5316 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5318 DoEndDragResizeRow();
5320 // Note: we are ending the event *after* doing
5321 // default processing in this case
5323 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5326 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5330 // ------------ Right button down
5332 else if ( event
.RightDown() )
5336 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5338 // no default action at the moment
5342 // ------------ Right double click
5344 else if ( event
.RightDClick() )
5348 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5350 // no default action at the moment
5354 // ------------ No buttons down and mouse moving
5356 else if ( event
.Moving() )
5358 m_dragRowOrCol
= YToEdgeOfRow( y
);
5359 if ( m_dragRowOrCol
>= 0 )
5361 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5363 // don't capture the mouse yet
5364 if ( CanDragRowSize() )
5365 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5368 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5370 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5375 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5378 wxPoint
pos( event
.GetPosition() );
5379 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5381 if ( event
.Dragging() )
5385 m_isDragging
= true;
5386 m_colLabelWin
->CaptureMouse();
5388 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
)
5389 m_dragRowOrCol
= XToCol( x
);
5392 if ( event
.LeftIsDown() )
5394 switch ( m_cursorMode
)
5396 case WXGRID_CURSOR_RESIZE_COL
:
5398 int cw
, ch
, dummy
, top
;
5399 m_gridWin
->GetClientSize( &cw
, &ch
);
5400 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5402 wxClientDC
dc( m_gridWin
);
5405 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5406 GetColMinimalWidth(m_dragRowOrCol
));
5407 dc
.SetLogicalFunction(wxINVERT
);
5408 if ( m_dragLastPos
>= 0 )
5410 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5412 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5417 case WXGRID_CURSOR_SELECT_COL
:
5419 if ( (col
= XToCol( x
)) >= 0 )
5423 m_selection
->SelectCol( col
,
5424 event
.ControlDown(),
5433 case WXGRID_CURSOR_MOVE_COL
:
5436 m_moveToCol
= GetColAt( 0 );
5438 m_moveToCol
= XToCol( x
);
5442 if ( m_moveToCol
< 0 )
5443 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5445 markerX
= GetColLeft( m_moveToCol
);
5447 if ( markerX
!= m_dragLastPos
)
5449 wxClientDC
dc( m_colLabelWin
);
5452 m_colLabelWin
->GetClientSize( &cw
, &ch
);
5456 //Clean up the last indicator
5457 if ( m_dragLastPos
>= 0 )
5459 wxPen
pen( m_colLabelWin
->GetBackgroundColour(), 2 );
5461 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
5462 dc
.SetPen(wxNullPen
);
5464 if ( XToCol( m_dragLastPos
) != -1 )
5465 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
5468 //Moving to the same place? Don't draw a marker
5469 if ( (m_moveToCol
== m_dragRowOrCol
)
5470 || (GetColPos( m_moveToCol
) == GetColPos( m_dragRowOrCol
) + 1)
5471 || (m_moveToCol
< 0 && m_dragRowOrCol
== GetColAt( m_numCols
- 1 )))
5478 wxPen
pen( *wxBLUE
, 2 );
5481 dc
.DrawLine( markerX
, 0, markerX
, ch
);
5483 dc
.SetPen(wxNullPen
);
5485 m_dragLastPos
= markerX
- 1;
5490 // default label to suppress warnings about "enumeration value
5491 // 'xxx' not handled in switch
5499 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5504 if (m_colLabelWin
->HasCapture())
5505 m_colLabelWin
->ReleaseMouse();
5506 m_isDragging
= false;
5509 // ------------ Entering or leaving the window
5511 if ( event
.Entering() || event
.Leaving() )
5513 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5516 // ------------ Left button pressed
5518 else if ( event
.LeftDown() )
5520 // don't send a label click event for a hit on the
5521 // edge of the col label - this is probably the user
5522 // wanting to resize the col
5524 if ( XToEdgeOfCol(x
) < 0 )
5528 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5530 if ( m_canDragColMove
)
5532 //Show button as pressed
5533 wxClientDC
dc( m_colLabelWin
);
5534 int colLeft
= GetColLeft( col
);
5535 int colRight
= GetColRight( col
) - 1;
5536 dc
.SetPen( wxPen( m_colLabelWin
->GetBackgroundColour(), 1 ) );
5537 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
5538 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
5540 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, m_colLabelWin
);
5544 if ( !event
.ShiftDown() && !event
.CmdDown() )
5548 if ( event
.ShiftDown() )
5550 m_selection
->SelectBlock( 0,
5551 m_currentCellCoords
.GetCol(),
5552 GetNumberRows() - 1, col
,
5553 event
.ControlDown(),
5560 m_selection
->SelectCol( col
,
5561 event
.ControlDown(),
5568 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5574 // starting to drag-resize a col
5576 if ( CanDragColSize() )
5577 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5581 // ------------ Left double click
5583 if ( event
.LeftDClick() )
5585 col
= XToEdgeOfCol(x
);
5590 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5592 // no default action at the moment
5597 // adjust column width depending on label text
5598 AutoSizeColLabelSize( col
);
5600 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5605 // ------------ Left button released
5607 else if ( event
.LeftUp() )
5609 switch ( m_cursorMode
)
5611 case WXGRID_CURSOR_RESIZE_COL
:
5612 DoEndDragResizeCol();
5614 // Note: we are ending the event *after* doing
5615 // default processing in this case
5617 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5620 case WXGRID_CURSOR_MOVE_COL
:
5623 SendEvent( wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
, event
);
5626 case WXGRID_CURSOR_SELECT_COL
:
5627 case WXGRID_CURSOR_SELECT_CELL
:
5628 case WXGRID_CURSOR_RESIZE_ROW
:
5629 case WXGRID_CURSOR_SELECT_ROW
:
5630 // nothing to do (?)
5634 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5638 // ------------ Right button down
5640 else if ( event
.RightDown() )
5644 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5646 // no default action at the moment
5650 // ------------ Right double click
5652 else if ( event
.RightDClick() )
5656 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5658 // no default action at the moment
5662 // ------------ No buttons down and mouse moving
5664 else if ( event
.Moving() )
5666 m_dragRowOrCol
= XToEdgeOfCol( x
);
5667 if ( m_dragRowOrCol
>= 0 )
5669 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5671 // don't capture the cursor yet
5672 if ( CanDragColSize() )
5673 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5676 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5678 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5683 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5685 if ( event
.LeftDown() )
5687 // indicate corner label by having both row and
5690 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5695 else if ( event
.LeftDClick() )
5697 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5699 else if ( event
.RightDown() )
5701 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5703 // no default action at the moment
5706 else if ( event
.RightDClick() )
5708 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5710 // no default action at the moment
5715 void wxGrid::ChangeCursorMode(CursorMode mode
,
5720 static const wxChar
*cursorModes
[] =
5730 wxLogTrace(_T("grid"),
5731 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5732 win
== m_colLabelWin
? _T("colLabelWin")
5733 : win
? _T("rowLabelWin")
5735 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5738 if ( mode
== m_cursorMode
&&
5739 win
== m_winCapture
&&
5740 captureMouse
== (m_winCapture
!= NULL
))
5745 // by default use the grid itself
5751 if (m_winCapture
->HasCapture())
5752 m_winCapture
->ReleaseMouse();
5753 m_winCapture
= (wxWindow
*)NULL
;
5756 m_cursorMode
= mode
;
5758 switch ( m_cursorMode
)
5760 case WXGRID_CURSOR_RESIZE_ROW
:
5761 win
->SetCursor( m_rowResizeCursor
);
5764 case WXGRID_CURSOR_RESIZE_COL
:
5765 win
->SetCursor( m_colResizeCursor
);
5768 case WXGRID_CURSOR_MOVE_COL
:
5769 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
5773 win
->SetCursor( *wxSTANDARD_CURSOR
);
5777 // we need to capture mouse when resizing
5778 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5779 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5781 if ( captureMouse
&& resize
)
5783 win
->CaptureMouse();
5788 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5791 wxPoint
pos( event
.GetPosition() );
5792 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5794 wxGridCellCoords coords
;
5795 XYToCell( x
, y
, coords
);
5797 int cell_rows
, cell_cols
;
5798 bool isFirstDrag
= !m_isDragging
;
5799 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5800 if ((cell_rows
< 0) || (cell_cols
< 0))
5802 coords
.SetRow(coords
.GetRow() + cell_rows
);
5803 coords
.SetCol(coords
.GetCol() + cell_cols
);
5806 if ( event
.Dragging() )
5808 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5810 // Don't start doing anything until the mouse has been dragged at
5811 // least 3 pixels in any direction...
5814 if (m_startDragPos
== wxDefaultPosition
)
5816 m_startDragPos
= pos
;
5819 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5823 m_isDragging
= true;
5824 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5826 // Hide the edit control, so it
5827 // won't interfere with drag-shrinking.
5828 if ( IsCellEditControlShown() )
5830 HideCellEditControl();
5831 SaveEditControlValue();
5834 // Have we captured the mouse yet?
5837 m_winCapture
= m_gridWin
;
5838 m_winCapture
->CaptureMouse();
5841 if ( coords
!= wxGridNoCellCoords
)
5843 if ( event
.CmdDown() )
5845 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5846 m_selectingKeyboard
= coords
;
5847 HighlightBlock( m_selectingKeyboard
, coords
);
5849 else if ( CanDragCell() )
5853 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5854 m_selectingKeyboard
= coords
;
5856 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5864 if ( !IsSelection() )
5866 HighlightBlock( coords
, coords
);
5870 HighlightBlock( m_currentCellCoords
, coords
);
5874 if (! IsVisible(coords
))
5876 MakeCellVisible(coords
);
5877 // TODO: need to introduce a delay or something here. The
5878 // scrolling is way to fast, at least on MSW - also on GTK.
5882 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5884 int cw
, ch
, left
, dummy
;
5885 m_gridWin
->GetClientSize( &cw
, &ch
);
5886 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5888 wxClientDC
dc( m_gridWin
);
5890 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5891 GetRowMinimalHeight(m_dragRowOrCol
) );
5892 dc
.SetLogicalFunction(wxINVERT
);
5893 if ( m_dragLastPos
>= 0 )
5895 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5897 dc
.DrawLine( left
, y
, left
+cw
, y
);
5900 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5902 int cw
, ch
, dummy
, top
;
5903 m_gridWin
->GetClientSize( &cw
, &ch
);
5904 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5906 wxClientDC
dc( m_gridWin
);
5908 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5909 GetColMinimalWidth(m_dragRowOrCol
) );
5910 dc
.SetLogicalFunction(wxINVERT
);
5911 if ( m_dragLastPos
>= 0 )
5913 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5915 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5922 m_isDragging
= false;
5923 m_startDragPos
= wxDefaultPosition
;
5925 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5926 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5929 if ( event
.Entering() || event
.Leaving() )
5931 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5932 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5937 // ------------ Left button pressed
5939 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5941 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5946 if ( !event
.CmdDown() )
5948 if ( event
.ShiftDown() )
5952 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5953 m_currentCellCoords
.GetCol(),
5956 event
.ControlDown(),
5962 else if ( XToEdgeOfCol(x
) < 0 &&
5963 YToEdgeOfRow(y
) < 0 )
5965 DisableCellEditControl();
5966 MakeCellVisible( coords
);
5968 if ( event
.CmdDown() )
5972 m_selection
->ToggleCellSelection( coords
.GetRow(),
5974 event
.ControlDown(),
5979 m_selectingTopLeft
= wxGridNoCellCoords
;
5980 m_selectingBottomRight
= wxGridNoCellCoords
;
5981 m_selectingKeyboard
= coords
;
5985 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5986 SetCurrentCell( coords
);
5989 if ( m_selection
->GetSelectionMode() !=
5990 wxGrid::wxGridSelectCells
)
5992 HighlightBlock( coords
, coords
);
6000 // ------------ Left double click
6002 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
6004 DisableCellEditControl();
6006 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
6008 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
6013 // we want double click to select a cell and start editing
6014 // (i.e. to behave in same way as sequence of two slow clicks):
6015 m_waitForSlowClick
= true;
6020 // ------------ Left button released
6022 else if ( event
.LeftUp() )
6024 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6028 if (m_winCapture
->HasCapture())
6029 m_winCapture
->ReleaseMouse();
6030 m_winCapture
= NULL
;
6033 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
6036 EnableCellEditControl();
6038 wxGridCellAttr
*attr
= GetCellAttr(coords
);
6039 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
6040 editor
->StartingClick();
6044 m_waitForSlowClick
= false;
6046 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6047 m_selectingBottomRight
!= wxGridNoCellCoords
)
6051 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6052 m_selectingTopLeft
.GetCol(),
6053 m_selectingBottomRight
.GetRow(),
6054 m_selectingBottomRight
.GetCol(),
6055 event
.ControlDown(),
6061 m_selectingTopLeft
= wxGridNoCellCoords
;
6062 m_selectingBottomRight
= wxGridNoCellCoords
;
6064 // Show the edit control, if it has been hidden for
6066 ShowCellEditControl();
6069 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6071 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6072 DoEndDragResizeRow();
6074 // Note: we are ending the event *after* doing
6075 // default processing in this case
6077 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
6079 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6081 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6082 DoEndDragResizeCol();
6084 // Note: we are ending the event *after* doing
6085 // default processing in this case
6087 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
6093 // ------------ Right button down
6095 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
6097 DisableCellEditControl();
6098 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
6103 // no default action at the moment
6107 // ------------ Right double click
6109 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
6111 DisableCellEditControl();
6112 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
6117 // no default action at the moment
6121 // ------------ Moving and no button action
6123 else if ( event
.Moving() && !event
.IsButton() )
6125 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
6127 // out of grid cell area
6128 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6132 int dragRow
= YToEdgeOfRow( y
);
6133 int dragCol
= XToEdgeOfCol( x
);
6135 // Dragging on the corner of a cell to resize in both
6136 // directions is not implemented yet...
6138 if ( dragRow
>= 0 && dragCol
>= 0 )
6140 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6146 m_dragRowOrCol
= dragRow
;
6148 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6150 if ( CanDragRowSize() && CanDragGridSize() )
6151 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
6154 else if ( dragCol
>= 0 )
6156 m_dragRowOrCol
= dragCol
;
6158 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6160 if ( CanDragColSize() && CanDragGridSize() )
6161 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
6164 else // Neither on a row or col edge
6166 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6168 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6174 void wxGrid::DoEndDragResizeRow()
6176 if ( m_dragLastPos
>= 0 )
6178 // erase the last line and resize the row
6180 int cw
, ch
, left
, dummy
;
6181 m_gridWin
->GetClientSize( &cw
, &ch
);
6182 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6184 wxClientDC
dc( m_gridWin
);
6186 dc
.SetLogicalFunction( wxINVERT
);
6187 dc
.DrawLine( left
, m_dragLastPos
, left
+ cw
, m_dragLastPos
);
6188 HideCellEditControl();
6189 SaveEditControlValue();
6191 int rowTop
= GetRowTop(m_dragRowOrCol
);
6192 SetRowSize( m_dragRowOrCol
,
6193 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
6195 if ( !GetBatchCount() )
6197 // Only needed to get the correct rect.y:
6198 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
6200 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6201 rect
.width
= m_rowLabelWidth
;
6202 rect
.height
= ch
- rect
.y
;
6203 m_rowLabelWin
->Refresh( true, &rect
);
6206 // if there is a multicell block, paint all of it
6209 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
6210 int leftCol
= XToCol(left
);
6211 int rightCol
= internalXToCol(left
+ cw
);
6214 for (i
=leftCol
; i
<rightCol
; i
++)
6216 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
6217 if (cell_rows
< subtract_rows
)
6218 subtract_rows
= cell_rows
;
6220 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
6221 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6222 rect
.height
= ch
- rect
.y
;
6225 m_gridWin
->Refresh( false, &rect
);
6228 ShowCellEditControl();
6233 void wxGrid::DoEndDragResizeCol()
6235 if ( m_dragLastPos
>= 0 )
6237 // erase the last line and resize the col
6239 int cw
, ch
, dummy
, top
;
6240 m_gridWin
->GetClientSize( &cw
, &ch
);
6241 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6243 wxClientDC
dc( m_gridWin
);
6245 dc
.SetLogicalFunction( wxINVERT
);
6246 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6247 HideCellEditControl();
6248 SaveEditControlValue();
6250 int colLeft
= GetColLeft(m_dragRowOrCol
);
6251 SetColSize( m_dragRowOrCol
,
6252 wxMax( m_dragLastPos
- colLeft
,
6253 GetColMinimalWidth(m_dragRowOrCol
) ) );
6255 if ( !GetBatchCount() )
6257 // Only needed to get the correct rect.x:
6258 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
6260 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6261 rect
.width
= cw
- rect
.x
;
6262 rect
.height
= m_colLabelHeight
;
6263 m_colLabelWin
->Refresh( true, &rect
);
6266 // if there is a multicell block, paint all of it
6269 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
6270 int topRow
= YToRow(top
);
6271 int bottomRow
= internalYToRow(top
+ cw
);
6274 for (i
=topRow
; i
<bottomRow
; i
++)
6276 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
6277 if (cell_cols
< subtract_cols
)
6278 subtract_cols
= cell_cols
;
6281 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
6282 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6283 rect
.width
= cw
- rect
.x
;
6287 m_gridWin
->Refresh( false, &rect
);
6290 ShowCellEditControl();
6294 void wxGrid::DoEndDragMoveCol()
6296 //The user clicked on the column but didn't actually drag
6297 if ( m_dragLastPos
< 0 )
6299 m_colLabelWin
->Refresh(); //Do this to "unpress" the column
6304 if ( m_moveToCol
== -1 )
6305 newPos
= m_numCols
- 1;
6308 newPos
= GetColPos( m_moveToCol
);
6309 if ( newPos
> GetColPos( m_dragRowOrCol
) )
6313 SetColPos( m_dragRowOrCol
, newPos
);
6316 void wxGrid::SetColPos( int colID
, int newPos
)
6318 if ( m_colAt
.IsEmpty() )
6320 m_colAt
.Alloc( m_numCols
);
6323 for ( i
= 0; i
< m_numCols
; i
++ )
6329 int oldPos
= GetColPos( colID
);
6331 //Reshuffle the m_colAt array
6332 if ( newPos
> oldPos
)
6335 for ( i
= oldPos
; i
< newPos
; i
++ )
6337 m_colAt
[i
] = m_colAt
[i
+1];
6343 for ( i
= oldPos
; i
> newPos
; i
-- )
6345 m_colAt
[i
] = m_colAt
[i
-1];
6349 m_colAt
[newPos
] = colID
;
6351 //Recalculate the column rights
6352 if ( !m_colWidths
.IsEmpty() )
6356 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6358 int colID
= GetColAt( colPos
);
6360 colRight
+= m_colWidths
[colID
];
6361 m_colRights
[colID
] = colRight
;
6365 m_colLabelWin
->Refresh();
6366 m_gridWin
->Refresh();
6371 void wxGrid::EnableDragColMove( bool enable
)
6373 if ( m_canDragColMove
== enable
)
6376 m_canDragColMove
= enable
;
6378 if ( !m_canDragColMove
)
6382 //Recalculate the column rights
6383 if ( !m_colWidths
.IsEmpty() )
6387 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6389 colRight
+= m_colWidths
[colPos
];
6390 m_colRights
[colPos
] = colRight
;
6394 m_colLabelWin
->Refresh();
6395 m_gridWin
->Refresh();
6401 // ------ interaction with data model
6403 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6405 switch ( msg
.GetId() )
6407 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6408 return GetModelValues();
6410 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6411 return SetModelValues();
6413 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6414 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6415 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6416 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6417 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6418 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6419 return Redimension( msg
);
6426 // The behaviour of this function depends on the grid table class
6427 // Clear() function. For the default wxGridStringTable class the
6428 // behavious is to replace all cell contents with wxEmptyString but
6429 // not to change the number of rows or cols.
6431 void wxGrid::ClearGrid()
6435 if (IsCellEditControlEnabled())
6436 DisableCellEditControl();
6439 if (!GetBatchCount())
6440 m_gridWin
->Refresh();
6444 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6446 // TODO: something with updateLabels flag
6450 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6456 if (IsCellEditControlEnabled())
6457 DisableCellEditControl();
6459 bool done
= m_table
->InsertRows( pos
, numRows
);
6462 // the table will have sent the results of the insert row
6463 // operation to this view object as a grid table message
6469 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6471 // TODO: something with updateLabels flag
6475 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6481 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6484 // the table will have sent the results of the append row
6485 // operation to this view object as a grid table message
6491 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6493 // TODO: something with updateLabels flag
6497 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6503 if (IsCellEditControlEnabled())
6504 DisableCellEditControl();
6506 bool done
= m_table
->DeleteRows( pos
, numRows
);
6508 // the table will have sent the results of the delete row
6509 // operation to this view object as a grid table message
6515 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6517 // TODO: something with updateLabels flag
6521 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6527 if (IsCellEditControlEnabled())
6528 DisableCellEditControl();
6530 bool done
= m_table
->InsertCols( pos
, numCols
);
6532 // the table will have sent the results of the insert col
6533 // operation to this view object as a grid table message
6539 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6541 // TODO: something with updateLabels flag
6545 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6551 bool done
= m_table
->AppendCols( numCols
);
6553 // the table will have sent the results of the append col
6554 // operation to this view object as a grid table message
6560 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6562 // TODO: something with updateLabels flag
6566 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6572 if (IsCellEditControlEnabled())
6573 DisableCellEditControl();
6575 bool done
= m_table
->DeleteCols( pos
, numCols
);
6577 // the table will have sent the results of the delete col
6578 // operation to this view object as a grid table message
6585 // ----- event handlers
6588 // Generate a grid event based on a mouse event and
6589 // return the result of ProcessEvent()
6591 int wxGrid::SendEvent( const wxEventType type
,
6593 wxMouseEvent
& mouseEv
)
6595 bool claimed
, vetoed
;
6597 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6599 int rowOrCol
= (row
== -1 ? col
: row
);
6601 wxGridSizeEvent
gridEvt( GetId(),
6605 mouseEv
.GetX() + GetRowLabelSize(),
6606 mouseEv
.GetY() + GetColLabelSize(),
6607 mouseEv
.ControlDown(),
6608 mouseEv
.ShiftDown(),
6610 mouseEv
.MetaDown() );
6612 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6613 vetoed
= !gridEvt
.IsAllowed();
6615 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6617 // Right now, it should _never_ end up here!
6618 wxGridRangeSelectEvent
gridEvt( GetId(),
6622 m_selectingBottomRight
,
6624 mouseEv
.ControlDown(),
6625 mouseEv
.ShiftDown(),
6627 mouseEv
.MetaDown() );
6629 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6630 vetoed
= !gridEvt
.IsAllowed();
6632 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
6633 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
6634 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
6635 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
6637 wxPoint pos
= mouseEv
.GetPosition();
6639 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
6640 pos
.y
+= GetColLabelSize();
6641 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
6642 pos
.x
+= GetRowLabelSize();
6644 wxGridEvent
gridEvt( GetId(),
6651 mouseEv
.ControlDown(),
6652 mouseEv
.ShiftDown(),
6654 mouseEv
.MetaDown() );
6655 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6656 vetoed
= !gridEvt
.IsAllowed();
6660 wxGridEvent
gridEvt( GetId(),
6664 mouseEv
.GetX() + GetRowLabelSize(),
6665 mouseEv
.GetY() + GetColLabelSize(),
6667 mouseEv
.ControlDown(),
6668 mouseEv
.ShiftDown(),
6670 mouseEv
.MetaDown() );
6671 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6672 vetoed
= !gridEvt
.IsAllowed();
6675 // A Veto'd event may not be `claimed' so test this first
6679 return claimed
? 1 : 0;
6682 // Generate a grid event of specified type and return the result
6683 // of ProcessEvent().
6685 int wxGrid::SendEvent( const wxEventType type
,
6688 bool claimed
, vetoed
;
6690 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6692 int rowOrCol
= (row
== -1 ? col
: row
);
6694 wxGridSizeEvent
gridEvt( GetId(), type
, this, rowOrCol
);
6696 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6697 vetoed
= !gridEvt
.IsAllowed();
6701 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
6703 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6704 vetoed
= !gridEvt
.IsAllowed();
6707 // A Veto'd event may not be `claimed' so test this first
6711 return claimed
? 1 : 0;
6714 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6716 // needed to prevent zillions of paint events on MSW
6720 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6722 // Don't do anything if between Begin/EndBatch...
6723 // EndBatch() will do all this on the last nested one anyway.
6724 if (! GetBatchCount())
6726 // Refresh to get correct scrolled position:
6727 wxScrolledWindow::Refresh(eraseb
, rect
);
6731 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6732 int width_label
, width_cell
, height_label
, height_cell
;
6735 // Copy rectangle can get scroll offsets..
6736 rect_x
= rect
->GetX();
6737 rect_y
= rect
->GetY();
6738 rectWidth
= rect
->GetWidth();
6739 rectHeight
= rect
->GetHeight();
6741 width_label
= m_rowLabelWidth
- rect_x
;
6742 if (width_label
> rectWidth
)
6743 width_label
= rectWidth
;
6745 height_label
= m_colLabelHeight
- rect_y
;
6746 if (height_label
> rectHeight
)
6747 height_label
= rectHeight
;
6749 if (rect_x
> m_rowLabelWidth
)
6751 x
= rect_x
- m_rowLabelWidth
;
6752 width_cell
= rectWidth
;
6757 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6760 if (rect_y
> m_colLabelHeight
)
6762 y
= rect_y
- m_colLabelHeight
;
6763 height_cell
= rectHeight
;
6768 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6771 // Paint corner label part intersecting rect.
6772 if ( width_label
> 0 && height_label
> 0 )
6774 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6775 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6778 // Paint col labels part intersecting rect.
6779 if ( width_cell
> 0 && height_label
> 0 )
6781 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6782 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6785 // Paint row labels part intersecting rect.
6786 if ( width_label
> 0 && height_cell
> 0 )
6788 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6789 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6792 // Paint cell area part intersecting rect.
6793 if ( width_cell
> 0 && height_cell
> 0 )
6795 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6796 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6801 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6802 m_colLabelWin
->Refresh(eraseb
, NULL
);
6803 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6804 m_gridWin
->Refresh(eraseb
, NULL
);
6809 void wxGrid::OnSize( wxSizeEvent
& event
)
6811 // position the child windows
6814 // don't call CalcDimensions() from here, the base class handles the size
6819 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6821 if ( m_inOnKeyDown
)
6823 // shouldn't be here - we are going round in circles...
6825 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6828 m_inOnKeyDown
= true;
6830 // propagate the event up and see if it gets processed
6831 wxWindow
*parent
= GetParent();
6832 wxKeyEvent
keyEvt( event
);
6833 keyEvt
.SetEventObject( parent
);
6835 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6837 if (GetLayoutDirection() == wxLayout_RightToLeft
)
6839 if (event
.GetKeyCode() == WXK_RIGHT
)
6840 event
.m_keyCode
= WXK_LEFT
;
6841 else if (event
.GetKeyCode() == WXK_LEFT
)
6842 event
.m_keyCode
= WXK_RIGHT
;
6845 // try local handlers
6846 switch ( event
.GetKeyCode() )
6849 if ( event
.ControlDown() )
6850 MoveCursorUpBlock( event
.ShiftDown() );
6852 MoveCursorUp( event
.ShiftDown() );
6856 if ( event
.ControlDown() )
6857 MoveCursorDownBlock( event
.ShiftDown() );
6859 MoveCursorDown( event
.ShiftDown() );
6863 if ( event
.ControlDown() )
6864 MoveCursorLeftBlock( event
.ShiftDown() );
6866 MoveCursorLeft( event
.ShiftDown() );
6870 if ( event
.ControlDown() )
6871 MoveCursorRightBlock( event
.ShiftDown() );
6873 MoveCursorRight( event
.ShiftDown() );
6877 case WXK_NUMPAD_ENTER
:
6878 if ( event
.ControlDown() )
6880 event
.Skip(); // to let the edit control have the return
6884 if ( GetGridCursorRow() < GetNumberRows()-1 )
6886 MoveCursorDown( event
.ShiftDown() );
6890 // at the bottom of a column
6891 DisableCellEditControl();
6901 if (event
.ShiftDown())
6903 if ( GetGridCursorCol() > 0 )
6905 MoveCursorLeft( false );
6910 DisableCellEditControl();
6915 if ( GetGridCursorCol() < GetNumberCols() - 1 )
6917 MoveCursorRight( false );
6922 DisableCellEditControl();
6928 if ( event
.ControlDown() )
6930 MakeCellVisible( 0, 0 );
6931 SetCurrentCell( 0, 0 );
6940 if ( event
.ControlDown() )
6942 MakeCellVisible( m_numRows
- 1, m_numCols
- 1 );
6943 SetCurrentCell( m_numRows
- 1, m_numCols
- 1 );
6960 if ( event
.ControlDown() )
6964 m_selection
->ToggleCellSelection(
6965 m_currentCellCoords
.GetRow(),
6966 m_currentCellCoords
.GetCol(),
6967 event
.ControlDown(),
6975 if ( !IsEditable() )
6976 MoveCursorRight( false );
6987 m_inOnKeyDown
= false;
6990 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6992 // try local handlers
6994 if ( event
.GetKeyCode() == WXK_SHIFT
)
6996 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6997 m_selectingBottomRight
!= wxGridNoCellCoords
)
7001 m_selection
->SelectBlock(
7002 m_selectingTopLeft
.GetRow(),
7003 m_selectingTopLeft
.GetCol(),
7004 m_selectingBottomRight
.GetRow(),
7005 m_selectingBottomRight
.GetCol(),
7006 event
.ControlDown(),
7013 m_selectingTopLeft
= wxGridNoCellCoords
;
7014 m_selectingBottomRight
= wxGridNoCellCoords
;
7015 m_selectingKeyboard
= wxGridNoCellCoords
;
7019 void wxGrid::OnChar( wxKeyEvent
& event
)
7021 // is it possible to edit the current cell at all?
7022 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7024 // yes, now check whether the cells editor accepts the key
7025 int row
= m_currentCellCoords
.GetRow();
7026 int col
= m_currentCellCoords
.GetCol();
7027 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7028 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7030 // <F2> is special and will always start editing, for
7031 // other keys - ask the editor itself
7032 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
7033 || editor
->IsAcceptedKey(event
) )
7035 // ensure cell is visble
7036 MakeCellVisible(row
, col
);
7037 EnableCellEditControl();
7039 // a problem can arise if the cell is not completely
7040 // visible (even after calling MakeCellVisible the
7041 // control is not created and calling StartingKey will
7043 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
7044 editor
->StartingKey(event
);
7060 void wxGrid::OnEraseBackground(wxEraseEvent
&)
7064 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
7066 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
7068 // the event has been intercepted - do nothing
7072 wxClientDC
dc( m_gridWin
);
7075 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
7077 DisableCellEditControl();
7079 if ( IsVisible( m_currentCellCoords
, false ) )
7082 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
7083 if ( !m_gridLinesEnabled
)
7091 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
7093 // Otherwise refresh redraws the highlight!
7094 m_currentCellCoords
= coords
;
7096 DrawGridCellArea( dc
, cells
);
7097 DrawAllGridLines( dc
, r
);
7101 m_currentCellCoords
= coords
;
7103 wxGridCellAttr
*attr
= GetCellAttr( coords
);
7104 DrawCellHighlight( dc
, attr
);
7108 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7111 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7115 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
7118 rightCol
= GetNumberCols() - 1;
7120 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
7123 bottomRow
= GetNumberRows() - 1;
7127 if ( topRow
> bottomRow
)
7134 if ( leftCol
> rightCol
)
7141 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7142 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7144 // First the case that we selected a completely new area
7145 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
7146 m_selectingBottomRight
== wxGridNoCellCoords
)
7149 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
7150 wxGridCellCoords ( bottomRow
, rightCol
) );
7151 m_gridWin
->Refresh( false, &rect
);
7154 // Now handle changing an existing selection area.
7155 else if ( m_selectingTopLeft
!= updateTopLeft
||
7156 m_selectingBottomRight
!= updateBottomRight
)
7158 // Compute two optimal update rectangles:
7159 // Either one rectangle is a real subset of the
7160 // other, or they are (almost) disjoint!
7162 bool need_refresh
[4];
7166 need_refresh
[3] = false;
7169 // Store intermediate values
7170 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
7171 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
7172 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
7173 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
7175 // Determine the outer/inner coordinates.
7176 if (oldLeft
> leftCol
)
7182 if (oldTop
> topRow
)
7188 if (oldRight
< rightCol
)
7191 oldRight
= rightCol
;
7194 if (oldBottom
< bottomRow
)
7197 oldBottom
= bottomRow
;
7201 // Now, either the stuff marked old is the outer
7202 // rectangle or we don't have a situation where one
7203 // is contained in the other.
7205 if ( oldLeft
< leftCol
)
7207 // Refresh the newly selected or deselected
7208 // area to the left of the old or new selection.
7209 need_refresh
[0] = true;
7210 rect
[0] = BlockToDeviceRect(
7211 wxGridCellCoords( oldTop
, oldLeft
),
7212 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
7215 if ( oldTop
< topRow
)
7217 // Refresh the newly selected or deselected
7218 // area above the old or new selection.
7219 need_refresh
[1] = true;
7220 rect
[1] = BlockToDeviceRect(
7221 wxGridCellCoords( oldTop
, leftCol
),
7222 wxGridCellCoords( topRow
- 1, rightCol
) );
7225 if ( oldRight
> rightCol
)
7227 // Refresh the newly selected or deselected
7228 // area to the right of the old or new selection.
7229 need_refresh
[2] = true;
7230 rect
[2] = BlockToDeviceRect(
7231 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
7232 wxGridCellCoords( oldBottom
, oldRight
) );
7235 if ( oldBottom
> bottomRow
)
7237 // Refresh the newly selected or deselected
7238 // area below the old or new selection.
7239 need_refresh
[3] = true;
7240 rect
[3] = BlockToDeviceRect(
7241 wxGridCellCoords( bottomRow
+ 1, leftCol
),
7242 wxGridCellCoords( oldBottom
, rightCol
) );
7245 // various Refresh() calls
7246 for (i
= 0; i
< 4; i
++ )
7247 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7248 m_gridWin
->Refresh( false, &(rect
[i
]) );
7252 m_selectingTopLeft
= updateTopLeft
;
7253 m_selectingBottomRight
= updateBottomRight
;
7257 // ------ functions to get/send data (see also public functions)
7260 bool wxGrid::GetModelValues()
7262 // Hide the editor, so it won't hide a changed value.
7263 HideCellEditControl();
7267 // all we need to do is repaint the grid
7269 m_gridWin
->Refresh();
7276 bool wxGrid::SetModelValues()
7280 // Disable the editor, so it won't hide a changed value.
7281 // Do we also want to save the current value of the editor first?
7283 DisableCellEditControl();
7287 for ( row
= 0; row
< m_numRows
; row
++ )
7289 for ( col
= 0; col
< m_numCols
; col
++ )
7291 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
7301 // Note - this function only draws cells that are in the list of
7302 // exposed cells (usually set from the update region by
7303 // CalcExposedCells)
7305 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7307 if ( !m_numRows
|| !m_numCols
)
7310 int i
, numCells
= cells
.GetCount();
7311 int row
, col
, cell_rows
, cell_cols
;
7312 wxGridCellCoordsArray redrawCells
;
7314 for ( i
= numCells
- 1; i
>= 0; i
-- )
7316 row
= cells
[i
].GetRow();
7317 col
= cells
[i
].GetCol();
7318 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7320 // If this cell is part of a multicell block, find owner for repaint
7321 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7323 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
7324 bool marked
= false;
7325 for ( int j
= 0; j
< numCells
; j
++ )
7327 if ( cell
== cells
[j
] )
7336 int count
= redrawCells
.GetCount();
7337 for (int j
= 0; j
< count
; j
++)
7339 if ( cell
== redrawCells
[j
] )
7347 redrawCells
.Add( cell
);
7350 // don't bother drawing this cell
7354 // If this cell is empty, find cell to left that might want to overflow
7355 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7357 for ( int l
= 0; l
< cell_rows
; l
++ )
7359 // find a cell in this row to leave already marked for repaint
7361 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7362 if ((redrawCells
[k
].GetCol() < left
) &&
7363 (redrawCells
[k
].GetRow() == row
))
7365 left
= redrawCells
[k
].GetCol();
7369 left
= 0; // oh well
7371 for (int j
= col
- 1; j
>= left
; j
--)
7373 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7375 if (GetCellOverflow(row
+ l
, j
))
7377 wxGridCellCoords
cell(row
+ l
, j
);
7378 bool marked
= false;
7380 for (int k
= 0; k
< numCells
; k
++)
7382 if ( cell
== cells
[k
] )
7391 int count
= redrawCells
.GetCount();
7392 for (int k
= 0; k
< count
; k
++)
7394 if ( cell
== redrawCells
[k
] )
7401 redrawCells
.Add( cell
);
7410 DrawCell( dc
, cells
[i
] );
7413 numCells
= redrawCells
.GetCount();
7415 for ( i
= numCells
- 1; i
>= 0; i
-- )
7417 DrawCell( dc
, redrawCells
[i
] );
7421 void wxGrid::DrawGridSpace( wxDC
& dc
)
7424 m_gridWin
->GetClientSize( &cw
, &ch
);
7427 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7429 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
7430 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7432 if ( right
> rightCol
|| bottom
> bottomRow
)
7435 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7437 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
7438 dc
.SetPen( *wxTRANSPARENT_PEN
);
7440 if ( right
> rightCol
)
7442 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7445 if ( bottom
> bottomRow
)
7447 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7452 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7454 int row
= coords
.GetRow();
7455 int col
= coords
.GetCol();
7457 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7460 // we draw the cell border ourselves
7461 #if !WXGRID_DRAW_LINES
7462 if ( m_gridLinesEnabled
)
7463 DrawCellBorder( dc
, coords
);
7466 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7468 bool isCurrent
= coords
== m_currentCellCoords
;
7470 wxRect rect
= CellToRect( row
, col
);
7472 // if the editor is shown, we should use it and not the renderer
7473 // Note: However, only if it is really _shown_, i.e. not hidden!
7474 if ( isCurrent
&& IsCellEditControlShown() )
7476 // NB: this "#if..." is temporary and fixes a problem where the
7477 // edit control is erased by this code after being rendered.
7478 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7479 // implicitly, causing this out-of order render.
7480 #if !defined(__WXMAC__) || wxMAC_USE_CORE_GRAPHICS
7481 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7482 editor
->PaintBackground(rect
, attr
);
7488 // but all the rest is drawn by the cell renderer and hence may be customized
7489 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7490 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7497 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7499 int row
= m_currentCellCoords
.GetRow();
7500 int col
= m_currentCellCoords
.GetCol();
7502 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7505 wxRect rect
= CellToRect(row
, col
);
7507 // hmmm... what could we do here to show that the cell is disabled?
7508 // for now, I just draw a thinner border than for the other ones, but
7509 // it doesn't look really good
7511 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7515 // The center of the drawn line is where the position/width/height of
7516 // the rectangle is actually at (on wxMSW at least), so the
7517 // size of the rectangle is reduced to compensate for the thickness of
7518 // the line. If this is too strange on non-wxMSW platforms then
7519 // please #ifdef this appropriately.
7520 rect
.x
+= penWidth
/ 2;
7521 rect
.y
+= penWidth
/ 2;
7522 rect
.width
-= penWidth
- 1;
7523 rect
.height
-= penWidth
- 1;
7525 // Now draw the rectangle
7526 // use the cellHighlightColour if the cell is inside a selection, this
7527 // will ensure the cell is always visible.
7528 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
: m_cellHighlightColour
, penWidth
, wxSOLID
));
7529 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7530 dc
.DrawRectangle(rect
);
7534 // VZ: my experiments with 3D borders...
7536 // how to properly set colours for arbitrary bg?
7537 wxCoord x1
= rect
.x
,
7539 x2
= rect
.x
+ rect
.width
- 1,
7540 y2
= rect
.y
+ rect
.height
- 1;
7542 dc
.SetPen(*wxWHITE_PEN
);
7543 dc
.DrawLine(x1
, y1
, x2
, y1
);
7544 dc
.DrawLine(x1
, y1
, x1
, y2
);
7546 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7547 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7549 dc
.SetPen(*wxBLACK_PEN
);
7550 dc
.DrawLine(x1
, y2
, x2
, y2
);
7551 dc
.DrawLine(x2
, y1
, x2
, y2
+ 1);
7555 wxPen
wxGrid::GetDefaultGridLinePen()
7557 return wxPen(GetGridLineColour(), 1, wxSOLID
);
7560 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
7562 return GetDefaultGridLinePen();
7565 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
7567 return GetDefaultGridLinePen();
7570 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7572 int row
= coords
.GetRow();
7573 int col
= coords
.GetCol();
7574 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7578 wxRect rect
= CellToRect( row
, col
);
7580 // right hand border
7581 dc
.SetPen( GetColGridLinePen(col
) );
7582 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7583 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7586 dc
.SetPen( GetRowGridLinePen(row
) );
7587 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7588 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7591 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7593 // This if block was previously in wxGrid::OnPaint but that doesn't
7594 // seem to get called under wxGTK - MB
7596 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7597 m_numRows
&& m_numCols
)
7599 m_currentCellCoords
.Set(0, 0);
7602 if ( IsCellEditControlShown() )
7604 // don't show highlight when the edit control is shown
7608 // if the active cell was repainted, repaint its highlight too because it
7609 // might have been damaged by the grid lines
7610 size_t count
= cells
.GetCount();
7611 for ( size_t n
= 0; n
< count
; n
++ )
7613 if ( cells
[n
] == m_currentCellCoords
)
7615 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7616 DrawCellHighlight(dc
, attr
);
7624 // TODO: remove this ???
7625 // This is used to redraw all grid lines e.g. when the grid line colour
7628 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7630 #if !WXGRID_DRAW_LINES
7634 if ( !m_gridLinesEnabled
|| !m_numRows
|| !m_numCols
)
7637 int top
, bottom
, left
, right
;
7639 #if 0 //#ifndef __WXGTK__
7643 m_gridWin
->GetClientSize(&cw
, &ch
);
7645 // virtual coords of visible area
7647 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7648 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7653 reg
.GetBox(x
, y
, w
, h
);
7654 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7655 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7659 m_gridWin
->GetClientSize(&cw
, &ch
);
7660 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7661 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7664 // avoid drawing grid lines past the last row and col
7666 right
= wxMin( right
, GetColRight(GetColAt( m_numCols
- 1 )) );
7667 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7669 // no gridlines inside multicells, clip them out
7670 int leftCol
= GetColPos( internalXToCol(left
) );
7671 int topRow
= internalYToRow(top
);
7672 int rightCol
= GetColPos( internalXToCol(right
) );
7673 int bottomRow
= internalYToRow(bottom
);
7676 // CS: I don't know why suddenly unscrolled coordinates are used for clipping
7677 wxRegion
clippedcells(0, 0, cw
, ch
);
7679 int i
, j
, cell_rows
, cell_cols
;
7682 for (j
=topRow
; j
<bottomRow
; j
++)
7685 for (colPos
=leftCol
; colPos
<rightCol
; colPos
++)
7687 i
= GetColAt( colPos
);
7689 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7690 if ((cell_rows
> 1) || (cell_cols
> 1))
7692 rect
= CellToRect(j
,i
);
7693 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7694 clippedcells
.Subtract(rect
);
7696 else if ((cell_rows
< 0) || (cell_cols
< 0))
7698 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7699 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7700 clippedcells
.Subtract(rect
);
7705 wxRegion
clippedcells( left
, top
, right
- left
, bottom
- top
);
7707 int i
, j
, cell_rows
, cell_cols
;
7710 for (j
=topRow
; j
<bottomRow
; j
++)
7712 for (i
=leftCol
; i
<rightCol
; i
++)
7714 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7715 if ((cell_rows
> 1) || (cell_cols
> 1))
7717 rect
= CellToRect(j
, i
);
7718 clippedcells
.Subtract(rect
);
7720 else if ((cell_rows
< 0) || (cell_cols
< 0))
7722 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7723 clippedcells
.Subtract(rect
);
7729 dc
.SetClippingRegion( clippedcells
);
7732 // horizontal grid lines
7734 // already declared above - int i;
7735 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7737 int bot
= GetRowBottom(i
) - 1;
7746 dc
.SetPen( GetRowGridLinePen(i
) );
7747 dc
.DrawLine( left
, bot
, right
, bot
);
7751 // vertical grid lines
7754 for ( colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
7756 i
= GetColAt( colPos
);
7758 int colRight
= GetColRight(i
);
7760 if (GetLayoutDirection() != wxLayout_RightToLeft
)
7764 if ( colRight
> right
)
7769 if ( colRight
>= left
)
7771 dc
.SetPen( GetColGridLinePen(i
) );
7772 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7776 dc
.DestroyClippingRegion();
7779 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
7785 size_t numLabels
= rows
.GetCount();
7787 for ( i
= 0; i
< numLabels
; i
++ )
7789 DrawRowLabel( dc
, rows
[i
] );
7793 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7795 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7802 rect
.SetY( GetRowTop(row
) + 1 );
7803 rect
.SetWidth( m_rowLabelWidth
- 2 );
7804 rect
.SetHeight( GetRowHeight(row
) - 2 );
7806 CalcScrolledPosition( 0, rect
.y
, NULL
, &rect
.y
);
7808 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7810 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7812 int rowTop
= GetRowTop(row
),
7813 rowBottom
= GetRowBottom(row
) - 1;
7815 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
7816 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
7817 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7818 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7820 dc
.SetPen( *wxWHITE_PEN
);
7821 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7822 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
7825 dc
.SetBackgroundMode( wxTRANSPARENT
);
7826 dc
.SetTextForeground( GetLabelTextColour() );
7827 dc
.SetFont( GetLabelFont() );
7830 GetRowLabelAlignment( &hAlign
, &vAlign
);
7833 rect
.SetY( GetRowTop(row
) + 2 );
7834 rect
.SetWidth( m_rowLabelWidth
- 4 );
7835 rect
.SetHeight( GetRowHeight(row
) - 4 );
7836 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7839 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7845 size_t numLabels
= cols
.GetCount();
7847 for ( i
= 0; i
< numLabels
; i
++ )
7849 DrawColLabel( dc
, cols
[i
] );
7853 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7855 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7858 int colLeft
= GetColLeft(col
);
7863 rect
.SetX( colLeft
+ 1 );
7865 rect
.SetWidth( GetColWidth(col
) - 2 );
7866 rect
.SetHeight( m_colLabelHeight
- 2 );
7868 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7870 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7872 int colRight
= GetColRight(col
) - 1;
7874 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
7875 dc
.DrawLine( colRight
, 0, colRight
, m_colLabelHeight
- 1 );
7876 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7877 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
7878 colRight
+ 1, m_colLabelHeight
- 1 );
7880 dc
.SetPen( *wxWHITE_PEN
);
7881 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
7882 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7885 dc
.SetBackgroundMode( wxTRANSPARENT
);
7886 dc
.SetTextForeground( GetLabelTextColour() );
7887 dc
.SetFont( GetLabelFont() );
7889 int hAlign
, vAlign
, orient
;
7890 GetColLabelAlignment( &hAlign
, &vAlign
);
7891 orient
= GetColLabelTextOrientation();
7893 rect
.SetX( colLeft
+ 2 );
7895 rect
.SetWidth( GetColWidth(col
) - 4 );
7896 rect
.SetHeight( m_colLabelHeight
- 4 );
7897 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7900 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7901 const wxString
& value
,
7905 int textOrientation
)
7907 wxArrayString lines
;
7909 StringToLines( value
, lines
);
7911 // Forward to new API.
7912 DrawTextRectangle( dc
,
7920 // VZ: this should be replaced with wxDC::DrawLabel() to which we just have to
7921 // add textOrientation support
7922 void wxGrid::DrawTextRectangle(wxDC
& dc
,
7923 const wxArrayString
& lines
,
7927 int textOrientation
)
7929 if ( lines
.empty() )
7932 wxDCClipper
clip(dc
, rect
);
7937 if ( textOrientation
== wxHORIZONTAL
)
7938 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
7940 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7944 switch ( vertAlign
)
7946 case wxALIGN_BOTTOM
:
7947 if ( textOrientation
== wxHORIZONTAL
)
7948 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7950 x
= rect
.x
+ rect
.width
- textWidth
;
7953 case wxALIGN_CENTRE
:
7954 if ( textOrientation
== wxHORIZONTAL
)
7955 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
7957 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
7962 if ( textOrientation
== wxHORIZONTAL
)
7969 // Align each line of a multi-line label
7970 size_t nLines
= lines
.GetCount();
7971 for ( size_t l
= 0; l
< nLines
; l
++ )
7973 const wxString
& line
= lines
[l
];
7977 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
7983 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
7985 switch ( horizAlign
)
7988 if ( textOrientation
== wxHORIZONTAL
)
7989 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7991 y
= rect
.y
+ lineWidth
+ 1;
7994 case wxALIGN_CENTRE
:
7995 if ( textOrientation
== wxHORIZONTAL
)
7996 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
7998 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
8003 if ( textOrientation
== wxHORIZONTAL
)
8006 y
= rect
.y
+ rect
.height
- 1;
8010 if ( textOrientation
== wxHORIZONTAL
)
8012 dc
.DrawText( line
, x
, y
);
8017 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
8023 // Split multi-line text up into an array of strings.
8024 // Any existing contents of the string array are preserved.
8026 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
8030 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
8031 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
8033 while ( startPos
< (int)tVal
.length() )
8035 pos
= tVal
.Mid(startPos
).Find( eol
);
8040 else if ( pos
== 0 )
8042 lines
.Add( wxEmptyString
);
8046 lines
.Add( value
.Mid(startPos
, pos
) );
8049 startPos
+= pos
+ 1;
8052 if ( startPos
< (int)value
.length() )
8054 lines
.Add( value
.Mid( startPos
) );
8058 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
8059 const wxArrayString
& lines
,
8060 long *width
, long *height
)
8064 long lineW
= 0, lineH
= 0;
8067 for ( i
= 0; i
< lines
.GetCount(); i
++ )
8069 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
8070 w
= wxMax( w
, lineW
);
8079 // ------ Batch processing.
8081 void wxGrid::EndBatch()
8083 if ( m_batchCount
> 0 )
8086 if ( !m_batchCount
)
8089 m_rowLabelWin
->Refresh();
8090 m_colLabelWin
->Refresh();
8091 m_cornerLabelWin
->Refresh();
8092 m_gridWin
->Refresh();
8097 // Use this, rather than wxWindow::Refresh(), to force an immediate
8098 // repainting of the grid. Has no effect if you are already inside a
8099 // BeginBatch / EndBatch block.
8101 void wxGrid::ForceRefresh()
8107 bool wxGrid::Enable(bool enable
)
8109 if ( !wxScrolledWindow::Enable(enable
) )
8112 // redraw in the new state
8113 m_gridWin
->Refresh();
8119 // ------ Edit control functions
8122 void wxGrid::EnableEditing( bool edit
)
8124 // TODO: improve this ?
8126 if ( edit
!= m_editable
)
8129 EnableCellEditControl(edit
);
8134 void wxGrid::EnableCellEditControl( bool enable
)
8139 if ( enable
!= m_cellEditCtrlEnabled
)
8143 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
8146 // this should be checked by the caller!
8147 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8149 // do it before ShowCellEditControl()
8150 m_cellEditCtrlEnabled
= enable
;
8152 ShowCellEditControl();
8156 //FIXME:add veto support
8157 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
8159 HideCellEditControl();
8160 SaveEditControlValue();
8162 // do it after HideCellEditControl()
8163 m_cellEditCtrlEnabled
= enable
;
8168 bool wxGrid::IsCurrentCellReadOnly() const
8171 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
8172 bool readonly
= attr
->IsReadOnly();
8178 bool wxGrid::CanEnableCellControl() const
8180 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
8181 !IsCurrentCellReadOnly();
8184 bool wxGrid::IsCellEditControlEnabled() const
8186 // the cell edit control might be disable for all cells or just for the
8187 // current one if it's read only
8188 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
8191 bool wxGrid::IsCellEditControlShown() const
8193 bool isShown
= false;
8195 if ( m_cellEditCtrlEnabled
)
8197 int row
= m_currentCellCoords
.GetRow();
8198 int col
= m_currentCellCoords
.GetCol();
8199 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8200 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
8205 if ( editor
->IsCreated() )
8207 isShown
= editor
->GetControl()->IsShown();
8217 void wxGrid::ShowCellEditControl()
8219 if ( IsCellEditControlEnabled() )
8221 if ( !IsVisible( m_currentCellCoords
, false ) )
8223 m_cellEditCtrlEnabled
= false;
8228 wxRect rect
= CellToRect( m_currentCellCoords
);
8229 int row
= m_currentCellCoords
.GetRow();
8230 int col
= m_currentCellCoords
.GetCol();
8232 // if this is part of a multicell, find owner (topleft)
8233 int cell_rows
, cell_cols
;
8234 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8235 if ( cell_rows
<= 0 || cell_cols
<= 0 )
8239 m_currentCellCoords
.SetRow( row
);
8240 m_currentCellCoords
.SetCol( col
);
8243 // erase the highlight and the cell contents because the editor
8244 // might not cover the entire cell
8245 wxClientDC
dc( m_gridWin
);
8247 dc
.SetBrush(wxBrush(GetCellAttr(row
, col
)->GetBackgroundColour(), wxSOLID
));
8248 dc
.SetPen(*wxTRANSPARENT_PEN
);
8249 dc
.DrawRectangle(rect
);
8251 // convert to scrolled coords
8252 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8258 // cell is shifted by one pixel
8259 // However, don't allow x or y to become negative
8260 // since the SetSize() method interprets that as
8267 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8268 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8269 if ( !editor
->IsCreated() )
8271 editor
->Create(m_gridWin
, wxID_ANY
,
8272 new wxGridCellEditorEvtHandler(this, editor
));
8274 wxGridEditorCreatedEvent
evt(GetId(),
8275 wxEVT_GRID_EDITOR_CREATED
,
8279 editor
->GetControl());
8280 GetEventHandler()->ProcessEvent(evt
);
8283 // resize editor to overflow into righthand cells if allowed
8284 int maxWidth
= rect
.width
;
8285 wxString value
= GetCellValue(row
, col
);
8286 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
8289 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
8290 if (maxWidth
< rect
.width
)
8291 maxWidth
= rect
.width
;
8294 int client_right
= m_gridWin
->GetClientSize().GetWidth();
8295 if (rect
.x
+ maxWidth
> client_right
)
8296 maxWidth
= client_right
- rect
.x
;
8298 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
8300 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8301 // may have changed earlier
8302 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
8305 GetCellSize( row
, i
, &c_rows
, &c_cols
);
8307 // looks weird going over a multicell
8308 if (m_table
->IsEmptyCell( row
, i
) &&
8309 (rect
.width
< maxWidth
) && (c_rows
== 1))
8311 rect
.width
+= GetColWidth( i
);
8317 if (rect
.GetRight() > client_right
)
8318 rect
.SetRight( client_right
- 1 );
8321 editor
->SetCellAttr( attr
);
8322 editor
->SetSize( rect
);
8324 editor
->GetControl()->Move(
8325 editor
->GetControl()->GetPosition().x
+ nXMove
,
8326 editor
->GetControl()->GetPosition().y
);
8327 editor
->Show( true, attr
);
8329 // recalc dimensions in case we need to
8330 // expand the scrolled window to account for editor
8333 editor
->BeginEdit(row
, col
, this);
8334 editor
->SetCellAttr(NULL
);
8342 void wxGrid::HideCellEditControl()
8344 if ( IsCellEditControlEnabled() )
8346 int row
= m_currentCellCoords
.GetRow();
8347 int col
= m_currentCellCoords
.GetCol();
8349 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8350 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
8351 editor
->Show( false );
8355 m_gridWin
->SetFocus();
8357 // refresh whole row to the right
8358 wxRect
rect( CellToRect(row
, col
) );
8359 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8360 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8363 // ensure that the pixels under the focus ring get refreshed as well
8364 rect
.Inflate(10, 10);
8367 m_gridWin
->Refresh( false, &rect
);
8371 void wxGrid::SaveEditControlValue()
8373 if ( IsCellEditControlEnabled() )
8375 int row
= m_currentCellCoords
.GetRow();
8376 int col
= m_currentCellCoords
.GetCol();
8378 wxString oldval
= GetCellValue(row
, col
);
8380 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8381 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8382 bool changed
= editor
->EndEdit(row
, col
, this);
8389 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
8390 m_currentCellCoords
.GetRow(),
8391 m_currentCellCoords
.GetCol() ) < 0 )
8393 // Event has been vetoed, set the data back.
8394 SetCellValue(row
, col
, oldval
);
8401 // ------ Grid location functions
8402 // Note that all of these functions work with the logical coordinates of
8403 // grid cells and labels so you will need to convert from device
8404 // coordinates for mouse events etc.
8407 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
8409 int row
= YToRow(y
);
8410 int col
= XToCol(x
);
8412 if ( row
== -1 || col
== -1 )
8414 coords
= wxGridNoCellCoords
;
8418 coords
.Set( row
, col
);
8422 // Internal Helper function for computing row or column from some
8423 // (unscrolled) coordinate value, using either
8424 // m_defaultRowHeight/m_defaultColWidth or binary search on array
8425 // of m_rowBottoms/m_ColRights to speed up the search!
8427 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
8428 const wxArrayInt
& BorderArray
, int nMax
,
8432 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
8437 size_t i_max
= coord
/ defaultDist
,
8440 if (BorderArray
.IsEmpty())
8442 if ((int) i_max
< nMax
)
8444 return clipToMinMax
? nMax
- 1 : -1;
8447 if ( i_max
>= BorderArray
.GetCount())
8449 i_max
= BorderArray
.GetCount() - 1;
8453 if ( coord
>= BorderArray
[i_max
])
8457 i_max
= coord
/ minDist
;
8459 i_max
= BorderArray
.GetCount() - 1;
8462 if ( i_max
>= BorderArray
.GetCount())
8463 i_max
= BorderArray
.GetCount() - 1;
8466 if ( coord
>= BorderArray
[i_max
])
8467 return clipToMinMax
? (int)i_max
: -1;
8468 if ( coord
< BorderArray
[0] )
8471 while ( i_max
- i_min
> 0 )
8473 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
8474 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8475 if (coord
>= BorderArray
[ i_max
- 1])
8479 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
8480 if (coord
< BorderArray
[median
])
8489 int wxGrid::YToRow( int y
)
8491 return CoordToRowOrCol(y
, m_defaultRowHeight
,
8492 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8495 int wxGrid::XToCol( int x
, bool clipToMinMax
)
8498 return clipToMinMax
&& (m_numCols
> 0) ? GetColAt( 0 ) : -1;
8500 if (!m_defaultColWidth
)
8501 m_defaultColWidth
= 1;
8503 int maxPos
= x
/ m_defaultColWidth
;
8506 if (m_colRights
.IsEmpty())
8508 if(maxPos
< m_numCols
)
8509 return GetColAt( maxPos
);
8510 return clipToMinMax
? GetColAt( m_numCols
- 1 ) : -1;
8513 if ( maxPos
>= m_numCols
)
8514 maxPos
= m_numCols
- 1;
8517 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8520 if (m_minAcceptableColWidth
)
8521 maxPos
= x
/ m_minAcceptableColWidth
;
8523 maxPos
= m_numCols
- 1;
8525 if ( maxPos
>= m_numCols
)
8526 maxPos
= m_numCols
- 1;
8529 //X is beyond the last column
8530 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8531 return clipToMinMax
? GetColAt( maxPos
) : -1;
8533 //X is before the first column
8534 if ( x
< m_colRights
[GetColAt( 0 )] )
8535 return GetColAt( 0 );
8537 //Perform a binary search
8538 while ( maxPos
- minPos
> 0 )
8540 wxCHECK_MSG(m_colRights
[GetColAt( minPos
)] <= x
&& x
< m_colRights
[GetColAt( maxPos
)],
8541 0, _T("wxGrid: internal error in XToCol"));
8543 if (x
>= m_colRights
[GetColAt( maxPos
- 1 )])
8544 return GetColAt( maxPos
);
8547 int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
8548 if (x
< m_colRights
[GetColAt( median
)])
8553 return GetColAt( maxPos
);
8556 // return the row number that that the y coord is near
8557 // the edge of, or -1 if not near an edge.
8558 // coords can only possibly be near an edge if
8559 // (a) the row/column is large enough to still allow for an "inner" area
8560 // that is _not_ nead the edge (i.e., if the height/width is smaller
8561 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8564 // (b) resizing rows/columns (the thing for which edge detection is
8565 // relevant at all) is enabled.
8567 int wxGrid::YToEdgeOfRow( int y
)
8570 i
= internalYToRow(y
);
8572 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragRowSize() )
8574 // We know that we are in row i, test whether we are
8575 // close enough to lower or upper border, respectively.
8576 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8578 else if ( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8585 // return the col number that that the x coord is near the edge of, or
8586 // -1 if not near an edge
8587 // See comment at YToEdgeOfRow for conditions on edge detection.
8589 int wxGrid::XToEdgeOfCol( int x
)
8592 i
= internalXToCol(x
);
8594 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragColSize() )
8596 // We know that we are in column i; test whether we are
8597 // close enough to right or left border, respectively.
8598 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8600 else if ( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8607 wxRect
wxGrid::CellToRect( int row
, int col
)
8609 wxRect
rect( -1, -1, -1, -1 );
8611 if ( row
>= 0 && row
< m_numRows
&&
8612 col
>= 0 && col
< m_numCols
)
8614 int i
, cell_rows
, cell_cols
;
8615 rect
.width
= rect
.height
= 0;
8616 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8617 // if negative then find multicell owner
8622 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8624 rect
.x
= GetColLeft(col
);
8625 rect
.y
= GetRowTop(row
);
8626 for (i
=col
; i
< col
+ cell_cols
; i
++)
8627 rect
.width
+= GetColWidth(i
);
8628 for (i
=row
; i
< row
+ cell_rows
; i
++)
8629 rect
.height
+= GetRowHeight(i
);
8632 // if grid lines are enabled, then the area of the cell is a bit smaller
8633 if (m_gridLinesEnabled
)
8642 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
8644 // get the cell rectangle in logical coords
8646 wxRect
r( CellToRect( row
, col
) );
8648 // convert to device coords
8650 int left
, top
, right
, bottom
;
8651 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8652 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8654 // check against the client area of the grid window
8656 m_gridWin
->GetClientSize( &cw
, &ch
);
8658 if ( wholeCellVisible
)
8660 // is the cell wholly visible ?
8661 return ( left
>= 0 && right
<= cw
&&
8662 top
>= 0 && bottom
<= ch
);
8666 // is the cell partly visible ?
8668 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8669 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8673 // make the specified cell location visible by doing a minimal amount
8676 void wxGrid::MakeCellVisible( int row
, int col
)
8679 int xpos
= -1, ypos
= -1;
8681 if ( row
>= 0 && row
< m_numRows
&&
8682 col
>= 0 && col
< m_numCols
)
8684 // get the cell rectangle in logical coords
8685 wxRect
r( CellToRect( row
, col
) );
8687 // convert to device coords
8688 int left
, top
, right
, bottom
;
8689 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8690 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8693 m_gridWin
->GetClientSize( &cw
, &ch
);
8699 else if ( bottom
> ch
)
8701 int h
= r
.GetHeight();
8703 for ( i
= row
- 1; i
>= 0; i
-- )
8705 int rowHeight
= GetRowHeight(i
);
8706 if ( h
+ rowHeight
> ch
)
8713 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8714 // have rounding errors (this is important, because if we do,
8715 // we might not scroll at all and some cells won't be redrawn)
8717 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
8718 // so just add a full scroll unit...
8719 ypos
+= m_scrollLineY
;
8722 // special handling for wide cells - show always left part of the cell!
8723 // Otherwise, e.g. when stepping from row to row, it would jump between
8724 // left and right part of the cell on every step!
8726 if ( left
< 0 || (right
- left
) >= cw
)
8730 else if ( right
> cw
)
8732 // position the view so that the cell is on the right
8734 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8735 xpos
= x0
+ (right
- cw
);
8737 // see comment for ypos above
8738 xpos
+= m_scrollLineX
;
8741 if ( xpos
!= -1 || ypos
!= -1 )
8744 xpos
/= m_scrollLineX
;
8746 ypos
/= m_scrollLineY
;
8747 Scroll( xpos
, ypos
);
8754 // ------ Grid cursor movement functions
8757 bool wxGrid::MoveCursorUp( bool expandSelection
)
8759 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8760 m_currentCellCoords
.GetRow() >= 0 )
8762 if ( expandSelection
)
8764 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8765 m_selectingKeyboard
= m_currentCellCoords
;
8766 if ( m_selectingKeyboard
.GetRow() > 0 )
8768 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8769 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8770 m_selectingKeyboard
.GetCol() );
8771 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8774 else if ( m_currentCellCoords
.GetRow() > 0 )
8776 int row
= m_currentCellCoords
.GetRow() - 1;
8777 int col
= m_currentCellCoords
.GetCol();
8779 MakeCellVisible( row
, col
);
8780 SetCurrentCell( row
, col
);
8791 bool wxGrid::MoveCursorDown( bool expandSelection
)
8793 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8794 m_currentCellCoords
.GetRow() < m_numRows
)
8796 if ( expandSelection
)
8798 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8799 m_selectingKeyboard
= m_currentCellCoords
;
8800 if ( m_selectingKeyboard
.GetRow() < m_numRows
- 1 )
8802 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8803 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8804 m_selectingKeyboard
.GetCol() );
8805 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8808 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8810 int row
= m_currentCellCoords
.GetRow() + 1;
8811 int col
= m_currentCellCoords
.GetCol();
8813 MakeCellVisible( row
, col
);
8814 SetCurrentCell( row
, col
);
8825 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8827 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8828 m_currentCellCoords
.GetCol() >= 0 )
8830 if ( expandSelection
)
8832 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8833 m_selectingKeyboard
= m_currentCellCoords
;
8834 if ( m_selectingKeyboard
.GetCol() > 0 )
8836 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8837 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8838 m_selectingKeyboard
.GetCol() );
8839 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8842 else if ( GetColPos( m_currentCellCoords
.GetCol() ) > 0 )
8844 int row
= m_currentCellCoords
.GetRow();
8845 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) - 1 );
8848 MakeCellVisible( row
, col
);
8849 SetCurrentCell( row
, col
);
8860 bool wxGrid::MoveCursorRight( bool expandSelection
)
8862 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8863 m_currentCellCoords
.GetCol() < m_numCols
)
8865 if ( expandSelection
)
8867 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8868 m_selectingKeyboard
= m_currentCellCoords
;
8869 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8871 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8872 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8873 m_selectingKeyboard
.GetCol() );
8874 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8877 else if ( GetColPos( m_currentCellCoords
.GetCol() ) < m_numCols
- 1 )
8879 int row
= m_currentCellCoords
.GetRow();
8880 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) + 1 );
8883 MakeCellVisible( row
, col
);
8884 SetCurrentCell( row
, col
);
8895 bool wxGrid::MovePageUp()
8897 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8900 int row
= m_currentCellCoords
.GetRow();
8904 m_gridWin
->GetClientSize( &cw
, &ch
);
8906 int y
= GetRowTop(row
);
8907 int newRow
= internalYToRow( y
- ch
+ 1 );
8909 if ( newRow
== row
)
8911 // row > 0, so newRow can never be less than 0 here.
8915 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8916 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8924 bool wxGrid::MovePageDown()
8926 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8929 int row
= m_currentCellCoords
.GetRow();
8930 if ( (row
+ 1) < m_numRows
)
8933 m_gridWin
->GetClientSize( &cw
, &ch
);
8935 int y
= GetRowTop(row
);
8936 int newRow
= internalYToRow( y
+ ch
);
8937 if ( newRow
== row
)
8939 // row < m_numRows, so newRow can't overflow here.
8943 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8944 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8952 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8955 m_currentCellCoords
!= wxGridNoCellCoords
&&
8956 m_currentCellCoords
.GetRow() > 0 )
8958 int row
= m_currentCellCoords
.GetRow();
8959 int col
= m_currentCellCoords
.GetCol();
8961 if ( m_table
->IsEmptyCell(row
, col
) )
8963 // starting in an empty cell: find the next block of
8969 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8973 else if ( m_table
->IsEmptyCell(row
- 1, col
) )
8975 // starting at the top of a block: find the next block
8981 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8987 // starting within a block: find the top of the block
8992 if ( m_table
->IsEmptyCell(row
, col
) )
9000 MakeCellVisible( row
, col
);
9001 if ( expandSelection
)
9003 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9004 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9009 SetCurrentCell( row
, col
);
9018 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
9021 m_currentCellCoords
!= wxGridNoCellCoords
&&
9022 m_currentCellCoords
.GetRow() < m_numRows
- 1 )
9024 int row
= m_currentCellCoords
.GetRow();
9025 int col
= m_currentCellCoords
.GetCol();
9027 if ( m_table
->IsEmptyCell(row
, col
) )
9029 // starting in an empty cell: find the next block of
9032 while ( row
< m_numRows
- 1 )
9035 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9039 else if ( m_table
->IsEmptyCell(row
+ 1, col
) )
9041 // starting at the bottom of a block: find the next block
9044 while ( row
< m_numRows
- 1 )
9047 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9053 // starting within a block: find the bottom of the block
9055 while ( row
< m_numRows
- 1 )
9058 if ( m_table
->IsEmptyCell(row
, col
) )
9066 MakeCellVisible( row
, col
);
9067 if ( expandSelection
)
9069 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9070 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9075 SetCurrentCell( row
, col
);
9084 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
9087 m_currentCellCoords
!= wxGridNoCellCoords
&&
9088 m_currentCellCoords
.GetCol() > 0 )
9090 int row
= m_currentCellCoords
.GetRow();
9091 int col
= m_currentCellCoords
.GetCol();
9093 if ( m_table
->IsEmptyCell(row
, col
) )
9095 // starting in an empty cell: find the next block of
9101 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9105 else if ( m_table
->IsEmptyCell(row
, col
- 1) )
9107 // starting at the left of a block: find the next block
9113 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9119 // starting within a block: find the left of the block
9124 if ( m_table
->IsEmptyCell(row
, col
) )
9132 MakeCellVisible( row
, col
);
9133 if ( expandSelection
)
9135 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9136 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9141 SetCurrentCell( row
, col
);
9150 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
9153 m_currentCellCoords
!= wxGridNoCellCoords
&&
9154 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
9156 int row
= m_currentCellCoords
.GetRow();
9157 int col
= m_currentCellCoords
.GetCol();
9159 if ( m_table
->IsEmptyCell(row
, col
) )
9161 // starting in an empty cell: find the next block of
9164 while ( col
< m_numCols
- 1 )
9167 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9171 else if ( m_table
->IsEmptyCell(row
, col
+ 1) )
9173 // starting at the right of a block: find the next block
9176 while ( col
< m_numCols
- 1 )
9179 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9185 // starting within a block: find the right of the block
9187 while ( col
< m_numCols
- 1 )
9190 if ( m_table
->IsEmptyCell(row
, col
) )
9198 MakeCellVisible( row
, col
);
9199 if ( expandSelection
)
9201 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9202 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9207 SetCurrentCell( row
, col
);
9217 // ------ Label values and formatting
9220 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
9223 *horiz
= m_rowLabelHorizAlign
;
9225 *vert
= m_rowLabelVertAlign
;
9228 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
9231 *horiz
= m_colLabelHorizAlign
;
9233 *vert
= m_colLabelVertAlign
;
9236 int wxGrid::GetColLabelTextOrientation()
9238 return m_colLabelTextOrientation
;
9241 wxString
wxGrid::GetRowLabelValue( int row
)
9245 return m_table
->GetRowLabelValue( row
);
9255 wxString
wxGrid::GetColLabelValue( int col
)
9259 return m_table
->GetColLabelValue( col
);
9269 void wxGrid::SetRowLabelSize( int width
)
9271 width
= wxMax( width
, 0 );
9272 if ( width
!= m_rowLabelWidth
)
9276 m_rowLabelWin
->Show( false );
9277 m_cornerLabelWin
->Show( false );
9279 else if ( m_rowLabelWidth
== 0 )
9281 m_rowLabelWin
->Show( true );
9282 if ( m_colLabelHeight
> 0 )
9283 m_cornerLabelWin
->Show( true );
9286 m_rowLabelWidth
= width
;
9288 wxScrolledWindow::Refresh( true );
9292 void wxGrid::SetColLabelSize( int height
)
9294 height
= wxMax( height
, 0 );
9295 if ( height
!= m_colLabelHeight
)
9299 m_colLabelWin
->Show( false );
9300 m_cornerLabelWin
->Show( false );
9302 else if ( m_colLabelHeight
== 0 )
9304 m_colLabelWin
->Show( true );
9305 if ( m_rowLabelWidth
> 0 )
9306 m_cornerLabelWin
->Show( true );
9309 m_colLabelHeight
= height
;
9311 wxScrolledWindow::Refresh( true );
9315 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
9317 if ( m_labelBackgroundColour
!= colour
)
9319 m_labelBackgroundColour
= colour
;
9320 m_rowLabelWin
->SetBackgroundColour( colour
);
9321 m_colLabelWin
->SetBackgroundColour( colour
);
9322 m_cornerLabelWin
->SetBackgroundColour( colour
);
9324 if ( !GetBatchCount() )
9326 m_rowLabelWin
->Refresh();
9327 m_colLabelWin
->Refresh();
9328 m_cornerLabelWin
->Refresh();
9333 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
9335 if ( m_labelTextColour
!= colour
)
9337 m_labelTextColour
= colour
;
9338 if ( !GetBatchCount() )
9340 m_rowLabelWin
->Refresh();
9341 m_colLabelWin
->Refresh();
9346 void wxGrid::SetLabelFont( const wxFont
& font
)
9349 if ( !GetBatchCount() )
9351 m_rowLabelWin
->Refresh();
9352 m_colLabelWin
->Refresh();
9356 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
9358 // allow old (incorrect) defs to be used
9361 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9362 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9363 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9368 case wxTOP
: vert
= wxALIGN_TOP
; break;
9369 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9370 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9373 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9375 m_rowLabelHorizAlign
= horiz
;
9378 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9380 m_rowLabelVertAlign
= vert
;
9383 if ( !GetBatchCount() )
9385 m_rowLabelWin
->Refresh();
9389 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
9391 // allow old (incorrect) defs to be used
9394 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9395 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9396 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9401 case wxTOP
: vert
= wxALIGN_TOP
; break;
9402 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9403 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9406 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9408 m_colLabelHorizAlign
= horiz
;
9411 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9413 m_colLabelVertAlign
= vert
;
9416 if ( !GetBatchCount() )
9418 m_colLabelWin
->Refresh();
9422 // Note: under MSW, the default column label font must be changed because it
9423 // does not support vertical printing
9425 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9426 // pGrid->SetLabelFont(font);
9427 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9429 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9431 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9432 m_colLabelTextOrientation
= textOrientation
;
9434 if ( !GetBatchCount() )
9435 m_colLabelWin
->Refresh();
9438 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9442 m_table
->SetRowLabelValue( row
, s
);
9443 if ( !GetBatchCount() )
9445 wxRect rect
= CellToRect( row
, 0 );
9446 if ( rect
.height
> 0 )
9448 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9450 rect
.width
= m_rowLabelWidth
;
9451 m_rowLabelWin
->Refresh( true, &rect
);
9457 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9461 m_table
->SetColLabelValue( col
, s
);
9462 if ( !GetBatchCount() )
9464 wxRect rect
= CellToRect( 0, col
);
9465 if ( rect
.width
> 0 )
9467 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9469 rect
.height
= m_colLabelHeight
;
9470 m_colLabelWin
->Refresh( true, &rect
);
9476 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9478 if ( m_gridLineColour
!= colour
)
9480 m_gridLineColour
= colour
;
9482 wxClientDC
dc( m_gridWin
);
9484 DrawAllGridLines( dc
, wxRegion() );
9488 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9490 if ( m_cellHighlightColour
!= colour
)
9492 m_cellHighlightColour
= colour
;
9494 wxClientDC
dc( m_gridWin
);
9496 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9497 DrawCellHighlight(dc
, attr
);
9502 void wxGrid::SetCellHighlightPenWidth(int width
)
9504 if (m_cellHighlightPenWidth
!= width
)
9506 m_cellHighlightPenWidth
= width
;
9508 // Just redrawing the cell highlight is not enough since that won't
9509 // make any visible change if the the thickness is getting smaller.
9510 int row
= m_currentCellCoords
.GetRow();
9511 int col
= m_currentCellCoords
.GetCol();
9512 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9515 wxRect rect
= CellToRect(row
, col
);
9516 m_gridWin
->Refresh(true, &rect
);
9520 void wxGrid::SetCellHighlightROPenWidth(int width
)
9522 if (m_cellHighlightROPenWidth
!= width
)
9524 m_cellHighlightROPenWidth
= width
;
9526 // Just redrawing the cell highlight is not enough since that won't
9527 // make any visible change if the the thickness is getting smaller.
9528 int row
= m_currentCellCoords
.GetRow();
9529 int col
= m_currentCellCoords
.GetCol();
9530 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9533 wxRect rect
= CellToRect(row
, col
);
9534 m_gridWin
->Refresh(true, &rect
);
9538 void wxGrid::EnableGridLines( bool enable
)
9540 if ( enable
!= m_gridLinesEnabled
)
9542 m_gridLinesEnabled
= enable
;
9544 if ( !GetBatchCount() )
9548 wxClientDC
dc( m_gridWin
);
9550 DrawAllGridLines( dc
, wxRegion() );
9554 m_gridWin
->Refresh();
9560 int wxGrid::GetDefaultRowSize()
9562 return m_defaultRowHeight
;
9565 int wxGrid::GetRowSize( int row
)
9567 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9569 return GetRowHeight(row
);
9572 int wxGrid::GetDefaultColSize()
9574 return m_defaultColWidth
;
9577 int wxGrid::GetColSize( int col
)
9579 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9581 return GetColWidth(col
);
9584 // ============================================================================
9585 // access to the grid attributes: each of them has a default value in the grid
9586 // itself and may be overidden on a per-cell basis
9587 // ============================================================================
9589 // ----------------------------------------------------------------------------
9590 // setting default attributes
9591 // ----------------------------------------------------------------------------
9593 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9595 m_defaultCellAttr
->SetBackgroundColour(col
);
9597 m_gridWin
->SetBackgroundColour(col
);
9601 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9603 m_defaultCellAttr
->SetTextColour(col
);
9606 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9608 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9611 void wxGrid::SetDefaultCellOverflow( bool allow
)
9613 m_defaultCellAttr
->SetOverflow(allow
);
9616 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9618 m_defaultCellAttr
->SetFont(font
);
9621 // For editors and renderers the type registry takes precedence over the
9622 // default attr, so we need to register the new editor/renderer for the string
9623 // data type in order to make setting a default editor/renderer appear to
9626 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9628 RegisterDataType(wxGRID_VALUE_STRING
,
9630 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9633 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9635 RegisterDataType(wxGRID_VALUE_STRING
,
9636 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9640 // ----------------------------------------------------------------------------
9641 // access to the default attrbiutes
9642 // ----------------------------------------------------------------------------
9644 wxColour
wxGrid::GetDefaultCellBackgroundColour()
9646 return m_defaultCellAttr
->GetBackgroundColour();
9649 wxColour
wxGrid::GetDefaultCellTextColour()
9651 return m_defaultCellAttr
->GetTextColour();
9654 wxFont
wxGrid::GetDefaultCellFont()
9656 return m_defaultCellAttr
->GetFont();
9659 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
9661 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9664 bool wxGrid::GetDefaultCellOverflow()
9666 return m_defaultCellAttr
->GetOverflow();
9669 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9671 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9674 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9676 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9679 // ----------------------------------------------------------------------------
9680 // access to cell attributes
9681 // ----------------------------------------------------------------------------
9683 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
9685 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9686 wxColour colour
= attr
->GetBackgroundColour();
9692 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
9694 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9695 wxColour colour
= attr
->GetTextColour();
9701 wxFont
wxGrid::GetCellFont( int row
, int col
)
9703 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9704 wxFont font
= attr
->GetFont();
9710 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
9712 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9713 attr
->GetAlignment(horiz
, vert
);
9717 bool wxGrid::GetCellOverflow( int row
, int col
)
9719 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9720 bool allow
= attr
->GetOverflow();
9726 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
9728 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9729 attr
->GetSize( num_rows
, num_cols
);
9733 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
9735 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9736 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9742 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
9744 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9745 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9751 bool wxGrid::IsReadOnly(int row
, int col
) const
9753 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9754 bool isReadOnly
= attr
->IsReadOnly();
9760 // ----------------------------------------------------------------------------
9761 // attribute support: cache, automatic provider creation, ...
9762 // ----------------------------------------------------------------------------
9764 bool wxGrid::CanHaveAttributes()
9771 return m_table
->CanHaveAttributes();
9774 void wxGrid::ClearAttrCache()
9776 if ( m_attrCache
.row
!= -1 )
9778 wxSafeDecRef(m_attrCache
.attr
);
9779 m_attrCache
.attr
= NULL
;
9780 m_attrCache
.row
= -1;
9784 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9788 wxGrid
*self
= (wxGrid
*)this; // const_cast
9790 self
->ClearAttrCache();
9791 self
->m_attrCache
.row
= row
;
9792 self
->m_attrCache
.col
= col
;
9793 self
->m_attrCache
.attr
= attr
;
9798 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9800 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9802 *attr
= m_attrCache
.attr
;
9803 wxSafeIncRef(m_attrCache
.attr
);
9805 #ifdef DEBUG_ATTR_CACHE
9806 gs_nAttrCacheHits
++;
9813 #ifdef DEBUG_ATTR_CACHE
9814 gs_nAttrCacheMisses
++;
9821 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9823 wxGridCellAttr
*attr
= NULL
;
9824 // Additional test to avoid looking at the cache e.g. for
9825 // wxNoCellCoords, as this will confuse memory management.
9828 if ( !LookupAttr(row
, col
, &attr
) )
9830 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9831 : (wxGridCellAttr
*)NULL
;
9832 CacheAttr(row
, col
, attr
);
9838 attr
->SetDefAttr(m_defaultCellAttr
);
9842 attr
= m_defaultCellAttr
;
9849 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9851 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9852 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9854 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9855 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9857 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9860 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9862 // artificially inc the ref count to match DecRef() in caller
9864 m_table
->SetAttr(attr
, row
, col
);
9870 // ----------------------------------------------------------------------------
9871 // setting column attributes (wrappers around SetColAttr)
9872 // ----------------------------------------------------------------------------
9874 void wxGrid::SetColFormatBool(int col
)
9876 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9879 void wxGrid::SetColFormatNumber(int col
)
9881 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9884 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9886 wxString typeName
= wxGRID_VALUE_FLOAT
;
9887 if ( (width
!= -1) || (precision
!= -1) )
9889 typeName
<< _T(':') << width
<< _T(',') << precision
;
9892 SetColFormatCustom(col
, typeName
);
9895 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9897 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9899 attr
= new wxGridCellAttr
;
9900 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9901 attr
->SetRenderer(renderer
);
9903 SetColAttr(col
, attr
);
9907 // ----------------------------------------------------------------------------
9908 // setting cell attributes: this is forwarded to the table
9909 // ----------------------------------------------------------------------------
9911 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9913 if ( CanHaveAttributes() )
9915 m_table
->SetAttr(attr
, row
, col
);
9924 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9926 if ( CanHaveAttributes() )
9928 m_table
->SetRowAttr(attr
, row
);
9937 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9939 if ( CanHaveAttributes() )
9941 m_table
->SetColAttr(attr
, col
);
9950 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9952 if ( CanHaveAttributes() )
9954 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9955 attr
->SetBackgroundColour(colour
);
9960 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9962 if ( CanHaveAttributes() )
9964 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9965 attr
->SetTextColour(colour
);
9970 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9972 if ( CanHaveAttributes() )
9974 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9975 attr
->SetFont(font
);
9980 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9982 if ( CanHaveAttributes() )
9984 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9985 attr
->SetAlignment(horiz
, vert
);
9990 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9992 if ( CanHaveAttributes() )
9994 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9995 attr
->SetOverflow(allow
);
10000 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
10002 if ( CanHaveAttributes() )
10004 int cell_rows
, cell_cols
;
10006 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10007 attr
->GetSize(&cell_rows
, &cell_cols
);
10008 attr
->SetSize(num_rows
, num_cols
);
10011 // Cannot set the size of a cell to 0 or negative values
10012 // While it is perfectly legal to do that, this function cannot
10013 // handle all the possibilies, do it by hand by getting the CellAttr.
10014 // You can only set the size of a cell to 1,1 or greater with this fn
10015 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
10016 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10017 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
10018 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10020 // if this was already a multicell then "turn off" the other cells first
10021 if ((cell_rows
> 1) || (cell_rows
> 1))
10024 for (j
=row
; j
< row
+ cell_rows
; j
++)
10026 for (i
=col
; i
< col
+ cell_cols
; i
++)
10028 if ((i
!= col
) || (j
!= row
))
10030 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10031 attr_stub
->SetSize( 1, 1 );
10032 attr_stub
->DecRef();
10038 // mark the cells that will be covered by this cell to
10039 // negative or zero values to point back at this cell
10040 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
10043 for (j
=row
; j
< row
+ num_rows
; j
++)
10045 for (i
=col
; i
< col
+ num_cols
; i
++)
10047 if ((i
!= col
) || (j
!= row
))
10049 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10050 attr_stub
->SetSize( row
- j
, col
- i
);
10051 attr_stub
->DecRef();
10059 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
10061 if ( CanHaveAttributes() )
10063 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10064 attr
->SetRenderer(renderer
);
10069 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
10071 if ( CanHaveAttributes() )
10073 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10074 attr
->SetEditor(editor
);
10079 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
10081 if ( CanHaveAttributes() )
10083 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10084 attr
->SetReadOnly(isReadOnly
);
10089 // ----------------------------------------------------------------------------
10090 // Data type registration
10091 // ----------------------------------------------------------------------------
10093 void wxGrid::RegisterDataType(const wxString
& typeName
,
10094 wxGridCellRenderer
* renderer
,
10095 wxGridCellEditor
* editor
)
10097 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
10101 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
10103 wxString typeName
= m_table
->GetTypeName(row
, col
);
10104 return GetDefaultEditorForType(typeName
);
10107 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
10109 wxString typeName
= m_table
->GetTypeName(row
, col
);
10110 return GetDefaultRendererForType(typeName
);
10113 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
10115 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10116 if ( index
== wxNOT_FOUND
)
10120 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
10121 wxFAIL_MSG(errStr
.c_str());
10126 return m_typeRegistry
->GetEditor(index
);
10129 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
10131 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10132 if ( index
== wxNOT_FOUND
)
10136 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
10137 wxFAIL_MSG(errStr
.c_str());
10142 return m_typeRegistry
->GetRenderer(index
);
10145 // ----------------------------------------------------------------------------
10147 // ----------------------------------------------------------------------------
10149 void wxGrid::EnableDragRowSize( bool enable
)
10151 m_canDragRowSize
= enable
;
10154 void wxGrid::EnableDragColSize( bool enable
)
10156 m_canDragColSize
= enable
;
10159 void wxGrid::EnableDragGridSize( bool enable
)
10161 m_canDragGridSize
= enable
;
10164 void wxGrid::EnableDragCell( bool enable
)
10166 m_canDragCell
= enable
;
10169 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
10171 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
10173 if ( resizeExistingRows
)
10175 // since we are resizing all rows to the default row size,
10176 // we can simply clear the row heights and row bottoms
10177 // arrays (which also allows us to take advantage of
10178 // some speed optimisations)
10179 m_rowHeights
.Empty();
10180 m_rowBottoms
.Empty();
10181 if ( !GetBatchCount() )
10186 void wxGrid::SetRowSize( int row
, int height
)
10188 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
10190 // See comment in SetColSize
10191 if ( height
< GetRowMinimalAcceptableHeight())
10194 if ( m_rowHeights
.IsEmpty() )
10196 // need to really create the array
10200 int h
= wxMax( 0, height
);
10201 int diff
= h
- m_rowHeights
[row
];
10203 m_rowHeights
[row
] = h
;
10205 for ( i
= row
; i
< m_numRows
; i
++ )
10207 m_rowBottoms
[i
] += diff
;
10210 if ( !GetBatchCount() )
10214 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
10216 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
10218 if ( resizeExistingCols
)
10220 // since we are resizing all columns to the default column size,
10221 // we can simply clear the col widths and col rights
10222 // arrays (which also allows us to take advantage of
10223 // some speed optimisations)
10224 m_colWidths
.Empty();
10225 m_colRights
.Empty();
10226 if ( !GetBatchCount() )
10231 void wxGrid::SetColSize( int col
, int width
)
10233 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
10235 // should we check that it's bigger than GetColMinimalWidth(col) here?
10237 // No, because it is reasonable to assume the library user know's
10238 // what he is doing. However we should test against the weaker
10239 // constraint of minimalAcceptableWidth, as this breaks rendering
10241 // This test then fixes sf.net bug #645734
10243 if ( width
< GetColMinimalAcceptableWidth() )
10246 if ( m_colWidths
.IsEmpty() )
10248 // need to really create the array
10252 // if < 0 then calculate new width from label
10256 wxArrayString lines
;
10257 wxClientDC
dc(m_colLabelWin
);
10258 dc
.SetFont(GetLabelFont());
10259 StringToLines(GetColLabelValue(col
), lines
);
10260 GetTextBoxSize(dc
, lines
, &w
, &h
);
10264 int w
= wxMax( 0, width
);
10265 int diff
= w
- m_colWidths
[col
];
10266 m_colWidths
[col
] = w
;
10270 for ( colPos
= GetColPos( col
); colPos
< m_numCols
; colPos
++ )
10272 i
= GetColAt( colPos
);
10273 m_colRights
[i
] += diff
;
10276 if ( !GetBatchCount() )
10280 void wxGrid::SetColMinimalWidth( int col
, int width
)
10282 if (width
> GetColMinimalAcceptableWidth())
10284 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10285 m_colMinWidths
[key
] = width
;
10289 void wxGrid::SetRowMinimalHeight( int row
, int width
)
10291 if (width
> GetRowMinimalAcceptableHeight())
10293 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10294 m_rowMinHeights
[key
] = width
;
10298 int wxGrid::GetColMinimalWidth(int col
) const
10300 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10301 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
10303 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
10306 int wxGrid::GetRowMinimalHeight(int row
) const
10308 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10309 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
10311 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
10314 void wxGrid::SetColMinimalAcceptableWidth( int width
)
10316 // We do allow a width of 0 since this gives us
10317 // an easy way to temporarily hiding columns.
10319 m_minAcceptableColWidth
= width
;
10322 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
10324 // We do allow a height of 0 since this gives us
10325 // an easy way to temporarily hiding rows.
10327 m_minAcceptableRowHeight
= height
;
10330 int wxGrid::GetColMinimalAcceptableWidth() const
10332 return m_minAcceptableColWidth
;
10335 int wxGrid::GetRowMinimalAcceptableHeight() const
10337 return m_minAcceptableRowHeight
;
10340 // ----------------------------------------------------------------------------
10342 // ----------------------------------------------------------------------------
10344 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
10346 wxClientDC
dc(m_gridWin
);
10348 // cancel editing of cell
10349 HideCellEditControl();
10350 SaveEditControlValue();
10352 // init both of them to avoid compiler warnings, even if we only need one
10360 wxCoord extent
, extentMax
= 0;
10361 int max
= column
? m_numRows
: m_numCols
;
10362 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
10369 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
10370 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
10373 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
10374 extent
= column
? size
.x
: size
.y
;
10375 if ( extent
> extentMax
)
10376 extentMax
= extent
;
10378 renderer
->DecRef();
10384 // now also compare with the column label extent
10386 dc
.SetFont( GetLabelFont() );
10390 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
10391 if ( GetColLabelTextOrientation() == wxVERTICAL
)
10395 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
10397 extent
= column
? w
: h
;
10398 if ( extent
> extentMax
)
10399 extentMax
= extent
;
10403 // empty column - give default extent (notice that if extentMax is less
10404 // than default extent but != 0, it's OK)
10405 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
10410 // leave some space around text
10418 SetColSize( col
, extentMax
);
10419 if ( !GetBatchCount() )
10422 m_gridWin
->GetClientSize( &cw
, &ch
);
10423 wxRect
rect ( CellToRect( 0, col
) );
10425 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10426 rect
.width
= cw
- rect
.x
;
10427 rect
.height
= m_colLabelHeight
;
10428 m_colLabelWin
->Refresh( true, &rect
);
10433 SetRowSize(row
, extentMax
);
10434 if ( !GetBatchCount() )
10437 m_gridWin
->GetClientSize( &cw
, &ch
);
10438 wxRect
rect( CellToRect( row
, 0 ) );
10440 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10441 rect
.width
= m_rowLabelWidth
;
10442 rect
.height
= ch
- rect
.y
;
10443 m_rowLabelWin
->Refresh( true, &rect
);
10450 SetColMinimalWidth(col
, extentMax
);
10452 SetRowMinimalHeight(row
, extentMax
);
10456 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10458 int width
= m_rowLabelWidth
;
10463 for ( int col
= 0; col
< m_numCols
; col
++ )
10466 AutoSizeColumn(col
, setAsMin
);
10468 width
+= GetColWidth(col
);
10477 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10479 int height
= m_colLabelHeight
;
10484 for ( int row
= 0; row
< m_numRows
; row
++ )
10487 AutoSizeRow(row
, setAsMin
);
10489 height
+= GetRowHeight(row
);
10498 void wxGrid::AutoSize()
10502 wxSize
size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
10504 // round up the size to a multiple of scroll step - this ensures that we
10505 // won't get the scrollbars if we're sized exactly to this width
10506 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
10509 GetScrollX(size
.x
+ m_extraWidth
+ 1) * m_scrollLineX
,
10510 GetScrollY(size
.y
+ m_extraHeight
+ 1) * m_scrollLineY
);
10512 // distribute the extra space between the columns/rows to avoid having
10513 // extra white space
10515 // Remove the extra m_extraWidth + 1 added above
10516 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
10517 if ( diff
&& m_numCols
)
10519 // try to resize the columns uniformly
10520 wxCoord diffPerCol
= diff
/ m_numCols
;
10523 for ( int col
= 0; col
< m_numCols
; col
++ )
10525 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
10529 // add remaining amount to the last columns
10530 diff
-= diffPerCol
* m_numCols
;
10533 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
10535 SetColSize(col
, GetColWidth(col
) + 1);
10541 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
10542 if ( diff
&& m_numRows
)
10544 // try to resize the columns uniformly
10545 wxCoord diffPerRow
= diff
/ m_numRows
;
10548 for ( int row
= 0; row
< m_numRows
; row
++ )
10550 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
10554 // add remaining amount to the last rows
10555 diff
-= diffPerRow
* m_numRows
;
10558 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
10560 SetRowSize(row
, GetRowHeight(row
) + 1);
10567 SetClientSize(sizeFit
);
10570 void wxGrid::AutoSizeRowLabelSize( int row
)
10572 wxArrayString lines
;
10575 // Hide the edit control, so it
10576 // won't interfere with drag-shrinking.
10577 if ( IsCellEditControlShown() )
10579 HideCellEditControl();
10580 SaveEditControlValue();
10583 // autosize row height depending on label text
10584 StringToLines( GetRowLabelValue( row
), lines
);
10585 wxClientDC
dc( m_rowLabelWin
);
10586 GetTextBoxSize( dc
, lines
, &w
, &h
);
10587 if ( h
< m_defaultRowHeight
)
10588 h
= m_defaultRowHeight
;
10589 SetRowSize(row
, h
);
10593 void wxGrid::AutoSizeColLabelSize( int col
)
10595 wxArrayString lines
;
10598 // Hide the edit control, so it
10599 // won't interfere with drag-shrinking.
10600 if ( IsCellEditControlShown() )
10602 HideCellEditControl();
10603 SaveEditControlValue();
10606 // autosize column width depending on label text
10607 StringToLines( GetColLabelValue( col
), lines
);
10608 wxClientDC
dc( m_colLabelWin
);
10609 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10610 GetTextBoxSize( dc
, lines
, &w
, &h
);
10612 GetTextBoxSize( dc
, lines
, &h
, &w
);
10613 if ( w
< m_defaultColWidth
)
10614 w
= m_defaultColWidth
;
10615 SetColSize(col
, w
);
10619 wxSize
wxGrid::DoGetBestSize() const
10621 // don't set sizes, only calculate them
10622 wxGrid
*self
= (wxGrid
*)this; // const_cast
10625 width
= self
->SetOrCalcColumnSizes(true);
10626 height
= self
->SetOrCalcRowSizes(true);
10633 // Round up to a multiple the scroll rate
10634 // NOTE: this still doesn't get rid of the scrollbars;
10635 // is there any magic incantation for that?
10637 GetScrollPixelsPerUnit(&xpu
, &ypu
);
10639 width
+= 1 + xpu
- (width
% xpu
);
10641 height
+= 1 + ypu
- (height
% ypu
);
10643 // limit to 1/4 of the screen size
10644 int maxwidth
, maxheight
;
10645 wxDisplaySize( &maxwidth
, &maxheight
);
10648 if ( width
> maxwidth
)
10650 if ( height
> maxheight
)
10651 height
= maxheight
;
10653 wxSize
best(width
, height
);
10655 // NOTE: This size should be cached, but first we need to add calls to
10656 // InvalidateBestSize everywhere that could change the results of this
10658 // CacheBestSize(size);
10668 wxPen
& wxGrid::GetDividerPen() const
10673 // ----------------------------------------------------------------------------
10674 // cell value accessor functions
10675 // ----------------------------------------------------------------------------
10677 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10681 m_table
->SetValue( row
, col
, s
);
10682 if ( !GetBatchCount() )
10685 wxRect
rect( CellToRect( row
, col
) );
10687 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10688 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10689 m_gridWin
->Refresh( false, &rect
);
10692 if ( m_currentCellCoords
.GetRow() == row
&&
10693 m_currentCellCoords
.GetCol() == col
&&
10694 IsCellEditControlShown())
10695 // Note: If we are using IsCellEditControlEnabled,
10696 // this interacts badly with calling SetCellValue from
10697 // an EVT_GRID_CELL_CHANGE handler.
10699 HideCellEditControl();
10700 ShowCellEditControl(); // will reread data from table
10705 // ----------------------------------------------------------------------------
10706 // block, row and column selection
10707 // ----------------------------------------------------------------------------
10709 void wxGrid::SelectRow( int row
, bool addToSelected
)
10711 if ( IsSelection() && !addToSelected
)
10715 m_selection
->SelectRow( row
, false, addToSelected
);
10718 void wxGrid::SelectCol( int col
, bool addToSelected
)
10720 if ( IsSelection() && !addToSelected
)
10724 m_selection
->SelectCol( col
, false, addToSelected
);
10727 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10728 bool addToSelected
)
10730 if ( IsSelection() && !addToSelected
)
10734 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10735 false, addToSelected
);
10738 void wxGrid::SelectAll()
10740 if ( m_numRows
> 0 && m_numCols
> 0 )
10743 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10747 // ----------------------------------------------------------------------------
10748 // cell, row and col deselection
10749 // ----------------------------------------------------------------------------
10751 void wxGrid::DeselectRow( int row
)
10753 if ( !m_selection
)
10756 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10758 if ( m_selection
->IsInSelection(row
, 0 ) )
10759 m_selection
->ToggleCellSelection(row
, 0);
10763 int nCols
= GetNumberCols();
10764 for ( int i
= 0; i
< nCols
; i
++ )
10766 if ( m_selection
->IsInSelection(row
, i
) )
10767 m_selection
->ToggleCellSelection(row
, i
);
10772 void wxGrid::DeselectCol( int col
)
10774 if ( !m_selection
)
10777 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10779 if ( m_selection
->IsInSelection(0, col
) )
10780 m_selection
->ToggleCellSelection(0, col
);
10784 int nRows
= GetNumberRows();
10785 for ( int i
= 0; i
< nRows
; i
++ )
10787 if ( m_selection
->IsInSelection(i
, col
) )
10788 m_selection
->ToggleCellSelection(i
, col
);
10793 void wxGrid::DeselectCell( int row
, int col
)
10795 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10796 m_selection
->ToggleCellSelection(row
, col
);
10799 bool wxGrid::IsSelection()
10801 return ( m_selection
&& (m_selection
->IsSelection() ||
10802 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10803 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10806 bool wxGrid::IsInSelection( int row
, int col
) const
10808 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10809 ( row
>= m_selectingTopLeft
.GetRow() &&
10810 col
>= m_selectingTopLeft
.GetCol() &&
10811 row
<= m_selectingBottomRight
.GetRow() &&
10812 col
<= m_selectingBottomRight
.GetCol() )) );
10815 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10819 wxGridCellCoordsArray a
;
10823 return m_selection
->m_cellSelection
;
10826 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10830 wxGridCellCoordsArray a
;
10834 return m_selection
->m_blockSelectionTopLeft
;
10837 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10841 wxGridCellCoordsArray a
;
10845 return m_selection
->m_blockSelectionBottomRight
;
10848 wxArrayInt
wxGrid::GetSelectedRows() const
10856 return m_selection
->m_rowSelection
;
10859 wxArrayInt
wxGrid::GetSelectedCols() const
10867 return m_selection
->m_colSelection
;
10870 void wxGrid::ClearSelection()
10872 m_selectingTopLeft
=
10873 m_selectingBottomRight
=
10874 m_selectingKeyboard
= wxGridNoCellCoords
;
10876 m_selection
->ClearSelection();
10879 // This function returns the rectangle that encloses the given block
10880 // in device coords clipped to the client size of the grid window.
10882 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10883 const wxGridCellCoords
&bottomRight
)
10885 wxRect
rect( wxGridNoCellRect
);
10888 cellRect
= CellToRect( topLeft
);
10889 if ( cellRect
!= wxGridNoCellRect
)
10895 rect
= wxRect(0, 0, 0, 0);
10898 cellRect
= CellToRect( bottomRight
);
10899 if ( cellRect
!= wxGridNoCellRect
)
10905 return wxGridNoCellRect
;
10909 int left
= rect
.GetLeft();
10910 int top
= rect
.GetTop();
10911 int right
= rect
.GetRight();
10912 int bottom
= rect
.GetBottom();
10914 int leftCol
= topLeft
.GetCol();
10915 int topRow
= topLeft
.GetRow();
10916 int rightCol
= bottomRight
.GetCol();
10917 int bottomRow
= bottomRight
.GetRow();
10925 leftCol
= rightCol
;
10935 topRow
= bottomRow
;
10939 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10941 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10943 if ((j
== topRow
) || (j
== bottomRow
) || (i
== leftCol
) || (i
== rightCol
))
10945 cellRect
= CellToRect( j
, i
);
10947 if (cellRect
.x
< left
)
10949 if (cellRect
.y
< top
)
10951 if (cellRect
.x
+ cellRect
.width
> right
)
10952 right
= cellRect
.x
+ cellRect
.width
;
10953 if (cellRect
.y
+ cellRect
.height
> bottom
)
10954 bottom
= cellRect
.y
+ cellRect
.height
;
10958 i
= rightCol
; // jump over inner cells.
10963 // convert to scrolled coords
10965 CalcScrolledPosition( left
, top
, &left
, &top
);
10966 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10969 m_gridWin
->GetClientSize( &cw
, &ch
);
10971 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10972 return wxRect(0,0,0,0);
10974 rect
.SetLeft( wxMax(0, left
) );
10975 rect
.SetTop( wxMax(0, top
) );
10976 rect
.SetRight( wxMin(cw
, right
) );
10977 rect
.SetBottom( wxMin(ch
, bottom
) );
10982 // ----------------------------------------------------------------------------
10983 // grid event classes
10984 // ----------------------------------------------------------------------------
10986 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10988 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10989 int row
, int col
, int x
, int y
, bool sel
,
10990 bool control
, bool shift
, bool alt
, bool meta
)
10991 : wxNotifyEvent( type
, id
)
10998 m_control
= control
;
11003 SetEventObject(obj
);
11007 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
11009 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
11010 int rowOrCol
, int x
, int y
,
11011 bool control
, bool shift
, bool alt
, bool meta
)
11012 : wxNotifyEvent( type
, id
)
11014 m_rowOrCol
= rowOrCol
;
11017 m_control
= control
;
11022 SetEventObject(obj
);
11026 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
11028 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
11029 const wxGridCellCoords
& topLeft
,
11030 const wxGridCellCoords
& bottomRight
,
11031 bool sel
, bool control
,
11032 bool shift
, bool alt
, bool meta
)
11033 : wxNotifyEvent( type
, id
)
11035 m_topLeft
= topLeft
;
11036 m_bottomRight
= bottomRight
;
11038 m_control
= control
;
11043 SetEventObject(obj
);
11047 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
11049 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
11050 wxObject
* obj
, int row
,
11051 int col
, wxControl
* ctrl
)
11052 : wxCommandEvent(type
, id
)
11054 SetEventObject(obj
);
11060 #endif // wxUSE_GRID