1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/generic/spinctlg.h
3 // Purpose: generic wxSpinCtrl class
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_GENERIC_SPINCTRL_H_
13 #define _WX_GENERIC_SPINCTRL_H_
15 // ----------------------------------------------------------------------------
16 // wxSpinCtrl is a combination of wxSpinButton and wxTextCtrl, so if
17 // wxSpinButton is available, this is what we do - but if it isn't, we still
18 // define wxSpinCtrl class which then has the same appearance as wxTextCtrl but
19 // the different interface. This allows to write programs using wxSpinCtrl
20 // without tons of #ifdefs.
21 // ----------------------------------------------------------------------------
25 class WXDLLIMPEXP_FWD_CORE wxSpinButton
;
26 class WXDLLIMPEXP_FWD_CORE wxTextCtrl
;
28 class wxSpinCtrlTextGeneric
; // wxTextCtrl used for the wxSpinCtrlGenericBase
30 // The !wxUSE_SPINBTN version's GetValue() function conflicts with the
31 // wxTextCtrl's GetValue() and so you have to input a dummy int value.
32 #define wxSPINCTRL_GETVALUE_FIX
34 // ----------------------------------------------------------------------------
35 // wxSpinCtrlGeneric is a combination of wxTextCtrl and wxSpinButton
37 // This class manages a double valued generic spinctrl through the DoGet/SetXXX
38 // functions that are made public as Get/SetXXX functions for int or double
39 // for the wxSpinCtrl and wxSpinCtrlDouble classes respectively to avoid
40 // function ambiguity.
41 // ----------------------------------------------------------------------------
43 class WXDLLIMPEXP_CORE wxSpinCtrlGenericBase
: public wxSpinCtrlBase
46 wxSpinCtrlGenericBase() { Init(); }
48 bool Create(wxWindow
*parent
,
49 wxWindowID id
= wxID_ANY
,
50 const wxString
& value
= wxEmptyString
,
51 const wxPoint
& pos
= wxDefaultPosition
,
52 const wxSize
& size
= wxDefaultSize
,
53 long style
= wxSP_ARROW_KEYS
,
54 double min
= 0, double max
= 100, double initial
= 0, double inc
= 1,
55 const wxString
& name
= _T("wxSpinCtrl"));
57 virtual ~wxSpinCtrlGenericBase();
63 // T GetIncrement() const
64 virtual bool GetSnapToTicks() const { return m_snap_to_ticks
; }
65 // unsigned GetDigits() const - wxSpinCtrlDouble only
68 virtual void SetValue(const wxString
& text
);
69 // void SetValue(T val)
70 // void SetRange(T minVal, T maxVal)
71 // void SetIncrement(T inc)
72 virtual void SetSnapToTicks(bool snap_to_ticks
);
73 // void SetDigits(unsigned digits) - wxSpinCtrlDouble only
75 // Select text in the textctrl
76 void SetSelection(long from
, long to
);
78 // implementation from now on
80 // forward these functions to all subcontrols
81 virtual bool Enable(bool enable
= true);
82 virtual bool Show(bool show
= true);
83 virtual bool Reparent(wxWindowBase
*newParent
);
85 // get the subcontrols
86 wxTextCtrl
*GetText() const { return m_textCtrl
; }
87 wxSpinButton
*GetSpinButton() const { return m_spinButton
; }
89 // forwarded events from children windows
90 void OnSpinButton(wxSpinEvent
& event
);
91 void OnTextEnter(wxCommandEvent
& event
);
92 void OnTextChar(wxKeyEvent
& event
);
94 friend class wxSpinCtrlTextGeneric
;
97 // override the base class virtuals involved into geometry calculations
98 virtual wxSize
DoGetBestSize() const;
99 virtual void DoMoveWindow(int x
, int y
, int width
, int height
);
101 // generic double valued functions
102 double DoGetValue() const { return m_value
; }
103 bool DoSetValue(double val
);
104 void DoSetRange(double min_val
, double max_val
);
105 void DoSetIncrement(double inc
);
107 // Ensure that the textctrl shows correct value
108 void SyncSpinToText();
110 // Send the correct event type
111 virtual void DoSendEvent() = 0;
113 bool InRange(double n
) const { return (n
>= m_min
) && (n
<= m_max
); }
119 bool m_snap_to_ticks
;
125 wxTextCtrl
*m_textCtrl
;
126 wxSpinButton
*m_spinButton
;
129 // common part of all ctors
133 #else // !wxUSE_SPINBTN
135 #define wxSPINCTRL_GETVALUE_FIX int = 1
137 // ----------------------------------------------------------------------------
138 // wxSpinCtrl is just a text control
139 // ----------------------------------------------------------------------------
141 #include "wx/textctrl.h"
143 class WXDLLIMPEXP_CORE wxSpinCtrlGenericBase
: public wxTextCtrl
146 wxSpinCtrlGenericBase() : m_value(0), m_min(0), m_max(100),
147 m_increment(1), m_snap_to_ticks(false),
148 m_format(wxT("%g")) { }
150 bool Create(wxWindow
*parent
,
151 wxWindowID id
= wxID_ANY
,
152 const wxString
& value
= wxEmptyString
,
153 const wxPoint
& pos
= wxDefaultPosition
,
154 const wxSize
& size
= wxDefaultSize
,
155 long style
= wxSP_ARROW_KEYS
,
156 double min
= 0, double max
= 100, double initial
= 0, double inc
= 1,
157 const wxString
& name
= _T("wxSpinCtrl"))
164 bool ok
= wxTextCtrl::Create(parent
, id
, value
, pos
, size
, style
,
165 wxDefaultValidator
, name
);
172 // T GetValue() const
175 // T GetIncrement() const
176 virtual bool GetSnapToTicks() const { return m_snap_to_ticks
; }
177 // unsigned GetDigits() const - wxSpinCtrlDouble only
180 virtual void SetValue(const wxString
& text
) { wxTextCtrl::SetValue(text
); }
181 // void SetValue(T val)
182 // void SetRange(T minVal, T maxVal)
183 // void SetIncrement(T inc)
184 virtual void SetSnapToTicks(bool snap_to_ticks
) { m_snap_to_ticks
= snap_to_ticks
; }
185 // void SetDigits(unsigned digits) - wxSpinCtrlDouble only
187 // Select text in the textctrl
188 //void SetSelection(long from, long to);
191 // generic double valued
192 double DoGetValue() const
195 if ( (wxSscanf(wxTextCtrl::GetValue(), wxT("%lf"), &n
) != 1) )
201 bool DoSetValue(double val
) { wxTextCtrl::SetValue(wxString::Format(m_format
.c_str(), val
)); return true; }
202 void DoSetRange(double min_val
, double max_val
) { m_min
= min_val
; m_max
= max_val
; }
203 void DoSetIncrement(double inc
) { m_increment
= inc
; } // Note: unused
209 bool m_snap_to_ticks
;
213 #endif // wxUSE_SPINBTN/!wxUSE_SPINBTN
215 #if !defined(wxHAS_NATIVE_SPINCTRL)
217 //-----------------------------------------------------------------------------
219 //-----------------------------------------------------------------------------
221 class WXDLLIMPEXP_CORE wxSpinCtrl
: public wxSpinCtrlGenericBase
225 wxSpinCtrl(wxWindow
*parent
,
226 wxWindowID id
= wxID_ANY
,
227 const wxString
& value
= wxEmptyString
,
228 const wxPoint
& pos
= wxDefaultPosition
,
229 const wxSize
& size
= wxDefaultSize
,
230 long style
= wxSP_ARROW_KEYS
,
231 int min
= 0, int max
= 100, int initial
= 0,
232 const wxString
& name
= _T("wxSpinCtrl"))
234 Create(parent
, id
, value
, pos
, size
, style
, min
, max
, initial
, name
);
237 bool Create(wxWindow
*parent
,
238 wxWindowID id
= wxID_ANY
,
239 const wxString
& value
= wxEmptyString
,
240 const wxPoint
& pos
= wxDefaultPosition
,
241 const wxSize
& size
= wxDefaultSize
,
242 long style
= wxSP_ARROW_KEYS
,
243 int min
= 0, int max
= 100, int initial
= 0,
244 const wxString
& name
= _T("wxSpinCtrl"))
246 return wxSpinCtrlGenericBase::Create(parent
, id
, value
, pos
, size
, style
, min
, max
, initial
, 1, name
);
250 int GetValue(wxSPINCTRL_GETVALUE_FIX
) const { return wxRound( DoGetValue() ); }
251 int GetMin() const { return wxRound( m_min
); }
252 int GetMax() const { return wxRound( m_max
); }
253 int GetIncrement() const { return wxRound( m_increment
); }
256 void SetValue(const wxString
& value
) { wxSpinCtrlGenericBase::SetValue(value
); } // visibility problem w/ gcc
257 void SetValue( int value
) { DoSetValue(value
); }
258 void SetRange( int minVal
, int maxVal
) { DoSetRange(minVal
, maxVal
); }
259 void SetIncrement( double inc
) { DoSetIncrement(inc
); }
262 virtual void DoSendEvent();
265 DECLARE_DYNAMIC_CLASS(wxSpinCtrl
)
268 #endif // wxHAS_NATIVE_SPINCTRL
270 //-----------------------------------------------------------------------------
272 //-----------------------------------------------------------------------------
274 class WXDLLIMPEXP_CORE wxSpinCtrlDouble
: public wxSpinCtrlGenericBase
277 wxSpinCtrlDouble() : m_digits(0) { }
278 wxSpinCtrlDouble(wxWindow
*parent
,
279 wxWindowID id
= wxID_ANY
,
280 const wxString
& value
= wxEmptyString
,
281 const wxPoint
& pos
= wxDefaultPosition
,
282 const wxSize
& size
= wxDefaultSize
,
283 long style
= wxSP_ARROW_KEYS
,
284 double min
= 0, double max
= 100, double initial
= 0, double inc
= 1,
285 const wxString
& name
= _T("wxSpinCtrlDouble"))
288 Create(parent
, id
, value
, pos
, size
, style
, min
, max
, initial
, inc
, name
);
291 bool Create(wxWindow
*parent
,
292 wxWindowID id
= wxID_ANY
,
293 const wxString
& value
= wxEmptyString
,
294 const wxPoint
& pos
= wxDefaultPosition
,
295 const wxSize
& size
= wxDefaultSize
,
296 long style
= wxSP_ARROW_KEYS
,
297 double min
= 0, double max
= 100, double initial
= 0, double inc
= 1,
298 const wxString
& name
= _T("wxSpinCtrlDouble"))
300 return wxSpinCtrlGenericBase::Create(parent
, id
, value
, pos
, size
, style
, min
, max
, initial
, inc
, name
);
304 double GetValue(wxSPINCTRL_GETVALUE_FIX
) const { return DoGetValue(); }
305 double GetMin() const { return m_min
; }
306 double GetMax() const { return m_max
; }
307 double GetIncrement() const { return m_increment
; }
308 unsigned GetDigits() const { return m_digits
; }
311 void SetValue(const wxString
& value
) { wxSpinCtrlGenericBase::SetValue(value
); } // visibility problem w/ gcc
312 void SetValue(double value
) { DoSetValue(value
); }
313 void SetRange(double minVal
, double maxVal
) { DoSetRange(minVal
, maxVal
); }
314 void SetIncrement(double inc
) { DoSetIncrement(inc
); }
315 void SetDigits(unsigned digits
);
318 virtual void DoSendEvent();
323 DECLARE_DYNAMIC_CLASS(wxSpinCtrlDouble
)
326 #endif // _WX_GENERIC_SPINCTRL_H_