]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/spinctlg.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/spinctlg.cpp
3 // Purpose: implements wxSpinCtrl as a composite control
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // License: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "spinctlg.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
31 #if !(defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXPM__)) || defined(__WXMAC__) || \
32 defined(__WXUNIVERSAL__)
35 #include "wx/textctrl.h"
40 #include "wx/spinbutt.h"
41 #include "wx/spinctrl.h"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 // the margin between the text control and the spin
48 static const wxCoord MARGIN
= 2;
50 // ----------------------------------------------------------------------------
51 // wxSpinCtrlText: text control used by spin control
52 // ----------------------------------------------------------------------------
54 class wxSpinCtrlText
: public wxTextCtrl
57 wxSpinCtrlText(wxSpinCtrl
*spin
, const wxString
& value
)
58 : wxTextCtrl(spin
->GetParent(), -1, value
)
64 void OnTextChange(wxCommandEvent
& event
)
67 if ( m_spin
->GetTextValue(&val
) )
69 m_spin
->GetSpinButton()->SetValue(val
);
75 bool ProcessEvent(wxEvent
&event
)
77 // Hand button down events to wxSpinCtrl. Doesn't work.
78 if (event
.GetEventType() == wxEVT_LEFT_DOWN
&& m_spin
->ProcessEvent( event
))
81 return wxTextCtrl::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
->GetParent())
106 SetWindowStyle(style
| wxSP_VERTICAL
);
110 void OnSpinButton(wxSpinEvent
& event
)
112 m_spin
->SetTextValue(event
.GetPosition());
120 DECLARE_EVENT_TABLE()
123 BEGIN_EVENT_TABLE(wxSpinCtrlButton
, wxSpinButton
)
124 EVT_SPIN(-1, wxSpinCtrlButton::OnSpinButton
)
127 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
, wxControl
)
129 // ============================================================================
131 // ============================================================================
133 // ----------------------------------------------------------------------------
134 // wxSpinCtrl creation
135 // ----------------------------------------------------------------------------
137 void wxSpinCtrl::Init()
143 bool wxSpinCtrl::Create(wxWindow
*parent
,
145 const wxString
& value
,
152 const wxString
& name
)
154 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
155 wxDefaultValidator
, name
) )
160 // the string value overrides the numeric one (for backwards compatibility
161 // reasons and also because it is simpler to satisfy the string value which
162 // comes much sooner in the list of arguments and leave the initial
163 // parameter unspecified)
164 if ( !value
.empty() )
167 if ( value
.ToLong(&l
) )
171 SetBackgroundColour(*wxRED
);
172 m_text
= new wxSpinCtrlText(this, value
);
173 m_btn
= new wxSpinCtrlButton(this, style
);
175 m_btn
->SetRange(min
, max
);
176 m_btn
->SetValue(initial
);
178 wxSize csize
= size
;
179 if ( size
.y
== -1 ) {
180 csize
.y
= m_text
->GetSize().y
;
182 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
184 DoSetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
186 // have to disable this window to avoid interfering it with message
187 // processing to the text and the button... but pretend it is enabled to
188 // make IsEnabled() return TRUE
189 wxControl::Enable(FALSE
); // don't use non virtual Disable() here!
192 // we don't even need to show this window itself - and not doing it avoids
193 // that it overwrites the text control
194 wxControl::Show(FALSE
);
201 wxSpinCtrl::~wxSpinCtrl()
203 // delete the controls now, don't leave them alive even though they woudl
204 // still be eventually deleted by our parent - but it will be too late, the
205 // user code expects them to be gone now
210 // ----------------------------------------------------------------------------
212 // ----------------------------------------------------------------------------
214 wxSize
wxSpinCtrl::DoGetBestClientSize() const
216 wxSize sizeBtn
= m_btn
->GetBestSize(),
217 sizeText
= m_text
->GetBestSize();
219 return wxSize(sizeBtn
.x
+ sizeText
.x
+ MARGIN
, sizeText
.y
);
222 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
224 wxControl::DoMoveWindow(x
, y
, width
, height
);
226 // position the subcontrols inside the client area
227 wxSize sizeBtn
= m_btn
->GetSize();
229 wxCoord wText
= width
- sizeBtn
.x
;
230 m_text
->SetSize(x
, y
, wText
, height
);
232 m_btn
->SetSize(x
+ wText
+ MARGIN
, y
, -1, -1);
234 m_btn
->SetSize(x
+ wText
+ MARGIN
, y
, -1, height
);
238 // ----------------------------------------------------------------------------
239 // operations forwarded to the subcontrols
240 // ----------------------------------------------------------------------------
242 bool wxSpinCtrl::Enable(bool enable
)
244 if ( !wxControl::Enable(enable
) )
247 m_btn
->Enable(enable
);
248 m_text
->Enable(enable
);
253 bool wxSpinCtrl::Show(bool show
)
255 if ( !wxControl::Show(show
) )
258 // under GTK Show() is called the first time before we are fully
269 // ----------------------------------------------------------------------------
270 // value and range access
271 // ----------------------------------------------------------------------------
273 bool wxSpinCtrl::GetTextValue(int *val
) const
276 if ( !m_text
->GetValue().ToLong(&l
) )
278 // not a number at all
282 if ( l
< GetMin() || l
> GetMax() )
293 int wxSpinCtrl::GetValue() const
295 return m_btn
? m_btn
->GetValue() : 0;
298 int wxSpinCtrl::GetMin() const
300 return m_btn
? m_btn
->GetMin() : 0;
303 int wxSpinCtrl::GetMax() const
305 return m_btn
? m_btn
->GetMax() : 0;
308 // ----------------------------------------------------------------------------
309 // changing value and range
310 // ----------------------------------------------------------------------------
312 void wxSpinCtrl::SetTextValue(int val
)
314 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetTextValue") );
316 m_text
->SetValue(wxString::Format(_T("%d"), val
));
319 m_text
->SetSelection(0, -1);
321 // and give focus to the control!
325 void wxSpinCtrl::SetValue(int val
)
327 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetValue") );
331 m_btn
->SetValue(val
);
334 void wxSpinCtrl::SetValue(const wxString
& text
)
336 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetValue") );
339 if ( text
.ToLong(&val
) && ((val
> INT_MIN
) && (val
< INT_MAX
)) )
343 else // not a number at all or out of range
345 m_text
->SetValue(text
);
346 m_text
->SetSelection(0, -1);
350 void wxSpinCtrl::SetRange(int min
, int max
)
352 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetRange") );
354 m_btn
->SetRange(min
, max
);
357 #endif // wxUSE_SPINCTRL
358 #endif // !wxPort-with-native-spinctrl