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 // ----------------------------------------------------------------------------
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__)) || \
32 defined(__WXMAC__) || 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
)
112 m_spin
->SetTextValue(eventSpin
.GetPosition());
114 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, m_spin
->GetId());
115 event
.SetEventObject(m_spin
);
116 event
.SetInt(eventSpin
.GetPosition());
118 m_spin
->GetEventHandler()->ProcessEvent(event
);
126 DECLARE_EVENT_TABLE()
129 BEGIN_EVENT_TABLE(wxSpinCtrlButton
, wxSpinButton
)
130 EVT_SPIN(-1, wxSpinCtrlButton::OnSpinButton
)
133 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
, wxControl
)
135 // ============================================================================
137 // ============================================================================
139 // ----------------------------------------------------------------------------
140 // wxSpinCtrl creation
141 // ----------------------------------------------------------------------------
143 void wxSpinCtrl::Init()
149 bool wxSpinCtrl::Create(wxWindow
*parent
,
151 const wxString
& value
,
158 const wxString
& name
)
160 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
161 wxDefaultValidator
, name
) )
166 // the string value overrides the numeric one (for backwards compatibility
167 // reasons and also because it is simpler to satisfy the string value which
168 // comes much sooner in the list of arguments and leave the initial
169 // parameter unspecified)
170 if ( !value
.empty() )
173 if ( value
.ToLong(&l
) )
177 SetBackgroundColour(*wxRED
);
178 m_text
= new wxSpinCtrlText(this, value
);
179 m_btn
= new wxSpinCtrlButton(this, style
);
181 m_btn
->SetRange(min
, max
);
182 m_btn
->SetValue(initial
);
184 wxSize csize
= size
;
185 if ( size
.y
== -1 ) {
186 csize
.y
= m_text
->GetSize().y
;
188 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
190 wxSize best
= GetBestSize();
191 if ( size
.x
!= -1 ) best
.x
= size
.x
;
192 if ( size
.y
!= -1 ) best
.y
= size
.y
;
193 DoSetSize(pos
.x
, pos
.y
, best
.x
, best
.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::DoGetBestSize() 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 // position the subcontrols inside the client area
236 wxSize sizeBtn
= m_btn
->GetSize();
238 wxCoord wText
= width
- sizeBtn
.x
;
239 m_text
->SetSize(x
, y
, wText
, height
);
241 m_btn
->SetSize(x
+ wText
+ MARGIN
, y
, -1, -1);
243 m_btn
->SetSize(x
+ wText
+ MARGIN
, y
, -1, height
);
247 // ----------------------------------------------------------------------------
248 // operations forwarded to the subcontrols
249 // ----------------------------------------------------------------------------
251 bool wxSpinCtrl::Enable(bool enable
)
253 if ( !wxControl::Enable(enable
) )
256 m_btn
->Enable(enable
);
257 m_text
->Enable(enable
);
262 bool wxSpinCtrl::Show(bool show
)
264 if ( !wxControl::Show(show
) )
267 // under GTK Show() is called the first time before we are fully
278 // ----------------------------------------------------------------------------
279 // value and range access
280 // ----------------------------------------------------------------------------
282 bool wxSpinCtrl::GetTextValue(int *val
) const
285 if ( !m_text
->GetValue().ToLong(&l
) )
287 // not a number at all
291 if ( l
< GetMin() || l
> GetMax() )
302 int wxSpinCtrl::GetValue() const
304 return m_btn
? m_btn
->GetValue() : 0;
307 int wxSpinCtrl::GetMin() const
309 return m_btn
? m_btn
->GetMin() : 0;
312 int wxSpinCtrl::GetMax() const
314 return m_btn
? m_btn
->GetMax() : 0;
317 // ----------------------------------------------------------------------------
318 // changing value and range
319 // ----------------------------------------------------------------------------
321 void wxSpinCtrl::SetTextValue(int val
)
323 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetTextValue") );
325 m_text
->SetValue(wxString::Format(_T("%d"), val
));
328 m_text
->SetSelection(0, -1);
330 // and give focus to the control!
331 // m_text->SetFocus(); Why???? TODO.
334 void wxSpinCtrl::SetValue(int val
)
336 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetValue") );
340 m_btn
->SetValue(val
);
343 void wxSpinCtrl::SetValue(const wxString
& text
)
345 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetValue") );
348 if ( text
.ToLong(&val
) && ((val
> INT_MIN
) && (val
< INT_MAX
)) )
352 else // not a number at all or out of range
354 m_text
->SetValue(text
);
355 m_text
->SetSelection(0, -1);
359 void wxSpinCtrl::SetRange(int min
, int max
)
361 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetRange") );
363 m_btn
->SetRange(min
, max
);
366 void wxSpinCtrl::SetSelection(long from
, long to
)
368 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetSelection") );
370 m_text
->SetSelection(from
, to
);
373 #endif // wxUSE_SPINCTRL
374 #endif // !wxPort-with-native-spinctrl