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__)) || \
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 #if defined(__WXMAC__) || defined(__WXMOTIF__)
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
, wxDefaultPosition
, wxDefaultSize
, 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 wxSize best
= GetBestSize();
194 if ( size
.x
!= -1 ) best
.x
= size
.x
;
195 if ( size
.y
!= -1 ) best
.y
= size
.y
;
196 DoSetSize(pos
.x
, pos
.y
, best
.x
, best
.y
);
198 // have to disable this window to avoid interfering it with message
199 // processing to the text and the button... but pretend it is enabled to
200 // make IsEnabled() return TRUE
201 wxControl::Enable(FALSE
); // don't use non virtual Disable() here!
204 // we don't even need to show this window itself - and not doing it avoids
205 // that it overwrites the text control
206 wxControl::Show(FALSE
);
213 wxSpinCtrl::~wxSpinCtrl()
215 // delete the controls now, don't leave them alive even though they woudl
216 // still be eventually deleted by our parent - but it will be too late, the
217 // user code expects them to be gone now
222 // ----------------------------------------------------------------------------
224 // ----------------------------------------------------------------------------
226 wxSize
wxSpinCtrl::DoGetBestSize() const
228 wxSize sizeBtn
= m_btn
->GetBestSize(),
229 sizeText
= m_text
->GetBestSize();
231 return wxSize(sizeBtn
.x
+ sizeText
.x
+ MARGIN
, sizeText
.y
);
234 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
236 wxControl::DoMoveWindow(x
, y
, width
, height
);
238 wxPoint p
= GetParent() ?
239 GetParent()->GetClientAreaOrigin() : wxPoint(0,0);
241 // position the subcontrols inside the client area
242 wxSize sizeBtn
= m_btn
->GetSize();
244 wxCoord wText
= width
- sizeBtn
.x
;
245 m_text
->SetSize(x
-p
.x
, y
-p
.y
, wText
, height
);
247 m_btn
->SetSize(x
-p
.x
+ wText
+ MARGIN
, y
-p
.y
, -1, -1);
249 m_btn
->SetSize(x
-p
.x
+ wText
+ MARGIN
, y
-p
.y
, -1, height
);
253 // ----------------------------------------------------------------------------
254 // operations forwarded to the subcontrols
255 // ----------------------------------------------------------------------------
257 bool wxSpinCtrl::Enable(bool enable
)
259 if ( !wxControl::Enable(enable
) )
262 m_btn
->Enable(enable
);
263 m_text
->Enable(enable
);
268 bool wxSpinCtrl::Show(bool show
)
270 if ( !wxControl::Show(show
) )
273 // under GTK Show() is called the first time before we are fully
284 // ----------------------------------------------------------------------------
285 // value and range access
286 // ----------------------------------------------------------------------------
288 bool wxSpinCtrl::GetTextValue(int *val
) const
291 if ( !m_text
->GetValue().ToLong(&l
) )
293 // not a number at all
297 if ( l
< GetMin() || l
> GetMax() )
308 int wxSpinCtrl::GetValue() const
310 return m_btn
? m_btn
->GetValue() : 0;
313 int wxSpinCtrl::GetMin() const
315 return m_btn
? m_btn
->GetMin() : 0;
318 int wxSpinCtrl::GetMax() const
320 return m_btn
? m_btn
->GetMax() : 0;
323 // ----------------------------------------------------------------------------
324 // changing value and range
325 // ----------------------------------------------------------------------------
327 void wxSpinCtrl::SetTextValue(int val
)
329 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetTextValue") );
331 m_text
->SetValue(wxString::Format(_T("%d"), val
));
334 m_text
->SetSelection(0, -1);
336 // and give focus to the control!
337 // m_text->SetFocus(); Why???? TODO.
340 void wxSpinCtrl::SetValue(int val
)
342 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetValue") );
346 m_btn
->SetValue(val
);
349 void wxSpinCtrl::SetValue(const wxString
& text
)
351 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetValue") );
354 if ( text
.ToLong(&val
) && ((val
> INT_MIN
) && (val
< INT_MAX
)) )
358 else // not a number at all or out of range
360 m_text
->SetValue(text
);
361 m_text
->SetSelection(0, -1);
365 void wxSpinCtrl::SetRange(int min
, int max
)
367 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetRange") );
369 m_btn
->SetRange(min
, max
);
372 #endif // wxUSE_SPINCTRL
373 #endif // !wxPort-with-native-spinctrl