]>
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__)) || \
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
);
79 BEGIN_EVENT_TABLE(wxSpinCtrlText
, wxTextCtrl
)
80 EVT_TEXT(-1, wxSpinCtrlText::OnTextChange
)
83 // ----------------------------------------------------------------------------
84 // wxSpinCtrlButton: spin button used by spin control
85 // ----------------------------------------------------------------------------
87 class wxSpinCtrlButton
: public wxSpinButton
90 wxSpinCtrlButton(wxSpinCtrl
*spin
, int style
)
91 : wxSpinButton(spin
->GetParent())
95 SetWindowStyle(style
);
99 void OnSpinButton(wxSpinEvent
& event
)
101 m_spin
->SetTextValue(event
.GetPosition());
109 DECLARE_EVENT_TABLE()
112 BEGIN_EVENT_TABLE(wxSpinCtrlButton
, wxSpinButton
)
113 EVT_SPIN(-1, wxSpinCtrlButton::OnSpinButton
)
116 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
, wxControl
)
118 // ============================================================================
120 // ============================================================================
122 // ----------------------------------------------------------------------------
123 // wxSpinCtrl creation
124 // ----------------------------------------------------------------------------
126 void wxSpinCtrl::Init()
132 bool wxSpinCtrl::Create(wxWindow
*parent
,
134 const wxString
& value
,
141 const wxString
& name
)
143 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
144 wxDefaultValidator
, name
) )
149 SetBackgroundColour(*wxRED
);
151 m_text
= new wxSpinCtrlText(this, value
);
152 m_btn
= new wxSpinCtrlButton(this, style
);
154 m_btn
->SetRange(min
, max
);
155 m_btn
->SetValue(initial
);
157 DoSetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
159 // have to disable this window to avoid interfering it with message
160 // processing to the text and the button... but pretend it is enabled to
161 // make IsEnabled() return TRUE
162 wxControl::Enable(FALSE
); // don't use non virtual Disable() here!
165 // we don't even need to show this window itself - and not doing it avoids
166 // that it overwrites the text control
167 wxControl::Show(FALSE
);
173 wxSpinCtrl::~wxSpinCtrl()
175 // delete the controls now, don't leave them alive even though they woudl
176 // still be eventually deleted by our parent - but it will be too late, the
177 // user code expects them to be gone now
182 // ----------------------------------------------------------------------------
184 // ----------------------------------------------------------------------------
186 wxSize
wxSpinCtrl::DoGetBestClientSize() const
188 wxSize sizeBtn
= m_btn
->GetBestSize(),
189 sizeText
= m_text
->GetBestSize();
191 return wxSize(sizeBtn
.x
+ sizeText
.x
+ MARGIN
, sizeText
.y
);
194 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
196 wxControl::DoMoveWindow(x
, y
, width
, height
);
198 // position the subcontrols inside the client area
199 wxSize sizeBtn
= m_btn
->GetSize();
201 wxCoord wText
= width
- sizeBtn
.x
;
202 m_text
->SetSize(x
, y
, wText
, height
);
203 m_btn
->SetSize(x
+ wText
+ MARGIN
, y
, -1, height
);
206 // ----------------------------------------------------------------------------
207 // operations forwarded to the subcontrols
208 // ----------------------------------------------------------------------------
210 bool wxSpinCtrl::Enable(bool enable
)
212 if ( !wxControl::Enable(enable
) )
215 m_btn
->Enable(enable
);
216 m_text
->Enable(enable
);
221 bool wxSpinCtrl::Show(bool show
)
223 if ( !wxControl::Show(show
) )
226 // under GTK Show() is called the first time before we are fully
237 // ----------------------------------------------------------------------------
238 // value and range access
239 // ----------------------------------------------------------------------------
241 bool wxSpinCtrl::GetTextValue(int *val
) const
244 if ( !m_text
->GetValue().ToLong(&l
) )
246 // not a number at all
250 if ( l
< GetMin() || l
> GetMax() )
261 int wxSpinCtrl::GetValue() const
263 return m_btn
? m_btn
->GetValue() : 0;
266 int wxSpinCtrl::GetMin() const
268 return m_btn
? m_btn
->GetMin() : 0;
271 int wxSpinCtrl::GetMax() const
273 return m_btn
? m_btn
->GetMax() : 0;
276 // ----------------------------------------------------------------------------
277 // changing value and range
278 // ----------------------------------------------------------------------------
280 void wxSpinCtrl::SetTextValue(int val
)
282 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetTextValue") );
284 m_text
->SetValue(wxString::Format(_T("%d"), val
));
287 m_text
->SetSelection(0, -1);
289 // and give focus to the control!
293 void wxSpinCtrl::SetValue(int val
)
295 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetValue") );
299 m_btn
->SetValue(val
);
302 void wxSpinCtrl::SetValue(const wxString
& text
)
304 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetValue") );
307 if ( text
.ToLong(&val
) && ((val
> INT_MIN
) && (val
< INT_MAX
)) )
311 else // not a number at all or out of range
313 m_text
->SetValue(text
);
314 m_text
->SetSelection(0, -1);
318 void wxSpinCtrl::SetRange(int min
, int max
)
320 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetRange") );
322 m_btn
->SetRange(min
, max
);
325 #endif // !wxPort-with-native-spinctrl