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
= wxT("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);
85 virtual void DoSetToolTip(wxToolTip
*tip
);
86 #endif // wxUSE_TOOLTIPS
88 // get the subcontrols
89 wxTextCtrl
*GetText() const { return m_textCtrl
; }
90 wxSpinButton
*GetSpinButton() const { return m_spinButton
; }
92 // forwarded events from children windows
93 void OnSpinButton(wxSpinEvent
& event
);
94 void OnTextLostFocus(wxFocusEvent
& event
);
95 void OnTextChar(wxKeyEvent
& event
);
97 // this window itself is used only as a container for its sub windows so it
98 // shouldn't accept the focus at all and any attempts to explicitly set
99 // focus to it should give focus to its text constol part
100 virtual bool AcceptsFocus() const { return false; }
101 virtual void SetFocus();
103 friend class wxSpinCtrlTextGeneric
;
106 // override the base class virtuals involved into geometry calculations
107 virtual wxSize
DoGetBestSize() const;
108 virtual void DoMoveWindow(int x
, int y
, int width
, int height
);
111 // and, for MSW, enabling this window itself
112 virtual void DoEnable(bool enable
);
115 // generic double valued functions
116 double DoGetValue() const { return m_value
; }
117 bool DoSetValue(double val
);
118 void DoSetRange(double min_val
, double max_val
);
119 void DoSetIncrement(double inc
);
121 // update our value to reflect the text control contents (if it has been
122 // modified by user, do nothing otherwise)
124 // can also change the text control if its value is invalid
126 // return true if our value has changed
127 bool SyncSpinToText();
129 // Send the correct event type
130 virtual void DoSendEvent() = 0;
132 // Convert the text to/from the corresponding value.
133 virtual bool DoTextToValue(const wxString
& text
, double *val
) = 0;
134 virtual wxString
DoValueToText(double val
) = 0;
136 // check if the value is in range
137 bool InRange(double n
) const { return (n
>= m_min
) && (n
<= m_max
); }
139 // ensure that the value is in range wrapping it round if necessary
140 double AdjustToFitInRange(double value
) const;
147 bool m_snap_to_ticks
;
152 wxTextCtrl
*m_textCtrl
;
153 wxSpinButton
*m_spinButton
;
156 // common part of all ctors
159 DECLARE_EVENT_TABLE()
162 #else // !wxUSE_SPINBTN
164 #define wxSPINCTRL_GETVALUE_FIX int = 1
166 // ----------------------------------------------------------------------------
167 // wxSpinCtrl is just a text control
168 // ----------------------------------------------------------------------------
170 #include "wx/textctrl.h"
172 class WXDLLIMPEXP_CORE wxSpinCtrlGenericBase
: public wxTextCtrl
175 wxSpinCtrlGenericBase() : m_value(0), m_min(0), m_max(100),
176 m_increment(1), m_snap_to_ticks(false),
177 m_format(wxT("%g")) { }
179 bool Create(wxWindow
*parent
,
180 wxWindowID id
= wxID_ANY
,
181 const wxString
& value
= wxEmptyString
,
182 const wxPoint
& pos
= wxDefaultPosition
,
183 const wxSize
& size
= wxDefaultSize
,
184 long style
= wxSP_ARROW_KEYS
| wxALIGN_RIGHT
,
185 double min
= 0, double max
= 100, double initial
= 0,
187 const wxString
& name
= wxT("wxSpinCtrl"))
194 bool ok
= wxTextCtrl::Create(parent
, id
, value
, pos
, size
, style
,
195 wxDefaultValidator
, name
);
202 // T GetValue() const
205 // T GetIncrement() const
206 virtual bool GetSnapToTicks() const { return m_snap_to_ticks
; }
207 // unsigned GetDigits() const - wxSpinCtrlDouble only
210 virtual void SetValue(const wxString
& text
) { wxTextCtrl::SetValue(text
); }
211 // void SetValue(T val)
212 // void SetRange(T minVal, T maxVal)
213 // void SetIncrement(T inc)
214 virtual void SetSnapToTicks(bool snap_to_ticks
)
215 { m_snap_to_ticks
= snap_to_ticks
; }
216 // void SetDigits(unsigned digits) - wxSpinCtrlDouble only
218 // Select text in the textctrl
219 //void SetSelection(long from, long to);
222 // generic double valued
223 double DoGetValue() const
226 if ( (wxSscanf(wxTextCtrl::GetValue(), wxT("%lf"), &n
) != 1) )
232 bool DoSetValue(double val
)
234 wxTextCtrl::SetValue(wxString::Format(m_format
.c_str(), val
));
237 void DoSetRange(double min_val
, double max_val
)
242 void DoSetIncrement(double inc
) { m_increment
= inc
; } // Note: unused
248 bool m_snap_to_ticks
;
252 #endif // wxUSE_SPINBTN/!wxUSE_SPINBTN
254 #if !defined(wxHAS_NATIVE_SPINCTRL)
256 //-----------------------------------------------------------------------------
258 //-----------------------------------------------------------------------------
260 class WXDLLIMPEXP_CORE wxSpinCtrl
: public wxSpinCtrlGenericBase
263 wxSpinCtrl() { Init(); }
264 wxSpinCtrl(wxWindow
*parent
,
265 wxWindowID id
= wxID_ANY
,
266 const wxString
& value
= wxEmptyString
,
267 const wxPoint
& pos
= wxDefaultPosition
,
268 const wxSize
& size
= wxDefaultSize
,
269 long style
= wxSP_ARROW_KEYS
| wxALIGN_RIGHT
,
270 int min
= 0, int max
= 100, int initial
= 0,
271 const wxString
& name
= wxT("wxSpinCtrl"))
275 Create(parent
, id
, value
, pos
, size
, style
, min
, max
, initial
, name
);
278 bool Create(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
| wxALIGN_RIGHT
,
284 int min
= 0, int max
= 100, int initial
= 0,
285 const wxString
& name
= wxT("wxSpinCtrl"))
287 return wxSpinCtrlGenericBase::Create(parent
, id
, value
, pos
, size
,
288 style
, min
, max
, initial
, 1, name
);
292 int GetValue(wxSPINCTRL_GETVALUE_FIX
) const { return int(DoGetValue()); }
293 int GetMin() const { return int(m_min
); }
294 int GetMax() const { return int(m_max
); }
295 int GetIncrement() const { return int(m_increment
); }
298 void SetValue(const wxString
& value
)
299 { wxSpinCtrlGenericBase::SetValue(value
); }
300 void SetValue( int value
) { DoSetValue(value
); }
301 void SetRange( int minVal
, int maxVal
) { DoSetRange(minVal
, maxVal
); }
302 void SetIncrement(int inc
) { DoSetIncrement(inc
); }
304 virtual int GetBase() const { return m_base
; }
305 virtual bool SetBase(int base
);
308 virtual void DoSendEvent();
310 virtual bool DoTextToValue(const wxString
& text
, double *val
);
311 virtual wxString
DoValueToText(double val
);
314 // Common part of all ctors.
322 DECLARE_DYNAMIC_CLASS(wxSpinCtrl
)
325 #endif // wxHAS_NATIVE_SPINCTRL
327 //-----------------------------------------------------------------------------
329 //-----------------------------------------------------------------------------
331 class WXDLLIMPEXP_CORE wxSpinCtrlDouble
: public wxSpinCtrlGenericBase
334 wxSpinCtrlDouble() { Init(); }
335 wxSpinCtrlDouble(wxWindow
*parent
,
336 wxWindowID id
= wxID_ANY
,
337 const wxString
& value
= wxEmptyString
,
338 const wxPoint
& pos
= wxDefaultPosition
,
339 const wxSize
& size
= wxDefaultSize
,
340 long style
= wxSP_ARROW_KEYS
| wxALIGN_RIGHT
,
341 double min
= 0, double max
= 100, double initial
= 0,
343 const wxString
& name
= wxT("wxSpinCtrlDouble"))
347 Create(parent
, id
, value
, pos
, size
, style
,
348 min
, max
, initial
, inc
, name
);
351 bool Create(wxWindow
*parent
,
352 wxWindowID id
= wxID_ANY
,
353 const wxString
& value
= wxEmptyString
,
354 const wxPoint
& pos
= wxDefaultPosition
,
355 const wxSize
& size
= wxDefaultSize
,
356 long style
= wxSP_ARROW_KEYS
| wxALIGN_RIGHT
,
357 double min
= 0, double max
= 100, double initial
= 0,
359 const wxString
& name
= wxT("wxSpinCtrlDouble"))
361 return wxSpinCtrlGenericBase::Create(parent
, id
, value
, pos
, size
,
362 style
, min
, max
, initial
,
367 double GetValue(wxSPINCTRL_GETVALUE_FIX
) const { return DoGetValue(); }
368 double GetMin() const { return m_min
; }
369 double GetMax() const { return m_max
; }
370 double GetIncrement() const { return m_increment
; }
371 unsigned GetDigits() const { return m_digits
; }
374 void SetValue(const wxString
& value
)
375 { wxSpinCtrlGenericBase::SetValue(value
); }
376 void SetValue(double value
) { DoSetValue(value
); }
377 void SetRange(double minVal
, double maxVal
) { DoSetRange(minVal
, maxVal
); }
378 void SetIncrement(double inc
) { DoSetIncrement(inc
); }
379 void SetDigits(unsigned digits
);
381 // We don't implement bases support for floating point numbers, this is not
382 // very useful in practice.
383 virtual int GetBase() const { return 10; }
384 virtual bool SetBase(int WXUNUSED(base
)) { return 0; }
387 virtual void DoSendEvent();
389 virtual bool DoTextToValue(const wxString
& text
, double *val
);
390 virtual wxString
DoValueToText(double val
);
395 // Common part of all ctors.
399 m_format
= wxS("%g");
404 DECLARE_DYNAMIC_CLASS(wxSpinCtrlDouble
)
407 #endif // _WX_GENERIC_SPINCTRL_H_