1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        wx/generic/spinctlg.h
3 // Purpose:     generic wxSpinCtrl class
4 // Author:      Vadim Zeitlin
5 // Modified by:
6 // Created:     28.10.99
7 // RCS-ID:      $Id: spinctlg.h 61872 2009-09-09 22:37:05Z VZ $
8 // Copyright:   (c) Vadim Zeitlin
9 // Licence:     wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11 
12 #ifndef _WX_GENERIC_SPINCTRL_H_
13 #define _WX_GENERIC_SPINCTRL_H_
14 
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 // ----------------------------------------------------------------------------
22 
23 #if wxUSE_SPINBTN
24 
25 class WXDLLEXPORT wxSpinButton;
26 class WXDLLEXPORT wxTextCtrl;
27 
28 // ----------------------------------------------------------------------------
29 // wxSpinCtrl is a combination of wxTextCtrl and wxSpinButton
30 // ----------------------------------------------------------------------------
31 
32 class WXDLLEXPORT wxSpinCtrl : public wxControl
33 {
34 public:
wxSpinCtrl()35     wxSpinCtrl() { Init(); }
36 
37     wxSpinCtrl(wxWindow *parent,
38                wxWindowID id = wxID_ANY,
39                const wxString& value = wxEmptyString,
40                const wxPoint& pos = wxDefaultPosition,
41                const wxSize& size = wxDefaultSize,
42                long style = wxSP_ARROW_KEYS,
43                int min = 0, int max = 100, int initial = 0,
44                const wxString& name = wxT("wxSpinCtrl"))
45     {
46         Init();
47         Create(parent, id, value, pos, size, style, min, max, initial, name);
48     }
49 
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,
56                 int min = 0, int max = 100, int initial = 0,
57                 const wxString& name = wxT("wxSpinCtrl"));
58 
59     virtual ~wxSpinCtrl();
60 
61     // operations
62     void SetValue(int val);
63     void SetValue(const wxString& text);
64     void SetRange(int min, int max);
65     void SetSelection(long from, long to);
66 
67     // accessors
68     int GetValue() const;
69     int GetMin() const;
70     int GetMax() const;
71 
72     // implementation from now on
73 
74     // forward these functions to all subcontrols
75     virtual bool Enable(bool enable = true);
76     virtual bool Show(bool show = true);
77     virtual bool Reparent(wxWindow *newParent);
78 
79     // get the subcontrols
GetText()80     wxTextCtrl *GetText() const { return m_text; }
GetSpinButton()81     wxSpinButton *GetSpinButton() const { return m_btn; }
82 
83     // set the value of the text (only)
84     void SetTextValue(int val);
85 
86     // put the numeric value of the string in the text ctrl into val and return
87     // true or return false if the text ctrl doesn't contain a number or if the
88     // number is out of range
89     bool GetTextValue(int *val) const;
90 
91 protected:
92     // override the base class virtuals involved into geometry calculations
93     virtual wxSize DoGetBestSize() const;
94     virtual void DoMoveWindow(int x, int y, int width, int height);
95 
96     // common part of all ctors
97     void Init();
98 
99 private:
100     // the subcontrols
101     wxTextCtrl *m_text;
102     wxSpinButton *m_btn;
103 
104 private:
105     DECLARE_DYNAMIC_CLASS(wxSpinCtrl)
106 };
107 
108 #else // !wxUSE_SPINBTN
109 
110 // ----------------------------------------------------------------------------
111 // wxSpinCtrl is just a text control
112 // ----------------------------------------------------------------------------
113 
114 #include "wx/textctrl.h"
115 
116 class WXDLLEXPORT wxSpinCtrl : public wxTextCtrl
117 {
118 public:
wxSpinCtrl()119     wxSpinCtrl() { Init(); }
120 
121     wxSpinCtrl(wxWindow *parent,
122                wxWindowID id = wxID_ANY,
123                const wxString& value = wxEmptyString,
124                const wxPoint& pos = wxDefaultPosition,
125                const wxSize& size = wxDefaultSize,
126                long style = wxSP_ARROW_KEYS,
127                int min = 0, int max = 100, int initial = 0,
128                const wxString& name = wxT("wxSpinCtrl"))
129     {
130         Create(parent, id, value, pos, size, style, min, max, initial, name);
131     }
132 
133     bool Create(wxWindow *parent,
134                 wxWindowID id = wxID_ANY,
135                 const wxString& value = wxEmptyString,
136                 const wxPoint& pos = wxDefaultPosition,
137                 const wxSize& size = wxDefaultSize,
138                 long style = wxSP_ARROW_KEYS,
139                 int min = 0, int max = 100, int initial = 0,
140                 const wxString& name = wxT("wxSpinCtrl"))
141     {
142         SetRange(min, max);
143 
144         bool ok = wxTextCtrl::Create(parent, id, value, pos, size, style,
145                                      wxDefaultValidator, name);
146         SetValue(initial);
147 
148         return ok;
149     }
150 
151     // accessors
WXUNUSED(dummy)152     int GetValue(int WXUNUSED(dummy) = 1) const
153     {
154         int n;
155         if ( (wxSscanf(wxTextCtrl::GetValue(), wxT("%d"), &n) != 1) )
156             n = INT_MIN;
157 
158         return n;
159     }
160 
GetMin()161     int GetMin() const { return m_min; }
GetMax()162     int GetMax() const { return m_max; }
163 
164     // operations
SetValue(const wxString & value)165     void SetValue(const wxString& value) { wxTextCtrl::SetValue(value); }
SetValue(int val)166     void SetValue(int val) { wxString s; s << val; wxTextCtrl::SetValue(s); }
SetRange(int min,int max)167     void SetRange(int min, int max) { m_min = min; m_max = max; }
168 
169 protected:
170     // initialize m_min/max with the default values
Init()171     void Init() { SetRange(0, 100); }
172 
173     int   m_min;
174     int   m_max;
175 
176 private:
177     DECLARE_DYNAMIC_CLASS(wxSpinCtrl)
178 };
179 
180 #endif // wxUSE_SPINBTN/!wxUSE_SPINBTN
181 
182 #endif // _WX_GENERIC_SPINCTRL_H_
183 
184