]>
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); | |
cb7ef329 VZ |
84 | #if wxUSE_TOOLTIPS |
85 | virtual void DoSetToolTip(wxToolTip *tip); | |
86 | #endif // wxUSE_TOOLTIPS | |
1e6feb95 VZ |
87 | |
88 | // get the subcontrols | |
8cd6a9ad VZ |
89 | wxTextCtrl *GetText() const { return m_textCtrl; } |
90 | wxSpinButton *GetSpinButton() const { return m_spinButton; } | |
1e6feb95 | 91 | |
8cd6a9ad VZ |
92 | // forwarded events from children windows |
93 | void OnSpinButton(wxSpinEvent& event); | |
c8139480 | 94 | void OnTextLostFocus(wxFocusEvent& event); |
8cd6a9ad | 95 | void OnTextChar(wxKeyEvent& event); |
1e6feb95 | 96 | |
64b62afe | 97 | // this window itself is used only as a container for its sub windows so it |
713c7336 VZ |
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 | |
64b62afe | 100 | virtual bool AcceptsFocus() const { return false; } |
fda43a0e | 101 | virtual void SetFocus(); |
64b62afe | 102 | |
405f0fef | 103 | friend class wxSpinCtrlTextGeneric; |
1e6feb95 VZ |
104 | |
105 | protected: | |
106 | // override the base class virtuals involved into geometry calculations | |
dba00620 | 107 | virtual wxSize DoGetBestSize() const; |
1e6feb95 VZ |
108 | virtual void DoMoveWindow(int x, int y, int width, int height); |
109 | ||
a2fb9138 VZ |
110 | #ifdef __WXMSW__ |
111 | // and, for MSW, enabling this window itself | |
112 | virtual void DoEnable(bool enable); | |
113 | #endif // __WXMSW__ | |
114 | ||
8cd6a9ad VZ |
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); | |
120 | ||
3a712105 VZ |
121 | // update our value to reflect the text control contents (if it has been |
122 | // modified by user, do nothing otherwise) | |
123 | // | |
124 | // can also change the text control if its value is invalid | |
125 | // | |
126 | // return true if our value has changed | |
127 | bool SyncSpinToText(); | |
8cd6a9ad VZ |
128 | |
129 | // Send the correct event type | |
130 | virtual void DoSendEvent() = 0; | |
131 | ||
62f96636 VZ |
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; | |
135 | ||
70c14728 | 136 | // check if the value is in range |
8cd6a9ad VZ |
137 | bool InRange(double n) const { return (n >= m_min) && (n <= m_max); } |
138 | ||
70c14728 VZ |
139 | // ensure that the value is in range wrapping it round if necessary |
140 | double AdjustToFitInRange(double value) const; | |
141 | ||
142 | ||
8cd6a9ad VZ |
143 | double m_value; |
144 | double m_min; | |
145 | double m_max; | |
146 | double m_increment; | |
147 | bool m_snap_to_ticks; | |
8cd6a9ad VZ |
148 | |
149 | int m_spin_value; | |
1e6feb95 | 150 | |
1e6feb95 | 151 | // the subcontrols |
8cd6a9ad VZ |
152 | wxTextCtrl *m_textCtrl; |
153 | wxSpinButton *m_spinButton; | |
739555e3 | 154 | |
6c0ba0d0 | 155 | private: |
8cd6a9ad VZ |
156 | // common part of all ctors |
157 | void Init(); | |
c8139480 VS |
158 | |
159 | DECLARE_EVENT_TABLE() | |
1e6feb95 VZ |
160 | }; |
161 | ||
162 | #else // !wxUSE_SPINBTN | |
669a6e11 | 163 | |
8cd6a9ad VZ |
164 | #define wxSPINCTRL_GETVALUE_FIX int = 1 |
165 | ||
669a6e11 | 166 | // ---------------------------------------------------------------------------- |
1e6feb95 | 167 | // wxSpinCtrl is just a text control |
669a6e11 VZ |
168 | // ---------------------------------------------------------------------------- |
169 | ||
1e6feb95 VZ |
170 | #include "wx/textctrl.h" |
171 | ||
53a2db12 | 172 | class WXDLLIMPEXP_CORE wxSpinCtrlGenericBase : public wxTextCtrl |
669a6e11 VZ |
173 | { |
174 | public: | |
8cd6a9ad VZ |
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")) { } | |
178 | ||
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, | |
f1ddb476 | 184 | long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT, |
c218ccee VZ |
185 | double min = 0, double max = 100, double initial = 0, |
186 | double inc = 1, | |
9a83f860 | 187 | const wxString& name = wxT("wxSpinCtrl")) |
8cd6a9ad VZ |
188 | { |
189 | m_min = min; | |
190 | m_max = max; | |
191 | m_value = initial; | |
192 | m_increment = inc; | |
193 | ||
194 | bool ok = wxTextCtrl::Create(parent, id, value, pos, size, style, | |
195 | wxDefaultValidator, name); | |
196 | DoSetValue(initial); | |
197 | ||
198 | return ok; | |
199 | } | |
200 | ||
201 | // accessors | |
202 | // T GetValue() const | |
203 | // T GetMin() const | |
204 | // T GetMax() const | |
205 | // T GetIncrement() const | |
206 | virtual bool GetSnapToTicks() const { return m_snap_to_ticks; } | |
207 | // unsigned GetDigits() const - wxSpinCtrlDouble only | |
208 | ||
209 | // operations | |
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) | |
c218ccee VZ |
214 | virtual void SetSnapToTicks(bool snap_to_ticks) |
215 | { m_snap_to_ticks = snap_to_ticks; } | |
8cd6a9ad | 216 | // void SetDigits(unsigned digits) - wxSpinCtrlDouble only |
c71830c3 | 217 | |
8cd6a9ad VZ |
218 | // Select text in the textctrl |
219 | //void SetSelection(long from, long to); | |
220 | ||
221 | protected: | |
222 | // generic double valued | |
223 | double DoGetValue() const | |
224 | { | |
225 | double n; | |
226 | if ( (wxSscanf(wxTextCtrl::GetValue(), wxT("%lf"), &n) != 1) ) | |
227 | n = INT_MIN; | |
228 | ||
229 | return n; | |
230 | } | |
231 | ||
c218ccee VZ |
232 | bool DoSetValue(double val) |
233 | { | |
234 | wxTextCtrl::SetValue(wxString::Format(m_format.c_str(), val)); | |
235 | return true; | |
236 | } | |
237 | void DoSetRange(double min_val, double max_val) | |
238 | { | |
239 | m_min = min_val; | |
240 | m_max = max_val; | |
241 | } | |
8cd6a9ad VZ |
242 | void DoSetIncrement(double inc) { m_increment = inc; } // Note: unused |
243 | ||
244 | double m_value; | |
245 | double m_min; | |
246 | double m_max; | |
247 | double m_increment; | |
248 | bool m_snap_to_ticks; | |
249 | wxString m_format; | |
250 | }; | |
251 | ||
252 | #endif // wxUSE_SPINBTN/!wxUSE_SPINBTN | |
253 | ||
254 | #if !defined(wxHAS_NATIVE_SPINCTRL) | |
255 | ||
256 | //----------------------------------------------------------------------------- | |
257 | // wxSpinCtrl | |
258 | //----------------------------------------------------------------------------- | |
259 | ||
260 | class WXDLLIMPEXP_CORE wxSpinCtrl : public wxSpinCtrlGenericBase | |
261 | { | |
262 | public: | |
9e565667 | 263 | wxSpinCtrl() { Init(); } |
c71830c3 | 264 | wxSpinCtrl(wxWindow *parent, |
ca65c044 | 265 | wxWindowID id = wxID_ANY, |
c71830c3 VZ |
266 | const wxString& value = wxEmptyString, |
267 | const wxPoint& pos = wxDefaultPosition, | |
268 | const wxSize& size = wxDefaultSize, | |
f1ddb476 | 269 | long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT, |
c71830c3 | 270 | int min = 0, int max = 100, int initial = 0, |
9a83f860 | 271 | const wxString& name = wxT("wxSpinCtrl")) |
c71830c3 | 272 | { |
9e565667 VZ |
273 | Init(); |
274 | ||
c71830c3 VZ |
275 | Create(parent, id, value, pos, size, style, min, max, initial, name); |
276 | } | |
277 | ||
278 | bool Create(wxWindow *parent, | |
ca65c044 | 279 | wxWindowID id = wxID_ANY, |
c71830c3 VZ |
280 | const wxString& value = wxEmptyString, |
281 | const wxPoint& pos = wxDefaultPosition, | |
282 | const wxSize& size = wxDefaultSize, | |
f1ddb476 | 283 | long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT, |
c71830c3 | 284 | int min = 0, int max = 100, int initial = 0, |
9a83f860 | 285 | const wxString& name = wxT("wxSpinCtrl")) |
c71830c3 | 286 | { |
c218ccee VZ |
287 | return wxSpinCtrlGenericBase::Create(parent, id, value, pos, size, |
288 | style, min, max, initial, 1, name); | |
c71830c3 | 289 | } |
669a6e11 VZ |
290 | |
291 | // accessors | |
f0368d28 PC |
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); } | |
8cd6a9ad VZ |
296 | |
297 | // operations | |
c218ccee VZ |
298 | void SetValue(const wxString& value) |
299 | { wxSpinCtrlGenericBase::SetValue(value); } | |
8cd6a9ad VZ |
300 | void SetValue( int value ) { DoSetValue(value); } |
301 | void SetRange( int minVal, int maxVal ) { DoSetRange(minVal, maxVal); } | |
92e164ff | 302 | void SetIncrement(int inc) { DoSetIncrement(inc); } |
8cd6a9ad | 303 | |
9e565667 VZ |
304 | virtual int GetBase() const { return m_base; } |
305 | virtual bool SetBase(int base); | |
306 | ||
8cd6a9ad VZ |
307 | protected: |
308 | virtual void DoSendEvent(); | |
309 | ||
62f96636 VZ |
310 | virtual bool DoTextToValue(const wxString& text, double *val); |
311 | virtual wxString DoValueToText(double val); | |
9e565667 VZ |
312 | |
313 | private: | |
314 | // Common part of all ctors. | |
315 | void Init() | |
316 | { | |
317 | m_base = 10; | |
318 | } | |
319 | ||
320 | int m_base; | |
321 | ||
8cd6a9ad VZ |
322 | DECLARE_DYNAMIC_CLASS(wxSpinCtrl) |
323 | }; | |
324 | ||
325 | #endif // wxHAS_NATIVE_SPINCTRL | |
326 | ||
327 | //----------------------------------------------------------------------------- | |
328 | // wxSpinCtrlDouble | |
329 | //----------------------------------------------------------------------------- | |
330 | ||
331 | class WXDLLIMPEXP_CORE wxSpinCtrlDouble : public wxSpinCtrlGenericBase | |
332 | { | |
333 | public: | |
62f96636 | 334 | wxSpinCtrlDouble() { Init(); } |
8cd6a9ad VZ |
335 | wxSpinCtrlDouble(wxWindow *parent, |
336 | wxWindowID id = wxID_ANY, | |
337 | const wxString& value = wxEmptyString, | |
338 | const wxPoint& pos = wxDefaultPosition, | |
339 | const wxSize& size = wxDefaultSize, | |
f1ddb476 | 340 | long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT, |
c218ccee VZ |
341 | double min = 0, double max = 100, double initial = 0, |
342 | double inc = 1, | |
9a83f860 | 343 | const wxString& name = wxT("wxSpinCtrlDouble")) |
669a6e11 | 344 | { |
62f96636 VZ |
345 | Init(); |
346 | ||
c218ccee VZ |
347 | Create(parent, id, value, pos, size, style, |
348 | min, max, initial, inc, name); | |
8cd6a9ad | 349 | } |
669a6e11 | 350 | |
8cd6a9ad VZ |
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, | |
f1ddb476 | 356 | long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT, |
c218ccee VZ |
357 | double min = 0, double max = 100, double initial = 0, |
358 | double inc = 1, | |
9a83f860 | 359 | const wxString& name = wxT("wxSpinCtrlDouble")) |
8cd6a9ad | 360 | { |
c218ccee VZ |
361 | return wxSpinCtrlGenericBase::Create(parent, id, value, pos, size, |
362 | style, min, max, initial, | |
363 | inc, name); | |
669a6e11 VZ |
364 | } |
365 | ||
8cd6a9ad VZ |
366 | // accessors |
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; } | |
669a6e11 VZ |
372 | |
373 | // operations | |
c218ccee VZ |
374 | void SetValue(const wxString& value) |
375 | { wxSpinCtrlGenericBase::SetValue(value); } | |
8cd6a9ad VZ |
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); | |
669a6e11 | 380 | |
9e565667 VZ |
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; } | |
385 | ||
669a6e11 | 386 | protected: |
8cd6a9ad | 387 | virtual void DoSendEvent(); |
669a6e11 | 388 | |
62f96636 VZ |
389 | virtual bool DoTextToValue(const wxString& text, double *val); |
390 | virtual wxString DoValueToText(double val); | |
391 | ||
8cd6a9ad | 392 | unsigned m_digits; |
5fde6fcc | 393 | |
62f96636 VZ |
394 | private: |
395 | // Common part of all ctors. | |
396 | void Init() | |
397 | { | |
398 | m_digits = 0; | |
399 | m_format = wxS("%g"); | |
400 | } | |
401 | ||
402 | wxString m_format; | |
403 | ||
8cd6a9ad | 404 | DECLARE_DYNAMIC_CLASS(wxSpinCtrlDouble) |
669a6e11 VZ |
405 | }; |
406 | ||
407 | #endif // _WX_GENERIC_SPINCTRL_H_ |