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 license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "spinctlg.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
31 #if !(defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXPM__)) || defined(__WXMAC__) || \
32 defined(__WXUNIVERSAL__)
35 #include "wx/textctrl.h"
40 #include "wx/spinbutt.h"
41 #include "wx/spinctrl.h"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 // the margin between the text control and the spin
48 static const wxCoord MARGIN
= 2;
50 // ----------------------------------------------------------------------------
51 // wxSpinCtrlText: text control used by spin control
52 // ----------------------------------------------------------------------------
54 class wxSpinCtrlText
: public wxTextCtrl
57 wxSpinCtrlText(wxSpinCtrl
*spin
, const wxString
& value
)
58 : wxTextCtrl(spin
->GetParent(), -1, value
)
64 void OnTextChange(wxCommandEvent
& event
)
67 if ( m_spin
->GetTextValue(&val
) )
69 m_spin
->GetSpinButton()->SetValue(val
);
75 bool ProcessEvent(wxEvent
&event
)
77 // Hand button down events to wxSpinCtrl. Doesn't work.
78 if (event
.GetEventType() == wxEVT_LEFT_DOWN
&& m_spin
->ProcessEvent( event
))
81 return wxTextCtrl::ProcessEvent( event
);
90 BEGIN_EVENT_TABLE(wxSpinCtrlText
, wxTextCtrl
)
91 EVT_TEXT(-1, wxSpinCtrlText::OnTextChange
)
94 // ----------------------------------------------------------------------------
95 // wxSpinCtrlButton: spin button used by spin control
96 // ----------------------------------------------------------------------------
98 class wxSpinCtrlButton
: public wxSpinButton
101 wxSpinCtrlButton(wxSpinCtrl
*spin
, int style
)
102 : wxSpinButton(spin
->GetParent())
106 SetWindowStyle(style
| wxSP_VERTICAL
);
110 void OnSpinButton(wxSpinEvent
& eventSpin
)
113 m_spin
->SetTextValue(eventSpin
.GetPosition());
115 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, m_spin
->GetId());
116 event
.SetEventObject(m_spin
);
117 event
.SetInt(eventSpin
.GetPosition());
119 m_spin
->GetEventHandler()->ProcessEvent(event
);
121 m_spin
->SetTextValue(eventSpin
.GetPosition());
129 DECLARE_EVENT_TABLE()
132 BEGIN_EVENT_TABLE(wxSpinCtrlButton
, wxSpinButton
)
133 EVT_SPIN(-1, wxSpinCtrlButton::OnSpinButton
)
136 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
, wxControl
)
138 // ============================================================================
140 // ============================================================================
142 // ----------------------------------------------------------------------------
143 // wxSpinCtrl creation
144 // ----------------------------------------------------------------------------
146 void wxSpinCtrl::Init()
152 bool wxSpinCtrl::Create(wxWindow
*parent
,
154 const wxString
& value
,
161 const wxString
& name
)
163 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
164 wxDefaultValidator
, name
) )
169 // the string value overrides the numeric one (for backwards compatibility
170 // reasons and also because it is simpler to satisfy the string value which
171 // comes much sooner in the list of arguments and leave the initial
172 // parameter unspecified)
173 if ( !value
.empty() )
176 if ( value
.ToLong(&l
) )
180 SetBackgroundColour(*wxRED
);
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
);
187 wxSize csize
= size
;
188 if ( size
.y
== -1 ) {
189 csize
.y
= m_text
->GetSize().y
;
191 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
193 DoSetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
195 // have to disable this window to avoid interfering it with message
196 // processing to the text and the button... but pretend it is enabled to
197 // make IsEnabled() return TRUE
198 wxControl::Enable(FALSE
); // don't use non virtual Disable() here!
201 // we don't even need to show this window itself - and not doing it avoids
202 // that it overwrites the text control
203 wxControl::Show(FALSE
);
210 wxSpinCtrl::~wxSpinCtrl()
212 // delete the controls now, don't leave them alive even though they woudl
213 // still be eventually deleted by our parent - but it will be too late, the
214 // user code expects them to be gone now
219 // ----------------------------------------------------------------------------
221 // ----------------------------------------------------------------------------
223 wxSize
wxSpinCtrl::DoGetBestClientSize() const
225 wxSize sizeBtn
= m_btn
->GetBestSize(),
226 sizeText
= m_text
->GetBestSize();
228 return wxSize(sizeBtn
.x
+ sizeText
.x
+ MARGIN
, sizeText
.y
);
231 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
233 wxControl::DoMoveWindow(x
, y
, width
, height
);
235 wxPoint p
= GetParent() ?
236 GetParent()->GetClientAreaOrigin() : wxPoint(0,0);
238 // position the subcontrols inside the client area
239 wxSize sizeBtn
= m_btn
->GetSize();
241 wxCoord wText
= width
- sizeBtn
.x
;
242 m_text
->SetSize(x
-p
.x
, y
-p
.y
, wText
, height
);
244 m_btn
->SetSize(x
-p
.x
+ wText
+ MARGIN
, y
-p
.y
, -1, -1);
246 m_btn
->SetSize(x
-p
.x
+ wText
+ MARGIN
, y
-p
.y
, -1, height
);
250 // ----------------------------------------------------------------------------
251 // operations forwarded to the subcontrols
252 // ----------------------------------------------------------------------------
254 bool wxSpinCtrl::Enable(bool enable
)
256 if ( !wxControl::Enable(enable
) )
259 m_btn
->Enable(enable
);
260 m_text
->Enable(enable
);
265 bool wxSpinCtrl::Show(bool show
)
267 if ( !wxControl::Show(show
) )
270 // under GTK Show() is called the first time before we are fully
281 // ----------------------------------------------------------------------------
282 // value and range access
283 // ----------------------------------------------------------------------------
285 bool wxSpinCtrl::GetTextValue(int *val
) const
288 if ( !m_text
->GetValue().ToLong(&l
) )
290 // not a number at all
294 if ( l
< GetMin() || l
> GetMax() )
305 int wxSpinCtrl::GetValue() const
307 return m_btn
? m_btn
->GetValue() : 0;
310 int wxSpinCtrl::GetMin() const
312 return m_btn
? m_btn
->GetMin() : 0;
315 int wxSpinCtrl::GetMax() const
317 return m_btn
? m_btn
->GetMax() : 0;
320 // ----------------------------------------------------------------------------
321 // changing value and range
322 // ----------------------------------------------------------------------------
324 void wxSpinCtrl::SetTextValue(int val
)
326 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetTextValue") );
328 m_text
->SetValue(wxString::Format(_T("%d"), val
));
331 m_text
->SetSelection(0, -1);
333 // and give focus to the control!
337 void wxSpinCtrl::SetValue(int val
)
339 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetValue") );
343 m_btn
->SetValue(val
);
346 void wxSpinCtrl::SetValue(const wxString
& text
)
348 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetValue") );
351 if ( text
.ToLong(&val
) && ((val
> INT_MIN
) && (val
< INT_MAX
)) )
355 else // not a number at all or out of range
357 m_text
->SetValue(text
);
358 m_text
->SetSelection(0, -1);
362 void wxSpinCtrl::SetRange(int min
, int max
)
364 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetRange") );
366 m_btn
->SetRange(min
, max
);
369 #endif // wxUSE_SPINCTRL
370 #endif // !wxPort-with-native-spinctrl