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
& WXUNUSED(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
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, m_spin
->GetId());
92 event
.SetEventObject(m_spin
);
94 m_spin
->HandleWindowEvent(event
);
96 m_spin
->m_oldValue
= l
;
100 void OnTextChange(wxCommandEvent
& event
)
103 if ( m_spin
->GetTextValue(&val
) )
105 m_spin
->GetSpinButton()->SetValue(val
);
107 // If we're already processing a text update from m_spin,
108 // don't send it again, since we could end up recursing
110 if (event
.GetId() == m_spin
->GetId())
116 // Send event that the text was manually changed
117 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_spin
->GetId());
118 event
.SetEventObject(m_spin
);
119 event
.SetString(m_spin
->GetText()->GetValue());
122 m_spin
->HandleWindowEvent(event
);
131 DECLARE_EVENT_TABLE()
134 BEGIN_EVENT_TABLE(wxSpinCtrlText
, wxTextCtrl
)
135 EVT_TEXT(wxID_ANY
, wxSpinCtrlText::OnTextChange
)
136 EVT_KILL_FOCUS( wxSpinCtrlText::OnKillFocus
)
139 // ----------------------------------------------------------------------------
140 // wxSpinCtrlButton: spin button used by spin control
141 // ----------------------------------------------------------------------------
143 class wxSpinCtrlButton
: public wxSpinButton
146 wxSpinCtrlButton(wxSpinCtrl
*spin
, int style
)
147 : wxSpinButton(spin
)
150 SetWindowStyle(style
| wxSP_VERTICAL
);
152 // TODO: The spin button gets truncated a little bit due to size
153 // differences so change it's default size a bit. SMALL still gets a
154 // bit truncated, but MINI seems to be too small... Readdress this
155 // when the textctrl issues are all sorted out.
156 //SetWindowVariant(wxWINDOW_VARIANT_SMALL);
158 // remove the default minsize, the spinctrl will have one instead
159 SetMinSize(wxDefaultSize
);
163 void OnSpinButton(wxSpinEvent
& eventSpin
)
165 int pos
= eventSpin
.GetPosition();
166 m_spin
->SetTextValue(pos
);
168 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, m_spin
->GetId());
169 event
.SetEventObject(m_spin
);
172 m_spin
->HandleWindowEvent(event
);
174 m_spin
->m_oldValue
= pos
;
180 DECLARE_EVENT_TABLE()
183 BEGIN_EVENT_TABLE(wxSpinCtrlButton
, wxSpinButton
)
184 EVT_SPIN(wxID_ANY
, wxSpinCtrlButton::OnSpinButton
)
187 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
, wxControl
)
189 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxControl
)
190 WX_EVENT_TABLE_CONTROL_CONTAINER(wxSpinCtrl
)
193 WX_DELEGATE_TO_CONTROL_CONTAINER(wxSpinCtrl
, wxControl
)
196 // ============================================================================
198 // ============================================================================
200 // ----------------------------------------------------------------------------
201 // wxSpinCtrl creation
202 // ----------------------------------------------------------------------------
204 void wxSpinCtrl::Init()
208 WX_INIT_CONTROL_CONTAINER();
211 bool wxSpinCtrl::Create(wxWindow
*parent
,
213 const wxString
& value
,
220 const wxString
& name
)
222 m_macIsUserPane
= true;
223 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
224 wxDefaultValidator
, name
) )
229 // the string value overrides the numeric one (for backwards compatibility
230 // reasons and also because it is simpler to satisfy the string value which
231 // comes much sooner in the list of arguments and leave the initial
232 // parameter unspecified)
233 if ( !value
.empty() )
236 if ( value
.ToLong(&l
) )
240 wxSize csize
= size
;
241 m_text
= new wxSpinCtrlText(this, value
);
242 m_btn
= new wxSpinCtrlButton(this, style
);
244 m_btn
->SetRange(min
, max
);
245 m_btn
->SetValue(initial
);
247 m_oldValue
= GetMin()-1;
249 if ( size
.x
== wxDefaultCoord
){
250 csize
.x
= m_text
->GetSize().x
+ MARGIN
+ m_btn
->GetSize().x
;
253 if ( size
.y
== wxDefaultCoord
) {
254 csize
.y
= m_text
->GetSize().y
+ 2 * TEXTBORDER
; //allow for text border highlights
255 if ( m_btn
->GetSize().y
> csize
.y
)
256 csize
.y
= m_btn
->GetSize().y
;
261 //MacPostControlCreate(pos, csize);
262 SetInitialSize(csize
);
267 wxSpinCtrl::~wxSpinCtrl()
269 // delete the controls now, don't leave them alive even though they would
270 // still be eventually deleted by our parent - but it will be too late, the
271 // user code expects them to be gone now
278 // ----------------------------------------------------------------------------
280 // ----------------------------------------------------------------------------
282 wxSize
wxSpinCtrl::DoGetBestSize() const
284 if (!m_btn
|| !m_text
)
287 wxSize sizeBtn
= m_btn
->GetBestSize(),
288 sizeText
= m_text
->GetBestSize();
290 sizeText
.y
+= 2 * TEXTBORDER
;
291 sizeText
.x
+= 2 * TEXTBORDER
;
294 if (sizeText
.y
> sizeBtn
.y
)
299 return wxSize(sizeBtn
.x
+ sizeText
.x
+ MARGIN
, height
);
302 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
304 // position the subcontrols inside the client area
305 wxSize sizeBtn
= m_btn
->GetSize();
306 wxSize sizeText
= m_text
->GetSize();
308 wxControl::DoMoveWindow(x
, y
, width
, height
);
310 wxCoord wText
= width
- sizeBtn
.x
- MARGIN
- 2 * TEXTBORDER
;
312 m_text
->SetSize(TEXTBORDER
, (height
- sizeText
.y
) / 2, wText
, -1);
313 m_btn
->SetSize(0 + wText
+ MARGIN
+ TEXTBORDER
, (height
- sizeBtn
.y
) / 2 , -1, -1 );
316 // ----------------------------------------------------------------------------
317 // operations forwarded to the subcontrols
318 // ----------------------------------------------------------------------------
320 bool wxSpinCtrl::Enable(bool enable
)
322 if ( !wxControl::Enable(enable
) )
327 bool wxSpinCtrl::Show(bool show
)
329 if ( !wxControl::Show(show
) )
334 // ----------------------------------------------------------------------------
335 // value and range access
336 // ----------------------------------------------------------------------------
338 bool wxSpinCtrl::GetTextValue(int *val
) const
341 if ( !m_text
->GetValue().ToLong(&l
) )
343 // not a number at all
347 if ( l
< GetMin() || l
> GetMax() )
358 int wxSpinCtrl::GetValue() const
360 return m_btn
? m_btn
->GetValue() : 0;
363 int wxSpinCtrl::GetMin() const
365 return m_btn
? m_btn
->GetMin() : 0;
368 int wxSpinCtrl::GetMax() const
370 return m_btn
? m_btn
->GetMax() : 0;
373 // ----------------------------------------------------------------------------
374 // changing value and range
375 // ----------------------------------------------------------------------------
377 void wxSpinCtrl::SetTextValue(int val
)
379 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetTextValue") );
381 m_text
->SetValue(wxString::Format(_T("%d"), val
));
384 m_text
->SetSelection(0, -1);
386 // and give focus to the control!
387 // m_text->SetFocus(); Why???? TODO.
390 void wxSpinCtrl::SetValue(int val
)
392 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetValue") );
396 m_btn
->SetValue(val
);
400 void wxSpinCtrl::SetValue(const wxString
& text
)
402 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetValue") );
405 if ( text
.ToLong(&val
) && ((val
> INT_MIN
) && (val
< INT_MAX
)) )
409 else // not a number at all or out of range
411 m_text
->SetValue(text
);
412 m_text
->SetSelection(0, -1);
416 void wxSpinCtrl::SetRange(int min
, int max
)
418 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetRange") );
420 m_btn
->SetRange(min
, max
);
423 void wxSpinCtrl::SetSelection(long from
, long to
)
425 // if from and to are both -1, it means (in wxWidgets) that all text should
427 if ( (from
== -1) && (to
== -1) )
431 m_text
->SetSelection(from
, to
);
434 #endif // wxUSE_SPINCTRL