1 /////////////////////////////////////////////////////////////////////////////
5 // Modified by: Mark Newsam (Based on GTK file)
7 // Copyright: (c) Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
12 #pragma implementation "spinctrl.h"
19 #include "wx/spinbutt.h"
20 #include "wx/spinctrl.h"
21 #include "wx/textctrl.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 static const wxCoord MARGIN
= 8 - TEXTBORDER
;
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
, wxDefaultPosition
, wxSize(40, -1))
46 // remove the default minsize, the spinctrl will have one instead
51 void OnTextChange(wxCommandEvent
& event
)
54 if ( m_spin
->GetTextValue(&val
) )
56 m_spin
->GetSpinButton()->SetValue(val
);
58 // Send event that the text was manually changed
59 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_spin
->GetId());
60 event
.SetEventObject(m_spin
);
63 m_spin
->GetEventHandler()->ProcessEvent(event
);
69 bool ProcessEvent(wxEvent
&event
)
71 // Hand button down events to wxSpinCtrl. Doesn't work.
72 if (event
.GetEventType() == wxEVT_LEFT_DOWN
&& m_spin
->ProcessEvent( event
))
75 return wxTextCtrl::ProcessEvent( event
);
84 BEGIN_EVENT_TABLE(wxSpinCtrlText
, wxTextCtrl
)
85 EVT_TEXT(-1, wxSpinCtrlText::OnTextChange
)
88 // ----------------------------------------------------------------------------
89 // wxSpinCtrlButton: spin button used by spin control
90 // ----------------------------------------------------------------------------
92 class wxSpinCtrlButton
: public wxSpinButton
95 wxSpinCtrlButton(wxSpinCtrl
*spin
, int style
)
99 SetWindowStyle(style
| wxSP_VERTICAL
);
101 // TODO: The spin button gets truncated a little bit due to size
102 // differences so change it's default size a bit. SMALL still gets a
103 // bit truncated, but MINI seems to be too small... Readdress this
104 // when the textctrl issues are all sorted out.
105 //SetWindowVariant(wxWINDOW_VARIANT_SMALL);
107 // remove the default minsize, the spinctrl will have one instead
112 void OnSpinButton(wxSpinEvent
& eventSpin
)
114 m_spin
->SetTextValue(eventSpin
.GetPosition());
116 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, m_spin
->GetId());
117 event
.SetEventObject(m_spin
);
118 event
.SetInt(eventSpin
.GetPosition());
120 m_spin
->GetEventHandler()->ProcessEvent(event
);
126 DECLARE_EVENT_TABLE()
129 BEGIN_EVENT_TABLE(wxSpinCtrlButton
, wxSpinButton
)
130 EVT_SPIN(-1, wxSpinCtrlButton::OnSpinButton
)
133 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
, wxControl
)
135 // ============================================================================
137 // ============================================================================
139 // ----------------------------------------------------------------------------
140 // wxSpinCtrl creation
141 // ----------------------------------------------------------------------------
143 void wxSpinCtrl::Init()
149 bool wxSpinCtrl::Create(wxWindow
*parent
,
151 const wxString
& value
,
158 const wxString
& name
)
160 m_macIsUserPane
= true;
161 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
162 wxDefaultValidator
, name
) )
167 // the string value overrides the numeric one (for backwards compatibility
168 // reasons and also because it is simpler to satisfy the string value which
169 // comes much sooner in the list of arguments and leave the initial
170 // parameter unspecified)
171 if ( !value
.empty() )
174 if ( value
.ToLong(&l
) )
178 wxSize csize
= size
;
179 m_text
= new wxSpinCtrlText(this, value
);
180 m_btn
= new wxSpinCtrlButton(this, style
);
182 m_btn
->SetRange(min
, max
);
183 m_btn
->SetValue(initial
);
186 csize
.x
= m_text
->GetSize().x
+ MARGIN
+ m_btn
->GetSize().x
;
189 if ( size
.y
== -1 ) {
190 csize
.y
= m_text
->GetSize().y
+ 2 * TEXTBORDER
; //allow for text border highlights
191 if ( m_btn
->GetSize().y
> csize
.y
)
192 csize
.y
= m_btn
->GetSize().y
;
197 //MacPostControlCreate(pos, csize);
198 SetInitialBestSize(csize
);
203 wxSpinCtrl::~wxSpinCtrl()
205 // delete the controls now, don't leave them alive even though they would
206 // still be eventually deleted by our parent - but it will be too late, the
207 // user code expects them to be gone now
214 // ----------------------------------------------------------------------------
216 // ----------------------------------------------------------------------------
218 wxSize
wxSpinCtrl::DoGetBestSize() const
220 if (!m_btn
|| !m_text
)
223 wxSize sizeBtn
= m_btn
->GetBestSize(),
224 sizeText
= m_text
->GetBestSize();
226 sizeText
.y
+= 2 * TEXTBORDER
;
227 sizeText
.x
+= 2 * TEXTBORDER
;
230 if (sizeText
.y
> sizeBtn
.y
)
235 return wxSize(sizeBtn
.x
+ sizeText
.x
+ MARGIN
, height
);
238 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
240 // position the subcontrols inside the client area
241 wxSize sizeBtn
= m_btn
->GetSize();
242 wxSize sizeText
= m_text
->GetSize();
244 wxControl::DoMoveWindow(x
, y
, width
, height
);
246 wxCoord wText
= width
- sizeBtn
.x
- MARGIN
- 2 * TEXTBORDER
;
248 m_text
->SetSize(TEXTBORDER
, (height
- sizeText
.y
) / 2, wText
, -1);
249 m_btn
->SetSize(0 + wText
+ MARGIN
+ 2 * TEXTBORDER
, (height
- sizeBtn
.y
) / 2 , -1, -1 );
252 // ----------------------------------------------------------------------------
253 // operations forwarded to the subcontrols
254 // ----------------------------------------------------------------------------
256 bool wxSpinCtrl::Enable(bool enable
)
258 if ( !wxControl::Enable(enable
) )
263 bool wxSpinCtrl::Show(bool show
)
265 if ( !wxControl::Show(show
) )
270 // ----------------------------------------------------------------------------
271 // value and range access
272 // ----------------------------------------------------------------------------
274 bool wxSpinCtrl::GetTextValue(int *val
) const
277 if ( !m_text
->GetValue().ToLong(&l
) )
279 // not a number at all
283 if ( l
< GetMin() || l
> GetMax() )
294 int wxSpinCtrl::GetValue() const
296 return m_btn
? m_btn
->GetValue() : 0;
299 int wxSpinCtrl::GetMin() const
301 return m_btn
? m_btn
->GetMin() : 0;
304 int wxSpinCtrl::GetMax() const
306 return m_btn
? m_btn
->GetMax() : 0;
309 // ----------------------------------------------------------------------------
310 // changing value and range
311 // ----------------------------------------------------------------------------
313 void wxSpinCtrl::SetTextValue(int val
)
315 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetTextValue") );
317 m_text
->SetValue(wxString::Format(_T("%d"), val
));
320 m_text
->SetSelection(0, -1);
322 // and give focus to the control!
323 // m_text->SetFocus(); Why???? TODO.
326 void wxSpinCtrl::SetValue(int val
)
328 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetValue") );
332 m_btn
->SetValue(val
);
335 void wxSpinCtrl::SetValue(const wxString
& text
)
337 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetValue") );
340 if ( text
.ToLong(&val
) && ((val
> INT_MIN
) && (val
< INT_MAX
)) )
344 else // not a number at all or out of range
346 m_text
->SetValue(text
);
347 m_text
->SetSelection(0, -1);
351 void wxSpinCtrl::SetRange(int min
, int max
)
353 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetRange") );
355 m_btn
->SetRange(min
, max
);
358 void wxSpinCtrl::SetSelection(long from
, long to
)
360 // if from and to are both -1, it means (in wxWidgets) that all text should
362 if ( (from
== -1) && (to
== -1) )
366 m_text
->SetSelection(from
, to
);
369 #endif // wxUSE_SPINCTRL