]>
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
);
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
);
150 m_text
= new wxSpinCtrlText(this, value
);
151 m_btn
= new wxSpinCtrlButton(this, style
);
153 m_btn
->SetRange(min
, max
);
154 m_btn
->SetValue(initial
);
156 DoSetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
158 DoMoveWindow( 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();
202 wxCoord wText
= width
- sizeBtn
.x
;
203 m_text
->SetSize(x
, y
, wText
, height
);
204 m_btn
->SetSize(x
+ wText
+ MARGIN
, y
, -1, height
);
207 // ----------------------------------------------------------------------------
208 // operations forwarded to the subcontrols
209 // ----------------------------------------------------------------------------
211 bool wxSpinCtrl::Enable(bool enable
)
213 if ( !wxControl::Enable(enable
) )
216 m_btn
->Enable(enable
);
217 m_text
->Enable(enable
);
222 bool wxSpinCtrl::Show(bool show
)
224 if ( !wxControl::Show(show
) )
227 // under GTK Show() is called the first time before we are fully
238 // ----------------------------------------------------------------------------
239 // value and range access
240 // ----------------------------------------------------------------------------
242 bool wxSpinCtrl::GetTextValue(int *val
) const
245 if ( !m_text
->GetValue().ToLong(&l
) )
247 // not a number at all
251 if ( l
< GetMin() || l
> GetMax() )
262 int wxSpinCtrl::GetValue() const
264 return m_btn
? m_btn
->GetValue() : 0;
267 int wxSpinCtrl::GetMin() const
269 return m_btn
? m_btn
->GetMin() : 0;
272 int wxSpinCtrl::GetMax() const
274 return m_btn
? m_btn
->GetMax() : 0;
277 // ----------------------------------------------------------------------------
278 // changing value and range
279 // ----------------------------------------------------------------------------
281 void wxSpinCtrl::SetTextValue(int val
)
283 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetTextValue") );
285 m_text
->SetValue(wxString::Format(_T("%d"), val
));
288 m_text
->SetSelection(0, -1);
290 // and give focus to the control!
294 void wxSpinCtrl::SetValue(int val
)
296 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetValue") );
300 m_btn
->SetValue(val
);
303 void wxSpinCtrl::SetValue(const wxString
& text
)
305 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetValue") );
308 if ( text
.ToLong(&val
) && ((val
> INT_MIN
) && (val
< INT_MAX
)) )
312 else // not a number at all or out of range
314 m_text
->SetValue(text
);
315 m_text
->SetSelection(0, -1);
319 void wxSpinCtrl::SetRange(int min
, int max
)
321 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetRange") );
323 m_btn
->SetRange(min
, max
);
326 #endif // !wxPort-with-native-spinctrl