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
| wxALIGN_RIGHT
,
54 double min
= 0, double max
= 100, double initial
= 0,
56 const wxString
& name
= _T("wxSpinCtrl"));
58 virtual ~wxSpinCtrlGenericBase();
64 // T GetIncrement() const
65 virtual bool GetSnapToTicks() const { return m_snap_to_ticks
; }
66 // unsigned GetDigits() const - wxSpinCtrlDouble only
69 virtual void SetValue(const wxString
& text
);
70 // void SetValue(T val)
71 // void SetRange(T minVal, T maxVal)
72 // void SetIncrement(T inc)
73 virtual void SetSnapToTicks(bool snap_to_ticks
);
74 // void SetDigits(unsigned digits) - wxSpinCtrlDouble only
76 // Select text in the textctrl
77 void SetSelection(long from
, long to
);
79 // implementation from now on
81 // forward these functions to all subcontrols
82 virtual bool Enable(bool enable
= true);
83 virtual bool Show(bool show
= true);
84 virtual bool Reparent(wxWindowBase
*newParent
);
86 // get the subcontrols
87 wxTextCtrl
*GetText() const { return m_textCtrl
; }
88 wxSpinButton
*GetSpinButton() const { return m_spinButton
; }
90 // forwarded events from children windows
91 void OnSpinButton(wxSpinEvent
& event
);
92 void OnTextEnter(wxCommandEvent
& event
);
93 void OnTextChar(wxKeyEvent
& event
);
95 friend class wxSpinCtrlTextGeneric
;
98 // override the base class virtuals involved into geometry calculations
99 virtual wxSize
DoGetBestSize() const;
100 virtual void DoMoveWindow(int x
, int y
, int width
, int height
);
102 // generic double valued functions
103 double DoGetValue() const { return m_value
; }
104 bool DoSetValue(double val
);
105 void DoSetRange(double min_val
, double max_val
);
106 void DoSetIncrement(double inc
);
108 // Ensure that the textctrl shows correct value
109 void SyncSpinToText();
111 // Send the correct event type
112 virtual void DoSendEvent() = 0;
114 // check if the value is in range
115 bool InRange(double n
) const { return (n
>= m_min
) && (n
<= m_max
); }
117 // ensure that the value is in range wrapping it round if necessary
118 double AdjustToFitInRange(double value
) const;
125 bool m_snap_to_ticks
;
131 wxTextCtrl
*m_textCtrl
;
132 wxSpinButton
*m_spinButton
;
135 // common part of all ctors
139 #else // !wxUSE_SPINBTN
141 #define wxSPINCTRL_GETVALUE_FIX int = 1
143 // ----------------------------------------------------------------------------
144 // wxSpinCtrl is just a text control
145 // ----------------------------------------------------------------------------
147 #include "wx/textctrl.h"
149 class WXDLLIMPEXP_CORE wxSpinCtrlGenericBase
: public wxTextCtrl
152 wxSpinCtrlGenericBase() : m_value(0), m_min(0), m_max(100),
153 m_increment(1), m_snap_to_ticks(false),
154 m_format(wxT("%g")) { }
156 bool Create(wxWindow
*parent
,
157 wxWindowID id
= wxID_ANY
,
158 const wxString
& value
= wxEmptyString
,
159 const wxPoint
& pos
= wxDefaultPosition
,
160 const wxSize
& size
= wxDefaultSize
,
161 long style
= wxSP_ARROW_KEYS
| wxALIGN_RIGHT
,
162 double min
= 0, double max
= 100, double initial
= 0,
164 const wxString
& name
= _T("wxSpinCtrl"))
171 bool ok
= wxTextCtrl::Create(parent
, id
, value
, pos
, size
, style
,
172 wxDefaultValidator
, name
);
179 // T GetValue() const
182 // T GetIncrement() const
183 virtual bool GetSnapToTicks() const { return m_snap_to_ticks
; }
184 // unsigned GetDigits() const - wxSpinCtrlDouble only
187 virtual void SetValue(const wxString
& text
) { wxTextCtrl::SetValue(text
); }
188 // void SetValue(T val)
189 // void SetRange(T minVal, T maxVal)
190 // void SetIncrement(T inc)
191 virtual void SetSnapToTicks(bool snap_to_ticks
)
192 { m_snap_to_ticks
= snap_to_ticks
; }
193 // void SetDigits(unsigned digits) - wxSpinCtrlDouble only
195 // Select text in the textctrl
196 //void SetSelection(long from, long to);
199 // generic double valued
200 double DoGetValue() const
203 if ( (wxSscanf(wxTextCtrl::GetValue(), wxT("%lf"), &n
) != 1) )
209 bool DoSetValue(double val
)
211 wxTextCtrl::SetValue(wxString::Format(m_format
.c_str(), val
));
214 void DoSetRange(double min_val
, double max_val
)
219 void DoSetIncrement(double inc
) { m_increment
= inc
; } // Note: unused
225 bool m_snap_to_ticks
;
229 #endif // wxUSE_SPINBTN/!wxUSE_SPINBTN
231 #if !defined(wxHAS_NATIVE_SPINCTRL)
233 //-----------------------------------------------------------------------------
235 //-----------------------------------------------------------------------------
237 class WXDLLIMPEXP_CORE wxSpinCtrl
: public wxSpinCtrlGenericBase
241 wxSpinCtrl(wxWindow
*parent
,
242 wxWindowID id
= wxID_ANY
,
243 const wxString
& value
= wxEmptyString
,
244 const wxPoint
& pos
= wxDefaultPosition
,
245 const wxSize
& size
= wxDefaultSize
,
246 long style
= wxSP_ARROW_KEYS
| wxALIGN_RIGHT
,
247 int min
= 0, int max
= 100, int initial
= 0,
248 const wxString
& name
= _T("wxSpinCtrl"))
250 Create(parent
, id
, value
, pos
, size
, style
, min
, max
, initial
, name
);
253 bool Create(wxWindow
*parent
,
254 wxWindowID id
= wxID_ANY
,
255 const wxString
& value
= wxEmptyString
,
256 const wxPoint
& pos
= wxDefaultPosition
,
257 const wxSize
& size
= wxDefaultSize
,
258 long style
= wxSP_ARROW_KEYS
| wxALIGN_RIGHT
,
259 int min
= 0, int max
= 100, int initial
= 0,
260 const wxString
& name
= _T("wxSpinCtrl"))
262 return wxSpinCtrlGenericBase::Create(parent
, id
, value
, pos
, size
,
263 style
, min
, max
, initial
, 1, name
);
267 int GetValue(wxSPINCTRL_GETVALUE_FIX
) const
268 { return wxRound( DoGetValue() ); }
269 int GetMin() const { return wxRound( m_min
); }
270 int GetMax() const { return wxRound( m_max
); }
271 int GetIncrement() const { return wxRound( m_increment
); }
274 void SetValue(const wxString
& value
)
275 { wxSpinCtrlGenericBase::SetValue(value
); }
276 void SetValue( int value
) { DoSetValue(value
); }
277 void SetRange( int minVal
, int maxVal
) { DoSetRange(minVal
, maxVal
); }
278 void SetIncrement( double inc
) { DoSetIncrement(inc
); }
281 virtual void DoSendEvent();
284 DECLARE_DYNAMIC_CLASS(wxSpinCtrl
)
287 #endif // wxHAS_NATIVE_SPINCTRL
289 //-----------------------------------------------------------------------------
291 //-----------------------------------------------------------------------------
293 class WXDLLIMPEXP_CORE wxSpinCtrlDouble
: public wxSpinCtrlGenericBase
296 wxSpinCtrlDouble() : m_digits(0) { }
297 wxSpinCtrlDouble(wxWindow
*parent
,
298 wxWindowID id
= wxID_ANY
,
299 const wxString
& value
= wxEmptyString
,
300 const wxPoint
& pos
= wxDefaultPosition
,
301 const wxSize
& size
= wxDefaultSize
,
302 long style
= wxSP_ARROW_KEYS
| wxALIGN_RIGHT
,
303 double min
= 0, double max
= 100, double initial
= 0,
305 const wxString
& name
= _T("wxSpinCtrlDouble"))
308 Create(parent
, id
, value
, pos
, size
, style
,
309 min
, max
, initial
, inc
, name
);
312 bool Create(wxWindow
*parent
,
313 wxWindowID id
= wxID_ANY
,
314 const wxString
& value
= wxEmptyString
,
315 const wxPoint
& pos
= wxDefaultPosition
,
316 const wxSize
& size
= wxDefaultSize
,
317 long style
= wxSP_ARROW_KEYS
| wxALIGN_RIGHT
,
318 double min
= 0, double max
= 100, double initial
= 0,
320 const wxString
& name
= _T("wxSpinCtrlDouble"))
322 return wxSpinCtrlGenericBase::Create(parent
, id
, value
, pos
, size
,
323 style
, min
, max
, initial
,
328 double GetValue(wxSPINCTRL_GETVALUE_FIX
) const { return DoGetValue(); }
329 double GetMin() const { return m_min
; }
330 double GetMax() const { return m_max
; }
331 double GetIncrement() const { return m_increment
; }
332 unsigned GetDigits() const { return m_digits
; }
335 void SetValue(const wxString
& value
)
336 { wxSpinCtrlGenericBase::SetValue(value
); }
337 void SetValue(double value
) { DoSetValue(value
); }
338 void SetRange(double minVal
, double maxVal
) { DoSetRange(minVal
, maxVal
); }
339 void SetIncrement(double inc
) { DoSetIncrement(inc
); }
340 void SetDigits(unsigned digits
);
343 virtual void DoSendEvent();
348 DECLARE_DYNAMIC_CLASS(wxSpinCtrlDouble
)
351 #endif // _WX_GENERIC_SPINCTRL_H_