]>
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"
32 #error "This file can only be compiled if wxSpinButton is available"
33 #endif // !wxUSE_SPINBTN
36 #include "wx/textctrl.h"
39 #include "wx/spinbutt.h"
40 #include "wx/spinctrl.h"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // the margin between the text control and the spin
47 static const wxCoord MARGIN
= 2;
49 // ----------------------------------------------------------------------------
50 // wxSpinCtrlText: text control used by spin control
51 // ----------------------------------------------------------------------------
53 class wxSpinCtrlText
: public wxTextCtrl
56 wxSpinCtrlText(wxSpinCtrl
*spin
, const wxString
& value
)
57 : wxTextCtrl(spin
->GetParent(), -1, value
)
63 void OnTextChange(wxCommandEvent
& event
)
66 if ( m_spin
->GetTextValue(&val
) )
68 m_spin
->GetSpinButton()->SetValue(val
);
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
)
92 : wxSpinButton(spin
->GetParent())
96 SetWindowStyle(style
);
100 void OnSpinButton(wxSpinEvent
& event
)
102 m_spin
->SetTextValue(event
.GetPosition());
110 DECLARE_EVENT_TABLE()
113 BEGIN_EVENT_TABLE(wxSpinCtrlButton
, wxSpinButton
)
114 EVT_SPIN(-1, wxSpinCtrlButton::OnSpinButton
)
117 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
, wxControl
)
119 // ============================================================================
121 // ============================================================================
123 // ----------------------------------------------------------------------------
124 // wxSpinCtrl creation
125 // ----------------------------------------------------------------------------
127 void wxSpinCtrl::Init()
133 bool wxSpinCtrl::Create(wxWindow
*parent
,
135 const wxString
& value
,
142 const wxString
& name
)
144 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
145 wxDefaultValidator
, name
) )
150 SetBackgroundColour(*wxRED
);
152 m_text
= new wxSpinCtrlText(this, value
);
153 m_btn
= new wxSpinCtrlButton(this, style
);
155 m_btn
->SetRange(min
, max
);
156 m_btn
->SetValue(initial
);
158 DoSetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
160 // have to disable this window to avoid interfering it with message
161 // processing to the text and the button... but pretend it is enabled to
162 // make IsEnabled() return TRUE
163 wxControl::Enable(FALSE
); // don't use non virtual Disable() here!
166 // we don't even need to show this window itself - and not doing it avoids
167 // that it overwrites the text control
168 wxControl::Show(FALSE
);
174 wxSpinCtrl::~wxSpinCtrl()
176 // delete the controls now, don't leave them alive even though they woudl
177 // still be eventually deleted by our parent - but it will be too late, the
178 // user code expects them to be gone now
183 // ----------------------------------------------------------------------------
185 // ----------------------------------------------------------------------------
187 wxSize
wxSpinCtrl::DoGetBestClientSize() const
189 wxSize sizeBtn
= m_btn
->GetBestSize(),
190 sizeText
= m_text
->GetBestSize();
192 return wxSize(sizeBtn
.x
+ sizeText
.x
+ MARGIN
, sizeText
.y
);
195 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
197 wxControl::DoMoveWindow(x
, y
, width
, height
);
199 // position the subcontrols inside the client area
200 wxSize sizeBtn
= m_btn
->GetSize(),
201 sizeText
= m_text
->GetSize();
203 wxCoord wText
= width
- sizeBtn
.x
;
204 m_text
->SetSize(x
, y
, wText
, height
);
205 m_btn
->SetSize(x
+ wText
+ MARGIN
, y
, -1, height
);
208 // ----------------------------------------------------------------------------
209 // operations forwarded to the subcontrols
210 // ----------------------------------------------------------------------------
212 bool wxSpinCtrl::Enable(bool enable
)
214 if ( !wxControl::Enable(enable
) )
217 m_btn
->Enable(enable
);
218 m_text
->Enable(enable
);
223 bool wxSpinCtrl::Show(bool show
)
225 if ( !wxControl::Show(show
) )
234 // ----------------------------------------------------------------------------
235 // value and range access
236 // ----------------------------------------------------------------------------
238 bool wxSpinCtrl::GetTextValue(int *val
) const
241 if ( !m_text
->GetValue().ToLong(&l
) )
243 // not a number at all
247 if ( l
< GetMin() || l
> GetMax() )
258 int wxSpinCtrl::GetValue() const
260 return m_btn
? m_btn
->GetValue() : 0;
263 int wxSpinCtrl::GetMin() const
265 return m_btn
? m_btn
->GetMin() : 0;
268 int wxSpinCtrl::GetMax() const
270 return m_btn
? m_btn
->GetMax() : 0;
273 // ----------------------------------------------------------------------------
274 // changing value and range
275 // ----------------------------------------------------------------------------
277 void wxSpinCtrl::SetTextValue(int val
)
279 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetTextValue") );
281 m_text
->SetValue(wxString::Format(_T("%d"), val
));
284 m_text
->SetSelection(0, -1);
286 // and give focus to the control!
290 void wxSpinCtrl::SetValue(int val
)
292 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetValue") );
296 m_btn
->SetValue(val
);
299 void wxSpinCtrl::SetValue(const wxString
& text
)
301 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetValue") );
304 if ( text
.ToLong(&val
) && ((val
> INT_MIN
) && (val
< INT_MAX
)) )
308 else // not a number at all or out of range
310 m_text
->SetValue(text
);
311 m_text
->SetSelection(0, -1);
315 void wxSpinCtrl::SetRange(int min
, int max
)
317 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetRange") );
319 m_btn
->SetRange(min
, max
);