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 margin between the text control and the spin
29 static const wxCoord MARGIN
= 8;
31 // ----------------------------------------------------------------------------
32 // wxSpinCtrlText: text control used by spin control
33 // ----------------------------------------------------------------------------
35 class wxSpinCtrlText
: public wxTextCtrl
38 wxSpinCtrlText(wxSpinCtrl
*spin
, const wxString
& value
)
39 : wxTextCtrl(spin
, -1, value
, wxDefaultPosition
, wxSize(40, 22))
43 // remove the default minsize, the spinctrl will have one instead
48 void OnTextChange(wxCommandEvent
& event
)
51 if ( m_spin
->GetTextValue(&val
) )
53 m_spin
->GetSpinButton()->SetValue(val
);
59 bool ProcessEvent(wxEvent
&event
)
61 // Hand button down events to wxSpinCtrl. Doesn't work.
62 if (event
.GetEventType() == wxEVT_LEFT_DOWN
&& m_spin
->ProcessEvent( event
))
65 return wxTextCtrl::ProcessEvent( event
);
74 BEGIN_EVENT_TABLE(wxSpinCtrlText
, wxTextCtrl
)
75 EVT_TEXT(-1, wxSpinCtrlText::OnTextChange
)
78 // ----------------------------------------------------------------------------
79 // wxSpinCtrlButton: spin button used by spin control
80 // ----------------------------------------------------------------------------
82 class wxSpinCtrlButton
: public wxSpinButton
85 wxSpinCtrlButton(wxSpinCtrl
*spin
, int style
)
89 SetWindowStyle(style
| wxSP_VERTICAL
);
91 // TODO: The spin button gets truncated a little bit due to size
92 // differences so change it's default size a bit. SMALL still gets a
93 // bit truncated, but MINI seems to be too small... Readdress this
94 // when the textctrl issues are all sorted out.
95 //SetWindowVariant(wxWINDOW_VARIANT_SMALL);
97 // remove the default minsize, the spinctrl will have one instead
102 void OnSpinButton(wxSpinEvent
& eventSpin
)
104 m_spin
->SetTextValue(eventSpin
.GetPosition());
106 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, m_spin
->GetId());
107 event
.SetEventObject(m_spin
);
108 event
.SetInt(eventSpin
.GetPosition());
110 m_spin
->GetEventHandler()->ProcessEvent(event
);
116 DECLARE_EVENT_TABLE()
119 BEGIN_EVENT_TABLE(wxSpinCtrlButton
, wxSpinButton
)
120 EVT_SPIN(-1, wxSpinCtrlButton::OnSpinButton
)
123 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
, wxControl
)
125 // ============================================================================
127 // ============================================================================
129 // ----------------------------------------------------------------------------
130 // wxSpinCtrl creation
131 // ----------------------------------------------------------------------------
133 void wxSpinCtrl::Init()
139 bool wxSpinCtrl::Create(wxWindow
*parent
,
141 const wxString
& value
,
148 const wxString
& name
)
150 m_macIsUserPane
= true;
151 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
152 wxDefaultValidator
, name
) )
157 // the string value overrides the numeric one (for backwards compatibility
158 // reasons and also because it is simpler to satisfy the string value which
159 // comes much sooner in the list of arguments and leave the initial
160 // parameter unspecified)
161 if ( !value
.empty() )
164 if ( value
.ToLong(&l
) )
168 wxSize csize
= size
;
169 m_text
= new wxSpinCtrlText(this, value
);
170 m_btn
= new wxSpinCtrlButton(this, style
);
172 m_btn
->SetRange(min
, max
);
173 m_btn
->SetValue(initial
);
176 csize
.x
= m_text
->GetSize().x
+ MARGIN
+ m_btn
->GetSize().x
;
179 if ( size
.y
== -1 ) {
180 csize
.y
= m_text
->GetSize().y
+ 4; //allow for text border highlights
185 MacPostControlCreate(pos
, csize
);
186 SetInitialBestSize(csize
);
191 wxSpinCtrl::~wxSpinCtrl()
193 // delete the controls now, don't leave them alive even though they would
194 // still be eventually deleted by our parent - but it will be too late, the
195 // user code expects them to be gone now
202 // ----------------------------------------------------------------------------
204 // ----------------------------------------------------------------------------
206 wxSize
wxSpinCtrl::DoGetBestSize() const
208 if (!m_btn
|| !m_text
)
211 wxSize sizeBtn
= m_btn
->GetBestSize(),
212 sizeText
= m_text
->GetBestSize();
215 if (sizeText
.y
> sizeBtn
.y
)
220 return wxSize(sizeBtn
.x
+ sizeText
.x
+ MARGIN
, height
+ MARGIN
);
223 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
225 // position the subcontrols inside the client area
226 wxSize sizeBtn
= m_btn
->GetSize();
228 wxControl::DoMoveWindow(x
, y
, width
, height
);
230 wxCoord wText
= width
- sizeBtn
.x
- MARGIN
;
232 //growing or shrinking a control like this doesn't really make sense
233 //so just leave the controls the same size and add whitespace
234 m_text
->SetSize(2, 2, wText
, 22);
235 m_btn
->SetSize(0 + wText
+ MARGIN
, 0, -1, height
);
238 // ----------------------------------------------------------------------------
239 // operations forwarded to the subcontrols
240 // ----------------------------------------------------------------------------
242 bool wxSpinCtrl::Enable(bool enable
)
244 if ( !wxControl::Enable(enable
) )
249 bool wxSpinCtrl::Show(bool show
)
251 if ( !wxControl::Show(show
) )
256 // ----------------------------------------------------------------------------
257 // value and range access
258 // ----------------------------------------------------------------------------
260 bool wxSpinCtrl::GetTextValue(int *val
) const
263 if ( !m_text
->GetValue().ToLong(&l
) )
265 // not a number at all
269 if ( l
< GetMin() || l
> GetMax() )
280 int wxSpinCtrl::GetValue() const
282 return m_btn
? m_btn
->GetValue() : 0;
285 int wxSpinCtrl::GetMin() const
287 return m_btn
? m_btn
->GetMin() : 0;
290 int wxSpinCtrl::GetMax() const
292 return m_btn
? m_btn
->GetMax() : 0;
295 // ----------------------------------------------------------------------------
296 // changing value and range
297 // ----------------------------------------------------------------------------
299 void wxSpinCtrl::SetTextValue(int val
)
301 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetTextValue") );
303 m_text
->SetValue(wxString::Format(_T("%d"), val
));
306 m_text
->SetSelection(0, -1);
308 // and give focus to the control!
309 // m_text->SetFocus(); Why???? TODO.
312 void wxSpinCtrl::SetValue(int val
)
314 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetValue") );
318 m_btn
->SetValue(val
);
321 void wxSpinCtrl::SetValue(const wxString
& text
)
323 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetValue") );
326 if ( text
.ToLong(&val
) && ((val
> INT_MIN
) && (val
< INT_MAX
)) )
330 else // not a number at all or out of range
332 m_text
->SetValue(text
);
333 m_text
->SetSelection(0, -1);
337 void wxSpinCtrl::SetRange(int min
, int max
)
339 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetRange") );
341 m_btn
->SetRange(min
, max
);
344 void wxSpinCtrl::SetSelection(long from
, long to
)
346 // if from and to are both -1, it means (in wxWidgets) that all text should
348 if ( (from
== -1) && (to
== -1) )
352 m_text
->SetSelection(from
, to
);
355 #endif // wxUSE_SPINCTRL