1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/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"
15 #include "wx/spinctrl.h"
18 #include "wx/textctrl.h"
19 #include "wx/containr.h"
22 #include "wx/spinbutt.h"
24 // ----------------------------------------------------------------------------
26 // ----------------------------------------------------------------------------
28 // the focus rect around a text may have 4 pixels in each direction
29 // we handle these problems right now in an extended vis region of a window
30 static const wxCoord TEXTBORDER
= 4 ;
31 // the margin between the text control and the spin
32 // HIG says 2px between text and stepper control,
33 // but a value of 3 leads to the same look as the
34 // spin controls in Apple's apps
35 static const wxCoord MARGIN
= 3;
37 // ----------------------------------------------------------------------------
38 // wxSpinCtrlText: text control used by spin control
39 // ----------------------------------------------------------------------------
41 class wxSpinCtrlText
: public wxTextCtrl
44 wxSpinCtrlText(wxSpinCtrl
*spin
, const wxString
& value
)
45 : wxTextCtrl(spin
, wxID_ANY
, value
, wxDefaultPosition
, wxSize(40, wxDefaultCoord
))
49 // remove the default minsize, the spinctrl will have one instead
50 SetMinSize(wxDefaultSize
);
53 bool ProcessEvent(wxEvent
&event
)
55 // Hand button down events to wxSpinCtrl. Doesn't work.
56 if (event
.GetEventType() == wxEVT_LEFT_DOWN
&& m_spin
->ProcessEvent( event
))
59 return wxTextCtrl::ProcessEvent( event
);
63 void OnKillFocus(wxFocusEvent
& event
)
66 if ( !GetValue().ToLong(&l
) )
68 // not a number at all
73 if (l
< m_spin
->GetMin())
75 if (l
> m_spin
->GetMax())
78 // Update text control
80 str
.Printf( wxT("%d"), (int)l
);
81 if (str
!= GetValue())
84 if (l
!= m_spin
->m_oldValue
)
86 // set value in spin button
87 // does that trigger an event?
88 m_spin
->m_btn
->SetValue( l
);
91 wxCommandEvent
cevent(wxEVT_COMMAND_SPINCTRL_UPDATED
, m_spin
->GetId());
92 cevent
.SetEventObject(m_spin
);
94 m_spin
->HandleWindowEvent(cevent
);
96 m_spin
->m_oldValue
= l
;
99 event
.SetEventObject( GetParent() );
100 GetParent()->HandleWindowEvent(event
);
103 void OnTextChange(wxCommandEvent
& event
)
106 if ( m_spin
->GetTextValue(&val
) )
108 m_spin
->GetSpinButton()->SetValue(val
);
110 // If we're already processing a text update from m_spin,
111 // don't send it again, since we could end up recursing
113 if (event
.GetId() == m_spin
->GetId())
119 // Send event that the text was manually changed
120 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_spin
->GetId());
121 event
.SetEventObject(m_spin
);
122 event
.SetString(m_spin
->GetText()->GetValue());
125 m_spin
->HandleWindowEvent(event
);
134 DECLARE_EVENT_TABLE()
137 BEGIN_EVENT_TABLE(wxSpinCtrlText
, wxTextCtrl
)
138 EVT_TEXT(wxID_ANY
, wxSpinCtrlText::OnTextChange
)
139 EVT_KILL_FOCUS( wxSpinCtrlText::OnKillFocus
)
142 // ----------------------------------------------------------------------------
143 // wxSpinCtrlButton: spin button used by spin control
144 // ----------------------------------------------------------------------------
146 class wxSpinCtrlButton
: public wxSpinButton
149 wxSpinCtrlButton(wxSpinCtrl
*spin
, int style
)
150 : wxSpinButton(spin
)
153 SetWindowStyle(style
| wxSP_VERTICAL
);
155 // TODO: The spin button gets truncated a little bit due to size
156 // differences so change it's default size a bit. SMALL still gets a
157 // bit truncated, but MINI seems to be too small... Readdress this
158 // when the textctrl issues are all sorted out.
159 //SetWindowVariant(wxWINDOW_VARIANT_SMALL);
161 // remove the default minsize, the spinctrl will have one instead
162 SetMinSize(wxDefaultSize
);
166 void OnSpinButton(wxSpinEvent
& eventSpin
)
168 int pos
= eventSpin
.GetPosition();
169 m_spin
->SetTextValue(pos
);
171 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, m_spin
->GetId());
172 event
.SetEventObject(m_spin
);
175 m_spin
->HandleWindowEvent(event
);
177 m_spin
->m_oldValue
= pos
;
183 DECLARE_EVENT_TABLE()
186 BEGIN_EVENT_TABLE(wxSpinCtrlButton
, wxSpinButton
)
187 EVT_SPIN(wxID_ANY
, wxSpinCtrlButton::OnSpinButton
)
190 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
, wxControl
)
192 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxControl
)
193 WX_EVENT_TABLE_CONTROL_CONTAINER(wxSpinCtrl
)
196 WX_DELEGATE_TO_CONTROL_CONTAINER(wxSpinCtrl
, wxControl
)
199 // ============================================================================
201 // ============================================================================
203 // ----------------------------------------------------------------------------
204 // wxSpinCtrl creation
205 // ----------------------------------------------------------------------------
207 void wxSpinCtrl::Init()
211 WX_INIT_CONTROL_CONTAINER();
214 bool wxSpinCtrl::Create(wxWindow
*parent
,
216 const wxString
& value
,
223 const wxString
& name
)
225 m_macIsUserPane
= true;
226 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
227 wxDefaultValidator
, name
) )
232 // the string value overrides the numeric one (for backwards compatibility
233 // reasons and also because it is simpler to satisfy the string value which
234 // comes much sooner in the list of arguments and leave the initial
235 // parameter unspecified)
236 if ( !value
.empty() )
239 if ( value
.ToLong(&l
) )
243 wxSize csize
= size
;
244 m_text
= new wxSpinCtrlText(this, value
);
245 m_btn
= new wxSpinCtrlButton(this, style
);
247 m_btn
->SetRange(min
, max
);
248 m_btn
->SetValue(initial
);
250 m_oldValue
= GetMin()-1;
252 if ( size
.x
== wxDefaultCoord
){
253 csize
.x
= m_text
->GetSize().x
+ MARGIN
+ m_btn
->GetSize().x
;
256 if ( size
.y
== wxDefaultCoord
) {
257 csize
.y
= m_text
->GetSize().y
+ 2 * TEXTBORDER
; //allow for text border highlights
258 if ( m_btn
->GetSize().y
> csize
.y
)
259 csize
.y
= m_btn
->GetSize().y
;
264 //MacPostControlCreate(pos, csize);
265 SetInitialSize(csize
);
270 wxSpinCtrl::~wxSpinCtrl()
272 // delete the controls now, don't leave them alive even though they would
273 // still be eventually deleted by our parent - but it will be too late, the
274 // user code expects them to be gone now
281 // ----------------------------------------------------------------------------
283 // ----------------------------------------------------------------------------
285 wxSize
wxSpinCtrl::DoGetBestSize() const
287 if (!m_btn
|| !m_text
)
290 wxSize sizeBtn
= m_btn
->GetBestSize(),
291 sizeText
= m_text
->GetBestSize();
293 sizeText
.y
+= 2 * TEXTBORDER
;
294 sizeText
.x
+= 2 * TEXTBORDER
;
297 if (sizeText
.y
> sizeBtn
.y
)
302 return wxSize(sizeBtn
.x
+ sizeText
.x
+ MARGIN
, height
);
305 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
307 // position the subcontrols inside the client area
308 wxSize sizeBtn
= m_btn
->GetSize();
309 wxSize sizeText
= m_text
->GetSize();
311 wxControl::DoMoveWindow(x
, y
, width
, height
);
313 wxCoord wText
= width
- sizeBtn
.x
- MARGIN
- 2 * TEXTBORDER
;
315 m_text
->SetSize(TEXTBORDER
, (height
- sizeText
.y
) / 2, wText
, -1);
316 m_btn
->SetSize(0 + wText
+ MARGIN
+ TEXTBORDER
, (height
- sizeBtn
.y
) / 2 , -1, -1 );
319 // ----------------------------------------------------------------------------
320 // operations forwarded to the subcontrols
321 // ----------------------------------------------------------------------------
323 bool wxSpinCtrl::Enable(bool enable
)
325 if ( !wxControl::Enable(enable
) )
330 bool wxSpinCtrl::Show(bool show
)
332 if ( !wxControl::Show(show
) )
337 // ----------------------------------------------------------------------------
338 // value and range access
339 // ----------------------------------------------------------------------------
341 bool wxSpinCtrl::GetTextValue(int *val
) const
344 if ( !m_text
->GetValue().ToLong(&l
) )
346 // not a number at all
350 if ( l
< GetMin() || l
> GetMax() )
361 int wxSpinCtrl::GetValue() const
363 return m_btn
? m_btn
->GetValue() : 0;
366 int wxSpinCtrl::GetMin() const
368 return m_btn
? m_btn
->GetMin() : 0;
371 int wxSpinCtrl::GetMax() const
373 return m_btn
? m_btn
->GetMax() : 0;
376 // ----------------------------------------------------------------------------
377 // changing value and range
378 // ----------------------------------------------------------------------------
380 void wxSpinCtrl::SetTextValue(int val
)
382 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetTextValue") );
384 m_text
->SetValue(wxString::Format(_T("%d"), val
));
387 m_text
->SetSelection(0, -1);
389 // and give focus to the control!
390 // m_text->SetFocus(); Why???? TODO.
393 void wxSpinCtrl::SetValue(int val
)
395 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetValue") );
399 m_btn
->SetValue(val
);
403 void wxSpinCtrl::SetValue(const wxString
& text
)
405 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetValue") );
408 if ( text
.ToLong(&val
) && ((val
> INT_MIN
) && (val
< INT_MAX
)) )
412 else // not a number at all or out of range
414 m_text
->SetValue(text
);
415 m_text
->SetSelection(0, -1);
419 void wxSpinCtrl::SetRange(int min
, int max
)
421 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetRange") );
423 m_btn
->SetRange(min
, max
);
426 void wxSpinCtrl::SetSelection(long from
, long to
)
428 // if from and to are both -1, it means (in wxWidgets) that all text should
430 if ( (from
== -1) && (to
== -1) )
434 m_text
->SetSelection(from
, to
);
437 #endif // wxUSE_SPINCTRL