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
->SetOverflow( m_overflow
== Overflow
);
2296 attr
->SetKind( m_attrkind
);
2301 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2303 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2304 SetTextColour(mergefrom
->GetTextColour());
2305 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2306 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2307 if ( !HasFont() && mergefrom
->HasFont() )
2308 SetFont(mergefrom
->GetFont());
2309 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2312 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2313 SetAlignment(hAlign
, vAlign
);
2315 if ( !HasSize() && mergefrom
->HasSize() )
2316 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2318 // Directly access member functions as GetRender/Editor don't just return
2319 // m_renderer/m_editor
2321 // Maybe add support for merge of Render and Editor?
2322 if (!HasRenderer() && mergefrom
->HasRenderer() )
2324 m_renderer
= mergefrom
->m_renderer
;
2325 m_renderer
->IncRef();
2327 if ( !HasEditor() && mergefrom
->HasEditor() )
2329 m_editor
= mergefrom
->m_editor
;
2332 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2333 SetReadOnly(mergefrom
->IsReadOnly());
2335 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2336 SetOverflow(mergefrom
->GetOverflow());
2338 SetDefAttr(mergefrom
->m_defGridAttr
);
2341 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2343 // The size of a cell is normally 1,1
2345 // If this cell is larger (2,2) then this is the top left cell
2346 // the other cells that will be covered (lower right cells) must be
2347 // set to negative or zero values such that
2348 // row + num_rows of the covered cell points to the larger cell (this cell)
2349 // same goes for the col + num_cols.
2351 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2353 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
2354 !((num_rows
<= 0) && (num_cols
> 0)) ||
2355 !((num_rows
== 0) && (num_cols
== 0))),
2356 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2358 m_sizeRows
= num_rows
;
2359 m_sizeCols
= num_cols
;
2362 const wxColour
& wxGridCellAttr::GetTextColour() const
2364 if (HasTextColour())
2368 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2370 return m_defGridAttr
->GetTextColour();
2374 wxFAIL_MSG(wxT("Missing default cell attribute"));
2375 return wxNullColour
;
2379 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2381 if (HasBackgroundColour())
2385 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2387 return m_defGridAttr
->GetBackgroundColour();
2391 wxFAIL_MSG(wxT("Missing default cell attribute"));
2392 return wxNullColour
;
2396 const wxFont
& wxGridCellAttr::GetFont() const
2402 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2404 return m_defGridAttr
->GetFont();
2408 wxFAIL_MSG(wxT("Missing default cell attribute"));
2413 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2422 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2424 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2428 wxFAIL_MSG(wxT("Missing default cell attribute"));
2432 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2435 *num_rows
= m_sizeRows
;
2437 *num_cols
= m_sizeCols
;
2440 // GetRenderer and GetEditor use a slightly different decision path about
2441 // which attribute to use. If a non-default attr object has one then it is
2442 // used, otherwise the default editor or renderer is fetched from the grid and
2443 // used. It should be the default for the data type of the cell. If it is
2444 // NULL (because the table has a type that the grid does not have in its
2445 // registry), then the grid's default editor or renderer is used.
2447 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2449 wxGridCellRenderer
*renderer
= NULL
;
2451 if ( m_renderer
&& this != m_defGridAttr
)
2453 // use the cells renderer if it has one
2454 renderer
= m_renderer
;
2457 else // no non-default cell renderer
2459 // get default renderer for the data type
2462 // GetDefaultRendererForCell() will do IncRef() for us
2463 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2466 if ( renderer
== NULL
)
2468 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2470 // if we still don't have one then use the grid default
2471 // (no need for IncRef() here neither)
2472 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2474 else // default grid attr
2476 // use m_renderer which we had decided not to use initially
2477 renderer
= m_renderer
;
2484 // we're supposed to always find something
2485 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2490 // same as above, except for s/renderer/editor/g
2491 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2493 wxGridCellEditor
*editor
= NULL
;
2495 if ( m_editor
&& this != m_defGridAttr
)
2497 // use the cells editor if it has one
2501 else // no non default cell editor
2503 // get default editor for the data type
2506 // GetDefaultEditorForCell() will do IncRef() for us
2507 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2510 if ( editor
== NULL
)
2512 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2514 // if we still don't have one then use the grid default
2515 // (no need for IncRef() here neither)
2516 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2518 else // default grid attr
2520 // use m_editor which we had decided not to use initially
2528 // we're supposed to always find something
2529 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2534 // ----------------------------------------------------------------------------
2535 // wxGridCellAttrData
2536 // ----------------------------------------------------------------------------
2538 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2540 int n
= FindIndex(row
, col
);
2541 if ( n
== wxNOT_FOUND
)
2543 // add the attribute
2544 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2548 // free the old attribute
2549 m_attrs
[(size_t)n
].attr
->DecRef();
2553 // change the attribute
2554 m_attrs
[(size_t)n
].attr
= attr
;
2558 // remove this attribute
2559 m_attrs
.RemoveAt((size_t)n
);
2564 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2566 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2568 int n
= FindIndex(row
, col
);
2569 if ( n
!= wxNOT_FOUND
)
2571 attr
= m_attrs
[(size_t)n
].attr
;
2578 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2580 size_t count
= m_attrs
.GetCount();
2581 for ( size_t n
= 0; n
< count
; n
++ )
2583 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2584 wxCoord row
= coords
.GetRow();
2585 if ((size_t)row
>= pos
)
2589 // If rows inserted, include row counter where necessary
2590 coords
.SetRow(row
+ numRows
);
2592 else if (numRows
< 0)
2594 // If rows deleted ...
2595 if ((size_t)row
>= pos
- numRows
)
2597 // ...either decrement row counter (if row still exists)...
2598 coords
.SetRow(row
+ numRows
);
2602 // ...or remove the attribute
2603 // No need to DecRef the attribute itself since this is
2604 // done be wxGridCellWithAttr's destructor!
2605 m_attrs
.RemoveAt(n
);
2614 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2616 size_t count
= m_attrs
.GetCount();
2617 for ( size_t n
= 0; n
< count
; n
++ )
2619 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2620 wxCoord col
= coords
.GetCol();
2621 if ( (size_t)col
>= pos
)
2625 // If rows inserted, include row counter where necessary
2626 coords
.SetCol(col
+ numCols
);
2628 else if (numCols
< 0)
2630 // If rows deleted ...
2631 if ((size_t)col
>= pos
- numCols
)
2633 // ...either decrement row counter (if row still exists)...
2634 coords
.SetCol(col
+ numCols
);
2638 // ...or remove the attribute
2639 // No need to DecRef the attribute itself since this is
2640 // done be wxGridCellWithAttr's destructor!
2641 m_attrs
.RemoveAt(n
);
2650 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2652 size_t count
= m_attrs
.GetCount();
2653 for ( size_t n
= 0; n
< count
; n
++ )
2655 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2656 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2665 // ----------------------------------------------------------------------------
2666 // wxGridRowOrColAttrData
2667 // ----------------------------------------------------------------------------
2669 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2671 size_t count
= m_attrs
.Count();
2672 for ( size_t n
= 0; n
< count
; n
++ )
2674 m_attrs
[n
]->DecRef();
2678 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2680 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2682 int n
= m_rowsOrCols
.Index(rowOrCol
);
2683 if ( n
!= wxNOT_FOUND
)
2685 attr
= m_attrs
[(size_t)n
];
2692 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2694 int i
= m_rowsOrCols
.Index(rowOrCol
);
2695 if ( i
== wxNOT_FOUND
)
2697 // add the attribute
2698 m_rowsOrCols
.Add(rowOrCol
);
2703 size_t n
= (size_t)i
;
2706 // change the attribute
2707 m_attrs
[n
]->DecRef();
2712 // remove this attribute
2713 m_attrs
[n
]->DecRef();
2714 m_rowsOrCols
.RemoveAt(n
);
2715 m_attrs
.RemoveAt(n
);
2720 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2722 size_t count
= m_attrs
.GetCount();
2723 for ( size_t n
= 0; n
< count
; n
++ )
2725 int & rowOrCol
= m_rowsOrCols
[n
];
2726 if ( (size_t)rowOrCol
>= pos
)
2728 if ( numRowsOrCols
> 0 )
2730 // If rows inserted, include row counter where necessary
2731 rowOrCol
+= numRowsOrCols
;
2733 else if ( numRowsOrCols
< 0)
2735 // If rows deleted, either decrement row counter (if row still exists)
2736 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2737 rowOrCol
+= numRowsOrCols
;
2740 m_rowsOrCols
.RemoveAt(n
);
2741 m_attrs
[n
]->DecRef();
2742 m_attrs
.RemoveAt(n
);
2751 // ----------------------------------------------------------------------------
2752 // wxGridCellAttrProvider
2753 // ----------------------------------------------------------------------------
2755 wxGridCellAttrProvider::wxGridCellAttrProvider()
2757 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2760 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2765 void wxGridCellAttrProvider::InitData()
2767 m_data
= new wxGridCellAttrProviderData
;
2770 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2771 wxGridCellAttr::wxAttrKind kind
) const
2773 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2778 case (wxGridCellAttr::Any
):
2779 // Get cached merge attributes.
2780 // Currently not used as no cache implemented as not mutable
2781 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2784 // Basically implement old version.
2785 // Also check merge cache, so we don't have to re-merge every time..
2786 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2787 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2788 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2790 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
2792 // Two or more are non NULL
2793 attr
= new wxGridCellAttr
;
2794 attr
->SetKind(wxGridCellAttr::Merged
);
2796 // Order is important..
2799 attr
->MergeWith(attrcell
);
2804 attr
->MergeWith(attrcol
);
2809 attr
->MergeWith(attrrow
);
2813 // store merge attr if cache implemented
2815 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2819 // one or none is non null return it or null.
2838 case (wxGridCellAttr::Cell
):
2839 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2842 case (wxGridCellAttr::Col
):
2843 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2846 case (wxGridCellAttr::Row
):
2847 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2852 // (wxGridCellAttr::Default):
2853 // (wxGridCellAttr::Merged):
2861 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2867 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2870 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2875 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2878 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2883 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2886 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2890 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2892 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2896 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2900 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2902 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2906 // ----------------------------------------------------------------------------
2907 // wxGridTypeRegistry
2908 // ----------------------------------------------------------------------------
2910 wxGridTypeRegistry::~wxGridTypeRegistry()
2912 size_t count
= m_typeinfo
.Count();
2913 for ( size_t i
= 0; i
< count
; i
++ )
2914 delete m_typeinfo
[i
];
2917 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2918 wxGridCellRenderer
* renderer
,
2919 wxGridCellEditor
* editor
)
2921 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2923 // is it already registered?
2924 int loc
= FindRegisteredDataType(typeName
);
2925 if ( loc
!= wxNOT_FOUND
)
2927 delete m_typeinfo
[loc
];
2928 m_typeinfo
[loc
] = info
;
2932 m_typeinfo
.Add(info
);
2936 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2938 size_t count
= m_typeinfo
.GetCount();
2939 for ( size_t i
= 0; i
< count
; i
++ )
2941 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2950 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2952 int index
= FindRegisteredDataType(typeName
);
2953 if ( index
== wxNOT_FOUND
)
2955 // check whether this is one of the standard ones, in which case
2956 // register it "on the fly"
2958 if ( typeName
== wxGRID_VALUE_STRING
)
2960 RegisterDataType(wxGRID_VALUE_STRING
,
2961 new wxGridCellStringRenderer
,
2962 new wxGridCellTextEditor
);
2965 #endif // wxUSE_TEXTCTRL
2967 if ( typeName
== wxGRID_VALUE_BOOL
)
2969 RegisterDataType(wxGRID_VALUE_BOOL
,
2970 new wxGridCellBoolRenderer
,
2971 new wxGridCellBoolEditor
);
2974 #endif // wxUSE_CHECKBOX
2976 if ( typeName
== wxGRID_VALUE_NUMBER
)
2978 RegisterDataType(wxGRID_VALUE_NUMBER
,
2979 new wxGridCellNumberRenderer
,
2980 new wxGridCellNumberEditor
);
2982 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2984 RegisterDataType(wxGRID_VALUE_FLOAT
,
2985 new wxGridCellFloatRenderer
,
2986 new wxGridCellFloatEditor
);
2989 #endif // wxUSE_TEXTCTRL
2991 if ( typeName
== wxGRID_VALUE_CHOICE
)
2993 RegisterDataType(wxGRID_VALUE_CHOICE
,
2994 new wxGridCellStringRenderer
,
2995 new wxGridCellChoiceEditor
);
2998 #endif // wxUSE_COMBOBOX
3003 // we get here only if just added the entry for this type, so return
3005 index
= m_typeinfo
.GetCount() - 1;
3011 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
3013 int index
= FindDataType(typeName
);
3014 if ( index
== wxNOT_FOUND
)
3016 // the first part of the typename is the "real" type, anything after ':'
3017 // are the parameters for the renderer
3018 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
3019 if ( index
== wxNOT_FOUND
)
3024 wxGridCellRenderer
*renderer
= GetRenderer(index
);
3025 wxGridCellRenderer
*rendererOld
= renderer
;
3026 renderer
= renderer
->Clone();
3027 rendererOld
->DecRef();
3029 wxGridCellEditor
*editor
= GetEditor(index
);
3030 wxGridCellEditor
*editorOld
= editor
;
3031 editor
= editor
->Clone();
3032 editorOld
->DecRef();
3034 // do it even if there are no parameters to reset them to defaults
3035 wxString params
= typeName
.AfterFirst(_T(':'));
3036 renderer
->SetParameters(params
);
3037 editor
->SetParameters(params
);
3039 // register the new typename
3040 RegisterDataType(typeName
, renderer
, editor
);
3042 // we just registered it, it's the last one
3043 index
= m_typeinfo
.GetCount() - 1;
3049 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
3051 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
3058 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
3060 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
3067 // ----------------------------------------------------------------------------
3069 // ----------------------------------------------------------------------------
3071 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
3073 wxGridTableBase::wxGridTableBase()
3075 m_view
= (wxGrid
*) NULL
;
3076 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
3079 wxGridTableBase::~wxGridTableBase()
3081 delete m_attrProvider
;
3084 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
3086 delete m_attrProvider
;
3087 m_attrProvider
= attrProvider
;
3090 bool wxGridTableBase::CanHaveAttributes()
3092 if ( ! GetAttrProvider() )
3094 // use the default attr provider by default
3095 SetAttrProvider(new wxGridCellAttrProvider
);
3101 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
3103 if ( m_attrProvider
)
3104 return m_attrProvider
->GetAttr(row
, col
, kind
);
3106 return (wxGridCellAttr
*)NULL
;
3109 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
3111 if ( m_attrProvider
)
3113 attr
->SetKind(wxGridCellAttr::Cell
);
3114 m_attrProvider
->SetAttr(attr
, row
, col
);
3118 // as we take ownership of the pointer and don't store it, we must
3124 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3126 if ( m_attrProvider
)
3128 attr
->SetKind(wxGridCellAttr::Row
);
3129 m_attrProvider
->SetRowAttr(attr
, row
);
3133 // as we take ownership of the pointer and don't store it, we must
3139 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3141 if ( m_attrProvider
)
3143 attr
->SetKind(wxGridCellAttr::Col
);
3144 m_attrProvider
->SetColAttr(attr
, col
);
3148 // as we take ownership of the pointer and don't store it, we must
3154 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3155 size_t WXUNUSED(numRows
) )
3157 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3162 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3164 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3169 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3170 size_t WXUNUSED(numRows
) )
3172 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3177 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3178 size_t WXUNUSED(numCols
) )
3180 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3185 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3187 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3192 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3193 size_t WXUNUSED(numCols
) )
3195 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3200 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3204 // RD: Starting the rows at zero confuses users,
3205 // no matter how much it makes sense to us geeks.
3211 wxString
wxGridTableBase::GetColLabelValue( int col
)
3213 // default col labels are:
3214 // cols 0 to 25 : A-Z
3215 // cols 26 to 675 : AA-ZZ
3220 for ( n
= 1; ; n
++ )
3222 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
3228 // reverse the string...
3230 for ( i
= 0; i
< n
; i
++ )
3238 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3240 return wxGRID_VALUE_STRING
;
3243 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3244 const wxString
& typeName
)
3246 return typeName
== wxGRID_VALUE_STRING
;
3249 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3251 return CanGetValueAs(row
, col
, typeName
);
3254 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3259 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3264 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3269 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3270 long WXUNUSED(value
) )
3274 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3275 double WXUNUSED(value
) )
3279 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3280 bool WXUNUSED(value
) )
3284 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3285 const wxString
& WXUNUSED(typeName
) )
3290 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3291 const wxString
& WXUNUSED(typeName
),
3292 void* WXUNUSED(value
) )
3296 //////////////////////////////////////////////////////////////////////
3298 // Message class for the grid table to send requests and notifications
3302 wxGridTableMessage::wxGridTableMessage()
3304 m_table
= (wxGridTableBase
*) NULL
;
3310 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3311 int commandInt1
, int commandInt2
)
3315 m_comInt1
= commandInt1
;
3316 m_comInt2
= commandInt2
;
3319 //////////////////////////////////////////////////////////////////////
3321 // A basic grid table for string data. An object of this class will
3322 // created by wxGrid if you don't specify an alternative table class.
3325 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3327 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3329 wxGridStringTable::wxGridStringTable()
3334 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3337 m_data
.Alloc( numRows
);
3340 sa
.Alloc( numCols
);
3341 sa
.Add( wxEmptyString
, numCols
);
3343 m_data
.Add( sa
, numRows
);
3346 wxGridStringTable::~wxGridStringTable()
3350 int wxGridStringTable::GetNumberRows()
3352 return m_data
.GetCount();
3355 int wxGridStringTable::GetNumberCols()
3357 if ( m_data
.GetCount() > 0 )
3358 return m_data
[0].GetCount();
3363 wxString
wxGridStringTable::GetValue( int row
, int col
)
3365 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3367 _T("invalid row or column index in wxGridStringTable") );
3369 return m_data
[row
][col
];
3372 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3374 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3375 _T("invalid row or column index in wxGridStringTable") );
3377 m_data
[row
][col
] = value
;
3380 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3382 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3384 _T("invalid row or column index in wxGridStringTable") );
3386 return (m_data
[row
][col
] == wxEmptyString
);
3389 void wxGridStringTable::Clear()
3392 int numRows
, numCols
;
3394 numRows
= m_data
.GetCount();
3397 numCols
= m_data
[0].GetCount();
3399 for ( row
= 0; row
< numRows
; row
++ )
3401 for ( col
= 0; col
< numCols
; col
++ )
3403 m_data
[row
][col
] = wxEmptyString
;
3409 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3411 size_t curNumRows
= m_data
.GetCount();
3412 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3413 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3415 if ( pos
>= curNumRows
)
3417 return AppendRows( numRows
);
3421 sa
.Alloc( curNumCols
);
3422 sa
.Add( wxEmptyString
, curNumCols
);
3423 m_data
.Insert( sa
, pos
, numRows
);
3427 wxGridTableMessage
msg( this,
3428 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3432 GetView()->ProcessTableMessage( msg
);
3438 bool wxGridStringTable::AppendRows( size_t numRows
)
3440 size_t curNumRows
= m_data
.GetCount();
3441 size_t curNumCols
= ( curNumRows
> 0
3442 ? m_data
[0].GetCount()
3443 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3446 if ( curNumCols
> 0 )
3448 sa
.Alloc( curNumCols
);
3449 sa
.Add( wxEmptyString
, curNumCols
);
3452 m_data
.Add( sa
, numRows
);
3456 wxGridTableMessage
msg( this,
3457 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3460 GetView()->ProcessTableMessage( msg
);
3466 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3468 size_t curNumRows
= m_data
.GetCount();
3470 if ( pos
>= curNumRows
)
3472 wxFAIL_MSG( wxString::Format
3474 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3476 (unsigned long)numRows
,
3477 (unsigned long)curNumRows
3483 if ( numRows
> curNumRows
- pos
)
3485 numRows
= curNumRows
- pos
;
3488 if ( numRows
>= curNumRows
)
3494 m_data
.RemoveAt( pos
, numRows
);
3499 wxGridTableMessage
msg( this,
3500 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3504 GetView()->ProcessTableMessage( msg
);
3510 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3514 size_t curNumRows
= m_data
.GetCount();
3515 size_t curNumCols
= ( curNumRows
> 0
3516 ? m_data
[0].GetCount()
3517 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3519 if ( pos
>= curNumCols
)
3521 return AppendCols( numCols
);
3524 if ( !m_colLabels
.IsEmpty() )
3526 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
3529 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
3530 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
3533 for ( row
= 0; row
< curNumRows
; row
++ )
3535 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3537 m_data
[row
].Insert( wxEmptyString
, col
);
3543 wxGridTableMessage
msg( this,
3544 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3548 GetView()->ProcessTableMessage( msg
);
3554 bool wxGridStringTable::AppendCols( size_t numCols
)
3558 size_t curNumRows
= m_data
.GetCount();
3563 // TODO: something better than this ?
3565 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3570 for ( row
= 0; row
< curNumRows
; row
++ )
3572 m_data
[row
].Add( wxEmptyString
, numCols
);
3577 wxGridTableMessage
msg( this,
3578 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3581 GetView()->ProcessTableMessage( msg
);
3587 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3591 size_t curNumRows
= m_data
.GetCount();
3592 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3593 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3595 if ( pos
>= curNumCols
)
3597 wxFAIL_MSG( wxString::Format
3599 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3601 (unsigned long)numCols
,
3602 (unsigned long)curNumCols
3609 colID
= GetView()->GetColAt( pos
);
3613 if ( numCols
> curNumCols
- colID
)
3615 numCols
= curNumCols
- colID
;
3618 if ( !m_colLabels
.IsEmpty() )
3620 m_colLabels
.RemoveAt( colID
, numCols
);
3623 for ( row
= 0; row
< curNumRows
; row
++ )
3625 if ( numCols
>= curNumCols
)
3627 m_data
[row
].Clear();
3631 m_data
[row
].RemoveAt( colID
, numCols
);
3637 wxGridTableMessage
msg( this,
3638 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3642 GetView()->ProcessTableMessage( msg
);
3648 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3650 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3652 // using default label
3654 return wxGridTableBase::GetRowLabelValue( row
);
3658 return m_rowLabels
[row
];
3662 wxString
wxGridStringTable::GetColLabelValue( int col
)
3664 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3666 // using default label
3668 return wxGridTableBase::GetColLabelValue( col
);
3672 return m_colLabels
[col
];
3676 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3678 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3680 int n
= m_rowLabels
.GetCount();
3683 for ( i
= n
; i
<= row
; i
++ )
3685 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3689 m_rowLabels
[row
] = value
;
3692 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3694 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3696 int n
= m_colLabels
.GetCount();
3699 for ( i
= n
; i
<= col
; i
++ )
3701 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3705 m_colLabels
[col
] = value
;
3709 //////////////////////////////////////////////////////////////////////
3710 //////////////////////////////////////////////////////////////////////
3712 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3714 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3715 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3716 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3717 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3718 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3719 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3720 EVT_CHAR( wxGridRowLabelWindow::OnChar
)
3723 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3725 const wxPoint
&pos
, const wxSize
&size
)
3726 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxFULL_REPAINT_ON_RESIZE
)
3731 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3735 // NO - don't do this because it will set both the x and y origin
3736 // coords to match the parent scrolled window and we just want to
3737 // set the y coord - MB
3739 // m_owner->PrepareDC( dc );
3742 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3743 wxPoint pt
= dc
.GetDeviceOrigin();
3744 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
3746 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3747 m_owner
->DrawRowLabels( dc
, rows
);
3750 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3752 m_owner
->ProcessRowLabelMouseEvent( event
);
3755 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3757 m_owner
->GetEventHandler()->ProcessEvent( event
);
3760 // This seems to be required for wxMotif otherwise the mouse
3761 // cursor must be in the cell edit control to get key events
3763 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3765 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3769 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3771 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3775 void wxGridRowLabelWindow::OnChar( wxKeyEvent
& event
)
3777 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3781 //////////////////////////////////////////////////////////////////////
3783 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3785 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3786 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3787 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3788 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3789 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3790 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3791 EVT_CHAR( wxGridColLabelWindow::OnChar
)
3794 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3796 const wxPoint
&pos
, const wxSize
&size
)
3797 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxFULL_REPAINT_ON_RESIZE
)
3802 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3806 // NO - don't do this because it will set both the x and y origin
3807 // coords to match the parent scrolled window and we just want to
3808 // set the x coord - MB
3810 // m_owner->PrepareDC( dc );
3813 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3814 wxPoint pt
= dc
.GetDeviceOrigin();
3815 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3816 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
3818 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
3820 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3821 m_owner
->DrawColLabels( dc
, cols
);
3824 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3826 m_owner
->ProcessColLabelMouseEvent( event
);
3829 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3831 m_owner
->GetEventHandler()->ProcessEvent( event
);
3834 // This seems to be required for wxMotif otherwise the mouse
3835 // cursor must be in the cell edit control to get key events
3837 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3839 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3843 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3845 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3849 void wxGridColLabelWindow::OnChar( wxKeyEvent
& event
)
3851 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3855 //////////////////////////////////////////////////////////////////////
3857 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3859 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3860 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3861 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3862 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3863 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3864 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3865 EVT_CHAR( wxGridCornerLabelWindow::OnChar
)
3868 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3870 const wxPoint
&pos
, const wxSize
&size
)
3871 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxFULL_REPAINT_ON_RESIZE
)
3876 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3880 int client_height
= 0;
3881 int client_width
= 0;
3882 GetClientSize( &client_width
, &client_height
);
3884 // VZ: any reason for this ifdef? (FIXME)
3889 rect
.SetWidth( client_width
- 2 );
3890 rect
.SetHeight( client_height
- 2 );
3892 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3894 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
3895 dc
.DrawLine( client_width
- 1, client_height
- 1, client_width
- 1, 0 );
3896 dc
.DrawLine( client_width
- 1, client_height
- 1, 0, client_height
- 1 );
3897 dc
.DrawLine( 0, 0, client_width
, 0 );
3898 dc
.DrawLine( 0, 0, 0, client_height
);
3900 dc
.SetPen( *wxWHITE_PEN
);
3901 dc
.DrawLine( 1, 1, client_width
- 1, 1 );
3902 dc
.DrawLine( 1, 1, 1, client_height
- 1 );
3906 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3908 m_owner
->ProcessCornerLabelMouseEvent( event
);
3911 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3913 m_owner
->GetEventHandler()->ProcessEvent(event
);
3916 // This seems to be required for wxMotif otherwise the mouse
3917 // cursor must be in the cell edit control to get key events
3919 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3921 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3925 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3927 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3931 void wxGridCornerLabelWindow::OnChar( wxKeyEvent
& event
)
3933 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3937 //////////////////////////////////////////////////////////////////////
3939 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3941 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3942 EVT_PAINT( wxGridWindow::OnPaint
)
3943 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3944 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3945 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3946 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3947 EVT_CHAR( wxGridWindow::OnChar
)
3948 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3949 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3950 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3953 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3954 wxGridRowLabelWindow
*rowLblWin
,
3955 wxGridColLabelWindow
*colLblWin
,
3958 const wxSize
&size
)
3960 parent
, id
, pos
, size
,
3961 wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
| wxFULL_REPAINT_ON_RESIZE
,
3962 wxT("grid window") )
3965 m_rowLabelWin
= rowLblWin
;
3966 m_colLabelWin
= colLblWin
;
3969 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3971 wxPaintDC
dc( this );
3972 m_owner
->PrepareDC( dc
);
3973 wxRegion reg
= GetUpdateRegion();
3974 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
3975 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
3977 #if WXGRID_DRAW_LINES
3978 m_owner
->DrawAllGridLines( dc
, reg
);
3981 m_owner
->DrawGridSpace( dc
);
3982 m_owner
->DrawHighlight( dc
, dirtyCells
);
3985 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3987 wxWindow::ScrollWindow( dx
, dy
, rect
);
3988 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3989 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3992 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3994 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
3997 m_owner
->ProcessGridCellMouseEvent( event
);
4000 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
4002 m_owner
->GetEventHandler()->ProcessEvent( event
);
4005 // This seems to be required for wxMotif/wxGTK otherwise the mouse
4006 // cursor must be in the cell edit control to get key events
4008 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
4010 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4014 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
4016 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4020 void wxGridWindow::OnChar( wxKeyEvent
& event
)
4022 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4026 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
4030 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
4032 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4036 //////////////////////////////////////////////////////////////////////
4038 // Internal Helper function for computing row or column from some
4039 // (unscrolled) coordinate value, using either
4040 // m_defaultRowHeight/m_defaultColWidth or binary search on array
4041 // of m_rowBottoms/m_ColRights to speed up the search!
4043 // Internal helper macros for simpler use of that function
4045 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
4046 const wxArrayInt
& BorderArray
, int nMax
,
4049 #define internalXToCol(x) XToCol(x, true)
4050 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
4051 m_minAcceptableRowHeight, \
4052 m_rowBottoms, m_numRows, true)
4054 /////////////////////////////////////////////////////////////////////
4056 #if wxUSE_EXTENDED_RTTI
4057 WX_DEFINE_FLAGS( wxGridStyle
)
4059 wxBEGIN_FLAGS( wxGridStyle
)
4060 // new style border flags, we put them first to
4061 // use them for streaming out
4062 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4063 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4064 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4065 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4066 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4067 wxFLAGS_MEMBER(wxBORDER_NONE
)
4069 // old style border flags
4070 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4071 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4072 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4073 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4074 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4075 wxFLAGS_MEMBER(wxBORDER
)
4077 // standard window styles
4078 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4079 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4080 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4081 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4082 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4083 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4084 wxFLAGS_MEMBER(wxVSCROLL
)
4085 wxFLAGS_MEMBER(wxHSCROLL
)
4087 wxEND_FLAGS( wxGridStyle
)
4089 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4091 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4092 wxHIDE_PROPERTY( Children
)
4093 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4094 wxEND_PROPERTIES_TABLE()
4096 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4097 wxEND_HANDLERS_TABLE()
4099 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4102 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
4105 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4108 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4109 EVT_PAINT( wxGrid::OnPaint
)
4110 EVT_SIZE( wxGrid::OnSize
)
4111 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
4112 EVT_KEY_UP( wxGrid::OnKeyUp
)
4113 EVT_CHAR ( wxGrid::OnChar
)
4114 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
4119 // in order to make sure that a size event is not
4120 // trigerred in a unfinished state
4121 m_cornerLabelWin
= NULL
;
4122 m_rowLabelWin
= NULL
;
4123 m_colLabelWin
= NULL
;
4127 wxGrid::wxGrid( wxWindow
*parent
,
4132 const wxString
& name
)
4133 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
4134 m_colMinWidths(GRID_HASH_SIZE
),
4135 m_rowMinHeights(GRID_HASH_SIZE
)
4138 SetBestFittingSize(size
);
4141 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
4142 const wxPoint
& pos
, const wxSize
& size
,
4143 long style
, const wxString
& name
)
4145 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
4146 style
| wxWANTS_CHARS
, name
))
4149 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4150 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4153 SetBestFittingSize(size
);
4160 // Must do this or ~wxScrollHelper will pop the wrong event handler
4161 SetTargetWindow(this);
4163 wxSafeDecRef(m_defaultCellAttr
);
4165 #ifdef DEBUG_ATTR_CACHE
4166 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4167 wxPrintf(_T("wxGrid attribute cache statistics: "
4168 "total: %u, hits: %u (%u%%)\n"),
4169 total
, gs_nAttrCacheHits
,
4170 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4176 delete m_typeRegistry
;
4181 // ----- internal init and update functions
4184 // NOTE: If using the default visual attributes works everywhere then this can
4185 // be removed as well as the #else cases below.
4186 #define _USE_VISATTR 0
4188 void wxGrid::Create()
4190 // set to true by CreateGrid
4193 // create the type registry
4194 m_typeRegistry
= new wxGridTypeRegistry
;
4197 m_table
= (wxGridTableBase
*) NULL
;
4200 m_cellEditCtrlEnabled
= false;
4202 m_defaultCellAttr
= new wxGridCellAttr();
4204 // Set default cell attributes
4205 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4206 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4207 m_defaultCellAttr
->SetFont(GetFont());
4208 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4209 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4210 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4213 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4214 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4216 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4217 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4220 m_defaultCellAttr
->SetTextColour(
4221 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4222 m_defaultCellAttr
->SetBackgroundColour(
4223 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4228 m_currentCellCoords
= wxGridNoCellCoords
;
4230 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4231 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4233 // subwindow components that make up the wxGrid
4234 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4239 m_colLabelWin
= new wxGridColLabelWindow( this,
4244 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4249 m_gridWin
= new wxGridWindow( this,
4256 SetTargetWindow( m_gridWin
);
4259 wxColour gfg
= gva
.colFg
;
4260 wxColour gbg
= gva
.colBg
;
4261 wxColour lfg
= lva
.colFg
;
4262 wxColour lbg
= lva
.colBg
;
4264 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4265 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4266 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4267 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4270 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4271 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4272 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4273 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4274 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4275 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4277 m_gridWin
->SetOwnForegroundColour(gfg
);
4278 m_gridWin
->SetOwnBackgroundColour(gbg
);
4283 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4284 wxGrid::wxGridSelectionModes selmode
)
4286 wxCHECK_MSG( !m_created
,
4288 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4290 m_numRows
= numRows
;
4291 m_numCols
= numCols
;
4293 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4294 m_table
->SetView( this );
4296 m_selection
= new wxGridSelection( this, selmode
);
4305 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4307 wxCHECK_RET( m_created
,
4308 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4310 m_selection
->SetSelectionMode( selmode
);
4313 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4315 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4316 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4318 return m_selection
->GetSelectionMode();
4321 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4322 wxGrid::wxGridSelectionModes selmode
)
4326 // stop all processing
4331 wxGridTableBase
*t
= m_table
;
4346 m_numRows
= table
->GetNumberRows();
4347 m_numCols
= table
->GetNumberCols();
4350 m_table
->SetView( this );
4351 m_ownTable
= takeOwnership
;
4352 m_selection
= new wxGridSelection( this, selmode
);
4364 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4365 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4367 if ( m_rowLabelWin
)
4369 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4373 m_labelBackgroundColour
= *wxWHITE
;
4376 m_labelTextColour
= *wxBLACK
;
4379 m_attrCache
.row
= -1;
4380 m_attrCache
.col
= -1;
4381 m_attrCache
.attr
= NULL
;
4383 // TODO: something better than this ?
4385 m_labelFont
= this->GetFont();
4386 m_labelFont
.SetWeight( wxBOLD
);
4388 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4389 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4391 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4392 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4393 m_colLabelTextOrientation
= wxHORIZONTAL
;
4395 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4396 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4398 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4399 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4401 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4402 m_defaultRowHeight
+= 8;
4404 m_defaultRowHeight
+= 4;
4407 m_gridLineColour
= wxColour( 192,192,192 );
4408 m_gridLinesEnabled
= true;
4409 m_cellHighlightColour
= *wxBLACK
;
4410 m_cellHighlightPenWidth
= 2;
4411 m_cellHighlightROPenWidth
= 1;
4413 m_canDragColMove
= false;
4415 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4416 m_winCapture
= (wxWindow
*)NULL
;
4417 m_canDragRowSize
= true;
4418 m_canDragColSize
= true;
4419 m_canDragGridSize
= true;
4420 m_canDragCell
= false;
4422 m_dragRowOrCol
= -1;
4423 m_isDragging
= false;
4424 m_startDragPos
= wxDefaultPosition
;
4426 m_waitForSlowClick
= false;
4428 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4429 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4431 m_currentCellCoords
= wxGridNoCellCoords
;
4435 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4436 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4438 m_editable
= true; // default for whole grid
4440 m_inOnKeyDown
= false;
4446 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4447 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4450 // ----------------------------------------------------------------------------
4451 // the idea is to call these functions only when necessary because they create
4452 // quite big arrays which eat memory mostly unnecessary - in particular, if
4453 // default widths/heights are used for all rows/columns, we may not use these
4456 // with some extra code, it should be possible to only store the
4457 // widths/heights different from default ones but this will be done later...
4458 // ----------------------------------------------------------------------------
4460 void wxGrid::InitRowHeights()
4462 m_rowHeights
.Empty();
4463 m_rowBottoms
.Empty();
4465 m_rowHeights
.Alloc( m_numRows
);
4466 m_rowBottoms
.Alloc( m_numRows
);
4470 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4472 for ( int i
= 0; i
< m_numRows
; i
++ )
4474 rowBottom
+= m_defaultRowHeight
;
4475 m_rowBottoms
.Add( rowBottom
);
4479 void wxGrid::InitColWidths()
4481 m_colWidths
.Empty();
4482 m_colRights
.Empty();
4484 m_colWidths
.Alloc( m_numCols
);
4485 m_colRights
.Alloc( m_numCols
);
4488 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4490 for ( int i
= 0; i
< m_numCols
; i
++ )
4492 colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
4493 m_colRights
.Add( colRight
);
4497 int wxGrid::GetColWidth(int col
) const
4499 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4502 int wxGrid::GetColLeft(int col
) const
4504 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
4505 : m_colRights
[col
] - m_colWidths
[col
];
4508 int wxGrid::GetColRight(int col
) const
4510 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
4514 int wxGrid::GetRowHeight(int row
) const
4516 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4519 int wxGrid::GetRowTop(int row
) const
4521 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4522 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4525 int wxGrid::GetRowBottom(int row
) const
4527 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4528 : m_rowBottoms
[row
];
4531 void wxGrid::CalcDimensions()
4534 GetClientSize( &cw
, &ch
);
4536 if ( m_rowLabelWin
->IsShown() )
4537 cw
-= m_rowLabelWidth
;
4538 if ( m_colLabelWin
->IsShown() )
4539 ch
-= m_colLabelHeight
;
4542 int w
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) + m_extraWidth
+ 1 : 0;
4543 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4545 // take into account editor if shown
4546 if ( IsCellEditControlShown() )
4549 int r
= m_currentCellCoords
.GetRow();
4550 int c
= m_currentCellCoords
.GetCol();
4551 int x
= GetColLeft(c
);
4552 int y
= GetRowTop(r
);
4554 // how big is the editor
4555 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4556 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4557 editor
->GetControl()->GetSize(&w2
, &h2
);
4568 // preserve (more or less) the previous position
4570 GetViewStart( &x
, &y
);
4572 // ensure the position is valid for the new scroll ranges
4574 x
= wxMax( w
- 1, 0 );
4576 y
= wxMax( h
- 1, 0 );
4578 // do set scrollbar parameters
4579 SetScrollbars( m_scrollLineX
, m_scrollLineY
,
4580 GetScrollX(w
), GetScrollY(h
), x
, y
,
4581 GetBatchCount() != 0);
4583 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4584 // still must reposition the children
4588 void wxGrid::CalcWindowSizes()
4590 // escape if the window is has not been fully created yet
4592 if ( m_cornerLabelWin
== NULL
)
4596 GetClientSize( &cw
, &ch
);
4598 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4599 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4601 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4602 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
- m_rowLabelWidth
, m_colLabelHeight
);
4604 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4605 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
- m_colLabelHeight
);
4607 if ( m_gridWin
&& m_gridWin
->IsShown() )
4608 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
- m_rowLabelWidth
, ch
- m_colLabelHeight
);
4611 // this is called when the grid table sends a message
4612 // to indicate that it has been redimensioned
4614 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4617 bool result
= false;
4619 // Clear the attribute cache as the attribute might refer to a different
4620 // cell than stored in the cache after adding/removing rows/columns.
4623 // By the same reasoning, the editor should be dismissed if columns are
4624 // added or removed. And for consistency, it should IMHO always be
4625 // removed, not only if the cell "underneath" it actually changes.
4626 // For now, I intentionally do not save the editor's content as the
4627 // cell it might want to save that stuff to might no longer exist.
4628 HideCellEditControl();
4631 // if we were using the default widths/heights so far, we must change them
4633 if ( m_colWidths
.IsEmpty() )
4638 if ( m_rowHeights
.IsEmpty() )
4644 switch ( msg
.GetId() )
4646 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4648 size_t pos
= msg
.GetCommandInt();
4649 int numRows
= msg
.GetCommandInt2();
4651 m_numRows
+= numRows
;
4653 if ( !m_rowHeights
.IsEmpty() )
4655 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4656 m_rowBottoms
.Insert( 0, pos
, numRows
);
4660 bottom
= m_rowBottoms
[pos
- 1];
4662 for ( i
= pos
; i
< m_numRows
; i
++ )
4664 bottom
+= m_rowHeights
[i
];
4665 m_rowBottoms
[i
] = bottom
;
4669 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4671 // if we have just inserted cols into an empty grid the current
4672 // cell will be undefined...
4674 SetCurrentCell( 0, 0 );
4678 m_selection
->UpdateRows( pos
, numRows
);
4679 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4681 attrProvider
->UpdateAttrRows( pos
, numRows
);
4683 if ( !GetBatchCount() )
4686 m_rowLabelWin
->Refresh();
4692 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4694 int numRows
= msg
.GetCommandInt();
4695 int oldNumRows
= m_numRows
;
4696 m_numRows
+= numRows
;
4698 if ( !m_rowHeights
.IsEmpty() )
4700 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4701 m_rowBottoms
.Add( 0, numRows
);
4704 if ( oldNumRows
> 0 )
4705 bottom
= m_rowBottoms
[oldNumRows
- 1];
4707 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4709 bottom
+= m_rowHeights
[i
];
4710 m_rowBottoms
[i
] = bottom
;
4714 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4716 // if we have just inserted cols into an empty grid the current
4717 // cell will be undefined...
4719 SetCurrentCell( 0, 0 );
4722 if ( !GetBatchCount() )
4725 m_rowLabelWin
->Refresh();
4731 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4733 size_t pos
= msg
.GetCommandInt();
4734 int numRows
= msg
.GetCommandInt2();
4735 m_numRows
-= numRows
;
4737 if ( !m_rowHeights
.IsEmpty() )
4739 m_rowHeights
.RemoveAt( pos
, numRows
);
4740 m_rowBottoms
.RemoveAt( pos
, numRows
);
4743 for ( i
= 0; i
< m_numRows
; i
++ )
4745 h
+= m_rowHeights
[i
];
4746 m_rowBottoms
[i
] = h
;
4752 m_currentCellCoords
= wxGridNoCellCoords
;
4756 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4757 m_currentCellCoords
.Set( 0, 0 );
4761 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4762 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4765 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4767 // ifdef'd out following patch from Paul Gammans
4769 // No need to touch column attributes, unless we
4770 // removed _all_ rows, in this case, we remove
4771 // all column attributes.
4772 // I hate to do this here, but the
4773 // needed data is not available inside UpdateAttrRows.
4774 if ( !GetNumberRows() )
4775 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4779 if ( !GetBatchCount() )
4782 m_rowLabelWin
->Refresh();
4788 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4790 size_t pos
= msg
.GetCommandInt();
4791 int numCols
= msg
.GetCommandInt2();
4792 m_numCols
+= numCols
;
4794 if ( !m_colAt
.IsEmpty() )
4796 //Shift the column IDs
4798 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
4800 if ( m_colAt
[i
] >= (int)pos
)
4801 m_colAt
[i
] += numCols
;
4804 m_colAt
.Insert( pos
, pos
, numCols
);
4806 //Set the new columns' positions
4807 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
4813 if ( !m_colWidths
.IsEmpty() )
4815 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4816 m_colRights
.Insert( 0, pos
, numCols
);
4820 right
= m_colRights
[GetColAt( pos
- 1 )];
4823 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
4825 i
= GetColAt( colPos
);
4827 right
+= m_colWidths
[i
];
4828 m_colRights
[i
] = right
;
4832 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4834 // if we have just inserted cols into an empty grid the current
4835 // cell will be undefined...
4837 SetCurrentCell( 0, 0 );
4841 m_selection
->UpdateCols( pos
, numCols
);
4842 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4844 attrProvider
->UpdateAttrCols( pos
, numCols
);
4845 if ( !GetBatchCount() )
4848 m_colLabelWin
->Refresh();
4854 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4856 int numCols
= msg
.GetCommandInt();
4857 int oldNumCols
= m_numCols
;
4858 m_numCols
+= numCols
;
4860 if ( !m_colAt
.IsEmpty() )
4862 m_colAt
.Add( 0, numCols
);
4864 //Set the new columns' positions
4866 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4872 if ( !m_colWidths
.IsEmpty() )
4874 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4875 m_colRights
.Add( 0, numCols
);
4878 if ( oldNumCols
> 0 )
4879 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
4882 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
4884 i
= GetColAt( colPos
);
4886 right
+= m_colWidths
[i
];
4887 m_colRights
[i
] = right
;
4891 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4893 // if we have just inserted cols into an empty grid the current
4894 // cell will be undefined...
4896 SetCurrentCell( 0, 0 );
4898 if ( !GetBatchCount() )
4901 m_colLabelWin
->Refresh();
4907 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4909 size_t pos
= msg
.GetCommandInt();
4910 int numCols
= msg
.GetCommandInt2();
4911 m_numCols
-= numCols
;
4913 if ( !m_colAt
.IsEmpty() )
4915 int colID
= GetColAt( pos
);
4917 m_colAt
.RemoveAt( pos
, numCols
);
4919 //Shift the column IDs
4921 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
4923 if ( m_colAt
[colPos
] > colID
)
4924 m_colAt
[colPos
] -= numCols
;
4928 if ( !m_colWidths
.IsEmpty() )
4930 m_colWidths
.RemoveAt( pos
, numCols
);
4931 m_colRights
.RemoveAt( pos
, numCols
);
4935 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
4937 i
= GetColAt( colPos
);
4939 w
+= m_colWidths
[i
];
4946 m_currentCellCoords
= wxGridNoCellCoords
;
4950 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4951 m_currentCellCoords
.Set( 0, 0 );
4955 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4956 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4959 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4961 // ifdef'd out following patch from Paul Gammans
4963 // No need to touch row attributes, unless we
4964 // removed _all_ columns, in this case, we remove
4965 // all row attributes.
4966 // I hate to do this here, but the
4967 // needed data is not available inside UpdateAttrCols.
4968 if ( !GetNumberCols() )
4969 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4973 if ( !GetBatchCount() )
4976 m_colLabelWin
->Refresh();
4983 if (result
&& !GetBatchCount() )
4984 m_gridWin
->Refresh();
4989 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4991 wxRegionIterator
iter( reg
);
4994 wxArrayInt rowlabels
;
5001 // TODO: remove this when we can...
5002 // There is a bug in wxMotif that gives garbage update
5003 // rectangles if you jump-scroll a long way by clicking the
5004 // scrollbar with middle button. This is a work-around
5006 #if defined(__WXMOTIF__)
5008 m_gridWin
->GetClientSize( &cw
, &ch
);
5009 if ( r
.GetTop() > ch
)
5011 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5014 // logical bounds of update region
5017 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
5018 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
5020 // find the row labels within these bounds
5023 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5025 if ( GetRowBottom(row
) < top
)
5028 if ( GetRowTop(row
) > bottom
)
5031 rowlabels
.Add( row
);
5040 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
5042 wxRegionIterator
iter( reg
);
5045 wxArrayInt colLabels
;
5052 // TODO: remove this when we can...
5053 // There is a bug in wxMotif that gives garbage update
5054 // rectangles if you jump-scroll a long way by clicking the
5055 // scrollbar with middle button. This is a work-around
5057 #if defined(__WXMOTIF__)
5059 m_gridWin
->GetClientSize( &cw
, &ch
);
5060 if ( r
.GetLeft() > cw
)
5062 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5065 // logical bounds of update region
5068 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
5069 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
5071 // find the cells within these bounds
5075 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5077 col
= GetColAt( colPos
);
5079 if ( GetColRight(col
) < left
)
5082 if ( GetColLeft(col
) > right
)
5085 colLabels
.Add( col
);
5094 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
5096 wxRegionIterator
iter( reg
);
5099 wxGridCellCoordsArray cellsExposed
;
5101 int left
, top
, right
, bottom
;
5106 // TODO: remove this when we can...
5107 // There is a bug in wxMotif that gives garbage update
5108 // rectangles if you jump-scroll a long way by clicking the
5109 // scrollbar with middle button. This is a work-around
5111 #if defined(__WXMOTIF__)
5113 m_gridWin
->GetClientSize( &cw
, &ch
);
5114 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5115 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5116 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5117 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5120 // logical bounds of update region
5122 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5123 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5125 // find the cells within these bounds
5128 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5130 if ( GetRowBottom(row
) <= top
)
5133 if ( GetRowTop(row
) > bottom
)
5137 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5139 col
= GetColAt( colPos
);
5141 if ( GetColRight(col
) <= left
)
5144 if ( GetColLeft(col
) > right
)
5147 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
5154 return cellsExposed
;
5158 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5161 wxPoint
pos( event
.GetPosition() );
5162 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5164 if ( event
.Dragging() )
5168 m_isDragging
= true;
5169 m_rowLabelWin
->CaptureMouse();
5172 if ( event
.LeftIsDown() )
5174 switch ( m_cursorMode
)
5176 case WXGRID_CURSOR_RESIZE_ROW
:
5178 int cw
, ch
, left
, dummy
;
5179 m_gridWin
->GetClientSize( &cw
, &ch
);
5180 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5182 wxClientDC
dc( m_gridWin
);
5185 GetRowTop(m_dragRowOrCol
) +
5186 GetRowMinimalHeight(m_dragRowOrCol
) );
5187 dc
.SetLogicalFunction(wxINVERT
);
5188 if ( m_dragLastPos
>= 0 )
5190 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5192 dc
.DrawLine( left
, y
, left
+cw
, y
);
5197 case WXGRID_CURSOR_SELECT_ROW
:
5199 if ( (row
= YToRow( y
)) >= 0 )
5203 m_selection
->SelectRow( row
,
5204 event
.ControlDown(),
5213 // default label to suppress warnings about "enumeration value
5214 // 'xxx' not handled in switch
5222 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5227 if (m_rowLabelWin
->HasCapture())
5228 m_rowLabelWin
->ReleaseMouse();
5229 m_isDragging
= false;
5232 // ------------ Entering or leaving the window
5234 if ( event
.Entering() || event
.Leaving() )
5236 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5239 // ------------ Left button pressed
5241 else if ( event
.LeftDown() )
5243 // don't send a label click event for a hit on the
5244 // edge of the row label - this is probably the user
5245 // wanting to resize the row
5247 if ( YToEdgeOfRow(y
) < 0 )
5251 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5253 if ( !event
.ShiftDown() && !event
.CmdDown() )
5257 if ( event
.ShiftDown() )
5259 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5262 GetNumberCols() - 1,
5263 event
.ControlDown(),
5270 m_selection
->SelectRow( row
,
5271 event
.ControlDown(),
5278 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5283 // starting to drag-resize a row
5284 if ( CanDragRowSize() )
5285 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5289 // ------------ Left double click
5291 else if (event
.LeftDClick() )
5293 row
= YToEdgeOfRow(y
);
5298 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5300 // no default action at the moment
5305 // adjust row height depending on label text
5306 AutoSizeRowLabelSize( row
);
5308 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5313 // ------------ Left button released
5315 else if ( event
.LeftUp() )
5317 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5319 DoEndDragResizeRow();
5321 // Note: we are ending the event *after* doing
5322 // default processing in this case
5324 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5327 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5331 // ------------ Right button down
5333 else if ( event
.RightDown() )
5337 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5339 // no default action at the moment
5343 // ------------ Right double click
5345 else if ( event
.RightDClick() )
5349 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5351 // no default action at the moment
5355 // ------------ No buttons down and mouse moving
5357 else if ( event
.Moving() )
5359 m_dragRowOrCol
= YToEdgeOfRow( y
);
5360 if ( m_dragRowOrCol
>= 0 )
5362 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5364 // don't capture the mouse yet
5365 if ( CanDragRowSize() )
5366 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5369 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5371 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5376 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5379 wxPoint
pos( event
.GetPosition() );
5380 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5382 if ( event
.Dragging() )
5386 m_isDragging
= true;
5387 m_colLabelWin
->CaptureMouse();
5389 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
)
5390 m_dragRowOrCol
= XToCol( x
);
5393 if ( event
.LeftIsDown() )
5395 switch ( m_cursorMode
)
5397 case WXGRID_CURSOR_RESIZE_COL
:
5399 int cw
, ch
, dummy
, top
;
5400 m_gridWin
->GetClientSize( &cw
, &ch
);
5401 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5403 wxClientDC
dc( m_gridWin
);
5406 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5407 GetColMinimalWidth(m_dragRowOrCol
));
5408 dc
.SetLogicalFunction(wxINVERT
);
5409 if ( m_dragLastPos
>= 0 )
5411 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5413 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5418 case WXGRID_CURSOR_SELECT_COL
:
5420 if ( (col
= XToCol( x
)) >= 0 )
5424 m_selection
->SelectCol( col
,
5425 event
.ControlDown(),
5434 case WXGRID_CURSOR_MOVE_COL
:
5437 m_moveToCol
= GetColAt( 0 );
5439 m_moveToCol
= XToCol( x
);
5443 if ( m_moveToCol
< 0 )
5444 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5446 markerX
= GetColLeft( m_moveToCol
);
5448 if ( markerX
!= m_dragLastPos
)
5450 wxClientDC
dc( m_colLabelWin
);
5453 m_colLabelWin
->GetClientSize( &cw
, &ch
);
5457 //Clean up the last indicator
5458 if ( m_dragLastPos
>= 0 )
5460 wxPen
pen( m_colLabelWin
->GetBackgroundColour(), 2 );
5462 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
5463 dc
.SetPen(wxNullPen
);
5465 if ( XToCol( m_dragLastPos
) != -1 )
5466 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
5469 //Moving to the same place? Don't draw a marker
5470 if ( (m_moveToCol
== m_dragRowOrCol
)
5471 || (GetColPos( m_moveToCol
) == GetColPos( m_dragRowOrCol
) + 1)
5472 || (m_moveToCol
< 0 && m_dragRowOrCol
== GetColAt( m_numCols
- 1 )))
5479 wxPen
pen( *wxBLUE
, 2 );
5482 dc
.DrawLine( markerX
, 0, markerX
, ch
);
5484 dc
.SetPen(wxNullPen
);
5486 m_dragLastPos
= markerX
- 1;
5491 // default label to suppress warnings about "enumeration value
5492 // 'xxx' not handled in switch
5500 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5505 if (m_colLabelWin
->HasCapture())
5506 m_colLabelWin
->ReleaseMouse();
5507 m_isDragging
= false;
5510 // ------------ Entering or leaving the window
5512 if ( event
.Entering() || event
.Leaving() )
5514 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5517 // ------------ Left button pressed
5519 else if ( event
.LeftDown() )
5521 // don't send a label click event for a hit on the
5522 // edge of the col label - this is probably the user
5523 // wanting to resize the col
5525 if ( XToEdgeOfCol(x
) < 0 )
5529 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5531 if ( m_canDragColMove
)
5533 //Show button as pressed
5534 wxClientDC
dc( m_colLabelWin
);
5535 int colLeft
= GetColLeft( col
);
5536 int colRight
= GetColRight( col
) - 1;
5537 dc
.SetPen( wxPen( m_colLabelWin
->GetBackgroundColour(), 1 ) );
5538 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
5539 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
5541 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, m_colLabelWin
);
5545 if ( !event
.ShiftDown() && !event
.CmdDown() )
5549 if ( event
.ShiftDown() )
5551 m_selection
->SelectBlock( 0,
5552 m_currentCellCoords
.GetCol(),
5553 GetNumberRows() - 1, col
,
5554 event
.ControlDown(),
5561 m_selection
->SelectCol( col
,
5562 event
.ControlDown(),
5569 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5575 // starting to drag-resize a col
5577 if ( CanDragColSize() )
5578 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5582 // ------------ Left double click
5584 if ( event
.LeftDClick() )
5586 col
= XToEdgeOfCol(x
);
5591 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5593 // no default action at the moment
5598 // adjust column width depending on label text
5599 AutoSizeColLabelSize( col
);
5601 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5606 // ------------ Left button released
5608 else if ( event
.LeftUp() )
5610 switch ( m_cursorMode
)
5612 case WXGRID_CURSOR_RESIZE_COL
:
5613 DoEndDragResizeCol();
5615 // Note: we are ending the event *after* doing
5616 // default processing in this case
5618 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5621 case WXGRID_CURSOR_MOVE_COL
:
5624 SendEvent( wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
, event
);
5627 case WXGRID_CURSOR_SELECT_COL
:
5628 case WXGRID_CURSOR_SELECT_CELL
:
5629 case WXGRID_CURSOR_RESIZE_ROW
:
5630 case WXGRID_CURSOR_SELECT_ROW
:
5631 // nothing to do (?)
5635 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5639 // ------------ Right button down
5641 else if ( event
.RightDown() )
5645 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5647 // no default action at the moment
5651 // ------------ Right double click
5653 else if ( event
.RightDClick() )
5657 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5659 // no default action at the moment
5663 // ------------ No buttons down and mouse moving
5665 else if ( event
.Moving() )
5667 m_dragRowOrCol
= XToEdgeOfCol( x
);
5668 if ( m_dragRowOrCol
>= 0 )
5670 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5672 // don't capture the cursor yet
5673 if ( CanDragColSize() )
5674 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5677 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5679 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5684 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5686 if ( event
.LeftDown() )
5688 // indicate corner label by having both row and
5691 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5696 else if ( event
.LeftDClick() )
5698 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5700 else if ( event
.RightDown() )
5702 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5704 // no default action at the moment
5707 else if ( event
.RightDClick() )
5709 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5711 // no default action at the moment
5716 void wxGrid::ChangeCursorMode(CursorMode mode
,
5721 static const wxChar
*cursorModes
[] =
5731 wxLogTrace(_T("grid"),
5732 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5733 win
== m_colLabelWin
? _T("colLabelWin")
5734 : win
? _T("rowLabelWin")
5736 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5739 if ( mode
== m_cursorMode
&&
5740 win
== m_winCapture
&&
5741 captureMouse
== (m_winCapture
!= NULL
))
5746 // by default use the grid itself
5752 if (m_winCapture
->HasCapture())
5753 m_winCapture
->ReleaseMouse();
5754 m_winCapture
= (wxWindow
*)NULL
;
5757 m_cursorMode
= mode
;
5759 switch ( m_cursorMode
)
5761 case WXGRID_CURSOR_RESIZE_ROW
:
5762 win
->SetCursor( m_rowResizeCursor
);
5765 case WXGRID_CURSOR_RESIZE_COL
:
5766 win
->SetCursor( m_colResizeCursor
);
5769 case WXGRID_CURSOR_MOVE_COL
:
5770 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
5774 win
->SetCursor( *wxSTANDARD_CURSOR
);
5778 // we need to capture mouse when resizing
5779 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5780 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5782 if ( captureMouse
&& resize
)
5784 win
->CaptureMouse();
5789 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5792 wxPoint
pos( event
.GetPosition() );
5793 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5795 wxGridCellCoords coords
;
5796 XYToCell( x
, y
, coords
);
5798 int cell_rows
, cell_cols
;
5799 bool isFirstDrag
= !m_isDragging
;
5800 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5801 if ((cell_rows
< 0) || (cell_cols
< 0))
5803 coords
.SetRow(coords
.GetRow() + cell_rows
);
5804 coords
.SetCol(coords
.GetCol() + cell_cols
);
5807 if ( event
.Dragging() )
5809 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5811 // Don't start doing anything until the mouse has been dragged at
5812 // least 3 pixels in any direction...
5815 if (m_startDragPos
== wxDefaultPosition
)
5817 m_startDragPos
= pos
;
5820 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5824 m_isDragging
= true;
5825 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5827 // Hide the edit control, so it
5828 // won't interfere with drag-shrinking.
5829 if ( IsCellEditControlShown() )
5831 HideCellEditControl();
5832 SaveEditControlValue();
5835 // Have we captured the mouse yet?
5838 m_winCapture
= m_gridWin
;
5839 m_winCapture
->CaptureMouse();
5842 if ( coords
!= wxGridNoCellCoords
)
5844 if ( event
.CmdDown() )
5846 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5847 m_selectingKeyboard
= coords
;
5848 HighlightBlock( m_selectingKeyboard
, coords
);
5850 else if ( CanDragCell() )
5854 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5855 m_selectingKeyboard
= coords
;
5857 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5865 if ( !IsSelection() )
5867 HighlightBlock( coords
, coords
);
5871 HighlightBlock( m_currentCellCoords
, coords
);
5875 if (! IsVisible(coords
))
5877 MakeCellVisible(coords
);
5878 // TODO: need to introduce a delay or something here. The
5879 // scrolling is way to fast, at least on MSW - also on GTK.
5883 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5885 int cw
, ch
, left
, dummy
;
5886 m_gridWin
->GetClientSize( &cw
, &ch
);
5887 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5889 wxClientDC
dc( m_gridWin
);
5891 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5892 GetRowMinimalHeight(m_dragRowOrCol
) );
5893 dc
.SetLogicalFunction(wxINVERT
);
5894 if ( m_dragLastPos
>= 0 )
5896 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5898 dc
.DrawLine( left
, y
, left
+cw
, y
);
5901 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5903 int cw
, ch
, dummy
, top
;
5904 m_gridWin
->GetClientSize( &cw
, &ch
);
5905 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5907 wxClientDC
dc( m_gridWin
);
5909 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5910 GetColMinimalWidth(m_dragRowOrCol
) );
5911 dc
.SetLogicalFunction(wxINVERT
);
5912 if ( m_dragLastPos
>= 0 )
5914 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5916 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5923 m_isDragging
= false;
5924 m_startDragPos
= wxDefaultPosition
;
5926 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5927 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5930 if ( event
.Entering() || event
.Leaving() )
5932 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5933 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5938 // ------------ Left button pressed
5940 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5942 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5947 if ( !event
.CmdDown() )
5949 if ( event
.ShiftDown() )
5953 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5954 m_currentCellCoords
.GetCol(),
5957 event
.ControlDown(),
5963 else if ( XToEdgeOfCol(x
) < 0 &&
5964 YToEdgeOfRow(y
) < 0 )
5966 DisableCellEditControl();
5967 MakeCellVisible( coords
);
5969 if ( event
.CmdDown() )
5973 m_selection
->ToggleCellSelection( coords
.GetRow(),
5975 event
.ControlDown(),
5980 m_selectingTopLeft
= wxGridNoCellCoords
;
5981 m_selectingBottomRight
= wxGridNoCellCoords
;
5982 m_selectingKeyboard
= coords
;
5986 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5987 SetCurrentCell( coords
);
5990 if ( m_selection
->GetSelectionMode() !=
5991 wxGrid::wxGridSelectCells
)
5993 HighlightBlock( coords
, coords
);
6001 // ------------ Left double click
6003 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
6005 DisableCellEditControl();
6007 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
6009 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
6014 // we want double click to select a cell and start editing
6015 // (i.e. to behave in same way as sequence of two slow clicks):
6016 m_waitForSlowClick
= true;
6021 // ------------ Left button released
6023 else if ( event
.LeftUp() )
6025 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6029 if (m_winCapture
->HasCapture())
6030 m_winCapture
->ReleaseMouse();
6031 m_winCapture
= NULL
;
6034 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
6037 EnableCellEditControl();
6039 wxGridCellAttr
*attr
= GetCellAttr(coords
);
6040 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
6041 editor
->StartingClick();
6045 m_waitForSlowClick
= false;
6047 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6048 m_selectingBottomRight
!= wxGridNoCellCoords
)
6052 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6053 m_selectingTopLeft
.GetCol(),
6054 m_selectingBottomRight
.GetRow(),
6055 m_selectingBottomRight
.GetCol(),
6056 event
.ControlDown(),
6062 m_selectingTopLeft
= wxGridNoCellCoords
;
6063 m_selectingBottomRight
= wxGridNoCellCoords
;
6065 // Show the edit control, if it has been hidden for
6067 ShowCellEditControl();
6070 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6072 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6073 DoEndDragResizeRow();
6075 // Note: we are ending the event *after* doing
6076 // default processing in this case
6078 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
6080 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6082 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6083 DoEndDragResizeCol();
6085 // Note: we are ending the event *after* doing
6086 // default processing in this case
6088 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
6094 // ------------ Right button down
6096 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
6098 DisableCellEditControl();
6099 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
6104 // no default action at the moment
6108 // ------------ Right double click
6110 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
6112 DisableCellEditControl();
6113 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
6118 // no default action at the moment
6122 // ------------ Moving and no button action
6124 else if ( event
.Moving() && !event
.IsButton() )
6126 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
6128 // out of grid cell area
6129 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6133 int dragRow
= YToEdgeOfRow( y
);
6134 int dragCol
= XToEdgeOfCol( x
);
6136 // Dragging on the corner of a cell to resize in both
6137 // directions is not implemented yet...
6139 if ( dragRow
>= 0 && dragCol
>= 0 )
6141 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6147 m_dragRowOrCol
= dragRow
;
6149 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6151 if ( CanDragRowSize() && CanDragGridSize() )
6152 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
6155 else if ( dragCol
>= 0 )
6157 m_dragRowOrCol
= dragCol
;
6159 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6161 if ( CanDragColSize() && CanDragGridSize() )
6162 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
6165 else // Neither on a row or col edge
6167 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6169 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6175 void wxGrid::DoEndDragResizeRow()
6177 if ( m_dragLastPos
>= 0 )
6179 // erase the last line and resize the row
6181 int cw
, ch
, left
, dummy
;
6182 m_gridWin
->GetClientSize( &cw
, &ch
);
6183 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6185 wxClientDC
dc( m_gridWin
);
6187 dc
.SetLogicalFunction( wxINVERT
);
6188 dc
.DrawLine( left
, m_dragLastPos
, left
+ cw
, m_dragLastPos
);
6189 HideCellEditControl();
6190 SaveEditControlValue();
6192 int rowTop
= GetRowTop(m_dragRowOrCol
);
6193 SetRowSize( m_dragRowOrCol
,
6194 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
6196 if ( !GetBatchCount() )
6198 // Only needed to get the correct rect.y:
6199 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
6201 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6202 rect
.width
= m_rowLabelWidth
;
6203 rect
.height
= ch
- rect
.y
;
6204 m_rowLabelWin
->Refresh( true, &rect
);
6207 // if there is a multicell block, paint all of it
6210 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
6211 int leftCol
= XToCol(left
);
6212 int rightCol
= internalXToCol(left
+ cw
);
6215 for (i
=leftCol
; i
<rightCol
; i
++)
6217 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
6218 if (cell_rows
< subtract_rows
)
6219 subtract_rows
= cell_rows
;
6221 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
6222 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6223 rect
.height
= ch
- rect
.y
;
6226 m_gridWin
->Refresh( false, &rect
);
6229 ShowCellEditControl();
6234 void wxGrid::DoEndDragResizeCol()
6236 if ( m_dragLastPos
>= 0 )
6238 // erase the last line and resize the col
6240 int cw
, ch
, dummy
, top
;
6241 m_gridWin
->GetClientSize( &cw
, &ch
);
6242 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6244 wxClientDC
dc( m_gridWin
);
6246 dc
.SetLogicalFunction( wxINVERT
);
6247 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6248 HideCellEditControl();
6249 SaveEditControlValue();
6251 int colLeft
= GetColLeft(m_dragRowOrCol
);
6252 SetColSize( m_dragRowOrCol
,
6253 wxMax( m_dragLastPos
- colLeft
,
6254 GetColMinimalWidth(m_dragRowOrCol
) ) );
6256 if ( !GetBatchCount() )
6258 // Only needed to get the correct rect.x:
6259 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
6261 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6262 rect
.width
= cw
- rect
.x
;
6263 rect
.height
= m_colLabelHeight
;
6264 m_colLabelWin
->Refresh( true, &rect
);
6267 // if there is a multicell block, paint all of it
6270 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
6271 int topRow
= YToRow(top
);
6272 int bottomRow
= internalYToRow(top
+ cw
);
6275 for (i
=topRow
; i
<bottomRow
; i
++)
6277 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
6278 if (cell_cols
< subtract_cols
)
6279 subtract_cols
= cell_cols
;
6282 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
6283 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6284 rect
.width
= cw
- rect
.x
;
6288 m_gridWin
->Refresh( false, &rect
);
6291 ShowCellEditControl();
6295 void wxGrid::DoEndDragMoveCol()
6297 //The user clicked on the column but didn't actually drag
6298 if ( m_dragLastPos
< 0 )
6300 m_colLabelWin
->Refresh(); //Do this to "unpress" the column
6305 if ( m_moveToCol
== -1 )
6306 newPos
= m_numCols
- 1;
6309 newPos
= GetColPos( m_moveToCol
);
6310 if ( newPos
> GetColPos( m_dragRowOrCol
) )
6314 SetColPos( m_dragRowOrCol
, newPos
);
6317 void wxGrid::SetColPos( int colID
, int newPos
)
6319 if ( m_colAt
.IsEmpty() )
6321 m_colAt
.Alloc( m_numCols
);
6324 for ( i
= 0; i
< m_numCols
; i
++ )
6330 int oldPos
= GetColPos( colID
);
6332 //Reshuffle the m_colAt array
6333 if ( newPos
> oldPos
)
6336 for ( i
= oldPos
; i
< newPos
; i
++ )
6338 m_colAt
[i
] = m_colAt
[i
+1];
6344 for ( i
= oldPos
; i
> newPos
; i
-- )
6346 m_colAt
[i
] = m_colAt
[i
-1];
6350 m_colAt
[newPos
] = colID
;
6352 //Recalculate the column rights
6353 if ( !m_colWidths
.IsEmpty() )
6357 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6359 int colID
= GetColAt( colPos
);
6361 colRight
+= m_colWidths
[colID
];
6362 m_colRights
[colID
] = colRight
;
6366 m_colLabelWin
->Refresh();
6367 m_gridWin
->Refresh();
6372 void wxGrid::EnableDragColMove( bool enable
)
6374 if ( m_canDragColMove
== enable
)
6377 m_canDragColMove
= enable
;
6379 if ( !m_canDragColMove
)
6383 //Recalculate the column rights
6384 if ( !m_colWidths
.IsEmpty() )
6388 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6390 colRight
+= m_colWidths
[colPos
];
6391 m_colRights
[colPos
] = colRight
;
6395 m_colLabelWin
->Refresh();
6396 m_gridWin
->Refresh();
6402 // ------ interaction with data model
6404 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6406 switch ( msg
.GetId() )
6408 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6409 return GetModelValues();
6411 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6412 return SetModelValues();
6414 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6415 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6416 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6417 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6418 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6419 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6420 return Redimension( msg
);
6427 // The behaviour of this function depends on the grid table class
6428 // Clear() function. For the default wxGridStringTable class the
6429 // behavious is to replace all cell contents with wxEmptyString but
6430 // not to change the number of rows or cols.
6432 void wxGrid::ClearGrid()
6436 if (IsCellEditControlEnabled())
6437 DisableCellEditControl();
6440 if (!GetBatchCount())
6441 m_gridWin
->Refresh();
6445 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6447 // TODO: something with updateLabels flag
6451 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6457 if (IsCellEditControlEnabled())
6458 DisableCellEditControl();
6460 bool done
= m_table
->InsertRows( pos
, numRows
);
6463 // the table will have sent the results of the insert row
6464 // operation to this view object as a grid table message
6470 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6472 // TODO: something with updateLabels flag
6476 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6482 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6485 // the table will have sent the results of the append row
6486 // operation to this view object as a grid table message
6492 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6494 // TODO: something with updateLabels flag
6498 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6504 if (IsCellEditControlEnabled())
6505 DisableCellEditControl();
6507 bool done
= m_table
->DeleteRows( pos
, numRows
);
6509 // the table will have sent the results of the delete row
6510 // operation to this view object as a grid table message
6516 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6518 // TODO: something with updateLabels flag
6522 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6528 if (IsCellEditControlEnabled())
6529 DisableCellEditControl();
6531 bool done
= m_table
->InsertCols( pos
, numCols
);
6533 // the table will have sent the results of the insert col
6534 // operation to this view object as a grid table message
6540 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6542 // TODO: something with updateLabels flag
6546 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6552 bool done
= m_table
->AppendCols( numCols
);
6554 // the table will have sent the results of the append col
6555 // operation to this view object as a grid table message
6561 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6563 // TODO: something with updateLabels flag
6567 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6573 if (IsCellEditControlEnabled())
6574 DisableCellEditControl();
6576 bool done
= m_table
->DeleteCols( pos
, numCols
);
6578 // the table will have sent the results of the delete col
6579 // operation to this view object as a grid table message
6586 // ----- event handlers
6589 // Generate a grid event based on a mouse event and
6590 // return the result of ProcessEvent()
6592 int wxGrid::SendEvent( const wxEventType type
,
6594 wxMouseEvent
& mouseEv
)
6596 bool claimed
, vetoed
;
6598 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6600 int rowOrCol
= (row
== -1 ? col
: row
);
6602 wxGridSizeEvent
gridEvt( GetId(),
6606 mouseEv
.GetX() + GetRowLabelSize(),
6607 mouseEv
.GetY() + GetColLabelSize(),
6608 mouseEv
.ControlDown(),
6609 mouseEv
.ShiftDown(),
6611 mouseEv
.MetaDown() );
6613 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6614 vetoed
= !gridEvt
.IsAllowed();
6616 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6618 // Right now, it should _never_ end up here!
6619 wxGridRangeSelectEvent
gridEvt( GetId(),
6623 m_selectingBottomRight
,
6625 mouseEv
.ControlDown(),
6626 mouseEv
.ShiftDown(),
6628 mouseEv
.MetaDown() );
6630 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6631 vetoed
= !gridEvt
.IsAllowed();
6633 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
6634 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
6635 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
6636 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
6638 wxPoint pos
= mouseEv
.GetPosition();
6640 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
6641 pos
.y
+= GetColLabelSize();
6642 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
6643 pos
.x
+= GetRowLabelSize();
6645 wxGridEvent
gridEvt( GetId(),
6652 mouseEv
.ControlDown(),
6653 mouseEv
.ShiftDown(),
6655 mouseEv
.MetaDown() );
6656 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6657 vetoed
= !gridEvt
.IsAllowed();
6661 wxGridEvent
gridEvt( GetId(),
6665 mouseEv
.GetX() + GetRowLabelSize(),
6666 mouseEv
.GetY() + GetColLabelSize(),
6668 mouseEv
.ControlDown(),
6669 mouseEv
.ShiftDown(),
6671 mouseEv
.MetaDown() );
6672 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6673 vetoed
= !gridEvt
.IsAllowed();
6676 // A Veto'd event may not be `claimed' so test this first
6680 return claimed
? 1 : 0;
6683 // Generate a grid event of specified type and return the result
6684 // of ProcessEvent().
6686 int wxGrid::SendEvent( const wxEventType type
,
6689 bool claimed
, vetoed
;
6691 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6693 int rowOrCol
= (row
== -1 ? col
: row
);
6695 wxGridSizeEvent
gridEvt( GetId(), type
, this, rowOrCol
);
6697 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6698 vetoed
= !gridEvt
.IsAllowed();
6702 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
6704 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6705 vetoed
= !gridEvt
.IsAllowed();
6708 // A Veto'd event may not be `claimed' so test this first
6712 return claimed
? 1 : 0;
6715 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6717 // needed to prevent zillions of paint events on MSW
6721 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6723 // Don't do anything if between Begin/EndBatch...
6724 // EndBatch() will do all this on the last nested one anyway.
6725 if (! GetBatchCount())
6727 // Refresh to get correct scrolled position:
6728 wxScrolledWindow::Refresh(eraseb
, rect
);
6732 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6733 int width_label
, width_cell
, height_label
, height_cell
;
6736 // Copy rectangle can get scroll offsets..
6737 rect_x
= rect
->GetX();
6738 rect_y
= rect
->GetY();
6739 rectWidth
= rect
->GetWidth();
6740 rectHeight
= rect
->GetHeight();
6742 width_label
= m_rowLabelWidth
- rect_x
;
6743 if (width_label
> rectWidth
)
6744 width_label
= rectWidth
;
6746 height_label
= m_colLabelHeight
- rect_y
;
6747 if (height_label
> rectHeight
)
6748 height_label
= rectHeight
;
6750 if (rect_x
> m_rowLabelWidth
)
6752 x
= rect_x
- m_rowLabelWidth
;
6753 width_cell
= rectWidth
;
6758 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6761 if (rect_y
> m_colLabelHeight
)
6763 y
= rect_y
- m_colLabelHeight
;
6764 height_cell
= rectHeight
;
6769 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6772 // Paint corner label part intersecting rect.
6773 if ( width_label
> 0 && height_label
> 0 )
6775 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6776 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6779 // Paint col labels part intersecting rect.
6780 if ( width_cell
> 0 && height_label
> 0 )
6782 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6783 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6786 // Paint row labels part intersecting rect.
6787 if ( width_label
> 0 && height_cell
> 0 )
6789 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6790 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6793 // Paint cell area part intersecting rect.
6794 if ( width_cell
> 0 && height_cell
> 0 )
6796 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6797 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6802 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6803 m_colLabelWin
->Refresh(eraseb
, NULL
);
6804 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6805 m_gridWin
->Refresh(eraseb
, NULL
);
6810 void wxGrid::OnSize( wxSizeEvent
& event
)
6812 // position the child windows
6815 // don't call CalcDimensions() from here, the base class handles the size
6820 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6822 if ( m_inOnKeyDown
)
6824 // shouldn't be here - we are going round in circles...
6826 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6829 m_inOnKeyDown
= true;
6831 // propagate the event up and see if it gets processed
6832 wxWindow
*parent
= GetParent();
6833 wxKeyEvent
keyEvt( event
);
6834 keyEvt
.SetEventObject( parent
);
6836 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6838 if (GetLayoutDirection() == wxLayout_RightToLeft
)
6840 if (event
.GetKeyCode() == WXK_RIGHT
)
6841 event
.m_keyCode
= WXK_LEFT
;
6842 else if (event
.GetKeyCode() == WXK_LEFT
)
6843 event
.m_keyCode
= WXK_RIGHT
;
6846 // try local handlers
6847 switch ( event
.GetKeyCode() )
6850 if ( event
.ControlDown() )
6851 MoveCursorUpBlock( event
.ShiftDown() );
6853 MoveCursorUp( event
.ShiftDown() );
6857 if ( event
.ControlDown() )
6858 MoveCursorDownBlock( event
.ShiftDown() );
6860 MoveCursorDown( event
.ShiftDown() );
6864 if ( event
.ControlDown() )
6865 MoveCursorLeftBlock( event
.ShiftDown() );
6867 MoveCursorLeft( event
.ShiftDown() );
6871 if ( event
.ControlDown() )
6872 MoveCursorRightBlock( event
.ShiftDown() );
6874 MoveCursorRight( event
.ShiftDown() );
6878 case WXK_NUMPAD_ENTER
:
6879 if ( event
.ControlDown() )
6881 event
.Skip(); // to let the edit control have the return
6885 if ( GetGridCursorRow() < GetNumberRows()-1 )
6887 MoveCursorDown( event
.ShiftDown() );
6891 // at the bottom of a column
6892 DisableCellEditControl();
6902 if (event
.ShiftDown())
6904 if ( GetGridCursorCol() > 0 )
6906 MoveCursorLeft( false );
6911 DisableCellEditControl();
6916 if ( GetGridCursorCol() < GetNumberCols() - 1 )
6918 MoveCursorRight( false );
6923 DisableCellEditControl();
6929 if ( event
.ControlDown() )
6931 MakeCellVisible( 0, 0 );
6932 SetCurrentCell( 0, 0 );
6941 if ( event
.ControlDown() )
6943 MakeCellVisible( m_numRows
- 1, m_numCols
- 1 );
6944 SetCurrentCell( m_numRows
- 1, m_numCols
- 1 );
6961 if ( event
.ControlDown() )
6965 m_selection
->ToggleCellSelection(
6966 m_currentCellCoords
.GetRow(),
6967 m_currentCellCoords
.GetCol(),
6968 event
.ControlDown(),
6976 if ( !IsEditable() )
6977 MoveCursorRight( false );
6988 m_inOnKeyDown
= false;
6991 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6993 // try local handlers
6995 if ( event
.GetKeyCode() == WXK_SHIFT
)
6997 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6998 m_selectingBottomRight
!= wxGridNoCellCoords
)
7002 m_selection
->SelectBlock(
7003 m_selectingTopLeft
.GetRow(),
7004 m_selectingTopLeft
.GetCol(),
7005 m_selectingBottomRight
.GetRow(),
7006 m_selectingBottomRight
.GetCol(),
7007 event
.ControlDown(),
7014 m_selectingTopLeft
= wxGridNoCellCoords
;
7015 m_selectingBottomRight
= wxGridNoCellCoords
;
7016 m_selectingKeyboard
= wxGridNoCellCoords
;
7020 void wxGrid::OnChar( wxKeyEvent
& event
)
7022 // is it possible to edit the current cell at all?
7023 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7025 // yes, now check whether the cells editor accepts the key
7026 int row
= m_currentCellCoords
.GetRow();
7027 int col
= m_currentCellCoords
.GetCol();
7028 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7029 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7031 // <F2> is special and will always start editing, for
7032 // other keys - ask the editor itself
7033 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
7034 || editor
->IsAcceptedKey(event
) )
7036 // ensure cell is visble
7037 MakeCellVisible(row
, col
);
7038 EnableCellEditControl();
7040 // a problem can arise if the cell is not completely
7041 // visible (even after calling MakeCellVisible the
7042 // control is not created and calling StartingKey will
7044 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
7045 editor
->StartingKey(event
);
7061 void wxGrid::OnEraseBackground(wxEraseEvent
&)
7065 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
7067 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
7069 // the event has been intercepted - do nothing
7073 wxClientDC
dc( m_gridWin
);
7076 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
7078 DisableCellEditControl();
7080 if ( IsVisible( m_currentCellCoords
, false ) )
7083 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
7084 if ( !m_gridLinesEnabled
)
7092 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
7094 // Otherwise refresh redraws the highlight!
7095 m_currentCellCoords
= coords
;
7097 DrawGridCellArea( dc
, cells
);
7098 DrawAllGridLines( dc
, r
);
7102 m_currentCellCoords
= coords
;
7104 wxGridCellAttr
*attr
= GetCellAttr( coords
);
7105 DrawCellHighlight( dc
, attr
);
7109 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7112 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7116 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
7119 rightCol
= GetNumberCols() - 1;
7121 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
7124 bottomRow
= GetNumberRows() - 1;
7128 if ( topRow
> bottomRow
)
7135 if ( leftCol
> rightCol
)
7142 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7143 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7145 // First the case that we selected a completely new area
7146 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
7147 m_selectingBottomRight
== wxGridNoCellCoords
)
7150 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
7151 wxGridCellCoords ( bottomRow
, rightCol
) );
7152 m_gridWin
->Refresh( false, &rect
);
7155 // Now handle changing an existing selection area.
7156 else if ( m_selectingTopLeft
!= updateTopLeft
||
7157 m_selectingBottomRight
!= updateBottomRight
)
7159 // Compute two optimal update rectangles:
7160 // Either one rectangle is a real subset of the
7161 // other, or they are (almost) disjoint!
7163 bool need_refresh
[4];
7167 need_refresh
[3] = false;
7170 // Store intermediate values
7171 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
7172 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
7173 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
7174 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
7176 // Determine the outer/inner coordinates.
7177 if (oldLeft
> leftCol
)
7183 if (oldTop
> topRow
)
7189 if (oldRight
< rightCol
)
7192 oldRight
= rightCol
;
7195 if (oldBottom
< bottomRow
)
7198 oldBottom
= bottomRow
;
7202 // Now, either the stuff marked old is the outer
7203 // rectangle or we don't have a situation where one
7204 // is contained in the other.
7206 if ( oldLeft
< leftCol
)
7208 // Refresh the newly selected or deselected
7209 // area to the left of the old or new selection.
7210 need_refresh
[0] = true;
7211 rect
[0] = BlockToDeviceRect(
7212 wxGridCellCoords( oldTop
, oldLeft
),
7213 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
7216 if ( oldTop
< topRow
)
7218 // Refresh the newly selected or deselected
7219 // area above the old or new selection.
7220 need_refresh
[1] = true;
7221 rect
[1] = BlockToDeviceRect(
7222 wxGridCellCoords( oldTop
, leftCol
),
7223 wxGridCellCoords( topRow
- 1, rightCol
) );
7226 if ( oldRight
> rightCol
)
7228 // Refresh the newly selected or deselected
7229 // area to the right of the old or new selection.
7230 need_refresh
[2] = true;
7231 rect
[2] = BlockToDeviceRect(
7232 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
7233 wxGridCellCoords( oldBottom
, oldRight
) );
7236 if ( oldBottom
> bottomRow
)
7238 // Refresh the newly selected or deselected
7239 // area below the old or new selection.
7240 need_refresh
[3] = true;
7241 rect
[3] = BlockToDeviceRect(
7242 wxGridCellCoords( bottomRow
+ 1, leftCol
),
7243 wxGridCellCoords( oldBottom
, rightCol
) );
7246 // various Refresh() calls
7247 for (i
= 0; i
< 4; i
++ )
7248 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7249 m_gridWin
->Refresh( false, &(rect
[i
]) );
7253 m_selectingTopLeft
= updateTopLeft
;
7254 m_selectingBottomRight
= updateBottomRight
;
7258 // ------ functions to get/send data (see also public functions)
7261 bool wxGrid::GetModelValues()
7263 // Hide the editor, so it won't hide a changed value.
7264 HideCellEditControl();
7268 // all we need to do is repaint the grid
7270 m_gridWin
->Refresh();
7277 bool wxGrid::SetModelValues()
7281 // Disable the editor, so it won't hide a changed value.
7282 // Do we also want to save the current value of the editor first?
7284 DisableCellEditControl();
7288 for ( row
= 0; row
< m_numRows
; row
++ )
7290 for ( col
= 0; col
< m_numCols
; col
++ )
7292 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
7302 // Note - this function only draws cells that are in the list of
7303 // exposed cells (usually set from the update region by
7304 // CalcExposedCells)
7306 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7308 if ( !m_numRows
|| !m_numCols
)
7311 int i
, numCells
= cells
.GetCount();
7312 int row
, col
, cell_rows
, cell_cols
;
7313 wxGridCellCoordsArray redrawCells
;
7315 for ( i
= numCells
- 1; i
>= 0; i
-- )
7317 row
= cells
[i
].GetRow();
7318 col
= cells
[i
].GetCol();
7319 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7321 // If this cell is part of a multicell block, find owner for repaint
7322 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7324 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
7325 bool marked
= false;
7326 for ( int j
= 0; j
< numCells
; j
++ )
7328 if ( cell
== cells
[j
] )
7337 int count
= redrawCells
.GetCount();
7338 for (int j
= 0; j
< count
; j
++)
7340 if ( cell
== redrawCells
[j
] )
7348 redrawCells
.Add( cell
);
7351 // don't bother drawing this cell
7355 // If this cell is empty, find cell to left that might want to overflow
7356 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7358 for ( int l
= 0; l
< cell_rows
; l
++ )
7360 // find a cell in this row to leave already marked for repaint
7362 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7363 if ((redrawCells
[k
].GetCol() < left
) &&
7364 (redrawCells
[k
].GetRow() == row
))
7366 left
= redrawCells
[k
].GetCol();
7370 left
= 0; // oh well
7372 for (int j
= col
- 1; j
>= left
; j
--)
7374 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7376 if (GetCellOverflow(row
+ l
, j
))
7378 wxGridCellCoords
cell(row
+ l
, j
);
7379 bool marked
= false;
7381 for (int k
= 0; k
< numCells
; k
++)
7383 if ( cell
== cells
[k
] )
7392 int count
= redrawCells
.GetCount();
7393 for (int k
= 0; k
< count
; k
++)
7395 if ( cell
== redrawCells
[k
] )
7402 redrawCells
.Add( cell
);
7411 DrawCell( dc
, cells
[i
] );
7414 numCells
= redrawCells
.GetCount();
7416 for ( i
= numCells
- 1; i
>= 0; i
-- )
7418 DrawCell( dc
, redrawCells
[i
] );
7422 void wxGrid::DrawGridSpace( wxDC
& dc
)
7425 m_gridWin
->GetClientSize( &cw
, &ch
);
7428 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7430 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
7431 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7433 if ( right
> rightCol
|| bottom
> bottomRow
)
7436 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7438 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
7439 dc
.SetPen( *wxTRANSPARENT_PEN
);
7441 if ( right
> rightCol
)
7443 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7446 if ( bottom
> bottomRow
)
7448 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7453 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7455 int row
= coords
.GetRow();
7456 int col
= coords
.GetCol();
7458 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7461 // we draw the cell border ourselves
7462 #if !WXGRID_DRAW_LINES
7463 if ( m_gridLinesEnabled
)
7464 DrawCellBorder( dc
, coords
);
7467 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7469 bool isCurrent
= coords
== m_currentCellCoords
;
7471 wxRect rect
= CellToRect( row
, col
);
7473 // if the editor is shown, we should use it and not the renderer
7474 // Note: However, only if it is really _shown_, i.e. not hidden!
7475 if ( isCurrent
&& IsCellEditControlShown() )
7477 // NB: this "#if..." is temporary and fixes a problem where the
7478 // edit control is erased by this code after being rendered.
7479 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7480 // implicitly, causing this out-of order render.
7481 #if !defined(__WXMAC__) || wxMAC_USE_CORE_GRAPHICS
7482 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7483 editor
->PaintBackground(rect
, attr
);
7489 // but all the rest is drawn by the cell renderer and hence may be customized
7490 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7491 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7498 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7500 int row
= m_currentCellCoords
.GetRow();
7501 int col
= m_currentCellCoords
.GetCol();
7503 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7506 wxRect rect
= CellToRect(row
, col
);
7508 // hmmm... what could we do here to show that the cell is disabled?
7509 // for now, I just draw a thinner border than for the other ones, but
7510 // it doesn't look really good
7512 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7516 // The center of the drawn line is where the position/width/height of
7517 // the rectangle is actually at (on wxMSW at least), so the
7518 // size of the rectangle is reduced to compensate for the thickness of
7519 // the line. If this is too strange on non-wxMSW platforms then
7520 // please #ifdef this appropriately.
7521 rect
.x
+= penWidth
/ 2;
7522 rect
.y
+= penWidth
/ 2;
7523 rect
.width
-= penWidth
- 1;
7524 rect
.height
-= penWidth
- 1;
7526 // Now draw the rectangle
7527 // use the cellHighlightColour if the cell is inside a selection, this
7528 // will ensure the cell is always visible.
7529 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
: m_cellHighlightColour
, penWidth
, wxSOLID
));
7530 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7531 dc
.DrawRectangle(rect
);
7535 // VZ: my experiments with 3D borders...
7537 // how to properly set colours for arbitrary bg?
7538 wxCoord x1
= rect
.x
,
7540 x2
= rect
.x
+ rect
.width
- 1,
7541 y2
= rect
.y
+ rect
.height
- 1;
7543 dc
.SetPen(*wxWHITE_PEN
);
7544 dc
.DrawLine(x1
, y1
, x2
, y1
);
7545 dc
.DrawLine(x1
, y1
, x1
, y2
);
7547 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7548 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7550 dc
.SetPen(*wxBLACK_PEN
);
7551 dc
.DrawLine(x1
, y2
, x2
, y2
);
7552 dc
.DrawLine(x2
, y1
, x2
, y2
+ 1);
7556 wxPen
wxGrid::GetDefaultGridLinePen()
7558 return wxPen(GetGridLineColour(), 1, wxSOLID
);
7561 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
7563 return GetDefaultGridLinePen();
7566 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
7568 return GetDefaultGridLinePen();
7571 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7573 int row
= coords
.GetRow();
7574 int col
= coords
.GetCol();
7575 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7579 wxRect rect
= CellToRect( row
, col
);
7581 // right hand border
7582 dc
.SetPen( GetColGridLinePen(col
) );
7583 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7584 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7587 dc
.SetPen( GetRowGridLinePen(row
) );
7588 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7589 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7592 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7594 // This if block was previously in wxGrid::OnPaint but that doesn't
7595 // seem to get called under wxGTK - MB
7597 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7598 m_numRows
&& m_numCols
)
7600 m_currentCellCoords
.Set(0, 0);
7603 if ( IsCellEditControlShown() )
7605 // don't show highlight when the edit control is shown
7609 // if the active cell was repainted, repaint its highlight too because it
7610 // might have been damaged by the grid lines
7611 size_t count
= cells
.GetCount();
7612 for ( size_t n
= 0; n
< count
; n
++ )
7614 if ( cells
[n
] == m_currentCellCoords
)
7616 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7617 DrawCellHighlight(dc
, attr
);
7625 // TODO: remove this ???
7626 // This is used to redraw all grid lines e.g. when the grid line colour
7629 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7631 #if !WXGRID_DRAW_LINES
7635 if ( !m_gridLinesEnabled
|| !m_numRows
|| !m_numCols
)
7638 int top
, bottom
, left
, right
;
7640 #if 0 //#ifndef __WXGTK__
7644 m_gridWin
->GetClientSize(&cw
, &ch
);
7646 // virtual coords of visible area
7648 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7649 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7654 reg
.GetBox(x
, y
, w
, h
);
7655 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7656 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7660 m_gridWin
->GetClientSize(&cw
, &ch
);
7661 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7662 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7665 // avoid drawing grid lines past the last row and col
7667 right
= wxMin( right
, GetColRight(GetColAt( m_numCols
- 1 )) );
7668 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7670 // no gridlines inside multicells, clip them out
7671 int leftCol
= GetColPos( internalXToCol(left
) );
7672 int topRow
= internalYToRow(top
);
7673 int rightCol
= GetColPos( internalXToCol(right
) );
7674 int bottomRow
= internalYToRow(bottom
);
7677 // CS: I don't know why suddenly unscrolled coordinates are used for clipping
7678 wxRegion
clippedcells(0, 0, cw
, ch
);
7680 int i
, j
, cell_rows
, cell_cols
;
7683 for (j
=topRow
; j
<bottomRow
; j
++)
7686 for (colPos
=leftCol
; colPos
<rightCol
; colPos
++)
7688 i
= GetColAt( colPos
);
7690 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7691 if ((cell_rows
> 1) || (cell_cols
> 1))
7693 rect
= CellToRect(j
,i
);
7694 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7695 clippedcells
.Subtract(rect
);
7697 else if ((cell_rows
< 0) || (cell_cols
< 0))
7699 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7700 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7701 clippedcells
.Subtract(rect
);
7706 wxRegion
clippedcells( left
, top
, right
- left
, bottom
- top
);
7708 int i
, j
, cell_rows
, cell_cols
;
7711 for (j
=topRow
; j
<bottomRow
; j
++)
7713 for (i
=leftCol
; i
<rightCol
; i
++)
7715 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7716 if ((cell_rows
> 1) || (cell_cols
> 1))
7718 rect
= CellToRect(j
, i
);
7719 clippedcells
.Subtract(rect
);
7721 else if ((cell_rows
< 0) || (cell_cols
< 0))
7723 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7724 clippedcells
.Subtract(rect
);
7730 dc
.SetClippingRegion( clippedcells
);
7733 // horizontal grid lines
7735 // already declared above - int i;
7736 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7738 int bot
= GetRowBottom(i
) - 1;
7747 dc
.SetPen( GetRowGridLinePen(i
) );
7748 dc
.DrawLine( left
, bot
, right
, bot
);
7752 // vertical grid lines
7755 for ( colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
7757 i
= GetColAt( colPos
);
7759 int colRight
= GetColRight(i
);
7761 if (GetLayoutDirection() != wxLayout_RightToLeft
)
7765 if ( colRight
> right
)
7770 if ( colRight
>= left
)
7772 dc
.SetPen( GetColGridLinePen(i
) );
7773 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7777 dc
.DestroyClippingRegion();
7780 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
7786 size_t numLabels
= rows
.GetCount();
7788 for ( i
= 0; i
< numLabels
; i
++ )
7790 DrawRowLabel( dc
, rows
[i
] );
7794 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7796 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7803 rect
.SetY( GetRowTop(row
) + 1 );
7804 rect
.SetWidth( m_rowLabelWidth
- 2 );
7805 rect
.SetHeight( GetRowHeight(row
) - 2 );
7807 CalcScrolledPosition( 0, rect
.y
, NULL
, &rect
.y
);
7809 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7811 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7813 int rowTop
= GetRowTop(row
),
7814 rowBottom
= GetRowBottom(row
) - 1;
7816 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
7817 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
7818 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7819 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7821 dc
.SetPen( *wxWHITE_PEN
);
7822 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7823 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
7826 dc
.SetBackgroundMode( wxTRANSPARENT
);
7827 dc
.SetTextForeground( GetLabelTextColour() );
7828 dc
.SetFont( GetLabelFont() );
7831 GetRowLabelAlignment( &hAlign
, &vAlign
);
7834 rect
.SetY( GetRowTop(row
) + 2 );
7835 rect
.SetWidth( m_rowLabelWidth
- 4 );
7836 rect
.SetHeight( GetRowHeight(row
) - 4 );
7837 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7840 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7846 size_t numLabels
= cols
.GetCount();
7848 for ( i
= 0; i
< numLabels
; i
++ )
7850 DrawColLabel( dc
, cols
[i
] );
7854 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7856 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7859 int colLeft
= GetColLeft(col
);
7864 rect
.SetX( colLeft
+ 1 );
7866 rect
.SetWidth( GetColWidth(col
) - 2 );
7867 rect
.SetHeight( m_colLabelHeight
- 2 );
7869 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7871 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7873 int colRight
= GetColRight(col
) - 1;
7875 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
7876 dc
.DrawLine( colRight
, 0, colRight
, m_colLabelHeight
- 1 );
7877 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7878 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
7879 colRight
+ 1, m_colLabelHeight
- 1 );
7881 dc
.SetPen( *wxWHITE_PEN
);
7882 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
7883 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7886 dc
.SetBackgroundMode( wxTRANSPARENT
);
7887 dc
.SetTextForeground( GetLabelTextColour() );
7888 dc
.SetFont( GetLabelFont() );
7890 int hAlign
, vAlign
, orient
;
7891 GetColLabelAlignment( &hAlign
, &vAlign
);
7892 orient
= GetColLabelTextOrientation();
7894 rect
.SetX( colLeft
+ 2 );
7896 rect
.SetWidth( GetColWidth(col
) - 4 );
7897 rect
.SetHeight( m_colLabelHeight
- 4 );
7898 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7901 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7902 const wxString
& value
,
7906 int textOrientation
)
7908 wxArrayString lines
;
7910 StringToLines( value
, lines
);
7912 // Forward to new API.
7913 DrawTextRectangle( dc
,
7921 // VZ: this should be replaced with wxDC::DrawLabel() to which we just have to
7922 // add textOrientation support
7923 void wxGrid::DrawTextRectangle(wxDC
& dc
,
7924 const wxArrayString
& lines
,
7928 int textOrientation
)
7930 if ( lines
.empty() )
7933 wxDCClipper
clip(dc
, rect
);
7938 if ( textOrientation
== wxHORIZONTAL
)
7939 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
7941 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7945 switch ( vertAlign
)
7947 case wxALIGN_BOTTOM
:
7948 if ( textOrientation
== wxHORIZONTAL
)
7949 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7951 x
= rect
.x
+ rect
.width
- textWidth
;
7954 case wxALIGN_CENTRE
:
7955 if ( textOrientation
== wxHORIZONTAL
)
7956 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
7958 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
7963 if ( textOrientation
== wxHORIZONTAL
)
7970 // Align each line of a multi-line label
7971 size_t nLines
= lines
.GetCount();
7972 for ( size_t l
= 0; l
< nLines
; l
++ )
7974 const wxString
& line
= lines
[l
];
7978 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
7984 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
7986 switch ( horizAlign
)
7989 if ( textOrientation
== wxHORIZONTAL
)
7990 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7992 y
= rect
.y
+ lineWidth
+ 1;
7995 case wxALIGN_CENTRE
:
7996 if ( textOrientation
== wxHORIZONTAL
)
7997 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
7999 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
8004 if ( textOrientation
== wxHORIZONTAL
)
8007 y
= rect
.y
+ rect
.height
- 1;
8011 if ( textOrientation
== wxHORIZONTAL
)
8013 dc
.DrawText( line
, x
, y
);
8018 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
8024 // Split multi-line text up into an array of strings.
8025 // Any existing contents of the string array are preserved.
8027 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
8031 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
8032 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
8034 while ( startPos
< (int)tVal
.length() )
8036 pos
= tVal
.Mid(startPos
).Find( eol
);
8041 else if ( pos
== 0 )
8043 lines
.Add( wxEmptyString
);
8047 lines
.Add( value
.Mid(startPos
, pos
) );
8050 startPos
+= pos
+ 1;
8053 if ( startPos
< (int)value
.length() )
8055 lines
.Add( value
.Mid( startPos
) );
8059 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
8060 const wxArrayString
& lines
,
8061 long *width
, long *height
)
8065 long lineW
= 0, lineH
= 0;
8068 for ( i
= 0; i
< lines
.GetCount(); i
++ )
8070 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
8071 w
= wxMax( w
, lineW
);
8080 // ------ Batch processing.
8082 void wxGrid::EndBatch()
8084 if ( m_batchCount
> 0 )
8087 if ( !m_batchCount
)
8090 m_rowLabelWin
->Refresh();
8091 m_colLabelWin
->Refresh();
8092 m_cornerLabelWin
->Refresh();
8093 m_gridWin
->Refresh();
8098 // Use this, rather than wxWindow::Refresh(), to force an immediate
8099 // repainting of the grid. Has no effect if you are already inside a
8100 // BeginBatch / EndBatch block.
8102 void wxGrid::ForceRefresh()
8108 bool wxGrid::Enable(bool enable
)
8110 if ( !wxScrolledWindow::Enable(enable
) )
8113 // redraw in the new state
8114 m_gridWin
->Refresh();
8120 // ------ Edit control functions
8123 void wxGrid::EnableEditing( bool edit
)
8125 // TODO: improve this ?
8127 if ( edit
!= m_editable
)
8130 EnableCellEditControl(edit
);
8135 void wxGrid::EnableCellEditControl( bool enable
)
8140 if ( enable
!= m_cellEditCtrlEnabled
)
8144 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
8147 // this should be checked by the caller!
8148 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8150 // do it before ShowCellEditControl()
8151 m_cellEditCtrlEnabled
= enable
;
8153 ShowCellEditControl();
8157 //FIXME:add veto support
8158 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
8160 HideCellEditControl();
8161 SaveEditControlValue();
8163 // do it after HideCellEditControl()
8164 m_cellEditCtrlEnabled
= enable
;
8169 bool wxGrid::IsCurrentCellReadOnly() const
8172 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
8173 bool readonly
= attr
->IsReadOnly();
8179 bool wxGrid::CanEnableCellControl() const
8181 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
8182 !IsCurrentCellReadOnly();
8185 bool wxGrid::IsCellEditControlEnabled() const
8187 // the cell edit control might be disable for all cells or just for the
8188 // current one if it's read only
8189 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
8192 bool wxGrid::IsCellEditControlShown() const
8194 bool isShown
= false;
8196 if ( m_cellEditCtrlEnabled
)
8198 int row
= m_currentCellCoords
.GetRow();
8199 int col
= m_currentCellCoords
.GetCol();
8200 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8201 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
8206 if ( editor
->IsCreated() )
8208 isShown
= editor
->GetControl()->IsShown();
8218 void wxGrid::ShowCellEditControl()
8220 if ( IsCellEditControlEnabled() )
8222 if ( !IsVisible( m_currentCellCoords
, false ) )
8224 m_cellEditCtrlEnabled
= false;
8229 wxRect rect
= CellToRect( m_currentCellCoords
);
8230 int row
= m_currentCellCoords
.GetRow();
8231 int col
= m_currentCellCoords
.GetCol();
8233 // if this is part of a multicell, find owner (topleft)
8234 int cell_rows
, cell_cols
;
8235 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8236 if ( cell_rows
<= 0 || cell_cols
<= 0 )
8240 m_currentCellCoords
.SetRow( row
);
8241 m_currentCellCoords
.SetCol( col
);
8244 // erase the highlight and the cell contents because the editor
8245 // might not cover the entire cell
8246 wxClientDC
dc( m_gridWin
);
8248 dc
.SetBrush(wxBrush(GetCellAttr(row
, col
)->GetBackgroundColour(), wxSOLID
));
8249 dc
.SetPen(*wxTRANSPARENT_PEN
);
8250 dc
.DrawRectangle(rect
);
8252 // convert to scrolled coords
8253 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8259 // cell is shifted by one pixel
8260 // However, don't allow x or y to become negative
8261 // since the SetSize() method interprets that as
8268 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8269 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8270 if ( !editor
->IsCreated() )
8272 editor
->Create(m_gridWin
, wxID_ANY
,
8273 new wxGridCellEditorEvtHandler(this, editor
));
8275 wxGridEditorCreatedEvent
evt(GetId(),
8276 wxEVT_GRID_EDITOR_CREATED
,
8280 editor
->GetControl());
8281 GetEventHandler()->ProcessEvent(evt
);
8284 // resize editor to overflow into righthand cells if allowed
8285 int maxWidth
= rect
.width
;
8286 wxString value
= GetCellValue(row
, col
);
8287 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
8290 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
8291 if (maxWidth
< rect
.width
)
8292 maxWidth
= rect
.width
;
8295 int client_right
= m_gridWin
->GetClientSize().GetWidth();
8296 if (rect
.x
+ maxWidth
> client_right
)
8297 maxWidth
= client_right
- rect
.x
;
8299 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
8301 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8302 // may have changed earlier
8303 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
8306 GetCellSize( row
, i
, &c_rows
, &c_cols
);
8308 // looks weird going over a multicell
8309 if (m_table
->IsEmptyCell( row
, i
) &&
8310 (rect
.width
< maxWidth
) && (c_rows
== 1))
8312 rect
.width
+= GetColWidth( i
);
8318 if (rect
.GetRight() > client_right
)
8319 rect
.SetRight( client_right
- 1 );
8322 editor
->SetCellAttr( attr
);
8323 editor
->SetSize( rect
);
8325 editor
->GetControl()->Move(
8326 editor
->GetControl()->GetPosition().x
+ nXMove
,
8327 editor
->GetControl()->GetPosition().y
);
8328 editor
->Show( true, attr
);
8330 // recalc dimensions in case we need to
8331 // expand the scrolled window to account for editor
8334 editor
->BeginEdit(row
, col
, this);
8335 editor
->SetCellAttr(NULL
);
8343 void wxGrid::HideCellEditControl()
8345 if ( IsCellEditControlEnabled() )
8347 int row
= m_currentCellCoords
.GetRow();
8348 int col
= m_currentCellCoords
.GetCol();
8350 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8351 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
8352 editor
->Show( false );
8356 m_gridWin
->SetFocus();
8358 // refresh whole row to the right
8359 wxRect
rect( CellToRect(row
, col
) );
8360 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8361 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8364 // ensure that the pixels under the focus ring get refreshed as well
8365 rect
.Inflate(10, 10);
8368 m_gridWin
->Refresh( false, &rect
);
8372 void wxGrid::SaveEditControlValue()
8374 if ( IsCellEditControlEnabled() )
8376 int row
= m_currentCellCoords
.GetRow();
8377 int col
= m_currentCellCoords
.GetCol();
8379 wxString oldval
= GetCellValue(row
, col
);
8381 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8382 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8383 bool changed
= editor
->EndEdit(row
, col
, this);
8390 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
8391 m_currentCellCoords
.GetRow(),
8392 m_currentCellCoords
.GetCol() ) < 0 )
8394 // Event has been vetoed, set the data back.
8395 SetCellValue(row
, col
, oldval
);
8402 // ------ Grid location functions
8403 // Note that all of these functions work with the logical coordinates of
8404 // grid cells and labels so you will need to convert from device
8405 // coordinates for mouse events etc.
8408 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
8410 int row
= YToRow(y
);
8411 int col
= XToCol(x
);
8413 if ( row
== -1 || col
== -1 )
8415 coords
= wxGridNoCellCoords
;
8419 coords
.Set( row
, col
);
8423 // Internal Helper function for computing row or column from some
8424 // (unscrolled) coordinate value, using either
8425 // m_defaultRowHeight/m_defaultColWidth or binary search on array
8426 // of m_rowBottoms/m_ColRights to speed up the search!
8428 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
8429 const wxArrayInt
& BorderArray
, int nMax
,
8433 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
8438 size_t i_max
= coord
/ defaultDist
,
8441 if (BorderArray
.IsEmpty())
8443 if ((int) i_max
< nMax
)
8445 return clipToMinMax
? nMax
- 1 : -1;
8448 if ( i_max
>= BorderArray
.GetCount())
8450 i_max
= BorderArray
.GetCount() - 1;
8454 if ( coord
>= BorderArray
[i_max
])
8458 i_max
= coord
/ minDist
;
8460 i_max
= BorderArray
.GetCount() - 1;
8463 if ( i_max
>= BorderArray
.GetCount())
8464 i_max
= BorderArray
.GetCount() - 1;
8467 if ( coord
>= BorderArray
[i_max
])
8468 return clipToMinMax
? (int)i_max
: -1;
8469 if ( coord
< BorderArray
[0] )
8472 while ( i_max
- i_min
> 0 )
8474 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
8475 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8476 if (coord
>= BorderArray
[ i_max
- 1])
8480 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
8481 if (coord
< BorderArray
[median
])
8490 int wxGrid::YToRow( int y
)
8492 return CoordToRowOrCol(y
, m_defaultRowHeight
,
8493 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8496 int wxGrid::XToCol( int x
, bool clipToMinMax
)
8499 return clipToMinMax
&& (m_numCols
> 0) ? GetColAt( 0 ) : -1;
8501 if (!m_defaultColWidth
)
8502 m_defaultColWidth
= 1;
8504 int maxPos
= x
/ m_defaultColWidth
;
8507 if (m_colRights
.IsEmpty())
8509 if(maxPos
< m_numCols
)
8510 return GetColAt( maxPos
);
8511 return clipToMinMax
? GetColAt( m_numCols
- 1 ) : -1;
8514 if ( maxPos
>= m_numCols
)
8515 maxPos
= m_numCols
- 1;
8518 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8521 if (m_minAcceptableColWidth
)
8522 maxPos
= x
/ m_minAcceptableColWidth
;
8524 maxPos
= m_numCols
- 1;
8526 if ( maxPos
>= m_numCols
)
8527 maxPos
= m_numCols
- 1;
8530 //X is beyond the last column
8531 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8532 return clipToMinMax
? GetColAt( maxPos
) : -1;
8534 //X is before the first column
8535 if ( x
< m_colRights
[GetColAt( 0 )] )
8536 return GetColAt( 0 );
8538 //Perform a binary search
8539 while ( maxPos
- minPos
> 0 )
8541 wxCHECK_MSG(m_colRights
[GetColAt( minPos
)] <= x
&& x
< m_colRights
[GetColAt( maxPos
)],
8542 0, _T("wxGrid: internal error in XToCol"));
8544 if (x
>= m_colRights
[GetColAt( maxPos
- 1 )])
8545 return GetColAt( maxPos
);
8548 int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
8549 if (x
< m_colRights
[GetColAt( median
)])
8554 return GetColAt( maxPos
);
8557 // return the row number that that the y coord is near
8558 // the edge of, or -1 if not near an edge.
8559 // coords can only possibly be near an edge if
8560 // (a) the row/column is large enough to still allow for an "inner" area
8561 // that is _not_ nead the edge (i.e., if the height/width is smaller
8562 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8565 // (b) resizing rows/columns (the thing for which edge detection is
8566 // relevant at all) is enabled.
8568 int wxGrid::YToEdgeOfRow( int y
)
8571 i
= internalYToRow(y
);
8573 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragRowSize() )
8575 // We know that we are in row i, test whether we are
8576 // close enough to lower or upper border, respectively.
8577 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8579 else if ( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8586 // return the col number that that the x coord is near the edge of, or
8587 // -1 if not near an edge
8588 // See comment at YToEdgeOfRow for conditions on edge detection.
8590 int wxGrid::XToEdgeOfCol( int x
)
8593 i
= internalXToCol(x
);
8595 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragColSize() )
8597 // We know that we are in column i; test whether we are
8598 // close enough to right or left border, respectively.
8599 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8601 else if ( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8608 wxRect
wxGrid::CellToRect( int row
, int col
)
8610 wxRect
rect( -1, -1, -1, -1 );
8612 if ( row
>= 0 && row
< m_numRows
&&
8613 col
>= 0 && col
< m_numCols
)
8615 int i
, cell_rows
, cell_cols
;
8616 rect
.width
= rect
.height
= 0;
8617 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8618 // if negative then find multicell owner
8623 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8625 rect
.x
= GetColLeft(col
);
8626 rect
.y
= GetRowTop(row
);
8627 for (i
=col
; i
< col
+ cell_cols
; i
++)
8628 rect
.width
+= GetColWidth(i
);
8629 for (i
=row
; i
< row
+ cell_rows
; i
++)
8630 rect
.height
+= GetRowHeight(i
);
8633 // if grid lines are enabled, then the area of the cell is a bit smaller
8634 if (m_gridLinesEnabled
)
8643 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
8645 // get the cell rectangle in logical coords
8647 wxRect
r( CellToRect( row
, col
) );
8649 // convert to device coords
8651 int left
, top
, right
, bottom
;
8652 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8653 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8655 // check against the client area of the grid window
8657 m_gridWin
->GetClientSize( &cw
, &ch
);
8659 if ( wholeCellVisible
)
8661 // is the cell wholly visible ?
8662 return ( left
>= 0 && right
<= cw
&&
8663 top
>= 0 && bottom
<= ch
);
8667 // is the cell partly visible ?
8669 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8670 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8674 // make the specified cell location visible by doing a minimal amount
8677 void wxGrid::MakeCellVisible( int row
, int col
)
8680 int xpos
= -1, ypos
= -1;
8682 if ( row
>= 0 && row
< m_numRows
&&
8683 col
>= 0 && col
< m_numCols
)
8685 // get the cell rectangle in logical coords
8686 wxRect
r( CellToRect( row
, col
) );
8688 // convert to device coords
8689 int left
, top
, right
, bottom
;
8690 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8691 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8694 m_gridWin
->GetClientSize( &cw
, &ch
);
8700 else if ( bottom
> ch
)
8702 int h
= r
.GetHeight();
8704 for ( i
= row
- 1; i
>= 0; i
-- )
8706 int rowHeight
= GetRowHeight(i
);
8707 if ( h
+ rowHeight
> ch
)
8714 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8715 // have rounding errors (this is important, because if we do,
8716 // we might not scroll at all and some cells won't be redrawn)
8718 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
8719 // so just add a full scroll unit...
8720 ypos
+= m_scrollLineY
;
8723 // special handling for wide cells - show always left part of the cell!
8724 // Otherwise, e.g. when stepping from row to row, it would jump between
8725 // left and right part of the cell on every step!
8727 if ( left
< 0 || (right
- left
) >= cw
)
8731 else if ( right
> cw
)
8733 // position the view so that the cell is on the right
8735 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8736 xpos
= x0
+ (right
- cw
);
8738 // see comment for ypos above
8739 xpos
+= m_scrollLineX
;
8742 if ( xpos
!= -1 || ypos
!= -1 )
8745 xpos
/= m_scrollLineX
;
8747 ypos
/= m_scrollLineY
;
8748 Scroll( xpos
, ypos
);
8755 // ------ Grid cursor movement functions
8758 bool wxGrid::MoveCursorUp( bool expandSelection
)
8760 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8761 m_currentCellCoords
.GetRow() >= 0 )
8763 if ( expandSelection
)
8765 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8766 m_selectingKeyboard
= m_currentCellCoords
;
8767 if ( m_selectingKeyboard
.GetRow() > 0 )
8769 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8770 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8771 m_selectingKeyboard
.GetCol() );
8772 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8775 else if ( m_currentCellCoords
.GetRow() > 0 )
8777 int row
= m_currentCellCoords
.GetRow() - 1;
8778 int col
= m_currentCellCoords
.GetCol();
8780 MakeCellVisible( row
, col
);
8781 SetCurrentCell( row
, col
);
8792 bool wxGrid::MoveCursorDown( bool expandSelection
)
8794 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8795 m_currentCellCoords
.GetRow() < m_numRows
)
8797 if ( expandSelection
)
8799 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8800 m_selectingKeyboard
= m_currentCellCoords
;
8801 if ( m_selectingKeyboard
.GetRow() < m_numRows
- 1 )
8803 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8804 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8805 m_selectingKeyboard
.GetCol() );
8806 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8809 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8811 int row
= m_currentCellCoords
.GetRow() + 1;
8812 int col
= m_currentCellCoords
.GetCol();
8814 MakeCellVisible( row
, col
);
8815 SetCurrentCell( row
, col
);
8826 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8828 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8829 m_currentCellCoords
.GetCol() >= 0 )
8831 if ( expandSelection
)
8833 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8834 m_selectingKeyboard
= m_currentCellCoords
;
8835 if ( m_selectingKeyboard
.GetCol() > 0 )
8837 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8838 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8839 m_selectingKeyboard
.GetCol() );
8840 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8843 else if ( GetColPos( m_currentCellCoords
.GetCol() ) > 0 )
8845 int row
= m_currentCellCoords
.GetRow();
8846 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) - 1 );
8849 MakeCellVisible( row
, col
);
8850 SetCurrentCell( row
, col
);
8861 bool wxGrid::MoveCursorRight( bool expandSelection
)
8863 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8864 m_currentCellCoords
.GetCol() < m_numCols
)
8866 if ( expandSelection
)
8868 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8869 m_selectingKeyboard
= m_currentCellCoords
;
8870 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8872 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8873 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8874 m_selectingKeyboard
.GetCol() );
8875 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8878 else if ( GetColPos( m_currentCellCoords
.GetCol() ) < m_numCols
- 1 )
8880 int row
= m_currentCellCoords
.GetRow();
8881 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) + 1 );
8884 MakeCellVisible( row
, col
);
8885 SetCurrentCell( row
, col
);
8896 bool wxGrid::MovePageUp()
8898 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8901 int row
= m_currentCellCoords
.GetRow();
8905 m_gridWin
->GetClientSize( &cw
, &ch
);
8907 int y
= GetRowTop(row
);
8908 int newRow
= internalYToRow( y
- ch
+ 1 );
8910 if ( newRow
== row
)
8912 // row > 0, so newRow can never be less than 0 here.
8916 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8917 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8925 bool wxGrid::MovePageDown()
8927 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8930 int row
= m_currentCellCoords
.GetRow();
8931 if ( (row
+ 1) < m_numRows
)
8934 m_gridWin
->GetClientSize( &cw
, &ch
);
8936 int y
= GetRowTop(row
);
8937 int newRow
= internalYToRow( y
+ ch
);
8938 if ( newRow
== row
)
8940 // row < m_numRows, so newRow can't overflow here.
8944 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8945 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8953 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8956 m_currentCellCoords
!= wxGridNoCellCoords
&&
8957 m_currentCellCoords
.GetRow() > 0 )
8959 int row
= m_currentCellCoords
.GetRow();
8960 int col
= m_currentCellCoords
.GetCol();
8962 if ( m_table
->IsEmptyCell(row
, col
) )
8964 // starting in an empty cell: find the next block of
8970 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8974 else if ( m_table
->IsEmptyCell(row
- 1, col
) )
8976 // starting at the top of a block: find the next block
8982 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8988 // starting within a block: find the top of the block
8993 if ( m_table
->IsEmptyCell(row
, col
) )
9001 MakeCellVisible( row
, col
);
9002 if ( expandSelection
)
9004 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9005 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9010 SetCurrentCell( row
, col
);
9019 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
9022 m_currentCellCoords
!= wxGridNoCellCoords
&&
9023 m_currentCellCoords
.GetRow() < m_numRows
- 1 )
9025 int row
= m_currentCellCoords
.GetRow();
9026 int col
= m_currentCellCoords
.GetCol();
9028 if ( m_table
->IsEmptyCell(row
, col
) )
9030 // starting in an empty cell: find the next block of
9033 while ( row
< m_numRows
- 1 )
9036 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9040 else if ( m_table
->IsEmptyCell(row
+ 1, col
) )
9042 // starting at the bottom of a block: find the next block
9045 while ( row
< m_numRows
- 1 )
9048 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9054 // starting within a block: find the bottom of the block
9056 while ( row
< m_numRows
- 1 )
9059 if ( m_table
->IsEmptyCell(row
, col
) )
9067 MakeCellVisible( row
, col
);
9068 if ( expandSelection
)
9070 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9071 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9076 SetCurrentCell( row
, col
);
9085 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
9088 m_currentCellCoords
!= wxGridNoCellCoords
&&
9089 m_currentCellCoords
.GetCol() > 0 )
9091 int row
= m_currentCellCoords
.GetRow();
9092 int col
= m_currentCellCoords
.GetCol();
9094 if ( m_table
->IsEmptyCell(row
, col
) )
9096 // starting in an empty cell: find the next block of
9102 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9106 else if ( m_table
->IsEmptyCell(row
, col
- 1) )
9108 // starting at the left of a block: find the next block
9114 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9120 // starting within a block: find the left of the block
9125 if ( m_table
->IsEmptyCell(row
, col
) )
9133 MakeCellVisible( row
, col
);
9134 if ( expandSelection
)
9136 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9137 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9142 SetCurrentCell( row
, col
);
9151 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
9154 m_currentCellCoords
!= wxGridNoCellCoords
&&
9155 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
9157 int row
= m_currentCellCoords
.GetRow();
9158 int col
= m_currentCellCoords
.GetCol();
9160 if ( m_table
->IsEmptyCell(row
, col
) )
9162 // starting in an empty cell: find the next block of
9165 while ( col
< m_numCols
- 1 )
9168 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9172 else if ( m_table
->IsEmptyCell(row
, col
+ 1) )
9174 // starting at the right of a block: find the next block
9177 while ( col
< m_numCols
- 1 )
9180 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9186 // starting within a block: find the right of the block
9188 while ( col
< m_numCols
- 1 )
9191 if ( m_table
->IsEmptyCell(row
, col
) )
9199 MakeCellVisible( row
, col
);
9200 if ( expandSelection
)
9202 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9203 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9208 SetCurrentCell( row
, col
);
9218 // ------ Label values and formatting
9221 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
9224 *horiz
= m_rowLabelHorizAlign
;
9226 *vert
= m_rowLabelVertAlign
;
9229 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
9232 *horiz
= m_colLabelHorizAlign
;
9234 *vert
= m_colLabelVertAlign
;
9237 int wxGrid::GetColLabelTextOrientation()
9239 return m_colLabelTextOrientation
;
9242 wxString
wxGrid::GetRowLabelValue( int row
)
9246 return m_table
->GetRowLabelValue( row
);
9256 wxString
wxGrid::GetColLabelValue( int col
)
9260 return m_table
->GetColLabelValue( col
);
9270 void wxGrid::SetRowLabelSize( int width
)
9272 width
= wxMax( width
, 0 );
9273 if ( width
!= m_rowLabelWidth
)
9277 m_rowLabelWin
->Show( false );
9278 m_cornerLabelWin
->Show( false );
9280 else if ( m_rowLabelWidth
== 0 )
9282 m_rowLabelWin
->Show( true );
9283 if ( m_colLabelHeight
> 0 )
9284 m_cornerLabelWin
->Show( true );
9287 m_rowLabelWidth
= width
;
9289 wxScrolledWindow::Refresh( true );
9293 void wxGrid::SetColLabelSize( int height
)
9295 height
= wxMax( height
, 0 );
9296 if ( height
!= m_colLabelHeight
)
9300 m_colLabelWin
->Show( false );
9301 m_cornerLabelWin
->Show( false );
9303 else if ( m_colLabelHeight
== 0 )
9305 m_colLabelWin
->Show( true );
9306 if ( m_rowLabelWidth
> 0 )
9307 m_cornerLabelWin
->Show( true );
9310 m_colLabelHeight
= height
;
9312 wxScrolledWindow::Refresh( true );
9316 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
9318 if ( m_labelBackgroundColour
!= colour
)
9320 m_labelBackgroundColour
= colour
;
9321 m_rowLabelWin
->SetBackgroundColour( colour
);
9322 m_colLabelWin
->SetBackgroundColour( colour
);
9323 m_cornerLabelWin
->SetBackgroundColour( colour
);
9325 if ( !GetBatchCount() )
9327 m_rowLabelWin
->Refresh();
9328 m_colLabelWin
->Refresh();
9329 m_cornerLabelWin
->Refresh();
9334 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
9336 if ( m_labelTextColour
!= colour
)
9338 m_labelTextColour
= colour
;
9339 if ( !GetBatchCount() )
9341 m_rowLabelWin
->Refresh();
9342 m_colLabelWin
->Refresh();
9347 void wxGrid::SetLabelFont( const wxFont
& font
)
9350 if ( !GetBatchCount() )
9352 m_rowLabelWin
->Refresh();
9353 m_colLabelWin
->Refresh();
9357 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
9359 // allow old (incorrect) defs to be used
9362 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9363 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9364 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9369 case wxTOP
: vert
= wxALIGN_TOP
; break;
9370 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9371 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9374 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9376 m_rowLabelHorizAlign
= horiz
;
9379 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9381 m_rowLabelVertAlign
= vert
;
9384 if ( !GetBatchCount() )
9386 m_rowLabelWin
->Refresh();
9390 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
9392 // allow old (incorrect) defs to be used
9395 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9396 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9397 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9402 case wxTOP
: vert
= wxALIGN_TOP
; break;
9403 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9404 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9407 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9409 m_colLabelHorizAlign
= horiz
;
9412 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9414 m_colLabelVertAlign
= vert
;
9417 if ( !GetBatchCount() )
9419 m_colLabelWin
->Refresh();
9423 // Note: under MSW, the default column label font must be changed because it
9424 // does not support vertical printing
9426 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9427 // pGrid->SetLabelFont(font);
9428 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9430 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9432 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9433 m_colLabelTextOrientation
= textOrientation
;
9435 if ( !GetBatchCount() )
9436 m_colLabelWin
->Refresh();
9439 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9443 m_table
->SetRowLabelValue( row
, s
);
9444 if ( !GetBatchCount() )
9446 wxRect rect
= CellToRect( row
, 0 );
9447 if ( rect
.height
> 0 )
9449 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9451 rect
.width
= m_rowLabelWidth
;
9452 m_rowLabelWin
->Refresh( true, &rect
);
9458 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9462 m_table
->SetColLabelValue( col
, s
);
9463 if ( !GetBatchCount() )
9465 wxRect rect
= CellToRect( 0, col
);
9466 if ( rect
.width
> 0 )
9468 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9470 rect
.height
= m_colLabelHeight
;
9471 m_colLabelWin
->Refresh( true, &rect
);
9477 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9479 if ( m_gridLineColour
!= colour
)
9481 m_gridLineColour
= colour
;
9483 wxClientDC
dc( m_gridWin
);
9485 DrawAllGridLines( dc
, wxRegion() );
9489 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9491 if ( m_cellHighlightColour
!= colour
)
9493 m_cellHighlightColour
= colour
;
9495 wxClientDC
dc( m_gridWin
);
9497 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9498 DrawCellHighlight(dc
, attr
);
9503 void wxGrid::SetCellHighlightPenWidth(int width
)
9505 if (m_cellHighlightPenWidth
!= width
)
9507 m_cellHighlightPenWidth
= width
;
9509 // Just redrawing the cell highlight is not enough since that won't
9510 // make any visible change if the the thickness is getting smaller.
9511 int row
= m_currentCellCoords
.GetRow();
9512 int col
= m_currentCellCoords
.GetCol();
9513 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9516 wxRect rect
= CellToRect(row
, col
);
9517 m_gridWin
->Refresh(true, &rect
);
9521 void wxGrid::SetCellHighlightROPenWidth(int width
)
9523 if (m_cellHighlightROPenWidth
!= width
)
9525 m_cellHighlightROPenWidth
= width
;
9527 // Just redrawing the cell highlight is not enough since that won't
9528 // make any visible change if the the thickness is getting smaller.
9529 int row
= m_currentCellCoords
.GetRow();
9530 int col
= m_currentCellCoords
.GetCol();
9531 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9534 wxRect rect
= CellToRect(row
, col
);
9535 m_gridWin
->Refresh(true, &rect
);
9539 void wxGrid::EnableGridLines( bool enable
)
9541 if ( enable
!= m_gridLinesEnabled
)
9543 m_gridLinesEnabled
= enable
;
9545 if ( !GetBatchCount() )
9549 wxClientDC
dc( m_gridWin
);
9551 DrawAllGridLines( dc
, wxRegion() );
9555 m_gridWin
->Refresh();
9561 int wxGrid::GetDefaultRowSize()
9563 return m_defaultRowHeight
;
9566 int wxGrid::GetRowSize( int row
)
9568 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9570 return GetRowHeight(row
);
9573 int wxGrid::GetDefaultColSize()
9575 return m_defaultColWidth
;
9578 int wxGrid::GetColSize( int col
)
9580 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9582 return GetColWidth(col
);
9585 // ============================================================================
9586 // access to the grid attributes: each of them has a default value in the grid
9587 // itself and may be overidden on a per-cell basis
9588 // ============================================================================
9590 // ----------------------------------------------------------------------------
9591 // setting default attributes
9592 // ----------------------------------------------------------------------------
9594 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9596 m_defaultCellAttr
->SetBackgroundColour(col
);
9598 m_gridWin
->SetBackgroundColour(col
);
9602 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9604 m_defaultCellAttr
->SetTextColour(col
);
9607 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9609 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9612 void wxGrid::SetDefaultCellOverflow( bool allow
)
9614 m_defaultCellAttr
->SetOverflow(allow
);
9617 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9619 m_defaultCellAttr
->SetFont(font
);
9622 // For editors and renderers the type registry takes precedence over the
9623 // default attr, so we need to register the new editor/renderer for the string
9624 // data type in order to make setting a default editor/renderer appear to
9627 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9629 RegisterDataType(wxGRID_VALUE_STRING
,
9631 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9634 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9636 RegisterDataType(wxGRID_VALUE_STRING
,
9637 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9641 // ----------------------------------------------------------------------------
9642 // access to the default attrbiutes
9643 // ----------------------------------------------------------------------------
9645 wxColour
wxGrid::GetDefaultCellBackgroundColour()
9647 return m_defaultCellAttr
->GetBackgroundColour();
9650 wxColour
wxGrid::GetDefaultCellTextColour()
9652 return m_defaultCellAttr
->GetTextColour();
9655 wxFont
wxGrid::GetDefaultCellFont()
9657 return m_defaultCellAttr
->GetFont();
9660 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
9662 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9665 bool wxGrid::GetDefaultCellOverflow()
9667 return m_defaultCellAttr
->GetOverflow();
9670 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9672 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9675 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9677 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9680 // ----------------------------------------------------------------------------
9681 // access to cell attributes
9682 // ----------------------------------------------------------------------------
9684 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
9686 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9687 wxColour colour
= attr
->GetBackgroundColour();
9693 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
9695 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9696 wxColour colour
= attr
->GetTextColour();
9702 wxFont
wxGrid::GetCellFont( int row
, int col
)
9704 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9705 wxFont font
= attr
->GetFont();
9711 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
9713 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9714 attr
->GetAlignment(horiz
, vert
);
9718 bool wxGrid::GetCellOverflow( int row
, int col
)
9720 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9721 bool allow
= attr
->GetOverflow();
9727 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
9729 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9730 attr
->GetSize( num_rows
, num_cols
);
9734 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
9736 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9737 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9743 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
9745 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9746 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9752 bool wxGrid::IsReadOnly(int row
, int col
) const
9754 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9755 bool isReadOnly
= attr
->IsReadOnly();
9761 // ----------------------------------------------------------------------------
9762 // attribute support: cache, automatic provider creation, ...
9763 // ----------------------------------------------------------------------------
9765 bool wxGrid::CanHaveAttributes()
9772 return m_table
->CanHaveAttributes();
9775 void wxGrid::ClearAttrCache()
9777 if ( m_attrCache
.row
!= -1 )
9779 wxSafeDecRef(m_attrCache
.attr
);
9780 m_attrCache
.attr
= NULL
;
9781 m_attrCache
.row
= -1;
9785 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9789 wxGrid
*self
= (wxGrid
*)this; // const_cast
9791 self
->ClearAttrCache();
9792 self
->m_attrCache
.row
= row
;
9793 self
->m_attrCache
.col
= col
;
9794 self
->m_attrCache
.attr
= attr
;
9799 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9801 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9803 *attr
= m_attrCache
.attr
;
9804 wxSafeIncRef(m_attrCache
.attr
);
9806 #ifdef DEBUG_ATTR_CACHE
9807 gs_nAttrCacheHits
++;
9814 #ifdef DEBUG_ATTR_CACHE
9815 gs_nAttrCacheMisses
++;
9822 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9824 wxGridCellAttr
*attr
= NULL
;
9825 // Additional test to avoid looking at the cache e.g. for
9826 // wxNoCellCoords, as this will confuse memory management.
9829 if ( !LookupAttr(row
, col
, &attr
) )
9831 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9832 : (wxGridCellAttr
*)NULL
;
9833 CacheAttr(row
, col
, attr
);
9839 attr
->SetDefAttr(m_defaultCellAttr
);
9843 attr
= m_defaultCellAttr
;
9850 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9852 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9853 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9855 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9856 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9858 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9861 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9863 // artificially inc the ref count to match DecRef() in caller
9865 m_table
->SetAttr(attr
, row
, col
);
9871 // ----------------------------------------------------------------------------
9872 // setting column attributes (wrappers around SetColAttr)
9873 // ----------------------------------------------------------------------------
9875 void wxGrid::SetColFormatBool(int col
)
9877 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9880 void wxGrid::SetColFormatNumber(int col
)
9882 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9885 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9887 wxString typeName
= wxGRID_VALUE_FLOAT
;
9888 if ( (width
!= -1) || (precision
!= -1) )
9890 typeName
<< _T(':') << width
<< _T(',') << precision
;
9893 SetColFormatCustom(col
, typeName
);
9896 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9898 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9900 attr
= new wxGridCellAttr
;
9901 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9902 attr
->SetRenderer(renderer
);
9904 SetColAttr(col
, attr
);
9908 // ----------------------------------------------------------------------------
9909 // setting cell attributes: this is forwarded to the table
9910 // ----------------------------------------------------------------------------
9912 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9914 if ( CanHaveAttributes() )
9916 m_table
->SetAttr(attr
, row
, col
);
9925 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9927 if ( CanHaveAttributes() )
9929 m_table
->SetRowAttr(attr
, row
);
9938 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9940 if ( CanHaveAttributes() )
9942 m_table
->SetColAttr(attr
, col
);
9951 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9953 if ( CanHaveAttributes() )
9955 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9956 attr
->SetBackgroundColour(colour
);
9961 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9963 if ( CanHaveAttributes() )
9965 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9966 attr
->SetTextColour(colour
);
9971 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9973 if ( CanHaveAttributes() )
9975 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9976 attr
->SetFont(font
);
9981 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9983 if ( CanHaveAttributes() )
9985 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9986 attr
->SetAlignment(horiz
, vert
);
9991 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9993 if ( CanHaveAttributes() )
9995 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9996 attr
->SetOverflow(allow
);
10001 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
10003 if ( CanHaveAttributes() )
10005 int cell_rows
, cell_cols
;
10007 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10008 attr
->GetSize(&cell_rows
, &cell_cols
);
10009 attr
->SetSize(num_rows
, num_cols
);
10012 // Cannot set the size of a cell to 0 or negative values
10013 // While it is perfectly legal to do that, this function cannot
10014 // handle all the possibilies, do it by hand by getting the CellAttr.
10015 // You can only set the size of a cell to 1,1 or greater with this fn
10016 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
10017 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10018 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
10019 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10021 // if this was already a multicell then "turn off" the other cells first
10022 if ((cell_rows
> 1) || (cell_rows
> 1))
10025 for (j
=row
; j
< row
+ cell_rows
; j
++)
10027 for (i
=col
; i
< col
+ cell_cols
; i
++)
10029 if ((i
!= col
) || (j
!= row
))
10031 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10032 attr_stub
->SetSize( 1, 1 );
10033 attr_stub
->DecRef();
10039 // mark the cells that will be covered by this cell to
10040 // negative or zero values to point back at this cell
10041 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
10044 for (j
=row
; j
< row
+ num_rows
; j
++)
10046 for (i
=col
; i
< col
+ num_cols
; i
++)
10048 if ((i
!= col
) || (j
!= row
))
10050 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10051 attr_stub
->SetSize( row
- j
, col
- i
);
10052 attr_stub
->DecRef();
10060 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
10062 if ( CanHaveAttributes() )
10064 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10065 attr
->SetRenderer(renderer
);
10070 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
10072 if ( CanHaveAttributes() )
10074 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10075 attr
->SetEditor(editor
);
10080 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
10082 if ( CanHaveAttributes() )
10084 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10085 attr
->SetReadOnly(isReadOnly
);
10090 // ----------------------------------------------------------------------------
10091 // Data type registration
10092 // ----------------------------------------------------------------------------
10094 void wxGrid::RegisterDataType(const wxString
& typeName
,
10095 wxGridCellRenderer
* renderer
,
10096 wxGridCellEditor
* editor
)
10098 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
10102 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
10104 wxString typeName
= m_table
->GetTypeName(row
, col
);
10105 return GetDefaultEditorForType(typeName
);
10108 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
10110 wxString typeName
= m_table
->GetTypeName(row
, col
);
10111 return GetDefaultRendererForType(typeName
);
10114 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
10116 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10117 if ( index
== wxNOT_FOUND
)
10121 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
10122 wxFAIL_MSG(errStr
.c_str());
10127 return m_typeRegistry
->GetEditor(index
);
10130 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
10132 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10133 if ( index
== wxNOT_FOUND
)
10137 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
10138 wxFAIL_MSG(errStr
.c_str());
10143 return m_typeRegistry
->GetRenderer(index
);
10146 // ----------------------------------------------------------------------------
10148 // ----------------------------------------------------------------------------
10150 void wxGrid::EnableDragRowSize( bool enable
)
10152 m_canDragRowSize
= enable
;
10155 void wxGrid::EnableDragColSize( bool enable
)
10157 m_canDragColSize
= enable
;
10160 void wxGrid::EnableDragGridSize( bool enable
)
10162 m_canDragGridSize
= enable
;
10165 void wxGrid::EnableDragCell( bool enable
)
10167 m_canDragCell
= enable
;
10170 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
10172 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
10174 if ( resizeExistingRows
)
10176 // since we are resizing all rows to the default row size,
10177 // we can simply clear the row heights and row bottoms
10178 // arrays (which also allows us to take advantage of
10179 // some speed optimisations)
10180 m_rowHeights
.Empty();
10181 m_rowBottoms
.Empty();
10182 if ( !GetBatchCount() )
10187 void wxGrid::SetRowSize( int row
, int height
)
10189 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
10191 // See comment in SetColSize
10192 if ( height
< GetRowMinimalAcceptableHeight())
10195 if ( m_rowHeights
.IsEmpty() )
10197 // need to really create the array
10201 int h
= wxMax( 0, height
);
10202 int diff
= h
- m_rowHeights
[row
];
10204 m_rowHeights
[row
] = h
;
10206 for ( i
= row
; i
< m_numRows
; i
++ )
10208 m_rowBottoms
[i
] += diff
;
10211 if ( !GetBatchCount() )
10215 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
10217 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
10219 if ( resizeExistingCols
)
10221 // since we are resizing all columns to the default column size,
10222 // we can simply clear the col widths and col rights
10223 // arrays (which also allows us to take advantage of
10224 // some speed optimisations)
10225 m_colWidths
.Empty();
10226 m_colRights
.Empty();
10227 if ( !GetBatchCount() )
10232 void wxGrid::SetColSize( int col
, int width
)
10234 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
10236 // should we check that it's bigger than GetColMinimalWidth(col) here?
10238 // No, because it is reasonable to assume the library user know's
10239 // what he is doing. However we should test against the weaker
10240 // constraint of minimalAcceptableWidth, as this breaks rendering
10242 // This test then fixes sf.net bug #645734
10244 if ( width
< GetColMinimalAcceptableWidth() )
10247 if ( m_colWidths
.IsEmpty() )
10249 // need to really create the array
10253 // if < 0 then calculate new width from label
10257 wxArrayString lines
;
10258 wxClientDC
dc(m_colLabelWin
);
10259 dc
.SetFont(GetLabelFont());
10260 StringToLines(GetColLabelValue(col
), lines
);
10261 GetTextBoxSize(dc
, lines
, &w
, &h
);
10265 int w
= wxMax( 0, width
);
10266 int diff
= w
- m_colWidths
[col
];
10267 m_colWidths
[col
] = w
;
10271 for ( colPos
= GetColPos( col
); colPos
< m_numCols
; colPos
++ )
10273 i
= GetColAt( colPos
);
10274 m_colRights
[i
] += diff
;
10277 if ( !GetBatchCount() )
10281 void wxGrid::SetColMinimalWidth( int col
, int width
)
10283 if (width
> GetColMinimalAcceptableWidth())
10285 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10286 m_colMinWidths
[key
] = width
;
10290 void wxGrid::SetRowMinimalHeight( int row
, int width
)
10292 if (width
> GetRowMinimalAcceptableHeight())
10294 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10295 m_rowMinHeights
[key
] = width
;
10299 int wxGrid::GetColMinimalWidth(int col
) const
10301 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10302 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
10304 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
10307 int wxGrid::GetRowMinimalHeight(int row
) const
10309 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10310 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
10312 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
10315 void wxGrid::SetColMinimalAcceptableWidth( int width
)
10317 // We do allow a width of 0 since this gives us
10318 // an easy way to temporarily hiding columns.
10320 m_minAcceptableColWidth
= width
;
10323 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
10325 // We do allow a height of 0 since this gives us
10326 // an easy way to temporarily hiding rows.
10328 m_minAcceptableRowHeight
= height
;
10331 int wxGrid::GetColMinimalAcceptableWidth() const
10333 return m_minAcceptableColWidth
;
10336 int wxGrid::GetRowMinimalAcceptableHeight() const
10338 return m_minAcceptableRowHeight
;
10341 // ----------------------------------------------------------------------------
10343 // ----------------------------------------------------------------------------
10345 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
10347 wxClientDC
dc(m_gridWin
);
10349 // cancel editing of cell
10350 HideCellEditControl();
10351 SaveEditControlValue();
10353 // init both of them to avoid compiler warnings, even if we only need one
10361 wxCoord extent
, extentMax
= 0;
10362 int max
= column
? m_numRows
: m_numCols
;
10363 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
10370 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
10371 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
10374 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
10375 extent
= column
? size
.x
: size
.y
;
10376 if ( extent
> extentMax
)
10377 extentMax
= extent
;
10379 renderer
->DecRef();
10385 // now also compare with the column label extent
10387 dc
.SetFont( GetLabelFont() );
10391 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
10392 if ( GetColLabelTextOrientation() == wxVERTICAL
)
10396 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
10398 extent
= column
? w
: h
;
10399 if ( extent
> extentMax
)
10400 extentMax
= extent
;
10404 // empty column - give default extent (notice that if extentMax is less
10405 // than default extent but != 0, it's OK)
10406 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
10411 // leave some space around text
10419 SetColSize( col
, extentMax
);
10420 if ( !GetBatchCount() )
10423 m_gridWin
->GetClientSize( &cw
, &ch
);
10424 wxRect
rect ( CellToRect( 0, col
) );
10426 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10427 rect
.width
= cw
- rect
.x
;
10428 rect
.height
= m_colLabelHeight
;
10429 m_colLabelWin
->Refresh( true, &rect
);
10434 SetRowSize(row
, extentMax
);
10435 if ( !GetBatchCount() )
10438 m_gridWin
->GetClientSize( &cw
, &ch
);
10439 wxRect
rect( CellToRect( row
, 0 ) );
10441 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10442 rect
.width
= m_rowLabelWidth
;
10443 rect
.height
= ch
- rect
.y
;
10444 m_rowLabelWin
->Refresh( true, &rect
);
10451 SetColMinimalWidth(col
, extentMax
);
10453 SetRowMinimalHeight(row
, extentMax
);
10457 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10459 int width
= m_rowLabelWidth
;
10464 for ( int col
= 0; col
< m_numCols
; col
++ )
10467 AutoSizeColumn(col
, setAsMin
);
10469 width
+= GetColWidth(col
);
10478 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10480 int height
= m_colLabelHeight
;
10485 for ( int row
= 0; row
< m_numRows
; row
++ )
10488 AutoSizeRow(row
, setAsMin
);
10490 height
+= GetRowHeight(row
);
10499 void wxGrid::AutoSize()
10503 wxSize
size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
10505 // round up the size to a multiple of scroll step - this ensures that we
10506 // won't get the scrollbars if we're sized exactly to this width
10507 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
10510 GetScrollX(size
.x
+ m_extraWidth
+ 1) * m_scrollLineX
,
10511 GetScrollY(size
.y
+ m_extraHeight
+ 1) * m_scrollLineY
);
10513 // distribute the extra space between the columns/rows to avoid having
10514 // extra white space
10516 // Remove the extra m_extraWidth + 1 added above
10517 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
10518 if ( diff
&& m_numCols
)
10520 // try to resize the columns uniformly
10521 wxCoord diffPerCol
= diff
/ m_numCols
;
10524 for ( int col
= 0; col
< m_numCols
; col
++ )
10526 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
10530 // add remaining amount to the last columns
10531 diff
-= diffPerCol
* m_numCols
;
10534 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
10536 SetColSize(col
, GetColWidth(col
) + 1);
10542 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
10543 if ( diff
&& m_numRows
)
10545 // try to resize the columns uniformly
10546 wxCoord diffPerRow
= diff
/ m_numRows
;
10549 for ( int row
= 0; row
< m_numRows
; row
++ )
10551 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
10555 // add remaining amount to the last rows
10556 diff
-= diffPerRow
* m_numRows
;
10559 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
10561 SetRowSize(row
, GetRowHeight(row
) + 1);
10568 SetClientSize(sizeFit
);
10571 void wxGrid::AutoSizeRowLabelSize( int row
)
10573 wxArrayString lines
;
10576 // Hide the edit control, so it
10577 // won't interfere with drag-shrinking.
10578 if ( IsCellEditControlShown() )
10580 HideCellEditControl();
10581 SaveEditControlValue();
10584 // autosize row height depending on label text
10585 StringToLines( GetRowLabelValue( row
), lines
);
10586 wxClientDC
dc( m_rowLabelWin
);
10587 GetTextBoxSize( dc
, lines
, &w
, &h
);
10588 if ( h
< m_defaultRowHeight
)
10589 h
= m_defaultRowHeight
;
10590 SetRowSize(row
, h
);
10594 void wxGrid::AutoSizeColLabelSize( int col
)
10596 wxArrayString lines
;
10599 // Hide the edit control, so it
10600 // won't interfere with drag-shrinking.
10601 if ( IsCellEditControlShown() )
10603 HideCellEditControl();
10604 SaveEditControlValue();
10607 // autosize column width depending on label text
10608 StringToLines( GetColLabelValue( col
), lines
);
10609 wxClientDC
dc( m_colLabelWin
);
10610 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10611 GetTextBoxSize( dc
, lines
, &w
, &h
);
10613 GetTextBoxSize( dc
, lines
, &h
, &w
);
10614 if ( w
< m_defaultColWidth
)
10615 w
= m_defaultColWidth
;
10616 SetColSize(col
, w
);
10620 wxSize
wxGrid::DoGetBestSize() const
10622 // don't set sizes, only calculate them
10623 wxGrid
*self
= (wxGrid
*)this; // const_cast
10626 width
= self
->SetOrCalcColumnSizes(true);
10627 height
= self
->SetOrCalcRowSizes(true);
10634 // Round up to a multiple the scroll rate
10635 // NOTE: this still doesn't get rid of the scrollbars;
10636 // is there any magic incantation for that?
10638 GetScrollPixelsPerUnit(&xpu
, &ypu
);
10640 width
+= 1 + xpu
- (width
% xpu
);
10642 height
+= 1 + ypu
- (height
% ypu
);
10644 // limit to 1/4 of the screen size
10645 int maxwidth
, maxheight
;
10646 wxDisplaySize( &maxwidth
, &maxheight
);
10649 if ( width
> maxwidth
)
10651 if ( height
> maxheight
)
10652 height
= maxheight
;
10654 wxSize
best(width
, height
);
10656 // NOTE: This size should be cached, but first we need to add calls to
10657 // InvalidateBestSize everywhere that could change the results of this
10659 // CacheBestSize(size);
10669 wxPen
& wxGrid::GetDividerPen() const
10674 // ----------------------------------------------------------------------------
10675 // cell value accessor functions
10676 // ----------------------------------------------------------------------------
10678 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10682 m_table
->SetValue( row
, col
, s
);
10683 if ( !GetBatchCount() )
10686 wxRect
rect( CellToRect( row
, col
) );
10688 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10689 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10690 m_gridWin
->Refresh( false, &rect
);
10693 if ( m_currentCellCoords
.GetRow() == row
&&
10694 m_currentCellCoords
.GetCol() == col
&&
10695 IsCellEditControlShown())
10696 // Note: If we are using IsCellEditControlEnabled,
10697 // this interacts badly with calling SetCellValue from
10698 // an EVT_GRID_CELL_CHANGE handler.
10700 HideCellEditControl();
10701 ShowCellEditControl(); // will reread data from table
10706 // ----------------------------------------------------------------------------
10707 // block, row and column selection
10708 // ----------------------------------------------------------------------------
10710 void wxGrid::SelectRow( int row
, bool addToSelected
)
10712 if ( IsSelection() && !addToSelected
)
10716 m_selection
->SelectRow( row
, false, addToSelected
);
10719 void wxGrid::SelectCol( int col
, bool addToSelected
)
10721 if ( IsSelection() && !addToSelected
)
10725 m_selection
->SelectCol( col
, false, addToSelected
);
10728 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10729 bool addToSelected
)
10731 if ( IsSelection() && !addToSelected
)
10735 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10736 false, addToSelected
);
10739 void wxGrid::SelectAll()
10741 if ( m_numRows
> 0 && m_numCols
> 0 )
10744 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10748 // ----------------------------------------------------------------------------
10749 // cell, row and col deselection
10750 // ----------------------------------------------------------------------------
10752 void wxGrid::DeselectRow( int row
)
10754 if ( !m_selection
)
10757 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10759 if ( m_selection
->IsInSelection(row
, 0 ) )
10760 m_selection
->ToggleCellSelection(row
, 0);
10764 int nCols
= GetNumberCols();
10765 for ( int i
= 0; i
< nCols
; i
++ )
10767 if ( m_selection
->IsInSelection(row
, i
) )
10768 m_selection
->ToggleCellSelection(row
, i
);
10773 void wxGrid::DeselectCol( int col
)
10775 if ( !m_selection
)
10778 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10780 if ( m_selection
->IsInSelection(0, col
) )
10781 m_selection
->ToggleCellSelection(0, col
);
10785 int nRows
= GetNumberRows();
10786 for ( int i
= 0; i
< nRows
; i
++ )
10788 if ( m_selection
->IsInSelection(i
, col
) )
10789 m_selection
->ToggleCellSelection(i
, col
);
10794 void wxGrid::DeselectCell( int row
, int col
)
10796 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10797 m_selection
->ToggleCellSelection(row
, col
);
10800 bool wxGrid::IsSelection()
10802 return ( m_selection
&& (m_selection
->IsSelection() ||
10803 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10804 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10807 bool wxGrid::IsInSelection( int row
, int col
) const
10809 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10810 ( row
>= m_selectingTopLeft
.GetRow() &&
10811 col
>= m_selectingTopLeft
.GetCol() &&
10812 row
<= m_selectingBottomRight
.GetRow() &&
10813 col
<= m_selectingBottomRight
.GetCol() )) );
10816 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10820 wxGridCellCoordsArray a
;
10824 return m_selection
->m_cellSelection
;
10827 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10831 wxGridCellCoordsArray a
;
10835 return m_selection
->m_blockSelectionTopLeft
;
10838 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10842 wxGridCellCoordsArray a
;
10846 return m_selection
->m_blockSelectionBottomRight
;
10849 wxArrayInt
wxGrid::GetSelectedRows() const
10857 return m_selection
->m_rowSelection
;
10860 wxArrayInt
wxGrid::GetSelectedCols() const
10868 return m_selection
->m_colSelection
;
10871 void wxGrid::ClearSelection()
10873 m_selectingTopLeft
=
10874 m_selectingBottomRight
=
10875 m_selectingKeyboard
= wxGridNoCellCoords
;
10877 m_selection
->ClearSelection();
10880 // This function returns the rectangle that encloses the given block
10881 // in device coords clipped to the client size of the grid window.
10883 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10884 const wxGridCellCoords
&bottomRight
)
10886 wxRect
rect( wxGridNoCellRect
);
10889 cellRect
= CellToRect( topLeft
);
10890 if ( cellRect
!= wxGridNoCellRect
)
10896 rect
= wxRect(0, 0, 0, 0);
10899 cellRect
= CellToRect( bottomRight
);
10900 if ( cellRect
!= wxGridNoCellRect
)
10906 return wxGridNoCellRect
;
10910 int left
= rect
.GetLeft();
10911 int top
= rect
.GetTop();
10912 int right
= rect
.GetRight();
10913 int bottom
= rect
.GetBottom();
10915 int leftCol
= topLeft
.GetCol();
10916 int topRow
= topLeft
.GetRow();
10917 int rightCol
= bottomRight
.GetCol();
10918 int bottomRow
= bottomRight
.GetRow();
10926 leftCol
= rightCol
;
10936 topRow
= bottomRow
;
10940 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10942 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10944 if ((j
== topRow
) || (j
== bottomRow
) || (i
== leftCol
) || (i
== rightCol
))
10946 cellRect
= CellToRect( j
, i
);
10948 if (cellRect
.x
< left
)
10950 if (cellRect
.y
< top
)
10952 if (cellRect
.x
+ cellRect
.width
> right
)
10953 right
= cellRect
.x
+ cellRect
.width
;
10954 if (cellRect
.y
+ cellRect
.height
> bottom
)
10955 bottom
= cellRect
.y
+ cellRect
.height
;
10959 i
= rightCol
; // jump over inner cells.
10964 // convert to scrolled coords
10966 CalcScrolledPosition( left
, top
, &left
, &top
);
10967 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10970 m_gridWin
->GetClientSize( &cw
, &ch
);
10972 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10973 return wxRect(0,0,0,0);
10975 rect
.SetLeft( wxMax(0, left
) );
10976 rect
.SetTop( wxMax(0, top
) );
10977 rect
.SetRight( wxMin(cw
, right
) );
10978 rect
.SetBottom( wxMin(ch
, bottom
) );
10983 // ----------------------------------------------------------------------------
10984 // grid event classes
10985 // ----------------------------------------------------------------------------
10987 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10989 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10990 int row
, int col
, int x
, int y
, bool sel
,
10991 bool control
, bool shift
, bool alt
, bool meta
)
10992 : wxNotifyEvent( type
, id
)
10999 m_control
= control
;
11004 SetEventObject(obj
);
11008 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
11010 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
11011 int rowOrCol
, int x
, int y
,
11012 bool control
, bool shift
, bool alt
, bool meta
)
11013 : wxNotifyEvent( type
, id
)
11015 m_rowOrCol
= rowOrCol
;
11018 m_control
= control
;
11023 SetEventObject(obj
);
11027 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
11029 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
11030 const wxGridCellCoords
& topLeft
,
11031 const wxGridCellCoords
& bottomRight
,
11032 bool sel
, bool control
,
11033 bool shift
, bool alt
, bool meta
)
11034 : wxNotifyEvent( type
, id
)
11036 m_topLeft
= topLeft
;
11037 m_bottomRight
= bottomRight
;
11039 m_control
= control
;
11044 SetEventObject(obj
);
11048 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
11050 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
11051 wxObject
* obj
, int row
,
11052 int col
, wxControl
* ctrl
)
11053 : wxCommandEvent(type
, id
)
11055 SetEventObject(obj
);
11061 #endif // wxUSE_GRID