1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/classic/spinbutt.cpp
5 // Modified by: Mark Newsam (Based on GTK file)
7 // Copyright: (c) Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
19 #include "wx/spinctrl.h"
22 #include "wx/textctrl.h"
25 #include "wx/spinbutt.h"
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
31 // the margin between the text control and the spin
32 static const wxCoord MARGIN
= 2;
34 // ----------------------------------------------------------------------------
35 // wxSpinCtrlText: text control used by spin control
36 // ----------------------------------------------------------------------------
38 class wxSpinCtrlText
: public wxTextCtrl
41 wxSpinCtrlText(wxSpinCtrl
*spin
, const wxString
& value
)
42 : wxTextCtrl(spin
, -1, value
)
48 void OnTextChange(wxCommandEvent
& event
)
51 if ( m_spin
->GetTextValue(&val
) )
53 m_spin
->GetSpinButton()->SetValue(val
);
59 bool ProcessEvent(wxEvent
&event
)
61 // Hand button down events to wxSpinCtrl. Doesn't work.
62 if (event
.GetEventType() == wxEVT_LEFT_DOWN
&& m_spin
->ProcessEvent( event
))
65 return wxTextCtrl::ProcessEvent( event
);
74 BEGIN_EVENT_TABLE(wxSpinCtrlText
, wxTextCtrl
)
75 EVT_TEXT(-1, wxSpinCtrlText::OnTextChange
)
78 // ----------------------------------------------------------------------------
79 // wxSpinCtrlButton: spin button used by spin control
80 // ----------------------------------------------------------------------------
82 class wxSpinCtrlButton
: public wxSpinButton
85 wxSpinCtrlButton(wxSpinCtrl
*spin
, int style
)
90 SetWindowStyle(style
| wxSP_VERTICAL
);
94 void OnSpinButton(wxSpinEvent
& eventSpin
)
96 #if defined(__WXMAC__) || defined(__WXMOTIF__)
97 m_spin
->SetTextValue(eventSpin
.GetPosition());
99 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, m_spin
->GetId());
100 event
.SetEventObject(m_spin
);
101 event
.SetInt(eventSpin
.GetPosition());
103 m_spin
->GetEventHandler()->ProcessEvent(event
);
105 m_spin
->SetTextValue(eventSpin
.GetPosition());
113 DECLARE_EVENT_TABLE()
116 BEGIN_EVENT_TABLE(wxSpinCtrlButton
, wxSpinButton
)
117 EVT_SPIN(-1, wxSpinCtrlButton::OnSpinButton
)
120 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
, wxControl
)
122 // ============================================================================
124 // ============================================================================
126 // ----------------------------------------------------------------------------
127 // wxSpinCtrl creation
128 // ----------------------------------------------------------------------------
130 void wxSpinCtrl::Init()
136 bool wxSpinCtrl::Create(wxWindow
*parent
,
138 const wxString
& value
,
145 const wxString
& name
)
147 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
, style
,
148 wxDefaultValidator
, name
) )
153 // the string value overrides the numeric one (for backwards compatibility
154 // reasons and also because it is simpler to satisfy the string value which
155 // comes much sooner in the list of arguments and leave the initial
156 // parameter unspecified)
157 if ( !value
.empty() )
160 if ( value
.ToLong(&l
) )
164 wxSize csize
= size
;
165 m_text
= new wxSpinCtrlText(this, value
);
166 m_btn
= new wxSpinCtrlButton(this, style
);
168 m_btn
->SetRange(min
, max
);
169 m_btn
->SetValue(initial
);
171 if ( size
.y
== -1 ) {
172 csize
.y
= m_text
->GetSize().y
;
174 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
179 wxSpinCtrl::~wxSpinCtrl()
181 // delete the controls now, don't leave them alive even though they would
182 // still be eventually deleted by our parent - but it will be too late, the
183 // user code expects them to be gone now
190 // ----------------------------------------------------------------------------
192 // ----------------------------------------------------------------------------
194 wxSize
wxSpinCtrl::DoGetBestSize() const
196 wxSize sizeBtn
= m_btn
->GetBestSize(),
197 sizeText
= m_text
->GetBestSize();
199 return wxSize(sizeBtn
.x
+ sizeText
.x
+ MARGIN
, sizeText
.y
);
202 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
204 wxControl::DoMoveWindow(x
, y
, width
, height
);
206 // position the subcontrols inside the client area
207 wxSize sizeBtn
= m_btn
->GetSize();
209 wxCoord wText
= width
- sizeBtn
.x
;
210 m_text
->SetSize(0, 0, wText
, height
);
211 m_btn
->SetSize(0 + wText
+ MARGIN
, 0, -1, -1);
214 // ----------------------------------------------------------------------------
215 // operations forwarded to the subcontrols
216 // ----------------------------------------------------------------------------
218 bool wxSpinCtrl::Enable(bool enable
)
220 if ( !wxControl::Enable(enable
) )
225 bool wxSpinCtrl::Show(bool show
)
227 if ( !wxControl::Show(show
) )
232 // ----------------------------------------------------------------------------
233 // value and range access
234 // ----------------------------------------------------------------------------
236 bool wxSpinCtrl::GetTextValue(int *val
) const
239 if ( !m_text
->GetValue().ToLong(&l
) )
241 // not a number at all
245 if ( l
< GetMin() || l
> GetMax() )
256 int wxSpinCtrl::GetValue() const
258 return m_btn
? m_btn
->GetValue() : 0;
261 int wxSpinCtrl::GetMin() const
263 return m_btn
? m_btn
->GetMin() : 0;
266 int wxSpinCtrl::GetMax() const
268 return m_btn
? m_btn
->GetMax() : 0;
271 // ----------------------------------------------------------------------------
272 // changing value and range
273 // ----------------------------------------------------------------------------
275 void wxSpinCtrl::SetTextValue(int val
)
277 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetTextValue") );
279 m_text
->SetValue(wxString::Format(_T("%d"), val
));
282 m_text
->SetSelection(0, -1);
284 // and give focus to the control!
285 // m_text->SetFocus(); Why???? TODO.
288 void wxSpinCtrl::SetValue(int val
)
290 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetValue") );
294 m_btn
->SetValue(val
);
297 void wxSpinCtrl::SetValue(const wxString
& text
)
299 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetValue") );
302 if ( text
.ToLong(&val
) && ((val
> INT_MIN
) && (val
< INT_MAX
)) )
306 else // not a number at all or out of range
308 m_text
->SetValue(text
);
309 m_text
->SetSelection(0, -1);
313 void wxSpinCtrl::SetRange(int min
, int max
)
315 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetRange") );
317 m_btn
->SetRange(min
, max
);
320 void wxSpinCtrl::SetSelection(long from
, long to
)
322 // if from and to are both -1, it means (in wxWidgets) that all text should
324 if ( (from
== -1) && (to
== -1) )
328 m_text
->SetSelection(from
, to
);
331 #endif // wxUSE_SPINCTRL