]>
Commit | Line | Data |
---|---|---|
669a6e11 VZ |
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$ | |
8 | // Copyright: (c) Vadim Zeitlin | |
65571936 | 9 | // Licence: wxWindows licence |
669a6e11 VZ |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifndef _WX_GENERIC_SPINCTRL_H_ | |
13 | #define _WX_GENERIC_SPINCTRL_H_ | |
14 | ||
1e6feb95 VZ |
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 | ||
739555e3 | 23 | #if wxUSE_SPINBTN |
1e6feb95 | 24 | |
b5dbe15d VS |
25 | class WXDLLIMPEXP_FWD_CORE wxSpinButton; |
26 | class WXDLLIMPEXP_FWD_CORE wxTextCtrl; | |
1e6feb95 | 27 | |
405f0fef | 28 | class wxSpinCtrlTextGeneric; // wxTextCtrl used for the wxSpinCtrlGenericBase |
8cd6a9ad VZ |
29 | |
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 | |
33 | ||
1e6feb95 | 34 | // ---------------------------------------------------------------------------- |
8cd6a9ad VZ |
35 | // wxSpinCtrlGeneric is a combination of wxTextCtrl and wxSpinButton |
36 | // | |
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. | |
1e6feb95 VZ |
41 | // ---------------------------------------------------------------------------- |
42 | ||
53a2db12 | 43 | class WXDLLIMPEXP_CORE wxSpinCtrlGenericBase : public wxSpinCtrlBase |
1e6feb95 VZ |
44 | { |
45 | public: | |
8cd6a9ad | 46 | wxSpinCtrlGenericBase() { Init(); } |
1e6feb95 VZ |
47 | |
48 | bool Create(wxWindow *parent, | |
ca65c044 | 49 | wxWindowID id = wxID_ANY, |
1e6feb95 VZ |
50 | const wxString& value = wxEmptyString, |
51 | const wxPoint& pos = wxDefaultPosition, | |
52 | const wxSize& size = wxDefaultSize, | |
53 | long style = wxSP_ARROW_KEYS, | |
8cd6a9ad | 54 | double min = 0, double max = 100, double initial = 0, double inc = 1, |
1e6feb95 VZ |
55 | const wxString& name = _T("wxSpinCtrl")); |
56 | ||
8cd6a9ad VZ |
57 | virtual ~wxSpinCtrlGenericBase(); |
58 | ||
59 | // accessors | |
60 | // T GetValue() const | |
61 | // T GetMin() const | |
62 | // T GetMax() const | |
63 | // T GetIncrement() const | |
64 | virtual bool GetSnapToTicks() const { return m_snap_to_ticks; } | |
65 | // unsigned GetDigits() const - wxSpinCtrlDouble only | |
1e6feb95 VZ |
66 | |
67 | // operations | |
8cd6a9ad VZ |
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 | |
74 | ||
75 | // Select text in the textctrl | |
739555e3 | 76 | void SetSelection(long from, long to); |
1e6feb95 | 77 | |
1e6feb95 VZ |
78 | // implementation from now on |
79 | ||
80 | // forward these functions to all subcontrols | |
ca65c044 WS |
81 | virtual bool Enable(bool enable = true); |
82 | virtual bool Show(bool show = true); | |
7f0cbaaa | 83 | virtual bool Reparent(wxWindowBase *newParent); |
1e6feb95 VZ |
84 | |
85 | // get the subcontrols | |
8cd6a9ad VZ |
86 | wxTextCtrl *GetText() const { return m_textCtrl; } |
87 | wxSpinButton *GetSpinButton() const { return m_spinButton; } | |
1e6feb95 | 88 | |
8cd6a9ad VZ |
89 | // forwarded events from children windows |
90 | void OnSpinButton(wxSpinEvent& event); | |
91 | void OnTextEnter(wxCommandEvent& event); | |
92 | void OnTextChar(wxKeyEvent& event); | |
1e6feb95 | 93 | |
405f0fef | 94 | friend class wxSpinCtrlTextGeneric; |
1e6feb95 VZ |
95 | |
96 | protected: | |
97 | // override the base class virtuals involved into geometry calculations | |
dba00620 | 98 | virtual wxSize DoGetBestSize() const; |
1e6feb95 VZ |
99 | virtual void DoMoveWindow(int x, int y, int width, int height); |
100 | ||
8cd6a9ad VZ |
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); | |
106 | ||
107 | // Ensure that the textctrl shows correct value | |
108 | void SyncSpinToText(); | |
109 | ||
110 | // Send the correct event type | |
111 | virtual void DoSendEvent() = 0; | |
112 | ||
70c14728 | 113 | // check if the value is in range |
8cd6a9ad VZ |
114 | bool InRange(double n) const { return (n >= m_min) && (n <= m_max); } |
115 | ||
70c14728 VZ |
116 | // ensure that the value is in range wrapping it round if necessary |
117 | double AdjustToFitInRange(double value) const; | |
118 | ||
119 | ||
8cd6a9ad VZ |
120 | double m_value; |
121 | double m_min; | |
122 | double m_max; | |
123 | double m_increment; | |
124 | bool m_snap_to_ticks; | |
125 | wxString m_format; | |
126 | ||
127 | int m_spin_value; | |
1e6feb95 | 128 | |
1e6feb95 | 129 | // the subcontrols |
8cd6a9ad VZ |
130 | wxTextCtrl *m_textCtrl; |
131 | wxSpinButton *m_spinButton; | |
739555e3 | 132 | |
6c0ba0d0 | 133 | private: |
8cd6a9ad VZ |
134 | // common part of all ctors |
135 | void Init(); | |
1e6feb95 VZ |
136 | }; |
137 | ||
138 | #else // !wxUSE_SPINBTN | |
669a6e11 | 139 | |
8cd6a9ad VZ |
140 | #define wxSPINCTRL_GETVALUE_FIX int = 1 |
141 | ||
669a6e11 | 142 | // ---------------------------------------------------------------------------- |
1e6feb95 | 143 | // wxSpinCtrl is just a text control |
669a6e11 VZ |
144 | // ---------------------------------------------------------------------------- |
145 | ||
1e6feb95 VZ |
146 | #include "wx/textctrl.h" |
147 | ||
53a2db12 | 148 | class WXDLLIMPEXP_CORE wxSpinCtrlGenericBase : public wxTextCtrl |
669a6e11 VZ |
149 | { |
150 | public: | |
8cd6a9ad VZ |
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")) { } | |
154 | ||
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")) | |
163 | { | |
164 | m_min = min; | |
165 | m_max = max; | |
166 | m_value = initial; | |
167 | m_increment = inc; | |
168 | ||
169 | bool ok = wxTextCtrl::Create(parent, id, value, pos, size, style, | |
170 | wxDefaultValidator, name); | |
171 | DoSetValue(initial); | |
172 | ||
173 | return ok; | |
174 | } | |
175 | ||
176 | // accessors | |
177 | // T GetValue() const | |
178 | // T GetMin() const | |
179 | // T GetMax() const | |
180 | // T GetIncrement() const | |
181 | virtual bool GetSnapToTicks() const { return m_snap_to_ticks; } | |
182 | // unsigned GetDigits() const - wxSpinCtrlDouble only | |
183 | ||
184 | // operations | |
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 | |
c71830c3 | 191 | |
8cd6a9ad VZ |
192 | // Select text in the textctrl |
193 | //void SetSelection(long from, long to); | |
194 | ||
195 | protected: | |
196 | // generic double valued | |
197 | double DoGetValue() const | |
198 | { | |
199 | double n; | |
200 | if ( (wxSscanf(wxTextCtrl::GetValue(), wxT("%lf"), &n) != 1) ) | |
201 | n = INT_MIN; | |
202 | ||
203 | return n; | |
204 | } | |
205 | ||
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 | |
209 | ||
210 | double m_value; | |
211 | double m_min; | |
212 | double m_max; | |
213 | double m_increment; | |
214 | bool m_snap_to_ticks; | |
215 | wxString m_format; | |
216 | }; | |
217 | ||
218 | #endif // wxUSE_SPINBTN/!wxUSE_SPINBTN | |
219 | ||
220 | #if !defined(wxHAS_NATIVE_SPINCTRL) | |
221 | ||
222 | //----------------------------------------------------------------------------- | |
223 | // wxSpinCtrl | |
224 | //----------------------------------------------------------------------------- | |
225 | ||
226 | class WXDLLIMPEXP_CORE wxSpinCtrl : public wxSpinCtrlGenericBase | |
227 | { | |
228 | public: | |
229 | wxSpinCtrl() {} | |
c71830c3 | 230 | wxSpinCtrl(wxWindow *parent, |
ca65c044 | 231 | wxWindowID id = wxID_ANY, |
c71830c3 VZ |
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")) | |
238 | { | |
239 | Create(parent, id, value, pos, size, style, min, max, initial, name); | |
240 | } | |
241 | ||
242 | bool Create(wxWindow *parent, | |
ca65c044 | 243 | wxWindowID id = wxID_ANY, |
c71830c3 VZ |
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")) | |
250 | { | |
8cd6a9ad | 251 | return wxSpinCtrlGenericBase::Create(parent, id, value, pos, size, style, min, max, initial, 1, name); |
c71830c3 | 252 | } |
669a6e11 VZ |
253 | |
254 | // accessors | |
674f0f27 RR |
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 ); } | |
8cd6a9ad VZ |
259 | |
260 | // operations | |
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); } | |
265 | ||
266 | protected: | |
267 | virtual void DoSendEvent(); | |
268 | ||
269 | private: | |
270 | DECLARE_DYNAMIC_CLASS(wxSpinCtrl) | |
271 | }; | |
272 | ||
273 | #endif // wxHAS_NATIVE_SPINCTRL | |
274 | ||
275 | //----------------------------------------------------------------------------- | |
276 | // wxSpinCtrlDouble | |
277 | //----------------------------------------------------------------------------- | |
278 | ||
279 | class WXDLLIMPEXP_CORE wxSpinCtrlDouble : public wxSpinCtrlGenericBase | |
280 | { | |
281 | public: | |
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")) | |
669a6e11 | 291 | { |
8cd6a9ad VZ |
292 | m_digits = 0; |
293 | Create(parent, id, value, pos, size, style, min, max, initial, inc, name); | |
294 | } | |
669a6e11 | 295 | |
8cd6a9ad VZ |
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")) | |
304 | { | |
305 | return wxSpinCtrlGenericBase::Create(parent, id, value, pos, size, style, min, max, initial, inc, name); | |
669a6e11 VZ |
306 | } |
307 | ||
8cd6a9ad VZ |
308 | // accessors |
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; } | |
669a6e11 VZ |
314 | |
315 | // operations | |
8cd6a9ad VZ |
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); | |
669a6e11 VZ |
321 | |
322 | protected: | |
8cd6a9ad | 323 | virtual void DoSendEvent(); |
669a6e11 | 324 | |
8cd6a9ad | 325 | unsigned m_digits; |
5fde6fcc GD |
326 | |
327 | private: | |
8cd6a9ad | 328 | DECLARE_DYNAMIC_CLASS(wxSpinCtrlDouble) |
669a6e11 VZ |
329 | }; |
330 | ||
331 | #endif // _WX_GENERIC_SPINCTRL_H_ |