]>
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"
38 #include "wx/spinbutt.h"
39 #include "wx/spinctrl.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 // the margin between the text control and the spin
46 static const wxCoord MARGIN
= 2;
48 // ----------------------------------------------------------------------------
49 // wxSpinCtrlText: text control used by spin control
50 // ----------------------------------------------------------------------------
52 class wxSpinCtrlText
: public wxTextCtrl
55 wxSpinCtrlText(wxSpinCtrl
*spin
, const wxString
& value
)
56 : wxTextCtrl(spin
->GetParent(), -1, value
)
62 void OnTextChange(wxCommandEvent
& event
)
65 if ( m_spin
->GetTextValue(&val
) )
67 m_spin
->GetSpinButton()->SetValue(val
);
73 bool ProcessEvent(wxEvent
&event
)
75 // Hand button down events to wxSpinCtrl. Doesn't work.
76 if (event
.GetEventType() == wxEVT_LEFT_DOWN
&& m_spin
->ProcessEvent( event
))
79 return wxTextCtrl::ProcessEvent( event
);
88 BEGIN_EVENT_TABLE(wxSpinCtrlText
, wxTextCtrl
)
89 EVT_TEXT(-1, wxSpinCtrlText::OnTextChange
)
92 // ----------------------------------------------------------------------------
93 // wxSpinCtrlButton: spin button used by spin control
94 // ----------------------------------------------------------------------------
96 class wxSpinCtrlButton
: public wxSpinButton
99 wxSpinCtrlButton(wxSpinCtrl
*spin
, int style
)
100 : wxSpinButton(spin
->GetParent())
104 SetWindowStyle(style
| wxSP_VERTICAL
);
108 void OnSpinButton(wxSpinEvent
& event
)
110 m_spin
->SetTextValue(event
.GetPosition());
118 DECLARE_EVENT_TABLE()
121 BEGIN_EVENT_TABLE(wxSpinCtrlButton
, wxSpinButton
)
122 EVT_SPIN(-1, wxSpinCtrlButton::OnSpinButton
)
125 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
, wxControl
)
127 // ============================================================================
129 // ============================================================================
131 // ----------------------------------------------------------------------------
132 // wxSpinCtrl creation
133 // ----------------------------------------------------------------------------
135 void wxSpinCtrl::Init()
141 bool wxSpinCtrl::Create(wxWindow
*parent
,
143 const wxString
& value
,
150 const wxString
& name
)
152 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
153 wxDefaultValidator
, name
) )
158 // the string value overrides the numeric one (for backwards compatibility
159 // reasons and also because it is simpler to satisfy the string value which
160 // comes much sooner in the list of arguments and leave the initial
161 // parameter unspecified)
162 if ( !value
.empty() )
165 if ( value
.ToLong(&l
) )
169 SetBackgroundColour(*wxRED
);
170 m_text
= new wxSpinCtrlText(this, value
);
171 m_btn
= new wxSpinCtrlButton(this, style
);
173 m_btn
->SetRange(min
, max
);
174 m_btn
->SetValue(initial
);
176 wxSize csize
= size
;
177 if ( size
.y
== -1 ) {
178 csize
.y
= m_text
->GetSize().y
;
180 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
182 DoSetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
184 // have to disable this window to avoid interfering it with message
185 // processing to the text and the button... but pretend it is enabled to
186 // make IsEnabled() return TRUE
187 wxControl::Enable(FALSE
); // don't use non virtual Disable() here!
190 // we don't even need to show this window itself - and not doing it avoids
191 // that it overwrites the text control
192 wxControl::Show(FALSE
);
199 wxSpinCtrl::~wxSpinCtrl()
201 // delete the controls now, don't leave them alive even though they woudl
202 // still be eventually deleted by our parent - but it will be too late, the
203 // user code expects them to be gone now
208 // ----------------------------------------------------------------------------
210 // ----------------------------------------------------------------------------
212 wxSize
wxSpinCtrl::DoGetBestClientSize() const
214 wxSize sizeBtn
= m_btn
->GetBestSize(),
215 sizeText
= m_text
->GetBestSize();
217 return wxSize(sizeBtn
.x
+ sizeText
.x
+ MARGIN
, sizeText
.y
);
220 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
222 wxControl::DoMoveWindow(x
, y
, width
, height
);
224 // position the subcontrols inside the client area
225 wxSize sizeBtn
= m_btn
->GetSize();
227 wxCoord wText
= width
- sizeBtn
.x
;
228 m_text
->SetSize(x
, y
, wText
, height
);
229 m_btn
->SetSize(x
+ wText
+ MARGIN
, y
, -1, height
);
232 // ----------------------------------------------------------------------------
233 // operations forwarded to the subcontrols
234 // ----------------------------------------------------------------------------
236 bool wxSpinCtrl::Enable(bool enable
)
238 if ( !wxControl::Enable(enable
) )
241 m_btn
->Enable(enable
);
242 m_text
->Enable(enable
);
247 bool wxSpinCtrl::Show(bool show
)
249 if ( !wxControl::Show(show
) )
252 // under GTK Show() is called the first time before we are fully
263 // ----------------------------------------------------------------------------
264 // value and range access
265 // ----------------------------------------------------------------------------
267 bool wxSpinCtrl::GetTextValue(int *val
) const
270 if ( !m_text
->GetValue().ToLong(&l
) )
272 // not a number at all
276 if ( l
< GetMin() || l
> GetMax() )
287 int wxSpinCtrl::GetValue() const
289 return m_btn
? m_btn
->GetValue() : 0;
292 int wxSpinCtrl::GetMin() const
294 return m_btn
? m_btn
->GetMin() : 0;
297 int wxSpinCtrl::GetMax() const
299 return m_btn
? m_btn
->GetMax() : 0;
302 // ----------------------------------------------------------------------------
303 // changing value and range
304 // ----------------------------------------------------------------------------
306 void wxSpinCtrl::SetTextValue(int val
)
308 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetTextValue") );
310 m_text
->SetValue(wxString::Format(_T("%d"), val
));
313 m_text
->SetSelection(0, -1);
315 // and give focus to the control!
319 void wxSpinCtrl::SetValue(int val
)
321 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetValue") );
325 m_btn
->SetValue(val
);
328 void wxSpinCtrl::SetValue(const wxString
& text
)
330 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetValue") );
333 if ( text
.ToLong(&val
) && ((val
> INT_MIN
) && (val
< INT_MAX
)) )
337 else // not a number at all or out of range
339 m_text
->SetValue(text
);
340 m_text
->SetSelection(0, -1);
344 void wxSpinCtrl::SetRange(int min
, int max
)
346 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetRange") );
348 m_btn
->SetRange(min
, max
);
351 #endif // !wxPort-with-native-spinctrl