]>
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, | |
f1ddb476 | 53 | long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT, |
c218ccee VZ |
54 | double min = 0, double max = 100, double initial = 0, |
55 | double inc = 1, | |
9a83f860 | 56 | const wxString& name = wxT("wxSpinCtrl")); |
1e6feb95 | 57 | |
8cd6a9ad VZ |
58 | virtual ~wxSpinCtrlGenericBase(); |
59 | ||
60 | // accessors | |
61 | // T GetValue() const | |
62 | // T GetMin() const | |
63 | // T GetMax() const | |
64 | // T GetIncrement() const | |
65 | virtual bool GetSnapToTicks() const { return m_snap_to_ticks; } | |
66 | // unsigned GetDigits() const - wxSpinCtrlDouble only | |
1e6feb95 VZ |
67 | |
68 | // operations | |
8cd6a9ad VZ |
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 | |
75 | ||
76 | // Select text in the textctrl | |
739555e3 | 77 | void SetSelection(long from, long to); |
1e6feb95 | 78 | |
1e6feb95 VZ |
79 | // implementation from now on |
80 | ||
81 | // forward these functions to all subcontrols | |
ca65c044 WS |
82 | virtual bool Enable(bool enable = true); |
83 | virtual bool Show(bool show = true); | |
7f0cbaaa | 84 | virtual bool Reparent(wxWindowBase *newParent); |
1e6feb95 VZ |
85 | |
86 | // get the subcontrols | |
8cd6a9ad VZ |
87 | wxTextCtrl *GetText() const { return m_textCtrl; } |
88 | wxSpinButton *GetSpinButton() const { return m_spinButton; } | |
1e6feb95 | 89 | |
8cd6a9ad VZ |
90 | // forwarded events from children windows |
91 | void OnSpinButton(wxSpinEvent& event); | |
92 | void OnTextEnter(wxCommandEvent& event); | |
93 | void OnTextChar(wxKeyEvent& event); | |
1e6feb95 | 94 | |
64b62afe VZ |
95 | // this window itself is used only as a container for its sub windows so it |
96 | // shouldn't accept the focus at all | |
97 | virtual bool AcceptsFocus() const { return false; } | |
98 | ||
405f0fef | 99 | friend class wxSpinCtrlTextGeneric; |
1e6feb95 VZ |
100 | |
101 | protected: | |
102 | // override the base class virtuals involved into geometry calculations | |
dba00620 | 103 | virtual wxSize DoGetBestSize() const; |
1e6feb95 VZ |
104 | virtual void DoMoveWindow(int x, int y, int width, int height); |
105 | ||
8cd6a9ad VZ |
106 | // generic double valued functions |
107 | double DoGetValue() const { return m_value; } | |
108 | bool DoSetValue(double val); | |
109 | void DoSetRange(double min_val, double max_val); | |
110 | void DoSetIncrement(double inc); | |
111 | ||
3a712105 VZ |
112 | // update our value to reflect the text control contents (if it has been |
113 | // modified by user, do nothing otherwise) | |
114 | // | |
115 | // can also change the text control if its value is invalid | |
116 | // | |
117 | // return true if our value has changed | |
118 | bool SyncSpinToText(); | |
8cd6a9ad VZ |
119 | |
120 | // Send the correct event type | |
121 | virtual void DoSendEvent() = 0; | |
122 | ||
70c14728 | 123 | // check if the value is in range |
8cd6a9ad VZ |
124 | bool InRange(double n) const { return (n >= m_min) && (n <= m_max); } |
125 | ||
70c14728 VZ |
126 | // ensure that the value is in range wrapping it round if necessary |
127 | double AdjustToFitInRange(double value) const; | |
128 | ||
129 | ||
8cd6a9ad VZ |
130 | double m_value; |
131 | double m_min; | |
132 | double m_max; | |
133 | double m_increment; | |
134 | bool m_snap_to_ticks; | |
135 | wxString m_format; | |
136 | ||
137 | int m_spin_value; | |
1e6feb95 | 138 | |
1e6feb95 | 139 | // the subcontrols |
8cd6a9ad VZ |
140 | wxTextCtrl *m_textCtrl; |
141 | wxSpinButton *m_spinButton; | |
739555e3 | 142 | |
6c0ba0d0 | 143 | private: |
8cd6a9ad VZ |
144 | // common part of all ctors |
145 | void Init(); | |
1e6feb95 VZ |
146 | }; |
147 | ||
148 | #else // !wxUSE_SPINBTN | |
669a6e11 | 149 | |
8cd6a9ad VZ |
150 | #define wxSPINCTRL_GETVALUE_FIX int = 1 |
151 | ||
669a6e11 | 152 | // ---------------------------------------------------------------------------- |
1e6feb95 | 153 | // wxSpinCtrl is just a text control |
669a6e11 VZ |
154 | // ---------------------------------------------------------------------------- |
155 | ||
1e6feb95 VZ |
156 | #include "wx/textctrl.h" |
157 | ||
53a2db12 | 158 | class WXDLLIMPEXP_CORE wxSpinCtrlGenericBase : public wxTextCtrl |
669a6e11 VZ |
159 | { |
160 | public: | |
8cd6a9ad VZ |
161 | wxSpinCtrlGenericBase() : m_value(0), m_min(0), m_max(100), |
162 | m_increment(1), m_snap_to_ticks(false), | |
163 | m_format(wxT("%g")) { } | |
164 | ||
165 | bool Create(wxWindow *parent, | |
166 | wxWindowID id = wxID_ANY, | |
167 | const wxString& value = wxEmptyString, | |
168 | const wxPoint& pos = wxDefaultPosition, | |
169 | const wxSize& size = wxDefaultSize, | |
f1ddb476 | 170 | long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT, |
c218ccee VZ |
171 | double min = 0, double max = 100, double initial = 0, |
172 | double inc = 1, | |
9a83f860 | 173 | const wxString& name = wxT("wxSpinCtrl")) |
8cd6a9ad VZ |
174 | { |
175 | m_min = min; | |
176 | m_max = max; | |
177 | m_value = initial; | |
178 | m_increment = inc; | |
179 | ||
180 | bool ok = wxTextCtrl::Create(parent, id, value, pos, size, style, | |
181 | wxDefaultValidator, name); | |
182 | DoSetValue(initial); | |
183 | ||
184 | return ok; | |
185 | } | |
186 | ||
187 | // accessors | |
188 | // T GetValue() const | |
189 | // T GetMin() const | |
190 | // T GetMax() const | |
191 | // T GetIncrement() const | |
192 | virtual bool GetSnapToTicks() const { return m_snap_to_ticks; } | |
193 | // unsigned GetDigits() const - wxSpinCtrlDouble only | |
194 | ||
195 | // operations | |
196 | virtual void SetValue(const wxString& text) { wxTextCtrl::SetValue(text); } | |
197 | // void SetValue(T val) | |
198 | // void SetRange(T minVal, T maxVal) | |
199 | // void SetIncrement(T inc) | |
c218ccee VZ |
200 | virtual void SetSnapToTicks(bool snap_to_ticks) |
201 | { m_snap_to_ticks = snap_to_ticks; } | |
8cd6a9ad | 202 | // void SetDigits(unsigned digits) - wxSpinCtrlDouble only |
c71830c3 | 203 | |
8cd6a9ad VZ |
204 | // Select text in the textctrl |
205 | //void SetSelection(long from, long to); | |
206 | ||
207 | protected: | |
208 | // generic double valued | |
209 | double DoGetValue() const | |
210 | { | |
211 | double n; | |
212 | if ( (wxSscanf(wxTextCtrl::GetValue(), wxT("%lf"), &n) != 1) ) | |
213 | n = INT_MIN; | |
214 | ||
215 | return n; | |
216 | } | |
217 | ||
c218ccee VZ |
218 | bool DoSetValue(double val) |
219 | { | |
220 | wxTextCtrl::SetValue(wxString::Format(m_format.c_str(), val)); | |
221 | return true; | |
222 | } | |
223 | void DoSetRange(double min_val, double max_val) | |
224 | { | |
225 | m_min = min_val; | |
226 | m_max = max_val; | |
227 | } | |
8cd6a9ad VZ |
228 | void DoSetIncrement(double inc) { m_increment = inc; } // Note: unused |
229 | ||
230 | double m_value; | |
231 | double m_min; | |
232 | double m_max; | |
233 | double m_increment; | |
234 | bool m_snap_to_ticks; | |
235 | wxString m_format; | |
236 | }; | |
237 | ||
238 | #endif // wxUSE_SPINBTN/!wxUSE_SPINBTN | |
239 | ||
240 | #if !defined(wxHAS_NATIVE_SPINCTRL) | |
241 | ||
242 | //----------------------------------------------------------------------------- | |
243 | // wxSpinCtrl | |
244 | //----------------------------------------------------------------------------- | |
245 | ||
246 | class WXDLLIMPEXP_CORE wxSpinCtrl : public wxSpinCtrlGenericBase | |
247 | { | |
248 | public: | |
249 | wxSpinCtrl() {} | |
c71830c3 | 250 | wxSpinCtrl(wxWindow *parent, |
ca65c044 | 251 | wxWindowID id = wxID_ANY, |
c71830c3 VZ |
252 | const wxString& value = wxEmptyString, |
253 | const wxPoint& pos = wxDefaultPosition, | |
254 | const wxSize& size = wxDefaultSize, | |
f1ddb476 | 255 | long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT, |
c71830c3 | 256 | int min = 0, int max = 100, int initial = 0, |
9a83f860 | 257 | const wxString& name = wxT("wxSpinCtrl")) |
c71830c3 VZ |
258 | { |
259 | Create(parent, id, value, pos, size, style, min, max, initial, name); | |
260 | } | |
261 | ||
262 | bool Create(wxWindow *parent, | |
ca65c044 | 263 | wxWindowID id = wxID_ANY, |
c71830c3 VZ |
264 | const wxString& value = wxEmptyString, |
265 | const wxPoint& pos = wxDefaultPosition, | |
266 | const wxSize& size = wxDefaultSize, | |
f1ddb476 | 267 | long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT, |
c71830c3 | 268 | int min = 0, int max = 100, int initial = 0, |
9a83f860 | 269 | const wxString& name = wxT("wxSpinCtrl")) |
c71830c3 | 270 | { |
c218ccee VZ |
271 | return wxSpinCtrlGenericBase::Create(parent, id, value, pos, size, |
272 | style, min, max, initial, 1, name); | |
c71830c3 | 273 | } |
669a6e11 VZ |
274 | |
275 | // accessors | |
f0368d28 PC |
276 | int GetValue(wxSPINCTRL_GETVALUE_FIX) const { return int(DoGetValue()); } |
277 | int GetMin() const { return int(m_min); } | |
278 | int GetMax() const { return int(m_max); } | |
279 | int GetIncrement() const { return int(m_increment); } | |
8cd6a9ad VZ |
280 | |
281 | // operations | |
c218ccee VZ |
282 | void SetValue(const wxString& value) |
283 | { wxSpinCtrlGenericBase::SetValue(value); } | |
8cd6a9ad VZ |
284 | void SetValue( int value ) { DoSetValue(value); } |
285 | void SetRange( int minVal, int maxVal ) { DoSetRange(minVal, maxVal); } | |
92e164ff | 286 | void SetIncrement(int inc) { DoSetIncrement(inc); } |
8cd6a9ad VZ |
287 | |
288 | protected: | |
289 | virtual void DoSendEvent(); | |
290 | ||
8cd6a9ad VZ |
291 | DECLARE_DYNAMIC_CLASS(wxSpinCtrl) |
292 | }; | |
293 | ||
294 | #endif // wxHAS_NATIVE_SPINCTRL | |
295 | ||
296 | //----------------------------------------------------------------------------- | |
297 | // wxSpinCtrlDouble | |
298 | //----------------------------------------------------------------------------- | |
299 | ||
300 | class WXDLLIMPEXP_CORE wxSpinCtrlDouble : public wxSpinCtrlGenericBase | |
301 | { | |
302 | public: | |
303 | wxSpinCtrlDouble() : m_digits(0) { } | |
304 | wxSpinCtrlDouble(wxWindow *parent, | |
305 | wxWindowID id = wxID_ANY, | |
306 | const wxString& value = wxEmptyString, | |
307 | const wxPoint& pos = wxDefaultPosition, | |
308 | const wxSize& size = wxDefaultSize, | |
f1ddb476 | 309 | long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT, |
c218ccee VZ |
310 | double min = 0, double max = 100, double initial = 0, |
311 | double inc = 1, | |
9a83f860 | 312 | const wxString& name = wxT("wxSpinCtrlDouble")) |
669a6e11 | 313 | { |
8cd6a9ad | 314 | m_digits = 0; |
c218ccee VZ |
315 | Create(parent, id, value, pos, size, style, |
316 | min, max, initial, inc, name); | |
8cd6a9ad | 317 | } |
669a6e11 | 318 | |
8cd6a9ad VZ |
319 | bool Create(wxWindow *parent, |
320 | wxWindowID id = wxID_ANY, | |
321 | const wxString& value = wxEmptyString, | |
322 | const wxPoint& pos = wxDefaultPosition, | |
323 | const wxSize& size = wxDefaultSize, | |
f1ddb476 | 324 | long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT, |
c218ccee VZ |
325 | double min = 0, double max = 100, double initial = 0, |
326 | double inc = 1, | |
9a83f860 | 327 | const wxString& name = wxT("wxSpinCtrlDouble")) |
8cd6a9ad | 328 | { |
c218ccee VZ |
329 | return wxSpinCtrlGenericBase::Create(parent, id, value, pos, size, |
330 | style, min, max, initial, | |
331 | inc, name); | |
669a6e11 VZ |
332 | } |
333 | ||
8cd6a9ad VZ |
334 | // accessors |
335 | double GetValue(wxSPINCTRL_GETVALUE_FIX) const { return DoGetValue(); } | |
336 | double GetMin() const { return m_min; } | |
337 | double GetMax() const { return m_max; } | |
338 | double GetIncrement() const { return m_increment; } | |
339 | unsigned GetDigits() const { return m_digits; } | |
669a6e11 VZ |
340 | |
341 | // operations | |
c218ccee VZ |
342 | void SetValue(const wxString& value) |
343 | { wxSpinCtrlGenericBase::SetValue(value); } | |
8cd6a9ad VZ |
344 | void SetValue(double value) { DoSetValue(value); } |
345 | void SetRange(double minVal, double maxVal) { DoSetRange(minVal, maxVal); } | |
346 | void SetIncrement(double inc) { DoSetIncrement(inc); } | |
347 | void SetDigits(unsigned digits); | |
669a6e11 VZ |
348 | |
349 | protected: | |
8cd6a9ad | 350 | virtual void DoSendEvent(); |
669a6e11 | 351 | |
8cd6a9ad | 352 | unsigned m_digits; |
5fde6fcc | 353 | |
8cd6a9ad | 354 | DECLARE_DYNAMIC_CLASS(wxSpinCtrlDouble) |
669a6e11 VZ |
355 | }; |
356 | ||
357 | #endif // _WX_GENERIC_SPINCTRL_H_ |