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"
18 #include "wx/containr.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
46 bool ProcessEvent(wxEvent
&event
)
48 // Hand button down events to wxSpinCtrl. Doesn't work.
49 if (event
.GetEventType() == wxEVT_LEFT_DOWN
&& m_spin
->ProcessEvent( event
))
52 return wxTextCtrl::ProcessEvent( event
);
56 void OnTextChange(wxCommandEvent
& event
)
59 if ( m_spin
->GetTextValue(&val
) )
61 m_spin
->GetSpinButton()->SetValue(val
);
63 // If we're already processing a text update from m_spin,
64 // don't send it again, since we could end up recursing
66 if (event
.GetId() == m_spin
->GetId())
72 // Send event that the text was manually changed
73 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_spin
->GetId());
74 event
.SetEventObject(m_spin
);
75 event
.SetString(m_spin
->GetText()->GetValue());
78 m_spin
->GetEventHandler()->ProcessEvent(event
);
90 BEGIN_EVENT_TABLE(wxSpinCtrlText
, wxTextCtrl
)
91 EVT_TEXT(-1, wxSpinCtrlText::OnTextChange
)
94 // ----------------------------------------------------------------------------
95 // wxSpinCtrlButton: spin button used by spin control
96 // ----------------------------------------------------------------------------
98 class wxSpinCtrlButton
: public wxSpinButton
101 wxSpinCtrlButton(wxSpinCtrl
*spin
, int style
)
102 : wxSpinButton(spin
)
105 SetWindowStyle(style
| wxSP_VERTICAL
);
107 // TODO: The spin button gets truncated a little bit due to size
108 // differences so change it's default size a bit. SMALL still gets a
109 // bit truncated, but MINI seems to be too small... Readdress this
110 // when the textctrl issues are all sorted out.
111 //SetWindowVariant(wxWINDOW_VARIANT_SMALL);
113 // remove the default minsize, the spinctrl will have one instead
118 void OnSpinButton(wxSpinEvent
& eventSpin
)
120 m_spin
->SetTextValue(eventSpin
.GetPosition());
122 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, m_spin
->GetId());
123 event
.SetEventObject(m_spin
);
124 event
.SetInt(eventSpin
.GetPosition());
126 m_spin
->GetEventHandler()->ProcessEvent(event
);
132 DECLARE_EVENT_TABLE()
135 BEGIN_EVENT_TABLE(wxSpinCtrlButton
, wxSpinButton
)
136 EVT_SPIN(-1, wxSpinCtrlButton::OnSpinButton
)
139 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
, wxControl
)
141 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxControl
)
142 WX_EVENT_TABLE_CONTROL_CONTAINER(wxSpinCtrl
)
145 WX_DELEGATE_TO_CONTROL_CONTAINER(wxSpinCtrl
)
148 // ============================================================================
150 // ============================================================================
152 // ----------------------------------------------------------------------------
153 // wxSpinCtrl creation
154 // ----------------------------------------------------------------------------
156 void wxSpinCtrl::Init()
160 m_container
.SetContainerWindow(this);
163 bool wxSpinCtrl::Create(wxWindow
*parent
,
165 const wxString
& value
,
172 const wxString
& name
)
174 m_macIsUserPane
= true;
175 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
176 wxDefaultValidator
, name
) )
181 // the string value overrides the numeric one (for backwards compatibility
182 // reasons and also because it is simpler to satisfy the string value which
183 // comes much sooner in the list of arguments and leave the initial
184 // parameter unspecified)
185 if ( !value
.empty() )
188 if ( value
.ToLong(&l
) )
192 wxSize csize
= size
;
193 m_text
= new wxSpinCtrlText(this, value
);
194 m_btn
= new wxSpinCtrlButton(this, style
);
196 m_btn
->SetRange(min
, max
);
197 m_btn
->SetValue(initial
);
200 csize
.x
= m_text
->GetSize().x
+ MARGIN
+ m_btn
->GetSize().x
;
203 if ( size
.y
== -1 ) {
204 csize
.y
= m_text
->GetSize().y
+ 2 * TEXTBORDER
; //allow for text border highlights
205 if ( m_btn
->GetSize().y
> csize
.y
)
206 csize
.y
= m_btn
->GetSize().y
;
211 //MacPostControlCreate(pos, csize);
212 SetInitialBestSize(csize
);
217 wxSpinCtrl::~wxSpinCtrl()
219 // delete the controls now, don't leave them alive even though they would
220 // still be eventually deleted by our parent - but it will be too late, the
221 // user code expects them to be gone now
228 // ----------------------------------------------------------------------------
230 // ----------------------------------------------------------------------------
232 wxSize
wxSpinCtrl::DoGetBestSize() const
234 if (!m_btn
|| !m_text
)
237 wxSize sizeBtn
= m_btn
->GetBestSize(),
238 sizeText
= m_text
->GetBestSize();
240 sizeText
.y
+= 2 * TEXTBORDER
;
241 sizeText
.x
+= 2 * TEXTBORDER
;
244 if (sizeText
.y
> sizeBtn
.y
)
249 return wxSize(sizeBtn
.x
+ sizeText
.x
+ MARGIN
, height
);
252 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
254 // position the subcontrols inside the client area
255 wxSize sizeBtn
= m_btn
->GetSize();
256 wxSize sizeText
= m_text
->GetSize();
258 wxControl::DoMoveWindow(x
, y
, width
, height
);
260 wxCoord wText
= width
- sizeBtn
.x
- MARGIN
- 2 * TEXTBORDER
;
262 m_text
->SetSize(TEXTBORDER
, (height
- sizeText
.y
) / 2, wText
, -1);
263 m_btn
->SetSize(0 + wText
+ MARGIN
+ 2 * TEXTBORDER
, (height
- sizeBtn
.y
) / 2 , -1, -1 );
266 // ----------------------------------------------------------------------------
267 // operations forwarded to the subcontrols
268 // ----------------------------------------------------------------------------
270 bool wxSpinCtrl::Enable(bool enable
)
272 if ( !wxControl::Enable(enable
) )
277 bool wxSpinCtrl::Show(bool show
)
279 if ( !wxControl::Show(show
) )
284 // ----------------------------------------------------------------------------
285 // value and range access
286 // ----------------------------------------------------------------------------
288 bool wxSpinCtrl::GetTextValue(int *val
) const
291 if ( !m_text
->GetValue().ToLong(&l
) )
293 // not a number at all
297 if ( l
< GetMin() || l
> GetMax() )
308 int wxSpinCtrl::GetValue() const
310 return m_btn
? m_btn
->GetValue() : 0;
313 int wxSpinCtrl::GetMin() const
315 return m_btn
? m_btn
->GetMin() : 0;
318 int wxSpinCtrl::GetMax() const
320 return m_btn
? m_btn
->GetMax() : 0;
323 // ----------------------------------------------------------------------------
324 // changing value and range
325 // ----------------------------------------------------------------------------
327 void wxSpinCtrl::SetTextValue(int val
)
329 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetTextValue") );
331 m_text
->SetValue(wxString::Format(_T("%d"), val
));
334 m_text
->SetSelection(0, -1);
336 // and give focus to the control!
337 // m_text->SetFocus(); Why???? TODO.
340 void wxSpinCtrl::SetValue(int val
)
342 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetValue") );
346 m_btn
->SetValue(val
);
349 void wxSpinCtrl::SetValue(const wxString
& text
)
351 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetValue") );
354 if ( text
.ToLong(&val
) && ((val
> INT_MIN
) && (val
< INT_MAX
)) )
358 else // not a number at all or out of range
360 m_text
->SetValue(text
);
361 m_text
->SetSelection(0, -1);
365 void wxSpinCtrl::SetRange(int min
, int max
)
367 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetRange") );
369 m_btn
->SetRange(min
, max
);
372 void wxSpinCtrl::SetSelection(long from
, long to
)
374 // if from and to are both -1, it means (in wxWidgets) that all text should
376 if ( (from
== -1) && (to
== -1) )
380 m_text
->SetSelection(from
, to
);
383 #endif // wxUSE_SPINCTRL