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 // check if the value is in range
114 bool InRange(double n
) const { return (n
>= m_min
) && (n
<= m_max
); }
116 // ensure that the value is in range wrapping it round if necessary
117 double AdjustToFitInRange(double value
) const;
124 bool m_snap_to_ticks
;
130 wxTextCtrl
*m_textCtrl
;
131 wxSpinButton
*m_spinButton
;
134 // common part of all ctors
138 #else // !wxUSE_SPINBTN
140 #define wxSPINCTRL_GETVALUE_FIX int = 1
142 // ----------------------------------------------------------------------------
143 // wxSpinCtrl is just a text control
144 // ----------------------------------------------------------------------------
146 #include "wx/textctrl.h"
148 class WXDLLIMPEXP_CORE wxSpinCtrlGenericBase
: public wxTextCtrl
151 wxSpinCtrlGenericBase() : m_value(0), m_min(0), m_max(100),
152 m_increment(1), m_snap_to_ticks(false),
153 m_format(wxT("%g")) { }
155 bool Create(wxWindow
*parent
,
156 wxWindowID id
= wxID_ANY
,
157 const wxString
& value
= wxEmptyString
,
158 const wxPoint
& pos
= wxDefaultPosition
,
159 const wxSize
& size
= wxDefaultSize
,
160 long style
= wxSP_ARROW_KEYS
,
161 double min
= 0, double max
= 100, double initial
= 0, double inc
= 1,
162 const wxString
& name
= _T("wxSpinCtrl"))
169 bool ok
= wxTextCtrl::Create(parent
, id
, value
, pos
, size
, style
,
170 wxDefaultValidator
, name
);
177 // T GetValue() const
180 // T GetIncrement() const
181 virtual bool GetSnapToTicks() const { return m_snap_to_ticks
; }
182 // unsigned GetDigits() const - wxSpinCtrlDouble only
185 virtual void SetValue(const wxString
& text
) { wxTextCtrl::SetValue(text
); }
186 // void SetValue(T val)
187 // void SetRange(T minVal, T maxVal)
188 // void SetIncrement(T inc)
189 virtual void SetSnapToTicks(bool snap_to_ticks
) { m_snap_to_ticks
= snap_to_ticks
; }
190 // void SetDigits(unsigned digits) - wxSpinCtrlDouble only
192 // Select text in the textctrl
193 //void SetSelection(long from, long to);
196 // generic double valued
197 double DoGetValue() const
200 if ( (wxSscanf(wxTextCtrl::GetValue(), wxT("%lf"), &n
) != 1) )
206 bool DoSetValue(double val
) { wxTextCtrl::SetValue(wxString::Format(m_format
.c_str(), val
)); return true; }
207 void DoSetRange(double min_val
, double max_val
) { m_min
= min_val
; m_max
= max_val
; }
208 void DoSetIncrement(double inc
) { m_increment
= inc
; } // Note: unused
214 bool m_snap_to_ticks
;
218 #endif // wxUSE_SPINBTN/!wxUSE_SPINBTN
220 #if !defined(wxHAS_NATIVE_SPINCTRL)
222 //-----------------------------------------------------------------------------
224 //-----------------------------------------------------------------------------
226 class WXDLLIMPEXP_CORE wxSpinCtrl
: public wxSpinCtrlGenericBase
230 wxSpinCtrl(wxWindow
*parent
,
231 wxWindowID id
= wxID_ANY
,
232 const wxString
& value
= wxEmptyString
,
233 const wxPoint
& pos
= wxDefaultPosition
,
234 const wxSize
& size
= wxDefaultSize
,
235 long style
= wxSP_ARROW_KEYS
,
236 int min
= 0, int max
= 100, int initial
= 0,
237 const wxString
& name
= _T("wxSpinCtrl"))
239 Create(parent
, id
, value
, pos
, size
, style
, min
, max
, initial
, name
);
242 bool Create(wxWindow
*parent
,
243 wxWindowID id
= wxID_ANY
,
244 const wxString
& value
= wxEmptyString
,
245 const wxPoint
& pos
= wxDefaultPosition
,
246 const wxSize
& size
= wxDefaultSize
,
247 long style
= wxSP_ARROW_KEYS
,
248 int min
= 0, int max
= 100, int initial
= 0,
249 const wxString
& name
= _T("wxSpinCtrl"))
251 return wxSpinCtrlGenericBase::Create(parent
, id
, value
, pos
, size
, style
, min
, max
, initial
, 1, name
);
255 int GetValue(wxSPINCTRL_GETVALUE_FIX
) const { return wxRound( DoGetValue() ); }
256 int GetMin() const { return wxRound( m_min
); }
257 int GetMax() const { return wxRound( m_max
); }
258 int GetIncrement() const { return wxRound( m_increment
); }
261 void SetValue(const wxString
& value
) { wxSpinCtrlGenericBase::SetValue(value
); } // visibility problem w/ gcc
262 void SetValue( int value
) { DoSetValue(value
); }
263 void SetRange( int minVal
, int maxVal
) { DoSetRange(minVal
, maxVal
); }
264 void SetIncrement( double inc
) { DoSetIncrement(inc
); }
267 virtual void DoSendEvent();
270 DECLARE_DYNAMIC_CLASS(wxSpinCtrl
)
273 #endif // wxHAS_NATIVE_SPINCTRL
275 //-----------------------------------------------------------------------------
277 //-----------------------------------------------------------------------------
279 class WXDLLIMPEXP_CORE wxSpinCtrlDouble
: public wxSpinCtrlGenericBase
282 wxSpinCtrlDouble() : m_digits(0) { }
283 wxSpinCtrlDouble(wxWindow
*parent
,
284 wxWindowID id
= wxID_ANY
,
285 const wxString
& value
= wxEmptyString
,
286 const wxPoint
& pos
= wxDefaultPosition
,
287 const wxSize
& size
= wxDefaultSize
,
288 long style
= wxSP_ARROW_KEYS
,
289 double min
= 0, double max
= 100, double initial
= 0, double inc
= 1,
290 const wxString
& name
= _T("wxSpinCtrlDouble"))
293 Create(parent
, id
, value
, pos
, size
, style
, min
, max
, initial
, inc
, name
);
296 bool Create(wxWindow
*parent
,
297 wxWindowID id
= wxID_ANY
,
298 const wxString
& value
= wxEmptyString
,
299 const wxPoint
& pos
= wxDefaultPosition
,
300 const wxSize
& size
= wxDefaultSize
,
301 long style
= wxSP_ARROW_KEYS
,
302 double min
= 0, double max
= 100, double initial
= 0, double inc
= 1,
303 const wxString
& name
= _T("wxSpinCtrlDouble"))
305 return wxSpinCtrlGenericBase::Create(parent
, id
, value
, pos
, size
, style
, min
, max
, initial
, inc
, name
);
309 double GetValue(wxSPINCTRL_GETVALUE_FIX
) const { return DoGetValue(); }
310 double GetMin() const { return m_min
; }
311 double GetMax() const { return m_max
; }
312 double GetIncrement() const { return m_increment
; }
313 unsigned GetDigits() const { return m_digits
; }
316 void SetValue(const wxString
& value
) { wxSpinCtrlGenericBase::SetValue(value
); } // visibility problem w/ gcc
317 void SetValue(double value
) { DoSetValue(value
); }
318 void SetRange(double minVal
, double maxVal
) { DoSetRange(minVal
, maxVal
); }
319 void SetIncrement(double inc
) { DoSetIncrement(inc
); }
320 void SetDigits(unsigned digits
);
323 virtual void DoSendEvent();
328 DECLARE_DYNAMIC_CLASS(wxSpinCtrlDouble
)
331 #endif // _WX_GENERIC_SPINCTRL_H_