1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/generic/spinctlg.h
3 // Purpose: generic wxSpinCtrl class
4 // Author: Vadim Zeitlin
7 // Copyright: (c) Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_GENERIC_SPINCTRL_H_
12 #define _WX_GENERIC_SPINCTRL_H_
14 // ----------------------------------------------------------------------------
15 // wxSpinCtrl is a combination of wxSpinButton and wxTextCtrl, so if
16 // wxSpinButton is available, this is what we do - but if it isn't, we still
17 // define wxSpinCtrl class which then has the same appearance as wxTextCtrl but
18 // the different interface. This allows to write programs using wxSpinCtrl
19 // without tons of #ifdefs.
20 // ----------------------------------------------------------------------------
24 #include "wx/compositewin.h"
26 class WXDLLIMPEXP_FWD_CORE wxSpinButton
;
27 class WXDLLIMPEXP_FWD_CORE wxTextCtrl
;
29 class wxSpinCtrlTextGeneric
; // wxTextCtrl used for the wxSpinCtrlGenericBase
31 // The !wxUSE_SPINBTN version's GetValue() function conflicts with the
32 // wxTextCtrl's GetValue() and so you have to input a dummy int value.
33 #define wxSPINCTRL_GETVALUE_FIX
35 // ----------------------------------------------------------------------------
36 // wxSpinCtrlGeneric is a combination of wxTextCtrl and wxSpinButton
38 // This class manages a double valued generic spinctrl through the DoGet/SetXXX
39 // functions that are made public as Get/SetXXX functions for int or double
40 // for the wxSpinCtrl and wxSpinCtrlDouble classes respectively to avoid
41 // function ambiguity.
42 // ----------------------------------------------------------------------------
44 class WXDLLIMPEXP_CORE wxSpinCtrlGenericBase
45 : public wxNavigationEnabled
<wxCompositeWindow
<wxSpinCtrlBase
> >
48 wxSpinCtrlGenericBase() { Init(); }
50 bool Create(wxWindow
*parent
,
51 wxWindowID id
= wxID_ANY
,
52 const wxString
& value
= wxEmptyString
,
53 const wxPoint
& pos
= wxDefaultPosition
,
54 const wxSize
& size
= wxDefaultSize
,
55 long style
= wxSP_ARROW_KEYS
| wxALIGN_RIGHT
,
56 double min
= 0, double max
= 100, double initial
= 0,
58 const wxString
& name
= wxT("wxSpinCtrl"));
60 virtual ~wxSpinCtrlGenericBase();
66 // T GetIncrement() const
67 virtual bool GetSnapToTicks() const { return m_snap_to_ticks
; }
68 // unsigned GetDigits() const - wxSpinCtrlDouble only
71 virtual void SetValue(const wxString
& text
);
72 // void SetValue(T val)
73 // void SetRange(T minVal, T maxVal)
74 // void SetIncrement(T inc)
75 virtual void SetSnapToTicks(bool snap_to_ticks
);
76 // void SetDigits(unsigned digits) - wxSpinCtrlDouble only
78 // Select text in the textctrl
79 void SetSelection(long from
, long to
);
81 // implementation from now on
83 // forward these functions to all subcontrols
84 virtual bool Enable(bool enable
= true);
85 virtual bool Show(bool show
= true);
87 virtual void DoSetToolTip(wxToolTip
*tip
);
88 #endif // wxUSE_TOOLTIPS
90 virtual bool SetBackgroundColour(const wxColour
& colour
);
92 // get the subcontrols
93 wxTextCtrl
*GetText() const { return m_textCtrl
; }
94 wxSpinButton
*GetSpinButton() const { return m_spinButton
; }
96 // forwarded events from children windows
97 void OnSpinButton(wxSpinEvent
& event
);
98 void OnTextLostFocus(wxFocusEvent
& event
);
99 void OnTextChar(wxKeyEvent
& event
);
101 // this window itself is used only as a container for its sub windows so it
102 // shouldn't accept the focus at all and any attempts to explicitly set
103 // focus to it should give focus to its text constol part
104 virtual bool AcceptsFocus() const { return false; }
105 virtual void SetFocus();
107 friend class wxSpinCtrlTextGeneric
;
110 // override the base class virtuals involved into geometry calculations
111 virtual wxSize
DoGetBestSize() const;
112 virtual wxSize
DoGetSizeFromTextSize(int xlen
, int ylen
= -1) const;
113 virtual void DoMoveWindow(int x
, int y
, int width
, int height
);
116 // and, for MSW, enabling this window itself
117 virtual void DoEnable(bool enable
);
120 // generic double valued functions
121 double DoGetValue() const { return m_value
; }
122 bool DoSetValue(double val
);
123 void DoSetRange(double min_val
, double max_val
);
124 void DoSetIncrement(double inc
);
126 // update our value to reflect the text control contents (if it has been
127 // modified by user, do nothing otherwise)
129 // can also change the text control if its value is invalid
131 // return true if our value has changed
132 bool SyncSpinToText();
134 // Send the correct event type
135 virtual void DoSendEvent() = 0;
137 // Convert the text to/from the corresponding value.
138 virtual bool DoTextToValue(const wxString
& text
, double *val
) = 0;
139 virtual wxString
DoValueToText(double val
) = 0;
141 // check if the value is in range
142 bool InRange(double n
) const { return (n
>= m_min
) && (n
<= m_max
); }
144 // ensure that the value is in range wrapping it round if necessary
145 double AdjustToFitInRange(double value
) const;
152 bool m_snap_to_ticks
;
157 wxTextCtrl
*m_textCtrl
;
158 wxSpinButton
*m_spinButton
;
161 // common part of all ctors
164 // Implement pure virtual function inherited from wxCompositeWindow.
165 virtual wxWindowList
GetCompositeWindowParts() const;
167 DECLARE_EVENT_TABLE()
170 #else // !wxUSE_SPINBTN
172 #define wxSPINCTRL_GETVALUE_FIX int = 1
174 // ----------------------------------------------------------------------------
175 // wxSpinCtrl is just a text control
176 // ----------------------------------------------------------------------------
178 #include "wx/textctrl.h"
180 class WXDLLIMPEXP_CORE wxSpinCtrlGenericBase
: public wxTextCtrl
183 wxSpinCtrlGenericBase() : m_value(0), m_min(0), m_max(100),
184 m_increment(1), m_snap_to_ticks(false),
185 m_format(wxT("%g")) { }
187 bool Create(wxWindow
*parent
,
188 wxWindowID id
= wxID_ANY
,
189 const wxString
& value
= wxEmptyString
,
190 const wxPoint
& pos
= wxDefaultPosition
,
191 const wxSize
& size
= wxDefaultSize
,
192 long style
= wxSP_ARROW_KEYS
| wxALIGN_RIGHT
,
193 double min
= 0, double max
= 100, double initial
= 0,
195 const wxString
& name
= wxT("wxSpinCtrl"))
202 bool ok
= wxTextCtrl::Create(parent
, id
, value
, pos
, size
, style
,
203 wxDefaultValidator
, name
);
210 // T GetValue() const
213 // T GetIncrement() const
214 virtual bool GetSnapToTicks() const { return m_snap_to_ticks
; }
215 // unsigned GetDigits() const - wxSpinCtrlDouble only
218 virtual void SetValue(const wxString
& text
) { wxTextCtrl::SetValue(text
); }
219 // void SetValue(T val)
220 // void SetRange(T minVal, T maxVal)
221 // void SetIncrement(T inc)
222 virtual void SetSnapToTicks(bool snap_to_ticks
)
223 { m_snap_to_ticks
= snap_to_ticks
; }
224 // void SetDigits(unsigned digits) - wxSpinCtrlDouble only
226 // Select text in the textctrl
227 //void SetSelection(long from, long to);
230 // generic double valued
231 double DoGetValue() const
234 if ( (wxSscanf(wxTextCtrl::GetValue(), wxT("%lf"), &n
) != 1) )
240 bool DoSetValue(double val
)
242 wxTextCtrl::SetValue(wxString::Format(m_format
.c_str(), val
));
245 void DoSetRange(double min_val
, double max_val
)
250 void DoSetIncrement(double inc
) { m_increment
= inc
; } // Note: unused
256 bool m_snap_to_ticks
;
260 #endif // wxUSE_SPINBTN/!wxUSE_SPINBTN
262 #if !defined(wxHAS_NATIVE_SPINCTRL)
264 //-----------------------------------------------------------------------------
266 //-----------------------------------------------------------------------------
268 class WXDLLIMPEXP_CORE wxSpinCtrl
: public wxSpinCtrlGenericBase
271 wxSpinCtrl() { Init(); }
272 wxSpinCtrl(wxWindow
*parent
,
273 wxWindowID id
= wxID_ANY
,
274 const wxString
& value
= wxEmptyString
,
275 const wxPoint
& pos
= wxDefaultPosition
,
276 const wxSize
& size
= wxDefaultSize
,
277 long style
= wxSP_ARROW_KEYS
| wxALIGN_RIGHT
,
278 int min
= 0, int max
= 100, int initial
= 0,
279 const wxString
& name
= wxT("wxSpinCtrl"))
283 Create(parent
, id
, value
, pos
, size
, style
, min
, max
, initial
, name
);
286 bool Create(wxWindow
*parent
,
287 wxWindowID id
= wxID_ANY
,
288 const wxString
& value
= wxEmptyString
,
289 const wxPoint
& pos
= wxDefaultPosition
,
290 const wxSize
& size
= wxDefaultSize
,
291 long style
= wxSP_ARROW_KEYS
| wxALIGN_RIGHT
,
292 int min
= 0, int max
= 100, int initial
= 0,
293 const wxString
& name
= wxT("wxSpinCtrl"))
295 return wxSpinCtrlGenericBase::Create(parent
, id
, value
, pos
, size
,
296 style
, min
, max
, initial
, 1, name
);
300 int GetValue(wxSPINCTRL_GETVALUE_FIX
) const { return int(DoGetValue()); }
301 int GetMin() const { return int(m_min
); }
302 int GetMax() const { return int(m_max
); }
303 int GetIncrement() const { return int(m_increment
); }
306 void SetValue(const wxString
& value
)
307 { wxSpinCtrlGenericBase::SetValue(value
); }
308 void SetValue( int value
) { DoSetValue(value
); }
309 void SetRange( int minVal
, int maxVal
) { DoSetRange(minVal
, maxVal
); }
310 void SetIncrement(int inc
) { DoSetIncrement(inc
); }
312 virtual int GetBase() const { return m_base
; }
313 virtual bool SetBase(int base
);
316 virtual void DoSendEvent();
318 virtual bool DoTextToValue(const wxString
& text
, double *val
);
319 virtual wxString
DoValueToText(double val
);
322 // Common part of all ctors.
330 DECLARE_DYNAMIC_CLASS(wxSpinCtrl
)
333 #endif // wxHAS_NATIVE_SPINCTRL
335 //-----------------------------------------------------------------------------
337 //-----------------------------------------------------------------------------
339 class WXDLLIMPEXP_CORE wxSpinCtrlDouble
: public wxSpinCtrlGenericBase
342 wxSpinCtrlDouble() { Init(); }
343 wxSpinCtrlDouble(wxWindow
*parent
,
344 wxWindowID id
= wxID_ANY
,
345 const wxString
& value
= wxEmptyString
,
346 const wxPoint
& pos
= wxDefaultPosition
,
347 const wxSize
& size
= wxDefaultSize
,
348 long style
= wxSP_ARROW_KEYS
| wxALIGN_RIGHT
,
349 double min
= 0, double max
= 100, double initial
= 0,
351 const wxString
& name
= wxT("wxSpinCtrlDouble"))
355 Create(parent
, id
, value
, pos
, size
, style
,
356 min
, max
, initial
, inc
, name
);
359 bool Create(wxWindow
*parent
,
360 wxWindowID id
= wxID_ANY
,
361 const wxString
& value
= wxEmptyString
,
362 const wxPoint
& pos
= wxDefaultPosition
,
363 const wxSize
& size
= wxDefaultSize
,
364 long style
= wxSP_ARROW_KEYS
| wxALIGN_RIGHT
,
365 double min
= 0, double max
= 100, double initial
= 0,
367 const wxString
& name
= wxT("wxSpinCtrlDouble"))
369 return wxSpinCtrlGenericBase::Create(parent
, id
, value
, pos
, size
,
370 style
, min
, max
, initial
,
375 double GetValue(wxSPINCTRL_GETVALUE_FIX
) const { return DoGetValue(); }
376 double GetMin() const { return m_min
; }
377 double GetMax() const { return m_max
; }
378 double GetIncrement() const { return m_increment
; }
379 unsigned GetDigits() const { return m_digits
; }
382 void SetValue(const wxString
& value
)
383 { wxSpinCtrlGenericBase::SetValue(value
); }
384 void SetValue(double value
) { DoSetValue(value
); }
385 void SetRange(double minVal
, double maxVal
) { DoSetRange(minVal
, maxVal
); }
386 void SetIncrement(double inc
) { DoSetIncrement(inc
); }
387 void SetDigits(unsigned digits
);
389 // We don't implement bases support for floating point numbers, this is not
390 // very useful in practice.
391 virtual int GetBase() const { return 10; }
392 virtual bool SetBase(int WXUNUSED(base
)) { return 0; }
395 virtual void DoSendEvent();
397 virtual bool DoTextToValue(const wxString
& text
, double *val
);
398 virtual wxString
DoValueToText(double val
);
403 // Common part of all ctors.
407 m_format
= wxS("%g");
412 DECLARE_DYNAMIC_CLASS(wxSpinCtrlDouble
)
415 #endif // _WX_GENERIC_SPINCTRL_H_