1 /////////////////////////////////////////////////////////////////////////////
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/spinbutt.h"
16 #include "wx/spinctrl.h"
17 #include "wx/textctrl.h"
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
24 // the focus rect around a text may have 4 pixels in each direction
25 // we handle these problems right now in an extended vis region of a window
26 static const wxCoord TEXTBORDER
= 4 ;
27 // the margin between the text control and the spin
28 static const wxCoord MARGIN
= 8 - TEXTBORDER
;
30 // ----------------------------------------------------------------------------
31 // wxSpinCtrlText: text control used by spin control
32 // ----------------------------------------------------------------------------
34 class wxSpinCtrlText
: public wxTextCtrl
37 wxSpinCtrlText(wxSpinCtrl
*spin
, const wxString
& value
)
38 : wxTextCtrl(spin
, -1, value
, wxDefaultPosition
, wxSize(40, -1))
42 // remove the default minsize, the spinctrl will have one instead
47 void OnTextChange(wxCommandEvent
& event
)
50 if ( m_spin
->GetTextValue(&val
) )
52 m_spin
->GetSpinButton()->SetValue(val
);
54 // Send event that the text was manually changed
55 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_spin
->GetId());
56 event
.SetEventObject(m_spin
);
59 m_spin
->GetEventHandler()->ProcessEvent(event
);
65 bool ProcessEvent(wxEvent
&event
)
67 // Hand button down events to wxSpinCtrl. Doesn't work.
68 if (event
.GetEventType() == wxEVT_LEFT_DOWN
&& m_spin
->ProcessEvent( event
))
71 return wxTextCtrl::ProcessEvent( event
);
80 BEGIN_EVENT_TABLE(wxSpinCtrlText
, wxTextCtrl
)
81 EVT_TEXT(-1, wxSpinCtrlText::OnTextChange
)
84 // ----------------------------------------------------------------------------
85 // wxSpinCtrlButton: spin button used by spin control
86 // ----------------------------------------------------------------------------
88 class wxSpinCtrlButton
: public wxSpinButton
91 wxSpinCtrlButton(wxSpinCtrl
*spin
, int style
)
95 SetWindowStyle(style
| wxSP_VERTICAL
);
97 // TODO: The spin button gets truncated a little bit due to size
98 // differences so change it's default size a bit. SMALL still gets a
99 // bit truncated, but MINI seems to be too small... Readdress this
100 // when the textctrl issues are all sorted out.
101 //SetWindowVariant(wxWINDOW_VARIANT_SMALL);
103 // remove the default minsize, the spinctrl will have one instead
108 void OnSpinButton(wxSpinEvent
& eventSpin
)
110 m_spin
->SetTextValue(eventSpin
.GetPosition());
112 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, m_spin
->GetId());
113 event
.SetEventObject(m_spin
);
114 event
.SetInt(eventSpin
.GetPosition());
116 m_spin
->GetEventHandler()->ProcessEvent(event
);
122 DECLARE_EVENT_TABLE()
125 BEGIN_EVENT_TABLE(wxSpinCtrlButton
, wxSpinButton
)
126 EVT_SPIN(-1, wxSpinCtrlButton::OnSpinButton
)
129 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
, wxControl
)
131 // ============================================================================
133 // ============================================================================
135 // ----------------------------------------------------------------------------
136 // wxSpinCtrl creation
137 // ----------------------------------------------------------------------------
139 void wxSpinCtrl::Init()
145 bool wxSpinCtrl::Create(wxWindow
*parent
,
147 const wxString
& value
,
154 const wxString
& name
)
156 m_macIsUserPane
= true;
157 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
158 wxDefaultValidator
, name
) )
163 // the string value overrides the numeric one (for backwards compatibility
164 // reasons and also because it is simpler to satisfy the string value which
165 // comes much sooner in the list of arguments and leave the initial
166 // parameter unspecified)
167 if ( !value
.empty() )
170 if ( value
.ToLong(&l
) )
174 wxSize csize
= size
;
175 m_text
= new wxSpinCtrlText(this, value
);
176 m_btn
= new wxSpinCtrlButton(this, style
);
178 m_btn
->SetRange(min
, max
);
179 m_btn
->SetValue(initial
);
182 csize
.x
= m_text
->GetSize().x
+ MARGIN
+ m_btn
->GetSize().x
;
185 if ( size
.y
== -1 ) {
186 csize
.y
= m_text
->GetSize().y
+ 2 * TEXTBORDER
; //allow for text border highlights
187 if ( m_btn
->GetSize().y
> csize
.y
)
188 csize
.y
= m_btn
->GetSize().y
;
193 //MacPostControlCreate(pos, csize);
194 SetInitialBestSize(csize
);
199 wxSpinCtrl::~wxSpinCtrl()
201 // delete the controls now, don't leave them alive even though they would
202 // still be eventually deleted by our parent - but it will be too late, the
203 // user code expects them to be gone now
210 // ----------------------------------------------------------------------------
212 // ----------------------------------------------------------------------------
214 wxSize
wxSpinCtrl::DoGetBestSize() const
216 if (!m_btn
|| !m_text
)
219 wxSize sizeBtn
= m_btn
->GetBestSize(),
220 sizeText
= m_text
->GetBestSize();
222 sizeText
.y
+= 2 * TEXTBORDER
;
223 sizeText
.x
+= 2 * TEXTBORDER
;
226 if (sizeText
.y
> sizeBtn
.y
)
231 return wxSize(sizeBtn
.x
+ sizeText
.x
+ MARGIN
, height
);
234 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
236 // position the subcontrols inside the client area
237 wxSize sizeBtn
= m_btn
->GetSize();
238 wxSize sizeText
= m_text
->GetSize();
240 wxControl::DoMoveWindow(x
, y
, width
, height
);
242 wxCoord wText
= width
- sizeBtn
.x
- MARGIN
- 2 * TEXTBORDER
;
244 m_text
->SetSize(TEXTBORDER
, (height
- sizeText
.y
) / 2, wText
, -1);
245 m_btn
->SetSize(0 + wText
+ MARGIN
+ 2 * TEXTBORDER
, (height
- sizeBtn
.y
) / 2 , -1, -1 );
248 // ----------------------------------------------------------------------------
249 // operations forwarded to the subcontrols
250 // ----------------------------------------------------------------------------
252 bool wxSpinCtrl::Enable(bool enable
)
254 if ( !wxControl::Enable(enable
) )
259 bool wxSpinCtrl::Show(bool show
)
261 if ( !wxControl::Show(show
) )
266 void wxSpinCtrl::SetFocus()
268 if ( m_text
!= NULL
) {
273 // ----------------------------------------------------------------------------
274 // value and range access
275 // ----------------------------------------------------------------------------
277 bool wxSpinCtrl::GetTextValue(int *val
) const
280 if ( !m_text
->GetValue().ToLong(&l
) )
282 // not a number at all
286 if ( l
< GetMin() || l
> GetMax() )
297 int wxSpinCtrl::GetValue() const
299 return m_btn
? m_btn
->GetValue() : 0;
302 int wxSpinCtrl::GetMin() const
304 return m_btn
? m_btn
->GetMin() : 0;
307 int wxSpinCtrl::GetMax() const
309 return m_btn
? m_btn
->GetMax() : 0;
312 // ----------------------------------------------------------------------------
313 // changing value and range
314 // ----------------------------------------------------------------------------
316 void wxSpinCtrl::SetTextValue(int val
)
318 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetTextValue") );
320 m_text
->SetValue(wxString::Format(_T("%d"), val
));
323 m_text
->SetSelection(0, -1);
325 // and give focus to the control!
326 // m_text->SetFocus(); Why???? TODO.
329 void wxSpinCtrl::SetValue(int val
)
331 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetValue") );
335 m_btn
->SetValue(val
);
338 void wxSpinCtrl::SetValue(const wxString
& text
)
340 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetValue") );
343 if ( text
.ToLong(&val
) && ((val
> INT_MIN
) && (val
< INT_MAX
)) )
347 else // not a number at all or out of range
349 m_text
->SetValue(text
);
350 m_text
->SetSelection(0, -1);
354 void wxSpinCtrl::SetRange(int min
, int max
)
356 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetRange") );
358 m_btn
->SetRange(min
, max
);
361 void wxSpinCtrl::SetSelection(long from
, long to
)
363 // if from and to are both -1, it means (in wxWidgets) that all text should
365 if ( (from
== -1) && (to
== -1) )
369 m_text
->SetSelection(from
, to
);
372 #endif // wxUSE_SPINCTRL