1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/spinctlg.cpp
3 // Purpose: implements wxSpinCtrl as a composite control
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 // There are port-specific versions for MSW, GTK, OS/2 and Mac, so exclude the
28 // contents of this file in those cases
29 #if !(defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXPM__) || \
30 defined(__WXMAC__)) || defined(__WXUNIVERSAL__)
33 #include "wx/textctrl.h"
38 #include "wx/spinbutt.h"
39 #include "wx/spinctrl.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 // the margin between the text control and the spin
46 static const wxCoord MARGIN
= 2;
48 // ----------------------------------------------------------------------------
49 // wxSpinCtrlText: text control used by spin control
50 // ----------------------------------------------------------------------------
52 class wxSpinCtrlText
: public wxTextCtrl
55 wxSpinCtrlText(wxSpinCtrl
*spin
, const wxString
& value
)
56 : wxTextCtrl(spin
->GetParent(), wxID_ANY
, value
)
60 // remove the default minsize, the spinctrl will have one instead
61 SetSizeHints(wxDefaultCoord
,wxDefaultCoord
);
65 void OnTextChange(wxCommandEvent
& event
)
68 if ( m_spin
->GetTextValue(&val
) )
70 m_spin
->GetSpinButton()->SetValue(val
);
76 bool ProcessEvent(wxEvent
&event
)
78 // Hand button down events to wxSpinCtrl. Doesn't work.
79 if (event
.GetEventType() == wxEVT_LEFT_DOWN
&& m_spin
->ProcessEvent( event
))
82 return wxTextCtrl::ProcessEvent( event
);
91 BEGIN_EVENT_TABLE(wxSpinCtrlText
, wxTextCtrl
)
92 EVT_TEXT(wxID_ANY
, wxSpinCtrlText::OnTextChange
)
95 // ----------------------------------------------------------------------------
96 // wxSpinCtrlButton: spin button used by spin control
97 // ----------------------------------------------------------------------------
99 class wxSpinCtrlButton
: public wxSpinButton
102 wxSpinCtrlButton(wxSpinCtrl
*spin
, int style
)
103 : wxSpinButton(spin
->GetParent())
107 SetWindowStyle(style
| wxSP_VERTICAL
);
109 // remove the default minsize, the spinctrl will have one instead
110 SetSizeHints(wxDefaultCoord
,wxDefaultCoord
);
114 void OnSpinButton(wxSpinEvent
& eventSpin
)
116 m_spin
->SetTextValue(eventSpin
.GetPosition());
118 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, m_spin
->GetId());
119 event
.SetEventObject(m_spin
);
120 event
.SetInt(eventSpin
.GetPosition());
122 m_spin
->GetEventHandler()->ProcessEvent(event
);
130 DECLARE_EVENT_TABLE()
133 BEGIN_EVENT_TABLE(wxSpinCtrlButton
, wxSpinButton
)
134 EVT_SPIN(wxID_ANY
, wxSpinCtrlButton::OnSpinButton
)
137 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
, wxControl
)
139 // ============================================================================
141 // ============================================================================
143 // ----------------------------------------------------------------------------
144 // wxSpinCtrl creation
145 // ----------------------------------------------------------------------------
147 void wxSpinCtrl::Init()
153 bool wxSpinCtrl::Create(wxWindow
*parent
,
155 const wxString
& value
,
162 const wxString
& name
)
164 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
165 wxDefaultValidator
, name
) )
170 // the string value overrides the numeric one (for backwards compatibility
171 // reasons and also because it is simpler to satisfy the string value which
172 // comes much sooner in the list of arguments and leave the initial
173 // parameter unspecified)
174 if ( !value
.empty() )
177 if ( value
.ToLong(&l
) )
181 m_text
= new wxSpinCtrlText(this, value
);
182 m_btn
= new wxSpinCtrlButton(this, style
);
184 m_btn
->SetRange(min
, max
);
185 m_btn
->SetValue(initial
);
189 // have to disable this window to avoid interfering it with message
190 // processing to the text and the button... but pretend it is enabled to
191 // make IsEnabled() return true
192 wxControl::Enable(false); // don't use non virtual Disable() here!
195 // we don't even need to show this window itself - and not doing it avoids
196 // that it overwrites the text control
197 wxControl::Show(false);
202 wxSpinCtrl::~wxSpinCtrl()
204 // delete the controls now, don't leave them alive even though they would
205 // still be eventually deleted by our parent - but it will be too late, the
206 // user code expects them to be gone now
213 // ----------------------------------------------------------------------------
215 // ----------------------------------------------------------------------------
217 wxSize
wxSpinCtrl::DoGetBestSize() const
219 wxSize sizeBtn
= m_btn
->GetBestSize(),
220 sizeText
= m_text
->GetBestSize();
222 return wxSize(sizeBtn
.x
+ sizeText
.x
+ MARGIN
, sizeText
.y
);
225 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
227 wxControl::DoMoveWindow(x
, y
, width
, height
);
229 // position the subcontrols inside the client area
230 wxSize sizeBtn
= m_btn
->GetSize();
232 wxCoord wText
= width
- sizeBtn
.x
;
233 m_text
->SetSize(x
, y
, wText
, height
);
234 m_btn
->SetSize(x
+ wText
+ MARGIN
, y
, wxDefaultCoord
, height
);
237 // ----------------------------------------------------------------------------
238 // operations forwarded to the subcontrols
239 // ----------------------------------------------------------------------------
241 bool wxSpinCtrl::Enable(bool enable
)
243 if ( !wxControl::Enable(enable
) )
246 m_btn
->Enable(enable
);
247 m_text
->Enable(enable
);
252 bool wxSpinCtrl::Show(bool show
)
254 if ( !wxControl::Show(show
) )
257 // under GTK Show() is called the first time before we are fully
268 // ----------------------------------------------------------------------------
269 // value and range access
270 // ----------------------------------------------------------------------------
272 bool wxSpinCtrl::GetTextValue(int *val
) const
275 if ( !m_text
->GetValue().ToLong(&l
) )
277 // not a number at all
281 if ( l
< GetMin() || l
> GetMax() )
292 int wxSpinCtrl::GetValue() const
294 return m_btn
? m_btn
->GetValue() : 0;
297 int wxSpinCtrl::GetMin() const
299 return m_btn
? m_btn
->GetMin() : 0;
302 int wxSpinCtrl::GetMax() const
304 return m_btn
? m_btn
->GetMax() : 0;
307 // ----------------------------------------------------------------------------
308 // changing value and range
309 // ----------------------------------------------------------------------------
311 void wxSpinCtrl::SetTextValue(int val
)
313 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetTextValue") );
315 m_text
->SetValue(wxString::Format(_T("%d"), val
));
318 m_text
->SetSelection(0, -1);
320 // and give focus to the control!
321 // m_text->SetFocus(); Why???? TODO.
324 void wxSpinCtrl::SetValue(int val
)
326 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetValue") );
330 m_btn
->SetValue(val
);
333 void wxSpinCtrl::SetValue(const wxString
& text
)
335 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetValue") );
338 if ( text
.ToLong(&val
) && ((val
> INT_MIN
) && (val
< INT_MAX
)) )
342 else // not a number at all or out of range
344 m_text
->SetValue(text
);
345 m_text
->SetSelection(0, -1);
349 void wxSpinCtrl::SetRange(int min
, int max
)
351 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetRange") );
353 m_btn
->SetRange(min
, max
);
356 void wxSpinCtrl::SetSelection(long from
, long to
)
358 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetSelection") );
360 m_text
->SetSelection(from
, to
);
363 #endif // wxUSE_SPINCTRL
364 #endif // !wxPort-with-native-spinctrl