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)
3890 rect
.SetWidth( client_width
- 2 );
3891 rect
.SetHeight( client_height
- 2 );
3893 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3895 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
3896 dc
.DrawLine( client_width
- 1, client_height
- 1, client_width
- 1, 0 );
3897 dc
.DrawLine( client_width
- 1, client_height
- 1, 0, client_height
- 1 );
3898 dc
.DrawLine( 0, 0, client_width
, 0 );
3899 dc
.DrawLine( 0, 0, 0, client_height
);
3901 dc
.SetPen( *wxWHITE_PEN
);
3902 dc
.DrawLine( 1, 1, client_width
- 1, 1 );
3903 dc
.DrawLine( 1, 1, 1, client_height
- 1 );
3907 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3909 m_owner
->ProcessCornerLabelMouseEvent( event
);
3912 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3914 m_owner
->GetEventHandler()->ProcessEvent(event
);
3917 // This seems to be required for wxMotif otherwise the mouse
3918 // cursor must be in the cell edit control to get key events
3920 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3922 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3926 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3928 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3932 void wxGridCornerLabelWindow::OnChar( wxKeyEvent
& event
)
3934 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3938 //////////////////////////////////////////////////////////////////////
3940 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3942 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3943 EVT_PAINT( wxGridWindow::OnPaint
)
3944 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3945 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3946 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3947 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3948 EVT_CHAR( wxGridWindow::OnChar
)
3949 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3950 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3951 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3954 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3955 wxGridRowLabelWindow
*rowLblWin
,
3956 wxGridColLabelWindow
*colLblWin
,
3959 const wxSize
&size
)
3961 parent
, id
, pos
, size
,
3962 wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
| wxFULL_REPAINT_ON_RESIZE
,
3963 wxT("grid window") )
3966 m_rowLabelWin
= rowLblWin
;
3967 m_colLabelWin
= colLblWin
;
3970 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3972 wxPaintDC
dc( this );
3973 m_owner
->PrepareDC( dc
);
3974 wxRegion reg
= GetUpdateRegion();
3975 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
3976 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
3978 #if WXGRID_DRAW_LINES
3979 m_owner
->DrawAllGridLines( dc
, reg
);
3982 m_owner
->DrawGridSpace( dc
);
3983 m_owner
->DrawHighlight( dc
, dirtyCells
);
3986 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3988 wxWindow::ScrollWindow( dx
, dy
, rect
);
3989 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3990 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3993 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3995 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
3998 m_owner
->ProcessGridCellMouseEvent( event
);
4001 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
4003 m_owner
->GetEventHandler()->ProcessEvent( event
);
4006 // This seems to be required for wxMotif/wxGTK otherwise the mouse
4007 // cursor must be in the cell edit control to get key events
4009 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
4011 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4015 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
4017 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4021 void wxGridWindow::OnChar( wxKeyEvent
& event
)
4023 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4027 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
4031 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
4033 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4037 //////////////////////////////////////////////////////////////////////
4039 // Internal Helper function for computing row or column from some
4040 // (unscrolled) coordinate value, using either
4041 // m_defaultRowHeight/m_defaultColWidth or binary search on array
4042 // of m_rowBottoms/m_ColRights to speed up the search!
4044 // Internal helper macros for simpler use of that function
4046 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
4047 const wxArrayInt
& BorderArray
, int nMax
,
4050 #define internalXToCol(x) XToCol(x, true)
4051 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
4052 m_minAcceptableRowHeight, \
4053 m_rowBottoms, m_numRows, true)
4055 /////////////////////////////////////////////////////////////////////
4057 #if wxUSE_EXTENDED_RTTI
4058 WX_DEFINE_FLAGS( wxGridStyle
)
4060 wxBEGIN_FLAGS( wxGridStyle
)
4061 // new style border flags, we put them first to
4062 // use them for streaming out
4063 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4064 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4065 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4066 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4067 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4068 wxFLAGS_MEMBER(wxBORDER_NONE
)
4070 // old style border flags
4071 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4072 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4073 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4074 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4075 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4076 wxFLAGS_MEMBER(wxBORDER
)
4078 // standard window styles
4079 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4080 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4081 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4082 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4083 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4084 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4085 wxFLAGS_MEMBER(wxVSCROLL
)
4086 wxFLAGS_MEMBER(wxHSCROLL
)
4088 wxEND_FLAGS( wxGridStyle
)
4090 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4092 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4093 wxHIDE_PROPERTY( Children
)
4094 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4095 wxEND_PROPERTIES_TABLE()
4097 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4098 wxEND_HANDLERS_TABLE()
4100 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4103 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
4106 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4109 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4110 EVT_PAINT( wxGrid::OnPaint
)
4111 EVT_SIZE( wxGrid::OnSize
)
4112 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
4113 EVT_KEY_UP( wxGrid::OnKeyUp
)
4114 EVT_CHAR ( wxGrid::OnChar
)
4115 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
4120 // in order to make sure that a size event is not
4121 // trigerred in a unfinished state
4122 m_cornerLabelWin
= NULL
;
4123 m_rowLabelWin
= NULL
;
4124 m_colLabelWin
= NULL
;
4128 wxGrid::wxGrid( wxWindow
*parent
,
4133 const wxString
& name
)
4134 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
4135 m_colMinWidths(GRID_HASH_SIZE
),
4136 m_rowMinHeights(GRID_HASH_SIZE
)
4139 SetBestFittingSize(size
);
4142 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
4143 const wxPoint
& pos
, const wxSize
& size
,
4144 long style
, const wxString
& name
)
4146 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
4147 style
| wxWANTS_CHARS
, name
))
4150 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4151 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4154 SetBestFittingSize(size
);
4161 // Must do this or ~wxScrollHelper will pop the wrong event handler
4162 SetTargetWindow(this);
4164 wxSafeDecRef(m_defaultCellAttr
);
4166 #ifdef DEBUG_ATTR_CACHE
4167 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4168 wxPrintf(_T("wxGrid attribute cache statistics: "
4169 "total: %u, hits: %u (%u%%)\n"),
4170 total
, gs_nAttrCacheHits
,
4171 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4177 delete m_typeRegistry
;
4182 // ----- internal init and update functions
4185 // NOTE: If using the default visual attributes works everywhere then this can
4186 // be removed as well as the #else cases below.
4187 #define _USE_VISATTR 0
4189 void wxGrid::Create()
4191 // set to true by CreateGrid
4194 // create the type registry
4195 m_typeRegistry
= new wxGridTypeRegistry
;
4198 m_table
= (wxGridTableBase
*) NULL
;
4201 m_cellEditCtrlEnabled
= false;
4203 m_defaultCellAttr
= new wxGridCellAttr();
4205 // Set default cell attributes
4206 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4207 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4208 m_defaultCellAttr
->SetFont(GetFont());
4209 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4210 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4211 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4214 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4215 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4217 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4218 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4221 m_defaultCellAttr
->SetTextColour(
4222 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4223 m_defaultCellAttr
->SetBackgroundColour(
4224 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4229 m_currentCellCoords
= wxGridNoCellCoords
;
4231 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4232 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4234 // subwindow components that make up the wxGrid
4235 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4240 m_colLabelWin
= new wxGridColLabelWindow( this,
4245 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4250 m_gridWin
= new wxGridWindow( this,
4257 SetTargetWindow( m_gridWin
);
4260 wxColour gfg
= gva
.colFg
;
4261 wxColour gbg
= gva
.colBg
;
4262 wxColour lfg
= lva
.colFg
;
4263 wxColour lbg
= lva
.colBg
;
4265 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4266 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4267 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4268 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4271 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4272 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4273 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4274 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4275 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4276 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4278 m_gridWin
->SetOwnForegroundColour(gfg
);
4279 m_gridWin
->SetOwnBackgroundColour(gbg
);
4284 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4285 wxGrid::wxGridSelectionModes selmode
)
4287 wxCHECK_MSG( !m_created
,
4289 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4291 m_numRows
= numRows
;
4292 m_numCols
= numCols
;
4294 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4295 m_table
->SetView( this );
4297 m_selection
= new wxGridSelection( this, selmode
);
4306 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4308 wxCHECK_RET( m_created
,
4309 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4311 m_selection
->SetSelectionMode( selmode
);
4314 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4316 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4317 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4319 return m_selection
->GetSelectionMode();
4322 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4323 wxGrid::wxGridSelectionModes selmode
)
4327 // stop all processing
4332 wxGridTableBase
*t
= m_table
;
4347 m_numRows
= table
->GetNumberRows();
4348 m_numCols
= table
->GetNumberCols();
4351 m_table
->SetView( this );
4352 m_ownTable
= takeOwnership
;
4353 m_selection
= new wxGridSelection( this, selmode
);
4365 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4366 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4368 if ( m_rowLabelWin
)
4370 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4374 m_labelBackgroundColour
= *wxWHITE
;
4377 m_labelTextColour
= *wxBLACK
;
4380 m_attrCache
.row
= -1;
4381 m_attrCache
.col
= -1;
4382 m_attrCache
.attr
= NULL
;
4384 // TODO: something better than this ?
4386 m_labelFont
= this->GetFont();
4387 m_labelFont
.SetWeight( wxBOLD
);
4389 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4390 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4392 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4393 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4394 m_colLabelTextOrientation
= wxHORIZONTAL
;
4396 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4397 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4399 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4400 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4402 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4403 m_defaultRowHeight
+= 8;
4405 m_defaultRowHeight
+= 4;
4408 m_gridLineColour
= wxColour( 192,192,192 );
4409 m_gridLinesEnabled
= true;
4410 m_cellHighlightColour
= *wxBLACK
;
4411 m_cellHighlightPenWidth
= 2;
4412 m_cellHighlightROPenWidth
= 1;
4414 m_canDragColMove
= false;
4416 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4417 m_winCapture
= (wxWindow
*)NULL
;
4418 m_canDragRowSize
= true;
4419 m_canDragColSize
= true;
4420 m_canDragGridSize
= true;
4421 m_canDragCell
= false;
4423 m_dragRowOrCol
= -1;
4424 m_isDragging
= false;
4425 m_startDragPos
= wxDefaultPosition
;
4427 m_waitForSlowClick
= false;
4429 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4430 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4432 m_currentCellCoords
= wxGridNoCellCoords
;
4436 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4437 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4439 m_editable
= true; // default for whole grid
4441 m_inOnKeyDown
= false;
4447 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4448 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4451 // ----------------------------------------------------------------------------
4452 // the idea is to call these functions only when necessary because they create
4453 // quite big arrays which eat memory mostly unnecessary - in particular, if
4454 // default widths/heights are used for all rows/columns, we may not use these
4457 // with some extra code, it should be possible to only store the
4458 // widths/heights different from default ones but this will be done later...
4459 // ----------------------------------------------------------------------------
4461 void wxGrid::InitRowHeights()
4463 m_rowHeights
.Empty();
4464 m_rowBottoms
.Empty();
4466 m_rowHeights
.Alloc( m_numRows
);
4467 m_rowBottoms
.Alloc( m_numRows
);
4471 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4473 for ( int i
= 0; i
< m_numRows
; i
++ )
4475 rowBottom
+= m_defaultRowHeight
;
4476 m_rowBottoms
.Add( rowBottom
);
4480 void wxGrid::InitColWidths()
4482 m_colWidths
.Empty();
4483 m_colRights
.Empty();
4485 m_colWidths
.Alloc( m_numCols
);
4486 m_colRights
.Alloc( m_numCols
);
4489 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4491 for ( int i
= 0; i
< m_numCols
; i
++ )
4493 colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
4494 m_colRights
.Add( colRight
);
4498 int wxGrid::GetColWidth(int col
) const
4500 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4503 int wxGrid::GetColLeft(int col
) const
4505 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
4506 : m_colRights
[col
] - m_colWidths
[col
];
4509 int wxGrid::GetColRight(int col
) const
4511 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
4515 int wxGrid::GetRowHeight(int row
) const
4517 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4520 int wxGrid::GetRowTop(int row
) const
4522 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4523 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4526 int wxGrid::GetRowBottom(int row
) const
4528 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4529 : m_rowBottoms
[row
];
4532 void wxGrid::CalcDimensions()
4535 GetClientSize( &cw
, &ch
);
4537 if ( m_rowLabelWin
->IsShown() )
4538 cw
-= m_rowLabelWidth
;
4539 if ( m_colLabelWin
->IsShown() )
4540 ch
-= m_colLabelHeight
;
4543 int w
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) + m_extraWidth
+ 1 : 0;
4544 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4546 // take into account editor if shown
4547 if ( IsCellEditControlShown() )
4550 int r
= m_currentCellCoords
.GetRow();
4551 int c
= m_currentCellCoords
.GetCol();
4552 int x
= GetColLeft(c
);
4553 int y
= GetRowTop(r
);
4555 // how big is the editor
4556 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4557 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4558 editor
->GetControl()->GetSize(&w2
, &h2
);
4569 // preserve (more or less) the previous position
4571 GetViewStart( &x
, &y
);
4573 // ensure the position is valid for the new scroll ranges
4575 x
= wxMax( w
- 1, 0 );
4577 y
= wxMax( h
- 1, 0 );
4579 // do set scrollbar parameters
4580 SetScrollbars( m_scrollLineX
, m_scrollLineY
,
4581 GetScrollX(w
), GetScrollY(h
), x
, y
,
4582 GetBatchCount() != 0);
4584 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4585 // still must reposition the children
4589 void wxGrid::CalcWindowSizes()
4591 // escape if the window is has not been fully created yet
4593 if ( m_cornerLabelWin
== NULL
)
4597 GetClientSize( &cw
, &ch
);
4599 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4600 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4602 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4603 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
- m_rowLabelWidth
, m_colLabelHeight
);
4605 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4606 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
- m_colLabelHeight
);
4608 if ( m_gridWin
&& m_gridWin
->IsShown() )
4609 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
- m_rowLabelWidth
, ch
- m_colLabelHeight
);
4612 // this is called when the grid table sends a message
4613 // to indicate that it has been redimensioned
4615 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4618 bool result
= false;
4620 // Clear the attribute cache as the attribute might refer to a different
4621 // cell than stored in the cache after adding/removing rows/columns.
4624 // By the same reasoning, the editor should be dismissed if columns are
4625 // added or removed. And for consistency, it should IMHO always be
4626 // removed, not only if the cell "underneath" it actually changes.
4627 // For now, I intentionally do not save the editor's content as the
4628 // cell it might want to save that stuff to might no longer exist.
4629 HideCellEditControl();
4632 // if we were using the default widths/heights so far, we must change them
4634 if ( m_colWidths
.IsEmpty() )
4639 if ( m_rowHeights
.IsEmpty() )
4645 switch ( msg
.GetId() )
4647 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4649 size_t pos
= msg
.GetCommandInt();
4650 int numRows
= msg
.GetCommandInt2();
4652 m_numRows
+= numRows
;
4654 if ( !m_rowHeights
.IsEmpty() )
4656 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4657 m_rowBottoms
.Insert( 0, pos
, numRows
);
4661 bottom
= m_rowBottoms
[pos
- 1];
4663 for ( i
= pos
; i
< m_numRows
; i
++ )
4665 bottom
+= m_rowHeights
[i
];
4666 m_rowBottoms
[i
] = bottom
;
4670 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4672 // if we have just inserted cols into an empty grid the current
4673 // cell will be undefined...
4675 SetCurrentCell( 0, 0 );
4679 m_selection
->UpdateRows( pos
, numRows
);
4680 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4682 attrProvider
->UpdateAttrRows( pos
, numRows
);
4684 if ( !GetBatchCount() )
4687 m_rowLabelWin
->Refresh();
4693 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4695 int numRows
= msg
.GetCommandInt();
4696 int oldNumRows
= m_numRows
;
4697 m_numRows
+= numRows
;
4699 if ( !m_rowHeights
.IsEmpty() )
4701 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4702 m_rowBottoms
.Add( 0, numRows
);
4705 if ( oldNumRows
> 0 )
4706 bottom
= m_rowBottoms
[oldNumRows
- 1];
4708 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4710 bottom
+= m_rowHeights
[i
];
4711 m_rowBottoms
[i
] = bottom
;
4715 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4717 // if we have just inserted cols into an empty grid the current
4718 // cell will be undefined...
4720 SetCurrentCell( 0, 0 );
4723 if ( !GetBatchCount() )
4726 m_rowLabelWin
->Refresh();
4732 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4734 size_t pos
= msg
.GetCommandInt();
4735 int numRows
= msg
.GetCommandInt2();
4736 m_numRows
-= numRows
;
4738 if ( !m_rowHeights
.IsEmpty() )
4740 m_rowHeights
.RemoveAt( pos
, numRows
);
4741 m_rowBottoms
.RemoveAt( pos
, numRows
);
4744 for ( i
= 0; i
< m_numRows
; i
++ )
4746 h
+= m_rowHeights
[i
];
4747 m_rowBottoms
[i
] = h
;
4753 m_currentCellCoords
= wxGridNoCellCoords
;
4757 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4758 m_currentCellCoords
.Set( 0, 0 );
4762 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4763 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4766 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4768 // ifdef'd out following patch from Paul Gammans
4770 // No need to touch column attributes, unless we
4771 // removed _all_ rows, in this case, we remove
4772 // all column attributes.
4773 // I hate to do this here, but the
4774 // needed data is not available inside UpdateAttrRows.
4775 if ( !GetNumberRows() )
4776 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4780 if ( !GetBatchCount() )
4783 m_rowLabelWin
->Refresh();
4789 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4791 size_t pos
= msg
.GetCommandInt();
4792 int numCols
= msg
.GetCommandInt2();
4793 m_numCols
+= numCols
;
4795 if ( !m_colAt
.IsEmpty() )
4797 //Shift the column IDs
4799 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
4801 if ( m_colAt
[i
] >= (int)pos
)
4802 m_colAt
[i
] += numCols
;
4805 m_colAt
.Insert( pos
, pos
, numCols
);
4807 //Set the new columns' positions
4808 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
4814 if ( !m_colWidths
.IsEmpty() )
4816 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4817 m_colRights
.Insert( 0, pos
, numCols
);
4821 right
= m_colRights
[GetColAt( pos
- 1 )];
4824 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
4826 i
= GetColAt( colPos
);
4828 right
+= m_colWidths
[i
];
4829 m_colRights
[i
] = right
;
4833 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4835 // if we have just inserted cols into an empty grid the current
4836 // cell will be undefined...
4838 SetCurrentCell( 0, 0 );
4842 m_selection
->UpdateCols( pos
, numCols
);
4843 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4845 attrProvider
->UpdateAttrCols( pos
, numCols
);
4846 if ( !GetBatchCount() )
4849 m_colLabelWin
->Refresh();
4855 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4857 int numCols
= msg
.GetCommandInt();
4858 int oldNumCols
= m_numCols
;
4859 m_numCols
+= numCols
;
4861 if ( !m_colAt
.IsEmpty() )
4863 m_colAt
.Add( 0, numCols
);
4865 //Set the new columns' positions
4867 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4873 if ( !m_colWidths
.IsEmpty() )
4875 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4876 m_colRights
.Add( 0, numCols
);
4879 if ( oldNumCols
> 0 )
4880 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
4883 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
4885 i
= GetColAt( colPos
);
4887 right
+= m_colWidths
[i
];
4888 m_colRights
[i
] = right
;
4892 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4894 // if we have just inserted cols into an empty grid the current
4895 // cell will be undefined...
4897 SetCurrentCell( 0, 0 );
4899 if ( !GetBatchCount() )
4902 m_colLabelWin
->Refresh();
4908 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4910 size_t pos
= msg
.GetCommandInt();
4911 int numCols
= msg
.GetCommandInt2();
4912 m_numCols
-= numCols
;
4914 if ( !m_colAt
.IsEmpty() )
4916 int colID
= GetColAt( pos
);
4918 m_colAt
.RemoveAt( pos
, numCols
);
4920 //Shift the column IDs
4922 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
4924 if ( m_colAt
[colPos
] > colID
)
4925 m_colAt
[colPos
] -= numCols
;
4929 if ( !m_colWidths
.IsEmpty() )
4931 m_colWidths
.RemoveAt( pos
, numCols
);
4932 m_colRights
.RemoveAt( pos
, numCols
);
4936 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
4938 i
= GetColAt( colPos
);
4940 w
+= m_colWidths
[i
];
4947 m_currentCellCoords
= wxGridNoCellCoords
;
4951 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4952 m_currentCellCoords
.Set( 0, 0 );
4956 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4957 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4960 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4962 // ifdef'd out following patch from Paul Gammans
4964 // No need to touch row attributes, unless we
4965 // removed _all_ columns, in this case, we remove
4966 // all row attributes.
4967 // I hate to do this here, but the
4968 // needed data is not available inside UpdateAttrCols.
4969 if ( !GetNumberCols() )
4970 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4974 if ( !GetBatchCount() )
4977 m_colLabelWin
->Refresh();
4984 if (result
&& !GetBatchCount() )
4985 m_gridWin
->Refresh();
4990 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4992 wxRegionIterator
iter( reg
);
4995 wxArrayInt rowlabels
;
5002 // TODO: remove this when we can...
5003 // There is a bug in wxMotif that gives garbage update
5004 // rectangles if you jump-scroll a long way by clicking the
5005 // scrollbar with middle button. This is a work-around
5007 #if defined(__WXMOTIF__)
5009 m_gridWin
->GetClientSize( &cw
, &ch
);
5010 if ( r
.GetTop() > ch
)
5012 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5015 // logical bounds of update region
5018 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
5019 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
5021 // find the row labels within these bounds
5024 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5026 if ( GetRowBottom(row
) < top
)
5029 if ( GetRowTop(row
) > bottom
)
5032 rowlabels
.Add( row
);
5041 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
5043 wxRegionIterator
iter( reg
);
5046 wxArrayInt colLabels
;
5053 // TODO: remove this when we can...
5054 // There is a bug in wxMotif that gives garbage update
5055 // rectangles if you jump-scroll a long way by clicking the
5056 // scrollbar with middle button. This is a work-around
5058 #if defined(__WXMOTIF__)
5060 m_gridWin
->GetClientSize( &cw
, &ch
);
5061 if ( r
.GetLeft() > cw
)
5063 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5066 // logical bounds of update region
5069 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
5070 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
5072 // find the cells within these bounds
5076 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5078 col
= GetColAt( colPos
);
5080 if ( GetColRight(col
) < left
)
5083 if ( GetColLeft(col
) > right
)
5086 colLabels
.Add( col
);
5095 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
5097 wxRegionIterator
iter( reg
);
5100 wxGridCellCoordsArray cellsExposed
;
5102 int left
, top
, right
, bottom
;
5107 // TODO: remove this when we can...
5108 // There is a bug in wxMotif that gives garbage update
5109 // rectangles if you jump-scroll a long way by clicking the
5110 // scrollbar with middle button. This is a work-around
5112 #if defined(__WXMOTIF__)
5114 m_gridWin
->GetClientSize( &cw
, &ch
);
5115 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5116 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5117 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5118 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5121 // logical bounds of update region
5123 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5124 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5126 // find the cells within these bounds
5129 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5131 if ( GetRowBottom(row
) <= top
)
5134 if ( GetRowTop(row
) > bottom
)
5138 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5140 col
= GetColAt( colPos
);
5142 if ( GetColRight(col
) <= left
)
5145 if ( GetColLeft(col
) > right
)
5148 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
5155 return cellsExposed
;
5159 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5162 wxPoint
pos( event
.GetPosition() );
5163 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5165 if ( event
.Dragging() )
5169 m_isDragging
= true;
5170 m_rowLabelWin
->CaptureMouse();
5173 if ( event
.LeftIsDown() )
5175 switch ( m_cursorMode
)
5177 case WXGRID_CURSOR_RESIZE_ROW
:
5179 int cw
, ch
, left
, dummy
;
5180 m_gridWin
->GetClientSize( &cw
, &ch
);
5181 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5183 wxClientDC
dc( m_gridWin
);
5186 GetRowTop(m_dragRowOrCol
) +
5187 GetRowMinimalHeight(m_dragRowOrCol
) );
5188 dc
.SetLogicalFunction(wxINVERT
);
5189 if ( m_dragLastPos
>= 0 )
5191 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5193 dc
.DrawLine( left
, y
, left
+cw
, y
);
5198 case WXGRID_CURSOR_SELECT_ROW
:
5200 if ( (row
= YToRow( y
)) >= 0 )
5204 m_selection
->SelectRow( row
,
5205 event
.ControlDown(),
5214 // default label to suppress warnings about "enumeration value
5215 // 'xxx' not handled in switch
5223 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5228 if (m_rowLabelWin
->HasCapture())
5229 m_rowLabelWin
->ReleaseMouse();
5230 m_isDragging
= false;
5233 // ------------ Entering or leaving the window
5235 if ( event
.Entering() || event
.Leaving() )
5237 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5240 // ------------ Left button pressed
5242 else if ( event
.LeftDown() )
5244 // don't send a label click event for a hit on the
5245 // edge of the row label - this is probably the user
5246 // wanting to resize the row
5248 if ( YToEdgeOfRow(y
) < 0 )
5252 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5254 if ( !event
.ShiftDown() && !event
.CmdDown() )
5258 if ( event
.ShiftDown() )
5260 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5263 GetNumberCols() - 1,
5264 event
.ControlDown(),
5271 m_selection
->SelectRow( row
,
5272 event
.ControlDown(),
5279 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5284 // starting to drag-resize a row
5285 if ( CanDragRowSize() )
5286 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5290 // ------------ Left double click
5292 else if (event
.LeftDClick() )
5294 row
= YToEdgeOfRow(y
);
5299 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5301 // no default action at the moment
5306 // adjust row height depending on label text
5307 AutoSizeRowLabelSize( row
);
5309 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5314 // ------------ Left button released
5316 else if ( event
.LeftUp() )
5318 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5320 DoEndDragResizeRow();
5322 // Note: we are ending the event *after* doing
5323 // default processing in this case
5325 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5328 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5332 // ------------ Right button down
5334 else if ( event
.RightDown() )
5338 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5340 // no default action at the moment
5344 // ------------ Right double click
5346 else if ( event
.RightDClick() )
5350 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5352 // no default action at the moment
5356 // ------------ No buttons down and mouse moving
5358 else if ( event
.Moving() )
5360 m_dragRowOrCol
= YToEdgeOfRow( y
);
5361 if ( m_dragRowOrCol
>= 0 )
5363 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5365 // don't capture the mouse yet
5366 if ( CanDragRowSize() )
5367 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5370 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5372 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5377 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5380 wxPoint
pos( event
.GetPosition() );
5381 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5383 if ( event
.Dragging() )
5387 m_isDragging
= true;
5388 m_colLabelWin
->CaptureMouse();
5390 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
)
5391 m_dragRowOrCol
= XToCol( x
);
5394 if ( event
.LeftIsDown() )
5396 switch ( m_cursorMode
)
5398 case WXGRID_CURSOR_RESIZE_COL
:
5400 int cw
, ch
, dummy
, top
;
5401 m_gridWin
->GetClientSize( &cw
, &ch
);
5402 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5404 wxClientDC
dc( m_gridWin
);
5407 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5408 GetColMinimalWidth(m_dragRowOrCol
));
5409 dc
.SetLogicalFunction(wxINVERT
);
5410 if ( m_dragLastPos
>= 0 )
5412 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5414 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5419 case WXGRID_CURSOR_SELECT_COL
:
5421 if ( (col
= XToCol( x
)) >= 0 )
5425 m_selection
->SelectCol( col
,
5426 event
.ControlDown(),
5435 case WXGRID_CURSOR_MOVE_COL
:
5438 m_moveToCol
= GetColAt( 0 );
5440 m_moveToCol
= XToCol( x
);
5444 if ( m_moveToCol
< 0 )
5445 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5447 markerX
= GetColLeft( m_moveToCol
);
5449 if ( markerX
!= m_dragLastPos
)
5451 wxClientDC
dc( m_colLabelWin
);
5454 m_colLabelWin
->GetClientSize( &cw
, &ch
);
5458 //Clean up the last indicator
5459 if ( m_dragLastPos
>= 0 )
5461 wxPen
pen( m_colLabelWin
->GetBackgroundColour(), 2 );
5463 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
5464 dc
.SetPen(wxNullPen
);
5466 if ( XToCol( m_dragLastPos
) != -1 )
5467 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
5470 //Moving to the same place? Don't draw a marker
5471 if ( (m_moveToCol
== m_dragRowOrCol
)
5472 || (GetColPos( m_moveToCol
) == GetColPos( m_dragRowOrCol
) + 1)
5473 || (m_moveToCol
< 0 && m_dragRowOrCol
== GetColAt( m_numCols
- 1 )))
5480 wxPen
pen( *wxBLUE
, 2 );
5483 dc
.DrawLine( markerX
, 0, markerX
, ch
);
5485 dc
.SetPen(wxNullPen
);
5487 m_dragLastPos
= markerX
- 1;
5492 // default label to suppress warnings about "enumeration value
5493 // 'xxx' not handled in switch
5501 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5506 if (m_colLabelWin
->HasCapture())
5507 m_colLabelWin
->ReleaseMouse();
5508 m_isDragging
= false;
5511 // ------------ Entering or leaving the window
5513 if ( event
.Entering() || event
.Leaving() )
5515 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5518 // ------------ Left button pressed
5520 else if ( event
.LeftDown() )
5522 // don't send a label click event for a hit on the
5523 // edge of the col label - this is probably the user
5524 // wanting to resize the col
5526 if ( XToEdgeOfCol(x
) < 0 )
5530 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5532 if ( m_canDragColMove
)
5534 //Show button as pressed
5535 wxClientDC
dc( m_colLabelWin
);
5536 int colLeft
= GetColLeft( col
);
5537 int colRight
= GetColRight( col
) - 1;
5538 dc
.SetPen( wxPen( m_colLabelWin
->GetBackgroundColour(), 1 ) );
5539 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
5540 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
5542 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, m_colLabelWin
);
5546 if ( !event
.ShiftDown() && !event
.CmdDown() )
5550 if ( event
.ShiftDown() )
5552 m_selection
->SelectBlock( 0,
5553 m_currentCellCoords
.GetCol(),
5554 GetNumberRows() - 1, col
,
5555 event
.ControlDown(),
5562 m_selection
->SelectCol( col
,
5563 event
.ControlDown(),
5570 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5576 // starting to drag-resize a col
5578 if ( CanDragColSize() )
5579 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5583 // ------------ Left double click
5585 if ( event
.LeftDClick() )
5587 col
= XToEdgeOfCol(x
);
5592 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5594 // no default action at the moment
5599 // adjust column width depending on label text
5600 AutoSizeColLabelSize( col
);
5602 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5607 // ------------ Left button released
5609 else if ( event
.LeftUp() )
5611 switch ( m_cursorMode
)
5613 case WXGRID_CURSOR_RESIZE_COL
:
5614 DoEndDragResizeCol();
5616 // Note: we are ending the event *after* doing
5617 // default processing in this case
5619 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5622 case WXGRID_CURSOR_MOVE_COL
:
5625 SendEvent( wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
, event
);
5628 case WXGRID_CURSOR_SELECT_COL
:
5629 case WXGRID_CURSOR_SELECT_CELL
:
5630 case WXGRID_CURSOR_RESIZE_ROW
:
5631 case WXGRID_CURSOR_SELECT_ROW
:
5632 // nothing to do (?)
5636 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5640 // ------------ Right button down
5642 else if ( event
.RightDown() )
5646 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5648 // no default action at the moment
5652 // ------------ Right double click
5654 else if ( event
.RightDClick() )
5658 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5660 // no default action at the moment
5664 // ------------ No buttons down and mouse moving
5666 else if ( event
.Moving() )
5668 m_dragRowOrCol
= XToEdgeOfCol( x
);
5669 if ( m_dragRowOrCol
>= 0 )
5671 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5673 // don't capture the cursor yet
5674 if ( CanDragColSize() )
5675 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5678 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5680 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5685 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5687 if ( event
.LeftDown() )
5689 // indicate corner label by having both row and
5692 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5697 else if ( event
.LeftDClick() )
5699 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5701 else if ( event
.RightDown() )
5703 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5705 // no default action at the moment
5708 else if ( event
.RightDClick() )
5710 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5712 // no default action at the moment
5717 void wxGrid::ChangeCursorMode(CursorMode mode
,
5722 static const wxChar
*cursorModes
[] =
5732 wxLogTrace(_T("grid"),
5733 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5734 win
== m_colLabelWin
? _T("colLabelWin")
5735 : win
? _T("rowLabelWin")
5737 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5740 if ( mode
== m_cursorMode
&&
5741 win
== m_winCapture
&&
5742 captureMouse
== (m_winCapture
!= NULL
))
5747 // by default use the grid itself
5753 if (m_winCapture
->HasCapture())
5754 m_winCapture
->ReleaseMouse();
5755 m_winCapture
= (wxWindow
*)NULL
;
5758 m_cursorMode
= mode
;
5760 switch ( m_cursorMode
)
5762 case WXGRID_CURSOR_RESIZE_ROW
:
5763 win
->SetCursor( m_rowResizeCursor
);
5766 case WXGRID_CURSOR_RESIZE_COL
:
5767 win
->SetCursor( m_colResizeCursor
);
5770 case WXGRID_CURSOR_MOVE_COL
:
5771 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
5775 win
->SetCursor( *wxSTANDARD_CURSOR
);
5779 // we need to capture mouse when resizing
5780 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5781 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5783 if ( captureMouse
&& resize
)
5785 win
->CaptureMouse();
5790 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5793 wxPoint
pos( event
.GetPosition() );
5794 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5796 wxGridCellCoords coords
;
5797 XYToCell( x
, y
, coords
);
5799 int cell_rows
, cell_cols
;
5800 bool isFirstDrag
= !m_isDragging
;
5801 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5802 if ((cell_rows
< 0) || (cell_cols
< 0))
5804 coords
.SetRow(coords
.GetRow() + cell_rows
);
5805 coords
.SetCol(coords
.GetCol() + cell_cols
);
5808 if ( event
.Dragging() )
5810 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5812 // Don't start doing anything until the mouse has been dragged at
5813 // least 3 pixels in any direction...
5816 if (m_startDragPos
== wxDefaultPosition
)
5818 m_startDragPos
= pos
;
5821 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5825 m_isDragging
= true;
5826 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5828 // Hide the edit control, so it
5829 // won't interfere with drag-shrinking.
5830 if ( IsCellEditControlShown() )
5832 HideCellEditControl();
5833 SaveEditControlValue();
5836 // Have we captured the mouse yet?
5839 m_winCapture
= m_gridWin
;
5840 m_winCapture
->CaptureMouse();
5843 if ( coords
!= wxGridNoCellCoords
)
5845 if ( event
.CmdDown() )
5847 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5848 m_selectingKeyboard
= coords
;
5849 HighlightBlock( m_selectingKeyboard
, coords
);
5851 else if ( CanDragCell() )
5855 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5856 m_selectingKeyboard
= coords
;
5858 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5866 if ( !IsSelection() )
5868 HighlightBlock( coords
, coords
);
5872 HighlightBlock( m_currentCellCoords
, coords
);
5876 if (! IsVisible(coords
))
5878 MakeCellVisible(coords
);
5879 // TODO: need to introduce a delay or something here. The
5880 // scrolling is way to fast, at least on MSW - also on GTK.
5884 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5886 int cw
, ch
, left
, dummy
;
5887 m_gridWin
->GetClientSize( &cw
, &ch
);
5888 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5890 wxClientDC
dc( m_gridWin
);
5892 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5893 GetRowMinimalHeight(m_dragRowOrCol
) );
5894 dc
.SetLogicalFunction(wxINVERT
);
5895 if ( m_dragLastPos
>= 0 )
5897 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5899 dc
.DrawLine( left
, y
, left
+cw
, y
);
5902 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5904 int cw
, ch
, dummy
, top
;
5905 m_gridWin
->GetClientSize( &cw
, &ch
);
5906 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5908 wxClientDC
dc( m_gridWin
);
5910 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5911 GetColMinimalWidth(m_dragRowOrCol
) );
5912 dc
.SetLogicalFunction(wxINVERT
);
5913 if ( m_dragLastPos
>= 0 )
5915 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5917 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5924 m_isDragging
= false;
5925 m_startDragPos
= wxDefaultPosition
;
5927 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5928 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5931 if ( event
.Entering() || event
.Leaving() )
5933 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5934 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5939 // ------------ Left button pressed
5941 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5943 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5948 if ( !event
.CmdDown() )
5950 if ( event
.ShiftDown() )
5954 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5955 m_currentCellCoords
.GetCol(),
5958 event
.ControlDown(),
5964 else if ( XToEdgeOfCol(x
) < 0 &&
5965 YToEdgeOfRow(y
) < 0 )
5967 DisableCellEditControl();
5968 MakeCellVisible( coords
);
5970 if ( event
.CmdDown() )
5974 m_selection
->ToggleCellSelection( coords
.GetRow(),
5976 event
.ControlDown(),
5981 m_selectingTopLeft
= wxGridNoCellCoords
;
5982 m_selectingBottomRight
= wxGridNoCellCoords
;
5983 m_selectingKeyboard
= coords
;
5987 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5988 SetCurrentCell( coords
);
5991 if ( m_selection
->GetSelectionMode() !=
5992 wxGrid::wxGridSelectCells
)
5994 HighlightBlock( coords
, coords
);
6002 // ------------ Left double click
6004 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
6006 DisableCellEditControl();
6008 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
6010 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
6015 // we want double click to select a cell and start editing
6016 // (i.e. to behave in same way as sequence of two slow clicks):
6017 m_waitForSlowClick
= true;
6022 // ------------ Left button released
6024 else if ( event
.LeftUp() )
6026 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6030 if (m_winCapture
->HasCapture())
6031 m_winCapture
->ReleaseMouse();
6032 m_winCapture
= NULL
;
6035 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
6038 EnableCellEditControl();
6040 wxGridCellAttr
*attr
= GetCellAttr(coords
);
6041 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
6042 editor
->StartingClick();
6046 m_waitForSlowClick
= false;
6048 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6049 m_selectingBottomRight
!= wxGridNoCellCoords
)
6053 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6054 m_selectingTopLeft
.GetCol(),
6055 m_selectingBottomRight
.GetRow(),
6056 m_selectingBottomRight
.GetCol(),
6057 event
.ControlDown(),
6063 m_selectingTopLeft
= wxGridNoCellCoords
;
6064 m_selectingBottomRight
= wxGridNoCellCoords
;
6066 // Show the edit control, if it has been hidden for
6068 ShowCellEditControl();
6071 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6073 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6074 DoEndDragResizeRow();
6076 // Note: we are ending the event *after* doing
6077 // default processing in this case
6079 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
6081 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6083 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6084 DoEndDragResizeCol();
6086 // Note: we are ending the event *after* doing
6087 // default processing in this case
6089 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
6095 // ------------ Right button down
6097 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
6099 DisableCellEditControl();
6100 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
6105 // no default action at the moment
6109 // ------------ Right double click
6111 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
6113 DisableCellEditControl();
6114 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
6119 // no default action at the moment
6123 // ------------ Moving and no button action
6125 else if ( event
.Moving() && !event
.IsButton() )
6127 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
6129 // out of grid cell area
6130 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6134 int dragRow
= YToEdgeOfRow( y
);
6135 int dragCol
= XToEdgeOfCol( x
);
6137 // Dragging on the corner of a cell to resize in both
6138 // directions is not implemented yet...
6140 if ( dragRow
>= 0 && dragCol
>= 0 )
6142 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6148 m_dragRowOrCol
= dragRow
;
6150 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6152 if ( CanDragRowSize() && CanDragGridSize() )
6153 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
6156 else if ( dragCol
>= 0 )
6158 m_dragRowOrCol
= dragCol
;
6160 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6162 if ( CanDragColSize() && CanDragGridSize() )
6163 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
6166 else // Neither on a row or col edge
6168 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6170 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6176 void wxGrid::DoEndDragResizeRow()
6178 if ( m_dragLastPos
>= 0 )
6180 // erase the last line and resize the row
6182 int cw
, ch
, left
, dummy
;
6183 m_gridWin
->GetClientSize( &cw
, &ch
);
6184 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6186 wxClientDC
dc( m_gridWin
);
6188 dc
.SetLogicalFunction( wxINVERT
);
6189 dc
.DrawLine( left
, m_dragLastPos
, left
+ cw
, m_dragLastPos
);
6190 HideCellEditControl();
6191 SaveEditControlValue();
6193 int rowTop
= GetRowTop(m_dragRowOrCol
);
6194 SetRowSize( m_dragRowOrCol
,
6195 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
6197 if ( !GetBatchCount() )
6199 // Only needed to get the correct rect.y:
6200 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
6202 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6203 rect
.width
= m_rowLabelWidth
;
6204 rect
.height
= ch
- rect
.y
;
6205 m_rowLabelWin
->Refresh( true, &rect
);
6208 // if there is a multicell block, paint all of it
6211 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
6212 int leftCol
= XToCol(left
);
6213 int rightCol
= internalXToCol(left
+ cw
);
6216 for (i
=leftCol
; i
<rightCol
; i
++)
6218 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
6219 if (cell_rows
< subtract_rows
)
6220 subtract_rows
= cell_rows
;
6222 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
6223 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6224 rect
.height
= ch
- rect
.y
;
6227 m_gridWin
->Refresh( false, &rect
);
6230 ShowCellEditControl();
6235 void wxGrid::DoEndDragResizeCol()
6237 if ( m_dragLastPos
>= 0 )
6239 // erase the last line and resize the col
6241 int cw
, ch
, dummy
, top
;
6242 m_gridWin
->GetClientSize( &cw
, &ch
);
6243 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6245 wxClientDC
dc( m_gridWin
);
6247 dc
.SetLogicalFunction( wxINVERT
);
6248 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6249 HideCellEditControl();
6250 SaveEditControlValue();
6252 int colLeft
= GetColLeft(m_dragRowOrCol
);
6253 SetColSize( m_dragRowOrCol
,
6254 wxMax( m_dragLastPos
- colLeft
,
6255 GetColMinimalWidth(m_dragRowOrCol
) ) );
6257 if ( !GetBatchCount() )
6259 // Only needed to get the correct rect.x:
6260 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
6262 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6263 rect
.width
= cw
- rect
.x
;
6264 rect
.height
= m_colLabelHeight
;
6265 m_colLabelWin
->Refresh( true, &rect
);
6268 // if there is a multicell block, paint all of it
6271 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
6272 int topRow
= YToRow(top
);
6273 int bottomRow
= internalYToRow(top
+ cw
);
6276 for (i
=topRow
; i
<bottomRow
; i
++)
6278 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
6279 if (cell_cols
< subtract_cols
)
6280 subtract_cols
= cell_cols
;
6283 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
6284 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6285 rect
.width
= cw
- rect
.x
;
6289 m_gridWin
->Refresh( false, &rect
);
6292 ShowCellEditControl();
6296 void wxGrid::DoEndDragMoveCol()
6298 //The user clicked on the column but didn't actually drag
6299 if ( m_dragLastPos
< 0 )
6301 m_colLabelWin
->Refresh(); //Do this to "unpress" the column
6306 if ( m_moveToCol
== -1 )
6307 newPos
= m_numCols
- 1;
6310 newPos
= GetColPos( m_moveToCol
);
6311 if ( newPos
> GetColPos( m_dragRowOrCol
) )
6315 SetColPos( m_dragRowOrCol
, newPos
);
6318 void wxGrid::SetColPos( int colID
, int newPos
)
6320 if ( m_colAt
.IsEmpty() )
6322 m_colAt
.Alloc( m_numCols
);
6325 for ( i
= 0; i
< m_numCols
; i
++ )
6331 int oldPos
= GetColPos( colID
);
6333 //Reshuffle the m_colAt array
6334 if ( newPos
> oldPos
)
6337 for ( i
= oldPos
; i
< newPos
; i
++ )
6339 m_colAt
[i
] = m_colAt
[i
+1];
6345 for ( i
= oldPos
; i
> newPos
; i
-- )
6347 m_colAt
[i
] = m_colAt
[i
-1];
6351 m_colAt
[newPos
] = colID
;
6353 //Recalculate the column rights
6354 if ( !m_colWidths
.IsEmpty() )
6358 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6360 int colID
= GetColAt( colPos
);
6362 colRight
+= m_colWidths
[colID
];
6363 m_colRights
[colID
] = colRight
;
6367 m_colLabelWin
->Refresh();
6368 m_gridWin
->Refresh();
6373 void wxGrid::EnableDragColMove( bool enable
)
6375 if ( m_canDragColMove
== enable
)
6378 m_canDragColMove
= enable
;
6380 if ( !m_canDragColMove
)
6384 //Recalculate the column rights
6385 if ( !m_colWidths
.IsEmpty() )
6389 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6391 colRight
+= m_colWidths
[colPos
];
6392 m_colRights
[colPos
] = colRight
;
6396 m_colLabelWin
->Refresh();
6397 m_gridWin
->Refresh();
6403 // ------ interaction with data model
6405 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6407 switch ( msg
.GetId() )
6409 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6410 return GetModelValues();
6412 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6413 return SetModelValues();
6415 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6416 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6417 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6418 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6419 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6420 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6421 return Redimension( msg
);
6428 // The behaviour of this function depends on the grid table class
6429 // Clear() function. For the default wxGridStringTable class the
6430 // behavious is to replace all cell contents with wxEmptyString but
6431 // not to change the number of rows or cols.
6433 void wxGrid::ClearGrid()
6437 if (IsCellEditControlEnabled())
6438 DisableCellEditControl();
6441 if (!GetBatchCount())
6442 m_gridWin
->Refresh();
6446 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6448 // TODO: something with updateLabels flag
6452 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6458 if (IsCellEditControlEnabled())
6459 DisableCellEditControl();
6461 bool done
= m_table
->InsertRows( pos
, numRows
);
6464 // the table will have sent the results of the insert row
6465 // operation to this view object as a grid table message
6471 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6473 // TODO: something with updateLabels flag
6477 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6483 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6486 // the table will have sent the results of the append row
6487 // operation to this view object as a grid table message
6493 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6495 // TODO: something with updateLabels flag
6499 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6505 if (IsCellEditControlEnabled())
6506 DisableCellEditControl();
6508 bool done
= m_table
->DeleteRows( pos
, numRows
);
6510 // the table will have sent the results of the delete row
6511 // operation to this view object as a grid table message
6517 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6519 // TODO: something with updateLabels flag
6523 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6529 if (IsCellEditControlEnabled())
6530 DisableCellEditControl();
6532 bool done
= m_table
->InsertCols( pos
, numCols
);
6534 // the table will have sent the results of the insert col
6535 // operation to this view object as a grid table message
6541 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6543 // TODO: something with updateLabels flag
6547 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6553 bool done
= m_table
->AppendCols( numCols
);
6555 // the table will have sent the results of the append col
6556 // operation to this view object as a grid table message
6562 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6564 // TODO: something with updateLabels flag
6568 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6574 if (IsCellEditControlEnabled())
6575 DisableCellEditControl();
6577 bool done
= m_table
->DeleteCols( pos
, numCols
);
6579 // the table will have sent the results of the delete col
6580 // operation to this view object as a grid table message
6587 // ----- event handlers
6590 // Generate a grid event based on a mouse event and
6591 // return the result of ProcessEvent()
6593 int wxGrid::SendEvent( const wxEventType type
,
6595 wxMouseEvent
& mouseEv
)
6597 bool claimed
, vetoed
;
6599 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6601 int rowOrCol
= (row
== -1 ? col
: row
);
6603 wxGridSizeEvent
gridEvt( GetId(),
6607 mouseEv
.GetX() + GetRowLabelSize(),
6608 mouseEv
.GetY() + GetColLabelSize(),
6609 mouseEv
.ControlDown(),
6610 mouseEv
.ShiftDown(),
6612 mouseEv
.MetaDown() );
6614 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6615 vetoed
= !gridEvt
.IsAllowed();
6617 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6619 // Right now, it should _never_ end up here!
6620 wxGridRangeSelectEvent
gridEvt( GetId(),
6624 m_selectingBottomRight
,
6626 mouseEv
.ControlDown(),
6627 mouseEv
.ShiftDown(),
6629 mouseEv
.MetaDown() );
6631 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6632 vetoed
= !gridEvt
.IsAllowed();
6634 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
6635 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
6636 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
6637 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
6639 wxPoint pos
= mouseEv
.GetPosition();
6641 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
6642 pos
.y
+= GetColLabelSize();
6643 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
6644 pos
.x
+= GetRowLabelSize();
6646 wxGridEvent
gridEvt( GetId(),
6653 mouseEv
.ControlDown(),
6654 mouseEv
.ShiftDown(),
6656 mouseEv
.MetaDown() );
6657 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6658 vetoed
= !gridEvt
.IsAllowed();
6662 wxGridEvent
gridEvt( GetId(),
6666 mouseEv
.GetX() + GetRowLabelSize(),
6667 mouseEv
.GetY() + GetColLabelSize(),
6669 mouseEv
.ControlDown(),
6670 mouseEv
.ShiftDown(),
6672 mouseEv
.MetaDown() );
6673 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6674 vetoed
= !gridEvt
.IsAllowed();
6677 // A Veto'd event may not be `claimed' so test this first
6681 return claimed
? 1 : 0;
6684 // Generate a grid event of specified type and return the result
6685 // of ProcessEvent().
6687 int wxGrid::SendEvent( const wxEventType type
,
6690 bool claimed
, vetoed
;
6692 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6694 int rowOrCol
= (row
== -1 ? col
: row
);
6696 wxGridSizeEvent
gridEvt( GetId(), type
, this, rowOrCol
);
6698 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6699 vetoed
= !gridEvt
.IsAllowed();
6703 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
6705 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6706 vetoed
= !gridEvt
.IsAllowed();
6709 // A Veto'd event may not be `claimed' so test this first
6713 return claimed
? 1 : 0;
6716 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6718 // needed to prevent zillions of paint events on MSW
6722 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6724 // Don't do anything if between Begin/EndBatch...
6725 // EndBatch() will do all this on the last nested one anyway.
6726 if (! GetBatchCount())
6728 // Refresh to get correct scrolled position:
6729 wxScrolledWindow::Refresh(eraseb
, rect
);
6733 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6734 int width_label
, width_cell
, height_label
, height_cell
;
6737 // Copy rectangle can get scroll offsets..
6738 rect_x
= rect
->GetX();
6739 rect_y
= rect
->GetY();
6740 rectWidth
= rect
->GetWidth();
6741 rectHeight
= rect
->GetHeight();
6743 width_label
= m_rowLabelWidth
- rect_x
;
6744 if (width_label
> rectWidth
)
6745 width_label
= rectWidth
;
6747 height_label
= m_colLabelHeight
- rect_y
;
6748 if (height_label
> rectHeight
)
6749 height_label
= rectHeight
;
6751 if (rect_x
> m_rowLabelWidth
)
6753 x
= rect_x
- m_rowLabelWidth
;
6754 width_cell
= rectWidth
;
6759 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6762 if (rect_y
> m_colLabelHeight
)
6764 y
= rect_y
- m_colLabelHeight
;
6765 height_cell
= rectHeight
;
6770 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6773 // Paint corner label part intersecting rect.
6774 if ( width_label
> 0 && height_label
> 0 )
6776 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6777 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6780 // Paint col labels part intersecting rect.
6781 if ( width_cell
> 0 && height_label
> 0 )
6783 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6784 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6787 // Paint row labels part intersecting rect.
6788 if ( width_label
> 0 && height_cell
> 0 )
6790 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6791 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6794 // Paint cell area part intersecting rect.
6795 if ( width_cell
> 0 && height_cell
> 0 )
6797 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6798 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6803 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6804 m_colLabelWin
->Refresh(eraseb
, NULL
);
6805 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6806 m_gridWin
->Refresh(eraseb
, NULL
);
6811 void wxGrid::OnSize( wxSizeEvent
& event
)
6813 // position the child windows
6816 // don't call CalcDimensions() from here, the base class handles the size
6821 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6823 if ( m_inOnKeyDown
)
6825 // shouldn't be here - we are going round in circles...
6827 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6830 m_inOnKeyDown
= true;
6832 // propagate the event up and see if it gets processed
6833 wxWindow
*parent
= GetParent();
6834 wxKeyEvent
keyEvt( event
);
6835 keyEvt
.SetEventObject( parent
);
6837 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6839 if (GetLayoutDirection() == wxLayout_RightToLeft
)
6841 if (event
.GetKeyCode() == WXK_RIGHT
)
6842 event
.m_keyCode
= WXK_LEFT
;
6843 else if (event
.GetKeyCode() == WXK_LEFT
)
6844 event
.m_keyCode
= WXK_RIGHT
;
6847 // try local handlers
6848 switch ( event
.GetKeyCode() )
6851 if ( event
.ControlDown() )
6852 MoveCursorUpBlock( event
.ShiftDown() );
6854 MoveCursorUp( event
.ShiftDown() );
6858 if ( event
.ControlDown() )
6859 MoveCursorDownBlock( event
.ShiftDown() );
6861 MoveCursorDown( event
.ShiftDown() );
6865 if ( event
.ControlDown() )
6866 MoveCursorLeftBlock( event
.ShiftDown() );
6868 MoveCursorLeft( event
.ShiftDown() );
6872 if ( event
.ControlDown() )
6873 MoveCursorRightBlock( event
.ShiftDown() );
6875 MoveCursorRight( event
.ShiftDown() );
6879 case WXK_NUMPAD_ENTER
:
6880 if ( event
.ControlDown() )
6882 event
.Skip(); // to let the edit control have the return
6886 if ( GetGridCursorRow() < GetNumberRows()-1 )
6888 MoveCursorDown( event
.ShiftDown() );
6892 // at the bottom of a column
6893 DisableCellEditControl();
6903 if (event
.ShiftDown())
6905 if ( GetGridCursorCol() > 0 )
6907 MoveCursorLeft( false );
6912 DisableCellEditControl();
6917 if ( GetGridCursorCol() < GetNumberCols() - 1 )
6919 MoveCursorRight( false );
6924 DisableCellEditControl();
6930 if ( event
.ControlDown() )
6932 MakeCellVisible( 0, 0 );
6933 SetCurrentCell( 0, 0 );
6942 if ( event
.ControlDown() )
6944 MakeCellVisible( m_numRows
- 1, m_numCols
- 1 );
6945 SetCurrentCell( m_numRows
- 1, m_numCols
- 1 );
6962 if ( event
.ControlDown() )
6966 m_selection
->ToggleCellSelection(
6967 m_currentCellCoords
.GetRow(),
6968 m_currentCellCoords
.GetCol(),
6969 event
.ControlDown(),
6977 if ( !IsEditable() )
6978 MoveCursorRight( false );
6989 m_inOnKeyDown
= false;
6992 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6994 // try local handlers
6996 if ( event
.GetKeyCode() == WXK_SHIFT
)
6998 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6999 m_selectingBottomRight
!= wxGridNoCellCoords
)
7003 m_selection
->SelectBlock(
7004 m_selectingTopLeft
.GetRow(),
7005 m_selectingTopLeft
.GetCol(),
7006 m_selectingBottomRight
.GetRow(),
7007 m_selectingBottomRight
.GetCol(),
7008 event
.ControlDown(),
7015 m_selectingTopLeft
= wxGridNoCellCoords
;
7016 m_selectingBottomRight
= wxGridNoCellCoords
;
7017 m_selectingKeyboard
= wxGridNoCellCoords
;
7021 void wxGrid::OnChar( wxKeyEvent
& event
)
7023 // is it possible to edit the current cell at all?
7024 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7026 // yes, now check whether the cells editor accepts the key
7027 int row
= m_currentCellCoords
.GetRow();
7028 int col
= m_currentCellCoords
.GetCol();
7029 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7030 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7032 // <F2> is special and will always start editing, for
7033 // other keys - ask the editor itself
7034 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
7035 || editor
->IsAcceptedKey(event
) )
7037 // ensure cell is visble
7038 MakeCellVisible(row
, col
);
7039 EnableCellEditControl();
7041 // a problem can arise if the cell is not completely
7042 // visible (even after calling MakeCellVisible the
7043 // control is not created and calling StartingKey will
7045 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
7046 editor
->StartingKey(event
);
7062 void wxGrid::OnEraseBackground(wxEraseEvent
&)
7066 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
7068 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
7070 // the event has been intercepted - do nothing
7074 wxClientDC
dc( m_gridWin
);
7077 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
7079 DisableCellEditControl();
7081 if ( IsVisible( m_currentCellCoords
, false ) )
7084 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
7085 if ( !m_gridLinesEnabled
)
7093 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
7095 // Otherwise refresh redraws the highlight!
7096 m_currentCellCoords
= coords
;
7098 DrawGridCellArea( dc
, cells
);
7099 DrawAllGridLines( dc
, r
);
7103 m_currentCellCoords
= coords
;
7105 wxGridCellAttr
*attr
= GetCellAttr( coords
);
7106 DrawCellHighlight( dc
, attr
);
7110 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7113 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7117 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
7120 rightCol
= GetNumberCols() - 1;
7122 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
7125 bottomRow
= GetNumberRows() - 1;
7129 if ( topRow
> bottomRow
)
7136 if ( leftCol
> rightCol
)
7143 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7144 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7146 // First the case that we selected a completely new area
7147 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
7148 m_selectingBottomRight
== wxGridNoCellCoords
)
7151 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
7152 wxGridCellCoords ( bottomRow
, rightCol
) );
7153 m_gridWin
->Refresh( false, &rect
);
7156 // Now handle changing an existing selection area.
7157 else if ( m_selectingTopLeft
!= updateTopLeft
||
7158 m_selectingBottomRight
!= updateBottomRight
)
7160 // Compute two optimal update rectangles:
7161 // Either one rectangle is a real subset of the
7162 // other, or they are (almost) disjoint!
7164 bool need_refresh
[4];
7168 need_refresh
[3] = false;
7171 // Store intermediate values
7172 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
7173 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
7174 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
7175 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
7177 // Determine the outer/inner coordinates.
7178 if (oldLeft
> leftCol
)
7184 if (oldTop
> topRow
)
7190 if (oldRight
< rightCol
)
7193 oldRight
= rightCol
;
7196 if (oldBottom
< bottomRow
)
7199 oldBottom
= bottomRow
;
7203 // Now, either the stuff marked old is the outer
7204 // rectangle or we don't have a situation where one
7205 // is contained in the other.
7207 if ( oldLeft
< leftCol
)
7209 // Refresh the newly selected or deselected
7210 // area to the left of the old or new selection.
7211 need_refresh
[0] = true;
7212 rect
[0] = BlockToDeviceRect(
7213 wxGridCellCoords( oldTop
, oldLeft
),
7214 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
7217 if ( oldTop
< topRow
)
7219 // Refresh the newly selected or deselected
7220 // area above the old or new selection.
7221 need_refresh
[1] = true;
7222 rect
[1] = BlockToDeviceRect(
7223 wxGridCellCoords( oldTop
, leftCol
),
7224 wxGridCellCoords( topRow
- 1, rightCol
) );
7227 if ( oldRight
> rightCol
)
7229 // Refresh the newly selected or deselected
7230 // area to the right of the old or new selection.
7231 need_refresh
[2] = true;
7232 rect
[2] = BlockToDeviceRect(
7233 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
7234 wxGridCellCoords( oldBottom
, oldRight
) );
7237 if ( oldBottom
> bottomRow
)
7239 // Refresh the newly selected or deselected
7240 // area below the old or new selection.
7241 need_refresh
[3] = true;
7242 rect
[3] = BlockToDeviceRect(
7243 wxGridCellCoords( bottomRow
+ 1, leftCol
),
7244 wxGridCellCoords( oldBottom
, rightCol
) );
7247 // various Refresh() calls
7248 for (i
= 0; i
< 4; i
++ )
7249 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7250 m_gridWin
->Refresh( false, &(rect
[i
]) );
7254 m_selectingTopLeft
= updateTopLeft
;
7255 m_selectingBottomRight
= updateBottomRight
;
7259 // ------ functions to get/send data (see also public functions)
7262 bool wxGrid::GetModelValues()
7264 // Hide the editor, so it won't hide a changed value.
7265 HideCellEditControl();
7269 // all we need to do is repaint the grid
7271 m_gridWin
->Refresh();
7278 bool wxGrid::SetModelValues()
7282 // Disable the editor, so it won't hide a changed value.
7283 // Do we also want to save the current value of the editor first?
7285 DisableCellEditControl();
7289 for ( row
= 0; row
< m_numRows
; row
++ )
7291 for ( col
= 0; col
< m_numCols
; col
++ )
7293 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
7303 // Note - this function only draws cells that are in the list of
7304 // exposed cells (usually set from the update region by
7305 // CalcExposedCells)
7307 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7309 if ( !m_numRows
|| !m_numCols
)
7312 int i
, numCells
= cells
.GetCount();
7313 int row
, col
, cell_rows
, cell_cols
;
7314 wxGridCellCoordsArray redrawCells
;
7316 for ( i
= numCells
- 1; i
>= 0; i
-- )
7318 row
= cells
[i
].GetRow();
7319 col
= cells
[i
].GetCol();
7320 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7322 // If this cell is part of a multicell block, find owner for repaint
7323 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7325 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
7326 bool marked
= false;
7327 for ( int j
= 0; j
< numCells
; j
++ )
7329 if ( cell
== cells
[j
] )
7338 int count
= redrawCells
.GetCount();
7339 for (int j
= 0; j
< count
; j
++)
7341 if ( cell
== redrawCells
[j
] )
7349 redrawCells
.Add( cell
);
7352 // don't bother drawing this cell
7356 // If this cell is empty, find cell to left that might want to overflow
7357 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7359 for ( int l
= 0; l
< cell_rows
; l
++ )
7361 // find a cell in this row to leave already marked for repaint
7363 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7364 if ((redrawCells
[k
].GetCol() < left
) &&
7365 (redrawCells
[k
].GetRow() == row
))
7367 left
= redrawCells
[k
].GetCol();
7371 left
= 0; // oh well
7373 for (int j
= col
- 1; j
>= left
; j
--)
7375 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7377 if (GetCellOverflow(row
+ l
, j
))
7379 wxGridCellCoords
cell(row
+ l
, j
);
7380 bool marked
= false;
7382 for (int k
= 0; k
< numCells
; k
++)
7384 if ( cell
== cells
[k
] )
7393 int count
= redrawCells
.GetCount();
7394 for (int k
= 0; k
< count
; k
++)
7396 if ( cell
== redrawCells
[k
] )
7403 redrawCells
.Add( cell
);
7412 DrawCell( dc
, cells
[i
] );
7415 numCells
= redrawCells
.GetCount();
7417 for ( i
= numCells
- 1; i
>= 0; i
-- )
7419 DrawCell( dc
, redrawCells
[i
] );
7423 void wxGrid::DrawGridSpace( wxDC
& dc
)
7426 m_gridWin
->GetClientSize( &cw
, &ch
);
7429 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7431 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
7432 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7434 if ( right
> rightCol
|| bottom
> bottomRow
)
7437 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7439 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
7440 dc
.SetPen( *wxTRANSPARENT_PEN
);
7442 if ( right
> rightCol
)
7444 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7447 if ( bottom
> bottomRow
)
7449 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7454 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7456 int row
= coords
.GetRow();
7457 int col
= coords
.GetCol();
7459 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7462 // we draw the cell border ourselves
7463 #if !WXGRID_DRAW_LINES
7464 if ( m_gridLinesEnabled
)
7465 DrawCellBorder( dc
, coords
);
7468 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7470 bool isCurrent
= coords
== m_currentCellCoords
;
7472 wxRect rect
= CellToRect( row
, col
);
7474 // if the editor is shown, we should use it and not the renderer
7475 // Note: However, only if it is really _shown_, i.e. not hidden!
7476 if ( isCurrent
&& IsCellEditControlShown() )
7478 // NB: this "#if..." is temporary and fixes a problem where the
7479 // edit control is erased by this code after being rendered.
7480 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7481 // implicitly, causing this out-of order render.
7482 #if !defined(__WXMAC__) || wxMAC_USE_CORE_GRAPHICS
7483 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7484 editor
->PaintBackground(rect
, attr
);
7490 // but all the rest is drawn by the cell renderer and hence may be customized
7491 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7492 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7499 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7501 int row
= m_currentCellCoords
.GetRow();
7502 int col
= m_currentCellCoords
.GetCol();
7504 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7507 wxRect rect
= CellToRect(row
, col
);
7509 // hmmm... what could we do here to show that the cell is disabled?
7510 // for now, I just draw a thinner border than for the other ones, but
7511 // it doesn't look really good
7513 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7517 // The center of the drawn line is where the position/width/height of
7518 // the rectangle is actually at (on wxMSW at least), so the
7519 // size of the rectangle is reduced to compensate for the thickness of
7520 // the line. If this is too strange on non-wxMSW platforms then
7521 // please #ifdef this appropriately.
7522 rect
.x
+= penWidth
/ 2;
7523 rect
.y
+= penWidth
/ 2;
7524 rect
.width
-= penWidth
- 1;
7525 rect
.height
-= penWidth
- 1;
7527 // Now draw the rectangle
7528 // use the cellHighlightColour if the cell is inside a selection, this
7529 // will ensure the cell is always visible.
7530 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
: m_cellHighlightColour
, penWidth
, wxSOLID
));
7531 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7532 dc
.DrawRectangle(rect
);
7536 // VZ: my experiments with 3D borders...
7538 // how to properly set colours for arbitrary bg?
7539 wxCoord x1
= rect
.x
,
7541 x2
= rect
.x
+ rect
.width
- 1,
7542 y2
= rect
.y
+ rect
.height
- 1;
7544 dc
.SetPen(*wxWHITE_PEN
);
7545 dc
.DrawLine(x1
, y1
, x2
, y1
);
7546 dc
.DrawLine(x1
, y1
, x1
, y2
);
7548 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7549 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7551 dc
.SetPen(*wxBLACK_PEN
);
7552 dc
.DrawLine(x1
, y2
, x2
, y2
);
7553 dc
.DrawLine(x2
, y1
, x2
, y2
+ 1);
7557 wxPen
wxGrid::GetDefaultGridLinePen()
7559 return wxPen(GetGridLineColour(), 1, wxSOLID
);
7562 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
7564 return GetDefaultGridLinePen();
7567 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
7569 return GetDefaultGridLinePen();
7572 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7574 int row
= coords
.GetRow();
7575 int col
= coords
.GetCol();
7576 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7580 wxRect rect
= CellToRect( row
, col
);
7582 // right hand border
7583 dc
.SetPen( GetColGridLinePen(col
) );
7584 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7585 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7588 dc
.SetPen( GetRowGridLinePen(row
) );
7589 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7590 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7593 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7595 // This if block was previously in wxGrid::OnPaint but that doesn't
7596 // seem to get called under wxGTK - MB
7598 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7599 m_numRows
&& m_numCols
)
7601 m_currentCellCoords
.Set(0, 0);
7604 if ( IsCellEditControlShown() )
7606 // don't show highlight when the edit control is shown
7610 // if the active cell was repainted, repaint its highlight too because it
7611 // might have been damaged by the grid lines
7612 size_t count
= cells
.GetCount();
7613 for ( size_t n
= 0; n
< count
; n
++ )
7615 if ( cells
[n
] == m_currentCellCoords
)
7617 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7618 DrawCellHighlight(dc
, attr
);
7626 // TODO: remove this ???
7627 // This is used to redraw all grid lines e.g. when the grid line colour
7630 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7632 #if !WXGRID_DRAW_LINES
7636 if ( !m_gridLinesEnabled
|| !m_numRows
|| !m_numCols
)
7639 int top
, bottom
, left
, right
;
7641 #if 0 //#ifndef __WXGTK__
7645 m_gridWin
->GetClientSize(&cw
, &ch
);
7647 // virtual coords of visible area
7649 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7650 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7655 reg
.GetBox(x
, y
, w
, h
);
7656 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7657 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7661 m_gridWin
->GetClientSize(&cw
, &ch
);
7662 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7663 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7666 // avoid drawing grid lines past the last row and col
7668 right
= wxMin( right
, GetColRight(GetColAt( m_numCols
- 1 )) );
7669 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7671 // no gridlines inside multicells, clip them out
7672 int leftCol
= GetColPos( internalXToCol(left
) );
7673 int topRow
= internalYToRow(top
);
7674 int rightCol
= GetColPos( internalXToCol(right
) );
7675 int bottomRow
= internalYToRow(bottom
);
7678 // CS: I don't know why suddenly unscrolled coordinates are used for clipping
7679 wxRegion
clippedcells(0, 0, cw
, ch
);
7681 int i
, j
, cell_rows
, cell_cols
;
7684 for (j
=topRow
; j
<bottomRow
; j
++)
7687 for (colPos
=leftCol
; colPos
<rightCol
; colPos
++)
7689 i
= GetColAt( colPos
);
7691 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7692 if ((cell_rows
> 1) || (cell_cols
> 1))
7694 rect
= CellToRect(j
,i
);
7695 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7696 clippedcells
.Subtract(rect
);
7698 else if ((cell_rows
< 0) || (cell_cols
< 0))
7700 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7701 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7702 clippedcells
.Subtract(rect
);
7707 wxRegion
clippedcells( left
, top
, right
- left
, bottom
- top
);
7709 int i
, j
, cell_rows
, cell_cols
;
7712 for (j
=topRow
; j
<bottomRow
; j
++)
7714 for (i
=leftCol
; i
<rightCol
; i
++)
7716 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7717 if ((cell_rows
> 1) || (cell_cols
> 1))
7719 rect
= CellToRect(j
, i
);
7720 clippedcells
.Subtract(rect
);
7722 else if ((cell_rows
< 0) || (cell_cols
< 0))
7724 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7725 clippedcells
.Subtract(rect
);
7731 dc
.SetClippingRegion( clippedcells
);
7734 // horizontal grid lines
7736 // already declared above - int i;
7737 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7739 int bot
= GetRowBottom(i
) - 1;
7748 dc
.SetPen( GetRowGridLinePen(i
) );
7749 dc
.DrawLine( left
, bot
, right
, bot
);
7753 // vertical grid lines
7756 for ( colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
7758 i
= GetColAt( colPos
);
7760 int colRight
= GetColRight(i
);
7762 if (GetLayoutDirection() != wxLayout_RightToLeft
)
7766 if ( colRight
> right
)
7771 if ( colRight
>= left
)
7773 dc
.SetPen( GetColGridLinePen(i
) );
7774 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7778 dc
.DestroyClippingRegion();
7781 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
7787 size_t numLabels
= rows
.GetCount();
7789 for ( i
= 0; i
< numLabels
; i
++ )
7791 DrawRowLabel( dc
, rows
[i
] );
7795 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7797 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7805 rect
.SetY( GetRowTop(row
) + 1 );
7806 rect
.SetWidth( m_rowLabelWidth
- 2 );
7807 rect
.SetHeight( GetRowHeight(row
) - 2 );
7809 CalcScrolledPosition( 0, rect
.y
, NULL
, &rect
.y
);
7811 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7813 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7815 int rowTop
= GetRowTop(row
),
7816 rowBottom
= GetRowBottom(row
) - 1;
7818 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
7819 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
7820 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7821 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7823 dc
.SetPen( *wxWHITE_PEN
);
7824 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7825 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
7828 dc
.SetBackgroundMode( wxTRANSPARENT
);
7829 dc
.SetTextForeground( GetLabelTextColour() );
7830 dc
.SetFont( GetLabelFont() );
7833 GetRowLabelAlignment( &hAlign
, &vAlign
);
7836 rect
.SetY( GetRowTop(row
) + 2 );
7837 rect
.SetWidth( m_rowLabelWidth
- 4 );
7838 rect
.SetHeight( GetRowHeight(row
) - 4 );
7839 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7842 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7848 size_t numLabels
= cols
.GetCount();
7850 for ( i
= 0; i
< numLabels
; i
++ )
7852 DrawColLabel( dc
, cols
[i
] );
7856 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7858 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7861 int colLeft
= GetColLeft(col
);
7867 rect
.SetX( colLeft
+ 1 );
7869 rect
.SetWidth( GetColWidth(col
) - 2 );
7870 rect
.SetHeight( m_colLabelHeight
- 2 );
7872 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7874 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7876 int colRight
= GetColRight(col
) - 1;
7878 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
7879 dc
.DrawLine( colRight
, 0, colRight
, m_colLabelHeight
- 1 );
7880 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7881 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
7882 colRight
+ 1, m_colLabelHeight
- 1 );
7884 dc
.SetPen( *wxWHITE_PEN
);
7885 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
7886 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7889 dc
.SetBackgroundMode( wxTRANSPARENT
);
7890 dc
.SetTextForeground( GetLabelTextColour() );
7891 dc
.SetFont( GetLabelFont() );
7893 int hAlign
, vAlign
, orient
;
7894 GetColLabelAlignment( &hAlign
, &vAlign
);
7895 orient
= GetColLabelTextOrientation();
7897 rect
.SetX( colLeft
+ 2 );
7899 rect
.SetWidth( GetColWidth(col
) - 4 );
7900 rect
.SetHeight( m_colLabelHeight
- 4 );
7901 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7904 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7905 const wxString
& value
,
7909 int textOrientation
)
7911 wxArrayString lines
;
7913 StringToLines( value
, lines
);
7915 // Forward to new API.
7916 DrawTextRectangle( dc
,
7924 // VZ: this should be replaced with wxDC::DrawLabel() to which we just have to
7925 // add textOrientation support
7926 void wxGrid::DrawTextRectangle(wxDC
& dc
,
7927 const wxArrayString
& lines
,
7931 int textOrientation
)
7933 if ( lines
.empty() )
7936 wxDCClipper
clip(dc
, rect
);
7941 if ( textOrientation
== wxHORIZONTAL
)
7942 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
7944 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7948 switch ( vertAlign
)
7950 case wxALIGN_BOTTOM
:
7951 if ( textOrientation
== wxHORIZONTAL
)
7952 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7954 x
= rect
.x
+ rect
.width
- textWidth
;
7957 case wxALIGN_CENTRE
:
7958 if ( textOrientation
== wxHORIZONTAL
)
7959 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
7961 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
7966 if ( textOrientation
== wxHORIZONTAL
)
7973 // Align each line of a multi-line label
7974 size_t nLines
= lines
.GetCount();
7975 for ( size_t l
= 0; l
< nLines
; l
++ )
7977 const wxString
& line
= lines
[l
];
7981 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
7987 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
7989 switch ( horizAlign
)
7992 if ( textOrientation
== wxHORIZONTAL
)
7993 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7995 y
= rect
.y
+ lineWidth
+ 1;
7998 case wxALIGN_CENTRE
:
7999 if ( textOrientation
== wxHORIZONTAL
)
8000 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
8002 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
8007 if ( textOrientation
== wxHORIZONTAL
)
8010 y
= rect
.y
+ rect
.height
- 1;
8014 if ( textOrientation
== wxHORIZONTAL
)
8016 dc
.DrawText( line
, x
, y
);
8021 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
8027 // Split multi-line text up into an array of strings.
8028 // Any existing contents of the string array are preserved.
8030 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
8034 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
8035 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
8037 while ( startPos
< (int)tVal
.length() )
8039 pos
= tVal
.Mid(startPos
).Find( eol
);
8044 else if ( pos
== 0 )
8046 lines
.Add( wxEmptyString
);
8050 lines
.Add( value
.Mid(startPos
, pos
) );
8053 startPos
+= pos
+ 1;
8056 if ( startPos
< (int)value
.length() )
8058 lines
.Add( value
.Mid( startPos
) );
8062 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
8063 const wxArrayString
& lines
,
8064 long *width
, long *height
)
8068 long lineW
= 0, lineH
= 0;
8071 for ( i
= 0; i
< lines
.GetCount(); i
++ )
8073 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
8074 w
= wxMax( w
, lineW
);
8083 // ------ Batch processing.
8085 void wxGrid::EndBatch()
8087 if ( m_batchCount
> 0 )
8090 if ( !m_batchCount
)
8093 m_rowLabelWin
->Refresh();
8094 m_colLabelWin
->Refresh();
8095 m_cornerLabelWin
->Refresh();
8096 m_gridWin
->Refresh();
8101 // Use this, rather than wxWindow::Refresh(), to force an immediate
8102 // repainting of the grid. Has no effect if you are already inside a
8103 // BeginBatch / EndBatch block.
8105 void wxGrid::ForceRefresh()
8111 bool wxGrid::Enable(bool enable
)
8113 if ( !wxScrolledWindow::Enable(enable
) )
8116 // redraw in the new state
8117 m_gridWin
->Refresh();
8123 // ------ Edit control functions
8126 void wxGrid::EnableEditing( bool edit
)
8128 // TODO: improve this ?
8130 if ( edit
!= m_editable
)
8133 EnableCellEditControl(edit
);
8138 void wxGrid::EnableCellEditControl( bool enable
)
8143 if ( enable
!= m_cellEditCtrlEnabled
)
8147 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
8150 // this should be checked by the caller!
8151 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8153 // do it before ShowCellEditControl()
8154 m_cellEditCtrlEnabled
= enable
;
8156 ShowCellEditControl();
8160 //FIXME:add veto support
8161 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
8163 HideCellEditControl();
8164 SaveEditControlValue();
8166 // do it after HideCellEditControl()
8167 m_cellEditCtrlEnabled
= enable
;
8172 bool wxGrid::IsCurrentCellReadOnly() const
8175 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
8176 bool readonly
= attr
->IsReadOnly();
8182 bool wxGrid::CanEnableCellControl() const
8184 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
8185 !IsCurrentCellReadOnly();
8188 bool wxGrid::IsCellEditControlEnabled() const
8190 // the cell edit control might be disable for all cells or just for the
8191 // current one if it's read only
8192 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
8195 bool wxGrid::IsCellEditControlShown() const
8197 bool isShown
= false;
8199 if ( m_cellEditCtrlEnabled
)
8201 int row
= m_currentCellCoords
.GetRow();
8202 int col
= m_currentCellCoords
.GetCol();
8203 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8204 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
8209 if ( editor
->IsCreated() )
8211 isShown
= editor
->GetControl()->IsShown();
8221 void wxGrid::ShowCellEditControl()
8223 if ( IsCellEditControlEnabled() )
8225 if ( !IsVisible( m_currentCellCoords
, false ) )
8227 m_cellEditCtrlEnabled
= false;
8232 wxRect rect
= CellToRect( m_currentCellCoords
);
8233 int row
= m_currentCellCoords
.GetRow();
8234 int col
= m_currentCellCoords
.GetCol();
8236 // if this is part of a multicell, find owner (topleft)
8237 int cell_rows
, cell_cols
;
8238 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8239 if ( cell_rows
<= 0 || cell_cols
<= 0 )
8243 m_currentCellCoords
.SetRow( row
);
8244 m_currentCellCoords
.SetCol( col
);
8247 // erase the highlight and the cell contents because the editor
8248 // might not cover the entire cell
8249 wxClientDC
dc( m_gridWin
);
8251 dc
.SetBrush(wxBrush(GetCellAttr(row
, col
)->GetBackgroundColour(), wxSOLID
));
8252 dc
.SetPen(*wxTRANSPARENT_PEN
);
8253 dc
.DrawRectangle(rect
);
8255 // convert to scrolled coords
8256 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8262 // cell is shifted by one pixel
8263 // However, don't allow x or y to become negative
8264 // since the SetSize() method interprets that as
8271 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8272 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8273 if ( !editor
->IsCreated() )
8275 editor
->Create(m_gridWin
, wxID_ANY
,
8276 new wxGridCellEditorEvtHandler(this, editor
));
8278 wxGridEditorCreatedEvent
evt(GetId(),
8279 wxEVT_GRID_EDITOR_CREATED
,
8283 editor
->GetControl());
8284 GetEventHandler()->ProcessEvent(evt
);
8287 // resize editor to overflow into righthand cells if allowed
8288 int maxWidth
= rect
.width
;
8289 wxString value
= GetCellValue(row
, col
);
8290 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
8293 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
8294 if (maxWidth
< rect
.width
)
8295 maxWidth
= rect
.width
;
8298 int client_right
= m_gridWin
->GetClientSize().GetWidth();
8299 if (rect
.x
+ maxWidth
> client_right
)
8300 maxWidth
= client_right
- rect
.x
;
8302 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
8304 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8305 // may have changed earlier
8306 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
8309 GetCellSize( row
, i
, &c_rows
, &c_cols
);
8311 // looks weird going over a multicell
8312 if (m_table
->IsEmptyCell( row
, i
) &&
8313 (rect
.width
< maxWidth
) && (c_rows
== 1))
8315 rect
.width
+= GetColWidth( i
);
8321 if (rect
.GetRight() > client_right
)
8322 rect
.SetRight( client_right
- 1 );
8325 editor
->SetCellAttr( attr
);
8326 editor
->SetSize( rect
);
8328 editor
->GetControl()->Move(
8329 editor
->GetControl()->GetPosition().x
+ nXMove
,
8330 editor
->GetControl()->GetPosition().y
);
8331 editor
->Show( true, attr
);
8333 // recalc dimensions in case we need to
8334 // expand the scrolled window to account for editor
8337 editor
->BeginEdit(row
, col
, this);
8338 editor
->SetCellAttr(NULL
);
8346 void wxGrid::HideCellEditControl()
8348 if ( IsCellEditControlEnabled() )
8350 int row
= m_currentCellCoords
.GetRow();
8351 int col
= m_currentCellCoords
.GetCol();
8353 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8354 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
8355 editor
->Show( false );
8359 m_gridWin
->SetFocus();
8361 // refresh whole row to the right
8362 wxRect
rect( CellToRect(row
, col
) );
8363 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8364 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8367 // ensure that the pixels under the focus ring get refreshed as well
8368 rect
.Inflate(10, 10);
8371 m_gridWin
->Refresh( false, &rect
);
8375 void wxGrid::SaveEditControlValue()
8377 if ( IsCellEditControlEnabled() )
8379 int row
= m_currentCellCoords
.GetRow();
8380 int col
= m_currentCellCoords
.GetCol();
8382 wxString oldval
= GetCellValue(row
, col
);
8384 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8385 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8386 bool changed
= editor
->EndEdit(row
, col
, this);
8393 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
8394 m_currentCellCoords
.GetRow(),
8395 m_currentCellCoords
.GetCol() ) < 0 )
8397 // Event has been vetoed, set the data back.
8398 SetCellValue(row
, col
, oldval
);
8405 // ------ Grid location functions
8406 // Note that all of these functions work with the logical coordinates of
8407 // grid cells and labels so you will need to convert from device
8408 // coordinates for mouse events etc.
8411 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
8413 int row
= YToRow(y
);
8414 int col
= XToCol(x
);
8416 if ( row
== -1 || col
== -1 )
8418 coords
= wxGridNoCellCoords
;
8422 coords
.Set( row
, col
);
8426 // Internal Helper function for computing row or column from some
8427 // (unscrolled) coordinate value, using either
8428 // m_defaultRowHeight/m_defaultColWidth or binary search on array
8429 // of m_rowBottoms/m_ColRights to speed up the search!
8431 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
8432 const wxArrayInt
& BorderArray
, int nMax
,
8436 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
8441 size_t i_max
= coord
/ defaultDist
,
8444 if (BorderArray
.IsEmpty())
8446 if ((int) i_max
< nMax
)
8448 return clipToMinMax
? nMax
- 1 : -1;
8451 if ( i_max
>= BorderArray
.GetCount())
8453 i_max
= BorderArray
.GetCount() - 1;
8457 if ( coord
>= BorderArray
[i_max
])
8461 i_max
= coord
/ minDist
;
8463 i_max
= BorderArray
.GetCount() - 1;
8466 if ( i_max
>= BorderArray
.GetCount())
8467 i_max
= BorderArray
.GetCount() - 1;
8470 if ( coord
>= BorderArray
[i_max
])
8471 return clipToMinMax
? (int)i_max
: -1;
8472 if ( coord
< BorderArray
[0] )
8475 while ( i_max
- i_min
> 0 )
8477 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
8478 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8479 if (coord
>= BorderArray
[ i_max
- 1])
8483 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
8484 if (coord
< BorderArray
[median
])
8493 int wxGrid::YToRow( int y
)
8495 return CoordToRowOrCol(y
, m_defaultRowHeight
,
8496 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8499 int wxGrid::XToCol( int x
, bool clipToMinMax
)
8502 return clipToMinMax
&& (m_numCols
> 0) ? GetColAt( 0 ) : -1;
8504 if (!m_defaultColWidth
)
8505 m_defaultColWidth
= 1;
8507 int maxPos
= x
/ m_defaultColWidth
;
8510 if (m_colRights
.IsEmpty())
8512 if(maxPos
< m_numCols
)
8513 return GetColAt( maxPos
);
8514 return clipToMinMax
? GetColAt( m_numCols
- 1 ) : -1;
8517 if ( maxPos
>= m_numCols
)
8518 maxPos
= m_numCols
- 1;
8521 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8524 if (m_minAcceptableColWidth
)
8525 maxPos
= x
/ m_minAcceptableColWidth
;
8527 maxPos
= m_numCols
- 1;
8529 if ( maxPos
>= m_numCols
)
8530 maxPos
= m_numCols
- 1;
8533 //X is beyond the last column
8534 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8535 return clipToMinMax
? GetColAt( maxPos
) : -1;
8537 //X is before the first column
8538 if ( x
< m_colRights
[GetColAt( 0 )] )
8539 return GetColAt( 0 );
8541 //Perform a binary search
8542 while ( maxPos
- minPos
> 0 )
8544 wxCHECK_MSG(m_colRights
[GetColAt( minPos
)] <= x
&& x
< m_colRights
[GetColAt( maxPos
)],
8545 0, _T("wxGrid: internal error in XToCol"));
8547 if (x
>= m_colRights
[GetColAt( maxPos
- 1 )])
8548 return GetColAt( maxPos
);
8551 int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
8552 if (x
< m_colRights
[GetColAt( median
)])
8557 return GetColAt( maxPos
);
8560 // return the row number that that the y coord is near
8561 // the edge of, or -1 if not near an edge.
8562 // coords can only possibly be near an edge if
8563 // (a) the row/column is large enough to still allow for an "inner" area
8564 // that is _not_ nead the edge (i.e., if the height/width is smaller
8565 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8568 // (b) resizing rows/columns (the thing for which edge detection is
8569 // relevant at all) is enabled.
8571 int wxGrid::YToEdgeOfRow( int y
)
8574 i
= internalYToRow(y
);
8576 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragRowSize() )
8578 // We know that we are in row i, test whether we are
8579 // close enough to lower or upper border, respectively.
8580 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8582 else if ( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8589 // return the col number that that the x coord is near the edge of, or
8590 // -1 if not near an edge
8591 // See comment at YToEdgeOfRow for conditions on edge detection.
8593 int wxGrid::XToEdgeOfCol( int x
)
8596 i
= internalXToCol(x
);
8598 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragColSize() )
8600 // We know that we are in column i; test whether we are
8601 // close enough to right or left border, respectively.
8602 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8604 else if ( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8611 wxRect
wxGrid::CellToRect( int row
, int col
)
8613 wxRect
rect( -1, -1, -1, -1 );
8615 if ( row
>= 0 && row
< m_numRows
&&
8616 col
>= 0 && col
< m_numCols
)
8618 int i
, cell_rows
, cell_cols
;
8619 rect
.width
= rect
.height
= 0;
8620 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8621 // if negative then find multicell owner
8626 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8628 rect
.x
= GetColLeft(col
);
8629 rect
.y
= GetRowTop(row
);
8630 for (i
=col
; i
< col
+ cell_cols
; i
++)
8631 rect
.width
+= GetColWidth(i
);
8632 for (i
=row
; i
< row
+ cell_rows
; i
++)
8633 rect
.height
+= GetRowHeight(i
);
8636 // if grid lines are enabled, then the area of the cell is a bit smaller
8637 if (m_gridLinesEnabled
)
8646 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
8648 // get the cell rectangle in logical coords
8650 wxRect
r( CellToRect( row
, col
) );
8652 // convert to device coords
8654 int left
, top
, right
, bottom
;
8655 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8656 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8658 // check against the client area of the grid window
8660 m_gridWin
->GetClientSize( &cw
, &ch
);
8662 if ( wholeCellVisible
)
8664 // is the cell wholly visible ?
8665 return ( left
>= 0 && right
<= cw
&&
8666 top
>= 0 && bottom
<= ch
);
8670 // is the cell partly visible ?
8672 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8673 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8677 // make the specified cell location visible by doing a minimal amount
8680 void wxGrid::MakeCellVisible( int row
, int col
)
8683 int xpos
= -1, ypos
= -1;
8685 if ( row
>= 0 && row
< m_numRows
&&
8686 col
>= 0 && col
< m_numCols
)
8688 // get the cell rectangle in logical coords
8689 wxRect
r( CellToRect( row
, col
) );
8691 // convert to device coords
8692 int left
, top
, right
, bottom
;
8693 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8694 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8697 m_gridWin
->GetClientSize( &cw
, &ch
);
8703 else if ( bottom
> ch
)
8705 int h
= r
.GetHeight();
8707 for ( i
= row
- 1; i
>= 0; i
-- )
8709 int rowHeight
= GetRowHeight(i
);
8710 if ( h
+ rowHeight
> ch
)
8717 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8718 // have rounding errors (this is important, because if we do,
8719 // we might not scroll at all and some cells won't be redrawn)
8721 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
8722 // so just add a full scroll unit...
8723 ypos
+= m_scrollLineY
;
8726 // special handling for wide cells - show always left part of the cell!
8727 // Otherwise, e.g. when stepping from row to row, it would jump between
8728 // left and right part of the cell on every step!
8730 if ( left
< 0 || (right
- left
) >= cw
)
8734 else if ( right
> cw
)
8736 // position the view so that the cell is on the right
8738 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8739 xpos
= x0
+ (right
- cw
);
8741 // see comment for ypos above
8742 xpos
+= m_scrollLineX
;
8745 if ( xpos
!= -1 || ypos
!= -1 )
8748 xpos
/= m_scrollLineX
;
8750 ypos
/= m_scrollLineY
;
8751 Scroll( xpos
, ypos
);
8758 // ------ Grid cursor movement functions
8761 bool wxGrid::MoveCursorUp( bool expandSelection
)
8763 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8764 m_currentCellCoords
.GetRow() >= 0 )
8766 if ( expandSelection
)
8768 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8769 m_selectingKeyboard
= m_currentCellCoords
;
8770 if ( m_selectingKeyboard
.GetRow() > 0 )
8772 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8773 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8774 m_selectingKeyboard
.GetCol() );
8775 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8778 else if ( m_currentCellCoords
.GetRow() > 0 )
8780 int row
= m_currentCellCoords
.GetRow() - 1;
8781 int col
= m_currentCellCoords
.GetCol();
8783 MakeCellVisible( row
, col
);
8784 SetCurrentCell( row
, col
);
8795 bool wxGrid::MoveCursorDown( bool expandSelection
)
8797 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8798 m_currentCellCoords
.GetRow() < m_numRows
)
8800 if ( expandSelection
)
8802 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8803 m_selectingKeyboard
= m_currentCellCoords
;
8804 if ( m_selectingKeyboard
.GetRow() < m_numRows
- 1 )
8806 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8807 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8808 m_selectingKeyboard
.GetCol() );
8809 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8812 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8814 int row
= m_currentCellCoords
.GetRow() + 1;
8815 int col
= m_currentCellCoords
.GetCol();
8817 MakeCellVisible( row
, col
);
8818 SetCurrentCell( row
, col
);
8829 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8831 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8832 m_currentCellCoords
.GetCol() >= 0 )
8834 if ( expandSelection
)
8836 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8837 m_selectingKeyboard
= m_currentCellCoords
;
8838 if ( m_selectingKeyboard
.GetCol() > 0 )
8840 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8841 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8842 m_selectingKeyboard
.GetCol() );
8843 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8846 else if ( GetColPos( m_currentCellCoords
.GetCol() ) > 0 )
8848 int row
= m_currentCellCoords
.GetRow();
8849 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) - 1 );
8852 MakeCellVisible( row
, col
);
8853 SetCurrentCell( row
, col
);
8864 bool wxGrid::MoveCursorRight( bool expandSelection
)
8866 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8867 m_currentCellCoords
.GetCol() < m_numCols
)
8869 if ( expandSelection
)
8871 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8872 m_selectingKeyboard
= m_currentCellCoords
;
8873 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8875 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8876 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8877 m_selectingKeyboard
.GetCol() );
8878 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8881 else if ( GetColPos( m_currentCellCoords
.GetCol() ) < m_numCols
- 1 )
8883 int row
= m_currentCellCoords
.GetRow();
8884 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) + 1 );
8887 MakeCellVisible( row
, col
);
8888 SetCurrentCell( row
, col
);
8899 bool wxGrid::MovePageUp()
8901 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8904 int row
= m_currentCellCoords
.GetRow();
8908 m_gridWin
->GetClientSize( &cw
, &ch
);
8910 int y
= GetRowTop(row
);
8911 int newRow
= internalYToRow( y
- ch
+ 1 );
8913 if ( newRow
== row
)
8915 // row > 0, so newRow can never be less than 0 here.
8919 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8920 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8928 bool wxGrid::MovePageDown()
8930 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8933 int row
= m_currentCellCoords
.GetRow();
8934 if ( (row
+ 1) < m_numRows
)
8937 m_gridWin
->GetClientSize( &cw
, &ch
);
8939 int y
= GetRowTop(row
);
8940 int newRow
= internalYToRow( y
+ ch
);
8941 if ( newRow
== row
)
8943 // row < m_numRows, so newRow can't overflow here.
8947 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8948 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8956 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8959 m_currentCellCoords
!= wxGridNoCellCoords
&&
8960 m_currentCellCoords
.GetRow() > 0 )
8962 int row
= m_currentCellCoords
.GetRow();
8963 int col
= m_currentCellCoords
.GetCol();
8965 if ( m_table
->IsEmptyCell(row
, col
) )
8967 // starting in an empty cell: find the next block of
8973 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8977 else if ( m_table
->IsEmptyCell(row
- 1, col
) )
8979 // starting at the top of a block: find the next block
8985 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8991 // starting within a block: find the top of the block
8996 if ( m_table
->IsEmptyCell(row
, col
) )
9004 MakeCellVisible( row
, col
);
9005 if ( expandSelection
)
9007 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9008 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9013 SetCurrentCell( row
, col
);
9022 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
9025 m_currentCellCoords
!= wxGridNoCellCoords
&&
9026 m_currentCellCoords
.GetRow() < m_numRows
- 1 )
9028 int row
= m_currentCellCoords
.GetRow();
9029 int col
= m_currentCellCoords
.GetCol();
9031 if ( m_table
->IsEmptyCell(row
, col
) )
9033 // starting in an empty cell: find the next block of
9036 while ( row
< m_numRows
- 1 )
9039 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9043 else if ( m_table
->IsEmptyCell(row
+ 1, col
) )
9045 // starting at the bottom of a block: find the next block
9048 while ( row
< m_numRows
- 1 )
9051 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9057 // starting within a block: find the bottom of the block
9059 while ( row
< m_numRows
- 1 )
9062 if ( m_table
->IsEmptyCell(row
, col
) )
9070 MakeCellVisible( row
, col
);
9071 if ( expandSelection
)
9073 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9074 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9079 SetCurrentCell( row
, col
);
9088 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
9091 m_currentCellCoords
!= wxGridNoCellCoords
&&
9092 m_currentCellCoords
.GetCol() > 0 )
9094 int row
= m_currentCellCoords
.GetRow();
9095 int col
= m_currentCellCoords
.GetCol();
9097 if ( m_table
->IsEmptyCell(row
, col
) )
9099 // starting in an empty cell: find the next block of
9105 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9109 else if ( m_table
->IsEmptyCell(row
, col
- 1) )
9111 // starting at the left of a block: find the next block
9117 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9123 // starting within a block: find the left of the block
9128 if ( m_table
->IsEmptyCell(row
, col
) )
9136 MakeCellVisible( row
, col
);
9137 if ( expandSelection
)
9139 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9140 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9145 SetCurrentCell( row
, col
);
9154 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
9157 m_currentCellCoords
!= wxGridNoCellCoords
&&
9158 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
9160 int row
= m_currentCellCoords
.GetRow();
9161 int col
= m_currentCellCoords
.GetCol();
9163 if ( m_table
->IsEmptyCell(row
, col
) )
9165 // starting in an empty cell: find the next block of
9168 while ( col
< m_numCols
- 1 )
9171 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9175 else if ( m_table
->IsEmptyCell(row
, col
+ 1) )
9177 // starting at the right of a block: find the next block
9180 while ( col
< m_numCols
- 1 )
9183 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9189 // starting within a block: find the right of the block
9191 while ( col
< m_numCols
- 1 )
9194 if ( m_table
->IsEmptyCell(row
, col
) )
9202 MakeCellVisible( row
, col
);
9203 if ( expandSelection
)
9205 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9206 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9211 SetCurrentCell( row
, col
);
9221 // ------ Label values and formatting
9224 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
9227 *horiz
= m_rowLabelHorizAlign
;
9229 *vert
= m_rowLabelVertAlign
;
9232 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
9235 *horiz
= m_colLabelHorizAlign
;
9237 *vert
= m_colLabelVertAlign
;
9240 int wxGrid::GetColLabelTextOrientation()
9242 return m_colLabelTextOrientation
;
9245 wxString
wxGrid::GetRowLabelValue( int row
)
9249 return m_table
->GetRowLabelValue( row
);
9259 wxString
wxGrid::GetColLabelValue( int col
)
9263 return m_table
->GetColLabelValue( col
);
9273 void wxGrid::SetRowLabelSize( int width
)
9275 width
= wxMax( width
, 0 );
9276 if ( width
!= m_rowLabelWidth
)
9280 m_rowLabelWin
->Show( false );
9281 m_cornerLabelWin
->Show( false );
9283 else if ( m_rowLabelWidth
== 0 )
9285 m_rowLabelWin
->Show( true );
9286 if ( m_colLabelHeight
> 0 )
9287 m_cornerLabelWin
->Show( true );
9290 m_rowLabelWidth
= width
;
9292 wxScrolledWindow::Refresh( true );
9296 void wxGrid::SetColLabelSize( int height
)
9298 height
= wxMax( height
, 0 );
9299 if ( height
!= m_colLabelHeight
)
9303 m_colLabelWin
->Show( false );
9304 m_cornerLabelWin
->Show( false );
9306 else if ( m_colLabelHeight
== 0 )
9308 m_colLabelWin
->Show( true );
9309 if ( m_rowLabelWidth
> 0 )
9310 m_cornerLabelWin
->Show( true );
9313 m_colLabelHeight
= height
;
9315 wxScrolledWindow::Refresh( true );
9319 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
9321 if ( m_labelBackgroundColour
!= colour
)
9323 m_labelBackgroundColour
= colour
;
9324 m_rowLabelWin
->SetBackgroundColour( colour
);
9325 m_colLabelWin
->SetBackgroundColour( colour
);
9326 m_cornerLabelWin
->SetBackgroundColour( colour
);
9328 if ( !GetBatchCount() )
9330 m_rowLabelWin
->Refresh();
9331 m_colLabelWin
->Refresh();
9332 m_cornerLabelWin
->Refresh();
9337 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
9339 if ( m_labelTextColour
!= colour
)
9341 m_labelTextColour
= colour
;
9342 if ( !GetBatchCount() )
9344 m_rowLabelWin
->Refresh();
9345 m_colLabelWin
->Refresh();
9350 void wxGrid::SetLabelFont( const wxFont
& font
)
9353 if ( !GetBatchCount() )
9355 m_rowLabelWin
->Refresh();
9356 m_colLabelWin
->Refresh();
9360 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
9362 // allow old (incorrect) defs to be used
9365 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9366 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9367 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9372 case wxTOP
: vert
= wxALIGN_TOP
; break;
9373 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9374 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9377 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9379 m_rowLabelHorizAlign
= horiz
;
9382 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9384 m_rowLabelVertAlign
= vert
;
9387 if ( !GetBatchCount() )
9389 m_rowLabelWin
->Refresh();
9393 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
9395 // allow old (incorrect) defs to be used
9398 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9399 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9400 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9405 case wxTOP
: vert
= wxALIGN_TOP
; break;
9406 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9407 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9410 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9412 m_colLabelHorizAlign
= horiz
;
9415 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9417 m_colLabelVertAlign
= vert
;
9420 if ( !GetBatchCount() )
9422 m_colLabelWin
->Refresh();
9426 // Note: under MSW, the default column label font must be changed because it
9427 // does not support vertical printing
9429 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9430 // pGrid->SetLabelFont(font);
9431 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9433 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9435 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9436 m_colLabelTextOrientation
= textOrientation
;
9438 if ( !GetBatchCount() )
9439 m_colLabelWin
->Refresh();
9442 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9446 m_table
->SetRowLabelValue( row
, s
);
9447 if ( !GetBatchCount() )
9449 wxRect rect
= CellToRect( row
, 0 );
9450 if ( rect
.height
> 0 )
9452 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9454 rect
.width
= m_rowLabelWidth
;
9455 m_rowLabelWin
->Refresh( true, &rect
);
9461 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9465 m_table
->SetColLabelValue( col
, s
);
9466 if ( !GetBatchCount() )
9468 wxRect rect
= CellToRect( 0, col
);
9469 if ( rect
.width
> 0 )
9471 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9473 rect
.height
= m_colLabelHeight
;
9474 m_colLabelWin
->Refresh( true, &rect
);
9480 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9482 if ( m_gridLineColour
!= colour
)
9484 m_gridLineColour
= colour
;
9486 wxClientDC
dc( m_gridWin
);
9488 DrawAllGridLines( dc
, wxRegion() );
9492 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9494 if ( m_cellHighlightColour
!= colour
)
9496 m_cellHighlightColour
= colour
;
9498 wxClientDC
dc( m_gridWin
);
9500 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9501 DrawCellHighlight(dc
, attr
);
9506 void wxGrid::SetCellHighlightPenWidth(int width
)
9508 if (m_cellHighlightPenWidth
!= width
)
9510 m_cellHighlightPenWidth
= width
;
9512 // Just redrawing the cell highlight is not enough since that won't
9513 // make any visible change if the the thickness is getting smaller.
9514 int row
= m_currentCellCoords
.GetRow();
9515 int col
= m_currentCellCoords
.GetCol();
9516 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9519 wxRect rect
= CellToRect(row
, col
);
9520 m_gridWin
->Refresh(true, &rect
);
9524 void wxGrid::SetCellHighlightROPenWidth(int width
)
9526 if (m_cellHighlightROPenWidth
!= width
)
9528 m_cellHighlightROPenWidth
= width
;
9530 // Just redrawing the cell highlight is not enough since that won't
9531 // make any visible change if the the thickness is getting smaller.
9532 int row
= m_currentCellCoords
.GetRow();
9533 int col
= m_currentCellCoords
.GetCol();
9534 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9537 wxRect rect
= CellToRect(row
, col
);
9538 m_gridWin
->Refresh(true, &rect
);
9542 void wxGrid::EnableGridLines( bool enable
)
9544 if ( enable
!= m_gridLinesEnabled
)
9546 m_gridLinesEnabled
= enable
;
9548 if ( !GetBatchCount() )
9552 wxClientDC
dc( m_gridWin
);
9554 DrawAllGridLines( dc
, wxRegion() );
9558 m_gridWin
->Refresh();
9564 int wxGrid::GetDefaultRowSize()
9566 return m_defaultRowHeight
;
9569 int wxGrid::GetRowSize( int row
)
9571 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9573 return GetRowHeight(row
);
9576 int wxGrid::GetDefaultColSize()
9578 return m_defaultColWidth
;
9581 int wxGrid::GetColSize( int col
)
9583 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9585 return GetColWidth(col
);
9588 // ============================================================================
9589 // access to the grid attributes: each of them has a default value in the grid
9590 // itself and may be overidden on a per-cell basis
9591 // ============================================================================
9593 // ----------------------------------------------------------------------------
9594 // setting default attributes
9595 // ----------------------------------------------------------------------------
9597 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9599 m_defaultCellAttr
->SetBackgroundColour(col
);
9601 m_gridWin
->SetBackgroundColour(col
);
9605 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9607 m_defaultCellAttr
->SetTextColour(col
);
9610 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9612 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9615 void wxGrid::SetDefaultCellOverflow( bool allow
)
9617 m_defaultCellAttr
->SetOverflow(allow
);
9620 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9622 m_defaultCellAttr
->SetFont(font
);
9625 // For editors and renderers the type registry takes precedence over the
9626 // default attr, so we need to register the new editor/renderer for the string
9627 // data type in order to make setting a default editor/renderer appear to
9630 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9632 RegisterDataType(wxGRID_VALUE_STRING
,
9634 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9637 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9639 RegisterDataType(wxGRID_VALUE_STRING
,
9640 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9644 // ----------------------------------------------------------------------------
9645 // access to the default attrbiutes
9646 // ----------------------------------------------------------------------------
9648 wxColour
wxGrid::GetDefaultCellBackgroundColour()
9650 return m_defaultCellAttr
->GetBackgroundColour();
9653 wxColour
wxGrid::GetDefaultCellTextColour()
9655 return m_defaultCellAttr
->GetTextColour();
9658 wxFont
wxGrid::GetDefaultCellFont()
9660 return m_defaultCellAttr
->GetFont();
9663 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
9665 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9668 bool wxGrid::GetDefaultCellOverflow()
9670 return m_defaultCellAttr
->GetOverflow();
9673 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9675 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9678 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9680 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9683 // ----------------------------------------------------------------------------
9684 // access to cell attributes
9685 // ----------------------------------------------------------------------------
9687 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
9689 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9690 wxColour colour
= attr
->GetBackgroundColour();
9696 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
9698 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9699 wxColour colour
= attr
->GetTextColour();
9705 wxFont
wxGrid::GetCellFont( int row
, int col
)
9707 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9708 wxFont font
= attr
->GetFont();
9714 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
9716 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9717 attr
->GetAlignment(horiz
, vert
);
9721 bool wxGrid::GetCellOverflow( int row
, int col
)
9723 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9724 bool allow
= attr
->GetOverflow();
9730 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
9732 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9733 attr
->GetSize( num_rows
, num_cols
);
9737 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
9739 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9740 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9746 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
9748 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9749 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9755 bool wxGrid::IsReadOnly(int row
, int col
) const
9757 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9758 bool isReadOnly
= attr
->IsReadOnly();
9764 // ----------------------------------------------------------------------------
9765 // attribute support: cache, automatic provider creation, ...
9766 // ----------------------------------------------------------------------------
9768 bool wxGrid::CanHaveAttributes()
9775 return m_table
->CanHaveAttributes();
9778 void wxGrid::ClearAttrCache()
9780 if ( m_attrCache
.row
!= -1 )
9782 wxSafeDecRef(m_attrCache
.attr
);
9783 m_attrCache
.attr
= NULL
;
9784 m_attrCache
.row
= -1;
9788 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9792 wxGrid
*self
= (wxGrid
*)this; // const_cast
9794 self
->ClearAttrCache();
9795 self
->m_attrCache
.row
= row
;
9796 self
->m_attrCache
.col
= col
;
9797 self
->m_attrCache
.attr
= attr
;
9802 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9804 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9806 *attr
= m_attrCache
.attr
;
9807 wxSafeIncRef(m_attrCache
.attr
);
9809 #ifdef DEBUG_ATTR_CACHE
9810 gs_nAttrCacheHits
++;
9817 #ifdef DEBUG_ATTR_CACHE
9818 gs_nAttrCacheMisses
++;
9825 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9827 wxGridCellAttr
*attr
= NULL
;
9828 // Additional test to avoid looking at the cache e.g. for
9829 // wxNoCellCoords, as this will confuse memory management.
9832 if ( !LookupAttr(row
, col
, &attr
) )
9834 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9835 : (wxGridCellAttr
*)NULL
;
9836 CacheAttr(row
, col
, attr
);
9842 attr
->SetDefAttr(m_defaultCellAttr
);
9846 attr
= m_defaultCellAttr
;
9853 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9855 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9856 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9858 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9859 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9861 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9864 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9866 // artificially inc the ref count to match DecRef() in caller
9868 m_table
->SetAttr(attr
, row
, col
);
9874 // ----------------------------------------------------------------------------
9875 // setting column attributes (wrappers around SetColAttr)
9876 // ----------------------------------------------------------------------------
9878 void wxGrid::SetColFormatBool(int col
)
9880 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9883 void wxGrid::SetColFormatNumber(int col
)
9885 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9888 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9890 wxString typeName
= wxGRID_VALUE_FLOAT
;
9891 if ( (width
!= -1) || (precision
!= -1) )
9893 typeName
<< _T(':') << width
<< _T(',') << precision
;
9896 SetColFormatCustom(col
, typeName
);
9899 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9901 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9903 attr
= new wxGridCellAttr
;
9904 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9905 attr
->SetRenderer(renderer
);
9907 SetColAttr(col
, attr
);
9911 // ----------------------------------------------------------------------------
9912 // setting cell attributes: this is forwarded to the table
9913 // ----------------------------------------------------------------------------
9915 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9917 if ( CanHaveAttributes() )
9919 m_table
->SetAttr(attr
, row
, col
);
9928 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9930 if ( CanHaveAttributes() )
9932 m_table
->SetRowAttr(attr
, row
);
9941 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9943 if ( CanHaveAttributes() )
9945 m_table
->SetColAttr(attr
, col
);
9954 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9956 if ( CanHaveAttributes() )
9958 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9959 attr
->SetBackgroundColour(colour
);
9964 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9966 if ( CanHaveAttributes() )
9968 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9969 attr
->SetTextColour(colour
);
9974 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9976 if ( CanHaveAttributes() )
9978 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9979 attr
->SetFont(font
);
9984 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9986 if ( CanHaveAttributes() )
9988 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9989 attr
->SetAlignment(horiz
, vert
);
9994 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9996 if ( CanHaveAttributes() )
9998 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9999 attr
->SetOverflow(allow
);
10004 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
10006 if ( CanHaveAttributes() )
10008 int cell_rows
, cell_cols
;
10010 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10011 attr
->GetSize(&cell_rows
, &cell_cols
);
10012 attr
->SetSize(num_rows
, num_cols
);
10015 // Cannot set the size of a cell to 0 or negative values
10016 // While it is perfectly legal to do that, this function cannot
10017 // handle all the possibilies, do it by hand by getting the CellAttr.
10018 // You can only set the size of a cell to 1,1 or greater with this fn
10019 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
10020 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10021 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
10022 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10024 // if this was already a multicell then "turn off" the other cells first
10025 if ((cell_rows
> 1) || (cell_rows
> 1))
10028 for (j
=row
; j
< row
+ cell_rows
; j
++)
10030 for (i
=col
; i
< col
+ cell_cols
; i
++)
10032 if ((i
!= col
) || (j
!= row
))
10034 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10035 attr_stub
->SetSize( 1, 1 );
10036 attr_stub
->DecRef();
10042 // mark the cells that will be covered by this cell to
10043 // negative or zero values to point back at this cell
10044 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
10047 for (j
=row
; j
< row
+ num_rows
; j
++)
10049 for (i
=col
; i
< col
+ num_cols
; i
++)
10051 if ((i
!= col
) || (j
!= row
))
10053 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10054 attr_stub
->SetSize( row
- j
, col
- i
);
10055 attr_stub
->DecRef();
10063 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
10065 if ( CanHaveAttributes() )
10067 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10068 attr
->SetRenderer(renderer
);
10073 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
10075 if ( CanHaveAttributes() )
10077 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10078 attr
->SetEditor(editor
);
10083 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
10085 if ( CanHaveAttributes() )
10087 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10088 attr
->SetReadOnly(isReadOnly
);
10093 // ----------------------------------------------------------------------------
10094 // Data type registration
10095 // ----------------------------------------------------------------------------
10097 void wxGrid::RegisterDataType(const wxString
& typeName
,
10098 wxGridCellRenderer
* renderer
,
10099 wxGridCellEditor
* editor
)
10101 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
10105 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
10107 wxString typeName
= m_table
->GetTypeName(row
, col
);
10108 return GetDefaultEditorForType(typeName
);
10111 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
10113 wxString typeName
= m_table
->GetTypeName(row
, col
);
10114 return GetDefaultRendererForType(typeName
);
10117 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
10119 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10120 if ( index
== wxNOT_FOUND
)
10124 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
10125 wxFAIL_MSG(errStr
.c_str());
10130 return m_typeRegistry
->GetEditor(index
);
10133 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
10135 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10136 if ( index
== wxNOT_FOUND
)
10140 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
10141 wxFAIL_MSG(errStr
.c_str());
10146 return m_typeRegistry
->GetRenderer(index
);
10149 // ----------------------------------------------------------------------------
10151 // ----------------------------------------------------------------------------
10153 void wxGrid::EnableDragRowSize( bool enable
)
10155 m_canDragRowSize
= enable
;
10158 void wxGrid::EnableDragColSize( bool enable
)
10160 m_canDragColSize
= enable
;
10163 void wxGrid::EnableDragGridSize( bool enable
)
10165 m_canDragGridSize
= enable
;
10168 void wxGrid::EnableDragCell( bool enable
)
10170 m_canDragCell
= enable
;
10173 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
10175 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
10177 if ( resizeExistingRows
)
10179 // since we are resizing all rows to the default row size,
10180 // we can simply clear the row heights and row bottoms
10181 // arrays (which also allows us to take advantage of
10182 // some speed optimisations)
10183 m_rowHeights
.Empty();
10184 m_rowBottoms
.Empty();
10185 if ( !GetBatchCount() )
10190 void wxGrid::SetRowSize( int row
, int height
)
10192 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
10194 // See comment in SetColSize
10195 if ( height
< GetRowMinimalAcceptableHeight())
10198 if ( m_rowHeights
.IsEmpty() )
10200 // need to really create the array
10204 int h
= wxMax( 0, height
);
10205 int diff
= h
- m_rowHeights
[row
];
10207 m_rowHeights
[row
] = h
;
10209 for ( i
= row
; i
< m_numRows
; i
++ )
10211 m_rowBottoms
[i
] += diff
;
10214 if ( !GetBatchCount() )
10218 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
10220 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
10222 if ( resizeExistingCols
)
10224 // since we are resizing all columns to the default column size,
10225 // we can simply clear the col widths and col rights
10226 // arrays (which also allows us to take advantage of
10227 // some speed optimisations)
10228 m_colWidths
.Empty();
10229 m_colRights
.Empty();
10230 if ( !GetBatchCount() )
10235 void wxGrid::SetColSize( int col
, int width
)
10237 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
10239 // should we check that it's bigger than GetColMinimalWidth(col) here?
10241 // No, because it is reasonable to assume the library user know's
10242 // what he is doing. However we should test against the weaker
10243 // constraint of minimalAcceptableWidth, as this breaks rendering
10245 // This test then fixes sf.net bug #645734
10247 if ( width
< GetColMinimalAcceptableWidth() )
10250 if ( m_colWidths
.IsEmpty() )
10252 // need to really create the array
10256 // if < 0 then calculate new width from label
10260 wxArrayString lines
;
10261 wxClientDC
dc(m_colLabelWin
);
10262 dc
.SetFont(GetLabelFont());
10263 StringToLines(GetColLabelValue(col
), lines
);
10264 GetTextBoxSize(dc
, lines
, &w
, &h
);
10268 int w
= wxMax( 0, width
);
10269 int diff
= w
- m_colWidths
[col
];
10270 m_colWidths
[col
] = w
;
10274 for ( colPos
= GetColPos( col
); colPos
< m_numCols
; colPos
++ )
10276 i
= GetColAt( colPos
);
10277 m_colRights
[i
] += diff
;
10280 if ( !GetBatchCount() )
10284 void wxGrid::SetColMinimalWidth( int col
, int width
)
10286 if (width
> GetColMinimalAcceptableWidth())
10288 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10289 m_colMinWidths
[key
] = width
;
10293 void wxGrid::SetRowMinimalHeight( int row
, int width
)
10295 if (width
> GetRowMinimalAcceptableHeight())
10297 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10298 m_rowMinHeights
[key
] = width
;
10302 int wxGrid::GetColMinimalWidth(int col
) const
10304 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10305 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
10307 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
10310 int wxGrid::GetRowMinimalHeight(int row
) const
10312 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10313 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
10315 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
10318 void wxGrid::SetColMinimalAcceptableWidth( int width
)
10320 // We do allow a width of 0 since this gives us
10321 // an easy way to temporarily hiding columns.
10323 m_minAcceptableColWidth
= width
;
10326 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
10328 // We do allow a height of 0 since this gives us
10329 // an easy way to temporarily hiding rows.
10331 m_minAcceptableRowHeight
= height
;
10334 int wxGrid::GetColMinimalAcceptableWidth() const
10336 return m_minAcceptableColWidth
;
10339 int wxGrid::GetRowMinimalAcceptableHeight() const
10341 return m_minAcceptableRowHeight
;
10344 // ----------------------------------------------------------------------------
10346 // ----------------------------------------------------------------------------
10348 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
10350 wxClientDC
dc(m_gridWin
);
10352 // cancel editing of cell
10353 HideCellEditControl();
10354 SaveEditControlValue();
10356 // init both of them to avoid compiler warnings, even if we only need one
10364 wxCoord extent
, extentMax
= 0;
10365 int max
= column
? m_numRows
: m_numCols
;
10366 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
10373 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
10374 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
10377 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
10378 extent
= column
? size
.x
: size
.y
;
10379 if ( extent
> extentMax
)
10380 extentMax
= extent
;
10382 renderer
->DecRef();
10388 // now also compare with the column label extent
10390 dc
.SetFont( GetLabelFont() );
10394 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
10395 if ( GetColLabelTextOrientation() == wxVERTICAL
)
10399 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
10401 extent
= column
? w
: h
;
10402 if ( extent
> extentMax
)
10403 extentMax
= extent
;
10407 // empty column - give default extent (notice that if extentMax is less
10408 // than default extent but != 0, it's OK)
10409 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
10414 // leave some space around text
10422 SetColSize( col
, extentMax
);
10423 if ( !GetBatchCount() )
10426 m_gridWin
->GetClientSize( &cw
, &ch
);
10427 wxRect
rect ( CellToRect( 0, col
) );
10429 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10430 rect
.width
= cw
- rect
.x
;
10431 rect
.height
= m_colLabelHeight
;
10432 m_colLabelWin
->Refresh( true, &rect
);
10437 SetRowSize(row
, extentMax
);
10438 if ( !GetBatchCount() )
10441 m_gridWin
->GetClientSize( &cw
, &ch
);
10442 wxRect
rect( CellToRect( row
, 0 ) );
10444 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10445 rect
.width
= m_rowLabelWidth
;
10446 rect
.height
= ch
- rect
.y
;
10447 m_rowLabelWin
->Refresh( true, &rect
);
10454 SetColMinimalWidth(col
, extentMax
);
10456 SetRowMinimalHeight(row
, extentMax
);
10460 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10462 int width
= m_rowLabelWidth
;
10467 for ( int col
= 0; col
< m_numCols
; col
++ )
10470 AutoSizeColumn(col
, setAsMin
);
10472 width
+= GetColWidth(col
);
10481 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10483 int height
= m_colLabelHeight
;
10488 for ( int row
= 0; row
< m_numRows
; row
++ )
10491 AutoSizeRow(row
, setAsMin
);
10493 height
+= GetRowHeight(row
);
10502 void wxGrid::AutoSize()
10506 wxSize
size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
10508 // round up the size to a multiple of scroll step - this ensures that we
10509 // won't get the scrollbars if we're sized exactly to this width
10510 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
10513 GetScrollX(size
.x
+ m_extraWidth
+ 1) * m_scrollLineX
,
10514 GetScrollY(size
.y
+ m_extraHeight
+ 1) * m_scrollLineY
);
10516 // distribute the extra space between the columns/rows to avoid having
10517 // extra white space
10519 // Remove the extra m_extraWidth + 1 added above
10520 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
10521 if ( diff
&& m_numCols
)
10523 // try to resize the columns uniformly
10524 wxCoord diffPerCol
= diff
/ m_numCols
;
10527 for ( int col
= 0; col
< m_numCols
; col
++ )
10529 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
10533 // add remaining amount to the last columns
10534 diff
-= diffPerCol
* m_numCols
;
10537 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
10539 SetColSize(col
, GetColWidth(col
) + 1);
10545 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
10546 if ( diff
&& m_numRows
)
10548 // try to resize the columns uniformly
10549 wxCoord diffPerRow
= diff
/ m_numRows
;
10552 for ( int row
= 0; row
< m_numRows
; row
++ )
10554 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
10558 // add remaining amount to the last rows
10559 diff
-= diffPerRow
* m_numRows
;
10562 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
10564 SetRowSize(row
, GetRowHeight(row
) + 1);
10571 SetClientSize(sizeFit
);
10574 void wxGrid::AutoSizeRowLabelSize( int row
)
10576 wxArrayString lines
;
10579 // Hide the edit control, so it
10580 // won't interfere with drag-shrinking.
10581 if ( IsCellEditControlShown() )
10583 HideCellEditControl();
10584 SaveEditControlValue();
10587 // autosize row height depending on label text
10588 StringToLines( GetRowLabelValue( row
), lines
);
10589 wxClientDC
dc( m_rowLabelWin
);
10590 GetTextBoxSize( dc
, lines
, &w
, &h
);
10591 if ( h
< m_defaultRowHeight
)
10592 h
= m_defaultRowHeight
;
10593 SetRowSize(row
, h
);
10597 void wxGrid::AutoSizeColLabelSize( int col
)
10599 wxArrayString lines
;
10602 // Hide the edit control, so it
10603 // won't interfere with drag-shrinking.
10604 if ( IsCellEditControlShown() )
10606 HideCellEditControl();
10607 SaveEditControlValue();
10610 // autosize column width depending on label text
10611 StringToLines( GetColLabelValue( col
), lines
);
10612 wxClientDC
dc( m_colLabelWin
);
10613 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10614 GetTextBoxSize( dc
, lines
, &w
, &h
);
10616 GetTextBoxSize( dc
, lines
, &h
, &w
);
10617 if ( w
< m_defaultColWidth
)
10618 w
= m_defaultColWidth
;
10619 SetColSize(col
, w
);
10623 wxSize
wxGrid::DoGetBestSize() const
10625 // don't set sizes, only calculate them
10626 wxGrid
*self
= (wxGrid
*)this; // const_cast
10629 width
= self
->SetOrCalcColumnSizes(true);
10630 height
= self
->SetOrCalcRowSizes(true);
10637 // Round up to a multiple the scroll rate
10638 // NOTE: this still doesn't get rid of the scrollbars;
10639 // is there any magic incantation for that?
10641 GetScrollPixelsPerUnit(&xpu
, &ypu
);
10643 width
+= 1 + xpu
- (width
% xpu
);
10645 height
+= 1 + ypu
- (height
% ypu
);
10647 // limit to 1/4 of the screen size
10648 int maxwidth
, maxheight
;
10649 wxDisplaySize( &maxwidth
, &maxheight
);
10652 if ( width
> maxwidth
)
10654 if ( height
> maxheight
)
10655 height
= maxheight
;
10657 wxSize
best(width
, height
);
10659 // NOTE: This size should be cached, but first we need to add calls to
10660 // InvalidateBestSize everywhere that could change the results of this
10662 // CacheBestSize(size);
10672 wxPen
& wxGrid::GetDividerPen() const
10677 // ----------------------------------------------------------------------------
10678 // cell value accessor functions
10679 // ----------------------------------------------------------------------------
10681 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10685 m_table
->SetValue( row
, col
, s
);
10686 if ( !GetBatchCount() )
10689 wxRect
rect( CellToRect( row
, col
) );
10691 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10692 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10693 m_gridWin
->Refresh( false, &rect
);
10696 if ( m_currentCellCoords
.GetRow() == row
&&
10697 m_currentCellCoords
.GetCol() == col
&&
10698 IsCellEditControlShown())
10699 // Note: If we are using IsCellEditControlEnabled,
10700 // this interacts badly with calling SetCellValue from
10701 // an EVT_GRID_CELL_CHANGE handler.
10703 HideCellEditControl();
10704 ShowCellEditControl(); // will reread data from table
10709 // ----------------------------------------------------------------------------
10710 // block, row and column selection
10711 // ----------------------------------------------------------------------------
10713 void wxGrid::SelectRow( int row
, bool addToSelected
)
10715 if ( IsSelection() && !addToSelected
)
10719 m_selection
->SelectRow( row
, false, addToSelected
);
10722 void wxGrid::SelectCol( int col
, bool addToSelected
)
10724 if ( IsSelection() && !addToSelected
)
10728 m_selection
->SelectCol( col
, false, addToSelected
);
10731 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10732 bool addToSelected
)
10734 if ( IsSelection() && !addToSelected
)
10738 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10739 false, addToSelected
);
10742 void wxGrid::SelectAll()
10744 if ( m_numRows
> 0 && m_numCols
> 0 )
10747 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10751 // ----------------------------------------------------------------------------
10752 // cell, row and col deselection
10753 // ----------------------------------------------------------------------------
10755 void wxGrid::DeselectRow( int row
)
10757 if ( !m_selection
)
10760 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10762 if ( m_selection
->IsInSelection(row
, 0 ) )
10763 m_selection
->ToggleCellSelection(row
, 0);
10767 int nCols
= GetNumberCols();
10768 for ( int i
= 0; i
< nCols
; i
++ )
10770 if ( m_selection
->IsInSelection(row
, i
) )
10771 m_selection
->ToggleCellSelection(row
, i
);
10776 void wxGrid::DeselectCol( int col
)
10778 if ( !m_selection
)
10781 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10783 if ( m_selection
->IsInSelection(0, col
) )
10784 m_selection
->ToggleCellSelection(0, col
);
10788 int nRows
= GetNumberRows();
10789 for ( int i
= 0; i
< nRows
; i
++ )
10791 if ( m_selection
->IsInSelection(i
, col
) )
10792 m_selection
->ToggleCellSelection(i
, col
);
10797 void wxGrid::DeselectCell( int row
, int col
)
10799 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10800 m_selection
->ToggleCellSelection(row
, col
);
10803 bool wxGrid::IsSelection()
10805 return ( m_selection
&& (m_selection
->IsSelection() ||
10806 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10807 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10810 bool wxGrid::IsInSelection( int row
, int col
) const
10812 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10813 ( row
>= m_selectingTopLeft
.GetRow() &&
10814 col
>= m_selectingTopLeft
.GetCol() &&
10815 row
<= m_selectingBottomRight
.GetRow() &&
10816 col
<= m_selectingBottomRight
.GetCol() )) );
10819 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10823 wxGridCellCoordsArray a
;
10827 return m_selection
->m_cellSelection
;
10830 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10834 wxGridCellCoordsArray a
;
10838 return m_selection
->m_blockSelectionTopLeft
;
10841 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10845 wxGridCellCoordsArray a
;
10849 return m_selection
->m_blockSelectionBottomRight
;
10852 wxArrayInt
wxGrid::GetSelectedRows() const
10860 return m_selection
->m_rowSelection
;
10863 wxArrayInt
wxGrid::GetSelectedCols() const
10871 return m_selection
->m_colSelection
;
10874 void wxGrid::ClearSelection()
10876 m_selectingTopLeft
=
10877 m_selectingBottomRight
=
10878 m_selectingKeyboard
= wxGridNoCellCoords
;
10880 m_selection
->ClearSelection();
10883 // This function returns the rectangle that encloses the given block
10884 // in device coords clipped to the client size of the grid window.
10886 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10887 const wxGridCellCoords
&bottomRight
)
10889 wxRect
rect( wxGridNoCellRect
);
10892 cellRect
= CellToRect( topLeft
);
10893 if ( cellRect
!= wxGridNoCellRect
)
10899 rect
= wxRect(0, 0, 0, 0);
10902 cellRect
= CellToRect( bottomRight
);
10903 if ( cellRect
!= wxGridNoCellRect
)
10909 return wxGridNoCellRect
;
10913 int left
= rect
.GetLeft();
10914 int top
= rect
.GetTop();
10915 int right
= rect
.GetRight();
10916 int bottom
= rect
.GetBottom();
10918 int leftCol
= topLeft
.GetCol();
10919 int topRow
= topLeft
.GetRow();
10920 int rightCol
= bottomRight
.GetCol();
10921 int bottomRow
= bottomRight
.GetRow();
10929 leftCol
= rightCol
;
10939 topRow
= bottomRow
;
10943 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10945 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10947 if ((j
== topRow
) || (j
== bottomRow
) || (i
== leftCol
) || (i
== rightCol
))
10949 cellRect
= CellToRect( j
, i
);
10951 if (cellRect
.x
< left
)
10953 if (cellRect
.y
< top
)
10955 if (cellRect
.x
+ cellRect
.width
> right
)
10956 right
= cellRect
.x
+ cellRect
.width
;
10957 if (cellRect
.y
+ cellRect
.height
> bottom
)
10958 bottom
= cellRect
.y
+ cellRect
.height
;
10962 i
= rightCol
; // jump over inner cells.
10967 // convert to scrolled coords
10969 CalcScrolledPosition( left
, top
, &left
, &top
);
10970 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10973 m_gridWin
->GetClientSize( &cw
, &ch
);
10975 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10976 return wxRect(0,0,0,0);
10978 rect
.SetLeft( wxMax(0, left
) );
10979 rect
.SetTop( wxMax(0, top
) );
10980 rect
.SetRight( wxMin(cw
, right
) );
10981 rect
.SetBottom( wxMin(ch
, bottom
) );
10986 // ----------------------------------------------------------------------------
10987 // grid event classes
10988 // ----------------------------------------------------------------------------
10990 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10992 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10993 int row
, int col
, int x
, int y
, bool sel
,
10994 bool control
, bool shift
, bool alt
, bool meta
)
10995 : wxNotifyEvent( type
, id
)
11002 m_control
= control
;
11007 SetEventObject(obj
);
11011 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
11013 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
11014 int rowOrCol
, int x
, int y
,
11015 bool control
, bool shift
, bool alt
, bool meta
)
11016 : wxNotifyEvent( type
, id
)
11018 m_rowOrCol
= rowOrCol
;
11021 m_control
= control
;
11026 SetEventObject(obj
);
11030 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
11032 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
11033 const wxGridCellCoords
& topLeft
,
11034 const wxGridCellCoords
& bottomRight
,
11035 bool sel
, bool control
,
11036 bool shift
, bool alt
, bool meta
)
11037 : wxNotifyEvent( type
, id
)
11039 m_topLeft
= topLeft
;
11040 m_bottomRight
= bottomRight
;
11042 m_control
= control
;
11047 SetEventObject(obj
);
11051 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
11053 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
11054 wxObject
* obj
, int row
,
11055 int col
, wxControl
* ctrl
)
11056 : wxCommandEvent(type
, id
)
11058 SetEventObject(obj
);
11064 #endif // wxUSE_GRID