]>
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
);
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 SetBackgroundColour(*wxRED
);
159 m_text
= new wxSpinCtrlText(this, value
);
160 m_btn
= new wxSpinCtrlButton(this, style
);
162 m_btn
->SetRange(min
, max
);
163 m_btn
->SetValue(initial
);
165 wxSize csize
= size
;
166 if ( size
.y
== -1 ) {
167 csize
.y
= m_text
->GetSize().y
;
169 DoSetSize(pos
.x
, pos
.y
, csize
.x
, csize
.y
);
171 DoSetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
173 // have to disable this window to avoid interfering it with message
174 // processing to the text and the button... but pretend it is enabled to
175 // make IsEnabled() return TRUE
176 wxControl::Enable(FALSE
); // don't use non virtual Disable() here!
179 // we don't even need to show this window itself - and not doing it avoids
180 // that it overwrites the text control
181 wxControl::Show(FALSE
);
188 wxSpinCtrl::~wxSpinCtrl()
190 // delete the controls now, don't leave them alive even though they woudl
191 // still be eventually deleted by our parent - but it will be too late, the
192 // user code expects them to be gone now
197 // ----------------------------------------------------------------------------
199 // ----------------------------------------------------------------------------
201 wxSize
wxSpinCtrl::DoGetBestClientSize() const
203 wxSize sizeBtn
= m_btn
->GetBestSize(),
204 sizeText
= m_text
->GetBestSize();
206 return wxSize(sizeBtn
.x
+ sizeText
.x
+ MARGIN
, sizeText
.y
);
209 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
211 wxControl::DoMoveWindow(x
, y
, width
, height
);
213 // position the subcontrols inside the client area
214 wxSize sizeBtn
= m_btn
->GetSize();
216 wxCoord wText
= width
- sizeBtn
.x
;
217 m_text
->SetSize(x
, y
, wText
, height
);
218 m_btn
->SetSize(x
+ wText
+ MARGIN
, y
, -1, height
);
221 // ----------------------------------------------------------------------------
222 // operations forwarded to the subcontrols
223 // ----------------------------------------------------------------------------
225 bool wxSpinCtrl::Enable(bool enable
)
227 if ( !wxControl::Enable(enable
) )
230 m_btn
->Enable(enable
);
231 m_text
->Enable(enable
);
236 bool wxSpinCtrl::Show(bool show
)
238 if ( !wxControl::Show(show
) )
241 // under GTK Show() is called the first time before we are fully
252 // ----------------------------------------------------------------------------
253 // value and range access
254 // ----------------------------------------------------------------------------
256 bool wxSpinCtrl::GetTextValue(int *val
) const
259 if ( !m_text
->GetValue().ToLong(&l
) )
261 // not a number at all
265 if ( l
< GetMin() || l
> GetMax() )
276 int wxSpinCtrl::GetValue() const
278 return m_btn
? m_btn
->GetValue() : 0;
281 int wxSpinCtrl::GetMin() const
283 return m_btn
? m_btn
->GetMin() : 0;
286 int wxSpinCtrl::GetMax() const
288 return m_btn
? m_btn
->GetMax() : 0;
291 // ----------------------------------------------------------------------------
292 // changing value and range
293 // ----------------------------------------------------------------------------
295 void wxSpinCtrl::SetTextValue(int val
)
297 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetTextValue") );
299 m_text
->SetValue(wxString::Format(_T("%d"), val
));
302 m_text
->SetSelection(0, -1);
304 // and give focus to the control!
308 void wxSpinCtrl::SetValue(int val
)
310 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetValue") );
314 m_btn
->SetValue(val
);
317 void wxSpinCtrl::SetValue(const wxString
& text
)
319 wxCHECK_RET( m_text
, _T("invalid call to wxSpinCtrl::SetValue") );
322 if ( text
.ToLong(&val
) && ((val
> INT_MIN
) && (val
< INT_MAX
)) )
326 else // not a number at all or out of range
328 m_text
->SetValue(text
);
329 m_text
->SetSelection(0, -1);
333 void wxSpinCtrl::SetRange(int min
, int max
)
335 wxCHECK_RET( m_btn
, _T("invalid call to wxSpinCtrl::SetRange") );
337 m_btn
->SetRange(min
, max
);
340 #endif // !wxPort-with-native-spinctrl