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
, int style
)
45 : wxTextCtrl(spin
, wxID_ANY
, value
, wxDefaultPosition
, wxSize(40, wxDefaultCoord
), style
)
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 OnSetFocus(wxFocusEvent
& event
)
65 // delegate to parent control
66 event
.SetEventObject( GetParent() );
67 GetParent()->HandleWindowEvent(event
);
70 void OnKillFocus(wxFocusEvent
& event
)
73 if ( !GetValue().ToLong(&l
) )
75 // not a number at all
80 if (l
< m_spin
->GetMin())
82 if (l
> m_spin
->GetMax())
85 // Update text control
87 str
.Printf( wxT("%d"), (int)l
);
88 if (str
!= GetValue())
91 if (l
!= m_spin
->m_oldValue
)
93 // set value in spin button
94 // does that trigger an event?
95 m_spin
->m_btn
->SetValue( l
);
98 wxCommandEvent
cevent(wxEVT_COMMAND_SPINCTRL_UPDATED
, m_spin
->GetId());
99 cevent
.SetEventObject(m_spin
);
101 m_spin
->HandleWindowEvent(cevent
);
103 m_spin
->m_oldValue
= l
;
106 // delegate to parent control
107 event
.SetEventObject( GetParent() );
108 GetParent()->HandleWindowEvent(event
);
111 void OnTextChange(wxCommandEvent
& event
)
114 if ( m_spin
->GetTextValue(&val
) )
116 m_spin
->GetSpinButton()->SetValue(val
);
118 // If we're already processing a text update from m_spin,
119 // don't send it again, since we could end up recursing
121 if (event
.GetId() == m_spin
->GetId())
127 // Send event that the text was manually changed
128 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_spin
->GetId());
129 event
.SetEventObject(m_spin
);
130 event
.SetString(m_spin
->GetText()->GetValue());
133 m_spin
->HandleWindowEvent(event
);
142 DECLARE_EVENT_TABLE()
145 BEGIN_EVENT_TABLE(wxSpinCtrlText
, wxTextCtrl
)
146 EVT_TEXT(wxID_ANY
, wxSpinCtrlText::OnTextChange
)
147 EVT_SET_FOCUS(wxSpinCtrlText::OnSetFocus
)
148 EVT_KILL_FOCUS(wxSpinCtrlText::OnKillFocus
)
151 // ----------------------------------------------------------------------------
152 // wxSpinCtrlButton: spin button used by spin control
153 // ----------------------------------------------------------------------------
155 class wxSpinCtrlButton
: public wxSpinButton
158 wxSpinCtrlButton(wxSpinCtrl
*spin
, int style
)
159 : wxSpinButton(spin
)
162 SetWindowStyle(style
| wxSP_VERTICAL
);
164 // TODO: The spin button gets truncated a little bit due to size
165 // differences so change it's default size a bit. SMALL still gets a
166 // bit truncated, but MINI seems to be too small... Readdress this
167 // when the textctrl issues are all sorted out.
168 //SetWindowVariant(wxWINDOW_VARIANT_SMALL);
170 // remove the default minsize, the spinctrl will have one instead
171 SetMinSize(wxDefaultSize
);
175 void OnSpinButton(wxSpinEvent
& eventSpin
)
177 int pos
= eventSpin
.GetPosition();
178 m_spin
->SetTextValue(pos
);
180 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, m_spin
->GetId());
181 event
.SetEventObject(m_spin
);
184 m_spin
->HandleWindowEvent(event
);
186 m_spin
->m_oldValue
= pos
;
192 DECLARE_EVENT_TABLE()
195 BEGIN_EVENT_TABLE(wxSpinCtrlButton
, wxSpinButton
)
196 EVT_SPIN(wxID_ANY
, wxSpinCtrlButton::OnSpinButton
)
199 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
, wxControl
)
201 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxControl
)
202 WX_EVENT_TABLE_CONTROL_CONTAINER(wxSpinCtrl
)
205 WX_DELEGATE_TO_CONTROL_CONTAINER(wxSpinCtrl
, wxControl
)
208 // ============================================================================
210 // ============================================================================
212 // ----------------------------------------------------------------------------
213 // wxSpinCtrl creation
214 // ----------------------------------------------------------------------------
216 void wxSpinCtrl::Init()
220 WX_INIT_CONTROL_CONTAINER();
223 bool wxSpinCtrl::Create(wxWindow
*parent
,
225 const wxString
& value
,
232 const wxString
& name
)
234 m_macIsUserPane
= true;
235 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
236 wxDefaultValidator
, name
) )
241 // the string value overrides the numeric one (for backwards compatibility
242 // reasons and also because it is simpler to satisfy the string value which
243 // comes much sooner in the list of arguments and leave the initial
244 // parameter unspecified)
245 if ( !value
.empty() )
248 if ( value
.ToLong(&l
) )
252 wxSize csize
= size
;
253 m_text
= new wxSpinCtrlText(this, value
, style
& ( wxTE_PROCESS_ENTER
| wxALIGN_MASK
) );
254 m_btn
= new wxSpinCtrlButton(this, style
);
256 m_btn
->SetRange(min
, max
);
257 m_btn
->SetValue(initial
);
259 m_oldValue
= GetMin()-1;
261 if ( size
.x
== wxDefaultCoord
){
262 csize
.x
= m_text
->GetSize().x
+ MARGIN
+ m_btn
->GetSize().x
;
265 if ( size
.y
== wxDefaultCoord
) {
266 csize
.y
= m_text
->GetSize().y
+ 2 * TEXTBORDER
; //allow for text border highlights
267 if ( m_btn
->GetSize().y
> csize
.y
)
268 csize
.y
= m_btn
->GetSize().y
;
273 //MacPostControlCreate(pos, csize);
274 SetInitialSize(csize
);
279 wxSpinCtrl::~wxSpinCtrl()
281 // delete the controls now, don't leave them alive even though they would
282 // still be eventually deleted by our parent - but it will be too late, the
283 // user code expects them to be gone now
288 // ----------------------------------------------------------------------------
290 // ----------------------------------------------------------------------------
292 wxSize
wxSpinCtrl::DoGetBestSize() const
294 if (!m_btn
|| !m_text
)
297 wxSize sizeBtn
= m_btn
->GetBestSize(),
298 sizeText
= m_text
->GetBestSize();
300 sizeText
.y
+= 2 * TEXTBORDER
;
301 sizeText
.x
+= 2 * TEXTBORDER
;
304 if (sizeText
.y
> sizeBtn
.y
)
309 return wxSize(sizeBtn
.x
+ sizeText
.x
+ MARGIN
, height
);
312 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
314 // position the subcontrols inside the client area
315 wxSize sizeBtn
= m_btn
->GetSize();
316 wxSize sizeText
= m_text
->GetSize();
318 wxControl::DoMoveWindow(x
, y
, width
, height
);
320 wxCoord wText
= width
- sizeBtn
.x
- MARGIN
- 2 * TEXTBORDER
;
322 m_text
->SetSize(TEXTBORDER
, (height
- sizeText
.y
) / 2, wText
, -1);
323 m_btn
->SetSize(0 + wText
+ MARGIN
+ TEXTBORDER
, (height
- sizeBtn
.y
) / 2 , -1, -1 );
326 // ----------------------------------------------------------------------------
327 // operations forwarded to the subcontrols
328 // ----------------------------------------------------------------------------
330 bool wxSpinCtrl::Enable(bool enable
)
332 if ( !wxControl::Enable(enable
) )
337 bool wxSpinCtrl::Show(bool show
)
339 if ( !wxControl::Show(show
) )
344 // ----------------------------------------------------------------------------
345 // value and range access
346 // ----------------------------------------------------------------------------
348 bool wxSpinCtrl::GetTextValue(int *val
) const
351 if ( !m_text
->GetValue().ToLong(&l
) )
353 // not a number at all
357 if ( l
< GetMin() || l
> GetMax() )
368 int wxSpinCtrl::GetValue() const
370 return m_btn
? m_btn
->GetValue() : 0;
373 int wxSpinCtrl::GetMin() const
375 return m_btn
? m_btn
->GetMin() : 0;
378 int wxSpinCtrl::GetMax() const
380 return m_btn
? m_btn
->GetMax() : 0;
383 // ----------------------------------------------------------------------------
384 // changing value and range
385 // ----------------------------------------------------------------------------
387 void wxSpinCtrl::SetTextValue(int val
)
389 wxCHECK_RET( m_text
, wxT("invalid call to wxSpinCtrl::SetTextValue") );
391 m_text
->SetValue(wxString::Format(wxT("%d"), val
));
394 m_text
->SetSelection(0, -1);
396 m_text
->SetInsertionPointEnd();
398 // and give focus to the control!
399 // m_text->SetFocus(); Why???? TODO.
402 void wxSpinCtrl::SetValue(int val
)
404 wxCHECK_RET( m_btn
, wxT("invalid call to wxSpinCtrl::SetValue") );
408 m_btn
->SetValue(val
);
412 void wxSpinCtrl::SetValue(const wxString
& text
)
414 wxCHECK_RET( m_text
, wxT("invalid call to wxSpinCtrl::SetValue") );
417 if ( text
.ToLong(&val
) && ((val
> INT_MIN
) && (val
< INT_MAX
)) )
421 else // not a number at all or out of range
423 m_text
->SetValue(text
);
424 m_text
->SetSelection(0, -1);
428 void wxSpinCtrl::SetRange(int min
, int max
)
430 wxCHECK_RET( m_btn
, wxT("invalid call to wxSpinCtrl::SetRange") );
432 m_btn
->SetRange(min
, max
);
435 void wxSpinCtrl::SetSelection(long from
, long to
)
437 // if from and to are both -1, it means (in wxWidgets) that all text should
439 if ( (from
== -1) && (to
== -1) )
443 m_text
->SetSelection(from
, to
);
446 #endif // wxUSE_SPINCTRL