1 /////////////////////////////////////////////////////////////////////////////
5 // Modified by: Mark Newsam (Based on GTK file)
7 // Copyright: (c) Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
14 #include "wx/textctrl.h"
19 #include "wx/spinbutt.h"
20 #include "wx/spinctrl.h"
23 #include "wx/spinctrl.h"
25 // ----------------------------------------------------------------------------
27 // ----------------------------------------------------------------------------
29 // the margin between the text control and the spin
30 static const wxCoord MARGIN
= 2;
32 // ----------------------------------------------------------------------------
33 // wxSpinCtrlText: text control used by spin control
34 // ----------------------------------------------------------------------------
36 class wxSpinCtrlText
: public wxTextCtrl
39 wxSpinCtrlText(wxSpinCtrl
*spin
, const wxString
& value
)
40 : wxTextCtrl(spin
, -1, value
)
46 void OnTextChange(wxCommandEvent
& event
)
49 if ( m_spin
->GetTextValue(&val
) )
51 m_spin
->GetSpinButton()->SetValue(val
);
57 bool ProcessEvent(wxEvent
&event
)
59 // Hand button down events to wxSpinCtrl. Doesn't work.
60 if (event
.GetEventType() == wxEVT_LEFT_DOWN
&& m_spin
->ProcessEvent( event
))
63 return wxTextCtrl::ProcessEvent( event
);
72 BEGIN_EVENT_TABLE(wxSpinCtrlText
, wxTextCtrl
)
73 EVT_TEXT(-1, wxSpinCtrlText::OnTextChange
)
76 // ----------------------------------------------------------------------------
77 // wxSpinCtrlButton: spin button used by spin control
78 // ----------------------------------------------------------------------------
80 class wxSpinCtrlButton
: public wxSpinButton
83 wxSpinCtrlButton(wxSpinCtrl
*spin
, int style
)
88 SetWindowStyle(style
| wxSP_VERTICAL
);
92 void OnSpinButton(wxSpinEvent
& eventSpin
)
94 #if defined(__WXMAC__) || defined(__WXMOTIF__)
95 m_spin
->SetTextValue(eventSpin
.GetPosition());
97 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, m_spin
->GetId());
98 event
.SetEventObject(m_spin
);
99 event
.SetInt(eventSpin
.GetPosition());
101 m_spin
->GetEventHandler()->ProcessEvent(event
);
103 m_spin
->SetTextValue(eventSpin
.GetPosition());
111 DECLARE_EVENT_TABLE()
114 BEGIN_EVENT_TABLE(wxSpinCtrlButton
, wxSpinButton
)
115 EVT_SPIN(-1, wxSpinCtrlButton::OnSpinButton
)
118 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
, wxControl
)
120 // ============================================================================
122 // ============================================================================
124 // ----------------------------------------------------------------------------
125 // wxSpinCtrl creation
126 // ----------------------------------------------------------------------------
128 void wxSpinCtrl::Init()
134 bool wxSpinCtrl::Create(wxWindow
*parent
,
136 const wxString
& value
,
143 const wxString
& name
)
145 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
146 wxDefaultValidator
, name
) )
151 // the string value overrides the numeric one (for backwards compatibility
152 // reasons and also because it is simpler to satisfy the string value which
153 // comes much sooner in the list of arguments and leave the initial
154 // parameter unspecified)
155 if ( !value
.empty() )
158 if ( value
.ToLong(&l
) )
162 wxSize csize
= size
;
163 m_text
= new wxSpinCtrlText(this, value
);
164 m_btn
= new wxSpinCtrlButton(this, style
);
166 m_btn
->SetRange(min
, max
);
167 m_btn
->SetValue(initial
);
169 if ( size
.y
== -1 ) {
170 csize
.y
= m_text
->GetSize().y
;
172 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
177 wxSpinCtrl::~wxSpinCtrl()
179 // delete the controls now, don't leave them alive even though they would
180 // still be eventually deleted by our parent - but it will be too late, the
181 // user code expects them to be gone now
188 // ----------------------------------------------------------------------------
190 // ----------------------------------------------------------------------------
192 wxSize
wxSpinCtrl::DoGetBestSize() const
194 wxSize sizeBtn
= m_btn
->GetBestSize(),
195 sizeText
= m_text
->GetBestSize();
197 return wxSize(sizeBtn
.x
+ sizeText
.x
+ MARGIN
, sizeText
.y
);
200 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
202 wxControl::DoMoveWindow(x
, y
, width
, height
);
204 // position the subcontrols inside the client area
205 wxSize sizeBtn
= m_btn
->GetSize();
207 wxCoord wText
= width
- sizeBtn
.x
;
208 m_text
->SetSize(0, 0, wText
, height
);
209 m_btn
->SetSize(0 + wText
+ MARGIN
, 0, -1, -1);
212 // ----------------------------------------------------------------------------
213 // operations forwarded to the subcontrols
214 // ----------------------------------------------------------------------------
216 bool wxSpinCtrl::Enable(bool enable
)
218 if ( !wxControl::Enable(enable
) )
223 bool wxSpinCtrl::Show(bool show
)
225 if ( !wxControl::Show(show
) )
230 // ----------------------------------------------------------------------------
231 // value and range access
232 // ----------------------------------------------------------------------------
234 bool wxSpinCtrl::GetTextValue(int *val
) const
237 if ( !m_text
->GetValue().ToLong(&l
) )
239 // not a number at all
243 if ( l
< GetMin() || l
> GetMax() )
254 int wxSpinCtrl::GetValue() const
256 return m_btn
? m_btn
->GetValue() : 0;
259 int wxSpinCtrl::GetMin() const
261 return m_btn
? m_btn
->GetMin() : 0;
264 int wxSpinCtrl::GetMax() const
266 return m_btn
? m_btn
->GetMax() : 0;
269 // ----------------------------------------------------------------------------
270 // changing value and range
271 // ----------------------------------------------------------------------------
273 void wxSpinCtrl::SetTextValue(int val
)
275 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetTextValue") );
277 m_text
->SetValue(wxString::Format(_T("%d"), val
));
280 m_text
->SetSelection(0, -1);
282 // and give focus to the control!
283 // m_text->SetFocus(); Why???? TODO.
286 void wxSpinCtrl::SetValue(int val
)
288 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetValue") );
292 m_btn
->SetValue(val
);
295 void wxSpinCtrl::SetValue(const wxString
& text
)
297 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetValue") );
300 if ( text
.ToLong(&val
) && ((val
> INT_MIN
) && (val
< INT_MAX
)) )
304 else // not a number at all or out of range
306 m_text
->SetValue(text
);
307 m_text
->SetSelection(0, -1);
311 void wxSpinCtrl::SetRange(int min
, int max
)
313 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetRange") );
315 m_btn
->SetRange(min
, max
);
318 void wxSpinCtrl::SetSelection(long from
, long to
)
320 // if from and to are both -1, it means (in wxWidgets) that all text should
322 if ( (from
== -1) && (to
== -1) )
326 m_text
->SetSelection(from
, to
);
329 #endif // wxUSE_SPINCTRL