]>
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 | |
29604c85 VZ |
25 | #include "wx/compositewin.h" |
26 | ||
b5dbe15d VS |
27 | class WXDLLIMPEXP_FWD_CORE wxSpinButton; |
28 | class WXDLLIMPEXP_FWD_CORE wxTextCtrl; | |
1e6feb95 | 29 | |
405f0fef | 30 | class wxSpinCtrlTextGeneric; // wxTextCtrl used for the wxSpinCtrlGenericBase |
8cd6a9ad VZ |
31 | |
32 | // The !wxUSE_SPINBTN version's GetValue() function conflicts with the | |
33 | // wxTextCtrl's GetValue() and so you have to input a dummy int value. | |
34 | #define wxSPINCTRL_GETVALUE_FIX | |
35 | ||
1e6feb95 | 36 | // ---------------------------------------------------------------------------- |
8cd6a9ad VZ |
37 | // wxSpinCtrlGeneric is a combination of wxTextCtrl and wxSpinButton |
38 | // | |
39 | // This class manages a double valued generic spinctrl through the DoGet/SetXXX | |
40 | // functions that are made public as Get/SetXXX functions for int or double | |
41 | // for the wxSpinCtrl and wxSpinCtrlDouble classes respectively to avoid | |
42 | // function ambiguity. | |
1e6feb95 VZ |
43 | // ---------------------------------------------------------------------------- |
44 | ||
29604c85 VZ |
45 | class WXDLLIMPEXP_CORE wxSpinCtrlGenericBase |
46 | : public wxCompositeWindow<wxSpinCtrlBase> | |
1e6feb95 VZ |
47 | { |
48 | public: | |
8cd6a9ad | 49 | wxSpinCtrlGenericBase() { Init(); } |
1e6feb95 VZ |
50 | |
51 | bool Create(wxWindow *parent, | |
ca65c044 | 52 | wxWindowID id = wxID_ANY, |
1e6feb95 VZ |
53 | const wxString& value = wxEmptyString, |
54 | const wxPoint& pos = wxDefaultPosition, | |
55 | const wxSize& size = wxDefaultSize, | |
f1ddb476 | 56 | long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT, |
c218ccee VZ |
57 | double min = 0, double max = 100, double initial = 0, |
58 | double inc = 1, | |
9a83f860 | 59 | const wxString& name = wxT("wxSpinCtrl")); |
1e6feb95 | 60 | |
8cd6a9ad VZ |
61 | virtual ~wxSpinCtrlGenericBase(); |
62 | ||
63 | // accessors | |
64 | // T GetValue() const | |
65 | // T GetMin() const | |
66 | // T GetMax() const | |
67 | // T GetIncrement() const | |
68 | virtual bool GetSnapToTicks() const { return m_snap_to_ticks; } | |
69 | // unsigned GetDigits() const - wxSpinCtrlDouble only | |
1e6feb95 VZ |
70 | |
71 | // operations | |
8cd6a9ad VZ |
72 | virtual void SetValue(const wxString& text); |
73 | // void SetValue(T val) | |
74 | // void SetRange(T minVal, T maxVal) | |
75 | // void SetIncrement(T inc) | |
76 | virtual void SetSnapToTicks(bool snap_to_ticks); | |
77 | // void SetDigits(unsigned digits) - wxSpinCtrlDouble only | |
78 | ||
79 | // Select text in the textctrl | |
739555e3 | 80 | void SetSelection(long from, long to); |
1e6feb95 | 81 | |
1e6feb95 VZ |
82 | // implementation from now on |
83 | ||
84 | // forward these functions to all subcontrols | |
ca65c044 WS |
85 | virtual bool Enable(bool enable = true); |
86 | virtual bool Show(bool show = true); | |
cb7ef329 VZ |
87 | #if wxUSE_TOOLTIPS |
88 | virtual void DoSetToolTip(wxToolTip *tip); | |
89 | #endif // wxUSE_TOOLTIPS | |
1e6feb95 VZ |
90 | |
91 | // get the subcontrols | |
8cd6a9ad VZ |
92 | wxTextCtrl *GetText() const { return m_textCtrl; } |
93 | wxSpinButton *GetSpinButton() const { return m_spinButton; } | |
1e6feb95 | 94 | |
8cd6a9ad VZ |
95 | // forwarded events from children windows |
96 | void OnSpinButton(wxSpinEvent& event); | |
c8139480 | 97 | void OnTextLostFocus(wxFocusEvent& event); |
8cd6a9ad | 98 | void OnTextChar(wxKeyEvent& event); |
1e6feb95 | 99 | |
64b62afe | 100 | // this window itself is used only as a container for its sub windows so it |
713c7336 VZ |
101 | // shouldn't accept the focus at all and any attempts to explicitly set |
102 | // focus to it should give focus to its text constol part | |
64b62afe | 103 | virtual bool AcceptsFocus() const { return false; } |
fda43a0e | 104 | virtual void SetFocus(); |
64b62afe | 105 | |
405f0fef | 106 | friend class wxSpinCtrlTextGeneric; |
1e6feb95 VZ |
107 | |
108 | protected: | |
109 | // override the base class virtuals involved into geometry calculations | |
dba00620 | 110 | virtual wxSize DoGetBestSize() const; |
40aa1a7e | 111 | virtual wxSize DoGetSizeFromTextSize(int xlen, int ylen = -1) const; |
1e6feb95 VZ |
112 | virtual void DoMoveWindow(int x, int y, int width, int height); |
113 | ||
a2fb9138 VZ |
114 | #ifdef __WXMSW__ |
115 | // and, for MSW, enabling this window itself | |
116 | virtual void DoEnable(bool enable); | |
117 | #endif // __WXMSW__ | |
118 | ||
8cd6a9ad VZ |
119 | // generic double valued functions |
120 | double DoGetValue() const { return m_value; } | |
121 | bool DoSetValue(double val); | |
122 | void DoSetRange(double min_val, double max_val); | |
123 | void DoSetIncrement(double inc); | |
124 | ||
3a712105 VZ |
125 | // update our value to reflect the text control contents (if it has been |
126 | // modified by user, do nothing otherwise) | |
127 | // | |
128 | // can also change the text control if its value is invalid | |
129 | // | |
130 | // return true if our value has changed | |
131 | bool SyncSpinToText(); | |
8cd6a9ad VZ |
132 | |
133 | // Send the correct event type | |
134 | virtual void DoSendEvent() = 0; | |
135 | ||
62f96636 VZ |
136 | // Convert the text to/from the corresponding value. |
137 | virtual bool DoTextToValue(const wxString& text, double *val) = 0; | |
138 | virtual wxString DoValueToText(double val) = 0; | |
139 | ||
70c14728 | 140 | // check if the value is in range |
8cd6a9ad VZ |
141 | bool InRange(double n) const { return (n >= m_min) && (n <= m_max); } |
142 | ||
70c14728 VZ |
143 | // ensure that the value is in range wrapping it round if necessary |
144 | double AdjustToFitInRange(double value) const; | |
145 | ||
146 | ||
8cd6a9ad VZ |
147 | double m_value; |
148 | double m_min; | |
149 | double m_max; | |
150 | double m_increment; | |
151 | bool m_snap_to_ticks; | |
8cd6a9ad VZ |
152 | |
153 | int m_spin_value; | |
1e6feb95 | 154 | |
1e6feb95 | 155 | // the subcontrols |
8cd6a9ad VZ |
156 | wxTextCtrl *m_textCtrl; |
157 | wxSpinButton *m_spinButton; | |
739555e3 | 158 | |
6c0ba0d0 | 159 | private: |
8cd6a9ad VZ |
160 | // common part of all ctors |
161 | void Init(); | |
c8139480 | 162 | |
29604c85 VZ |
163 | // Implement pure virtual function inherited from wxCompositeWindow. |
164 | virtual wxWindowList GetCompositeWindowParts() const; | |
165 | ||
c8139480 | 166 | DECLARE_EVENT_TABLE() |
1e6feb95 VZ |
167 | }; |
168 | ||
169 | #else // !wxUSE_SPINBTN | |
669a6e11 | 170 | |
8cd6a9ad VZ |
171 | #define wxSPINCTRL_GETVALUE_FIX int = 1 |
172 | ||
669a6e11 | 173 | // ---------------------------------------------------------------------------- |
1e6feb95 | 174 | // wxSpinCtrl is just a text control |
669a6e11 VZ |
175 | // ---------------------------------------------------------------------------- |
176 | ||
1e6feb95 VZ |
177 | #include "wx/textctrl.h" |
178 | ||
53a2db12 | 179 | class WXDLLIMPEXP_CORE wxSpinCtrlGenericBase : public wxTextCtrl |
669a6e11 VZ |
180 | { |
181 | public: | |
8cd6a9ad VZ |
182 | wxSpinCtrlGenericBase() : m_value(0), m_min(0), m_max(100), |
183 | m_increment(1), m_snap_to_ticks(false), | |
184 | m_format(wxT("%g")) { } | |
185 | ||
186 | bool Create(wxWindow *parent, | |
187 | wxWindowID id = wxID_ANY, | |
188 | const wxString& value = wxEmptyString, | |
189 | const wxPoint& pos = wxDefaultPosition, | |
190 | const wxSize& size = wxDefaultSize, | |
f1ddb476 | 191 | long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT, |
c218ccee VZ |
192 | double min = 0, double max = 100, double initial = 0, |
193 | double inc = 1, | |
9a83f860 | 194 | const wxString& name = wxT("wxSpinCtrl")) |
8cd6a9ad VZ |
195 | { |
196 | m_min = min; | |
197 | m_max = max; | |
198 | m_value = initial; | |
199 | m_increment = inc; | |
200 | ||
201 | bool ok = wxTextCtrl::Create(parent, id, value, pos, size, style, | |
202 | wxDefaultValidator, name); | |
203 | DoSetValue(initial); | |
204 | ||
205 | return ok; | |
206 | } | |
207 | ||
208 | // accessors | |
209 | // T GetValue() const | |
210 | // T GetMin() const | |
211 | // T GetMax() const | |
212 | // T GetIncrement() const | |
213 | virtual bool GetSnapToTicks() const { return m_snap_to_ticks; } | |
214 | // unsigned GetDigits() const - wxSpinCtrlDouble only | |
215 | ||
216 | // operations | |
217 | virtual void SetValue(const wxString& text) { wxTextCtrl::SetValue(text); } | |
218 | // void SetValue(T val) | |
219 | // void SetRange(T minVal, T maxVal) | |
220 | // void SetIncrement(T inc) | |
c218ccee VZ |
221 | virtual void SetSnapToTicks(bool snap_to_ticks) |
222 | { m_snap_to_ticks = snap_to_ticks; } | |
8cd6a9ad | 223 | // void SetDigits(unsigned digits) - wxSpinCtrlDouble only |
c71830c3 | 224 | |
8cd6a9ad VZ |
225 | // Select text in the textctrl |
226 | //void SetSelection(long from, long to); | |
227 | ||
228 | protected: | |
229 | // generic double valued | |
230 | double DoGetValue() const | |
231 | { | |
232 | double n; | |
233 | if ( (wxSscanf(wxTextCtrl::GetValue(), wxT("%lf"), &n) != 1) ) | |
234 | n = INT_MIN; | |
235 | ||
236 | return n; | |
237 | } | |
238 | ||
c218ccee VZ |
239 | bool DoSetValue(double val) |
240 | { | |
241 | wxTextCtrl::SetValue(wxString::Format(m_format.c_str(), val)); | |
242 | return true; | |
243 | } | |
244 | void DoSetRange(double min_val, double max_val) | |
245 | { | |
246 | m_min = min_val; | |
247 | m_max = max_val; | |
248 | } | |
8cd6a9ad VZ |
249 | void DoSetIncrement(double inc) { m_increment = inc; } // Note: unused |
250 | ||
251 | double m_value; | |
252 | double m_min; | |
253 | double m_max; | |
254 | double m_increment; | |
255 | bool m_snap_to_ticks; | |
256 | wxString m_format; | |
257 | }; | |
258 | ||
259 | #endif // wxUSE_SPINBTN/!wxUSE_SPINBTN | |
260 | ||
261 | #if !defined(wxHAS_NATIVE_SPINCTRL) | |
262 | ||
263 | //----------------------------------------------------------------------------- | |
264 | // wxSpinCtrl | |
265 | //----------------------------------------------------------------------------- | |
266 | ||
267 | class WXDLLIMPEXP_CORE wxSpinCtrl : public wxSpinCtrlGenericBase | |
268 | { | |
269 | public: | |
9e565667 | 270 | wxSpinCtrl() { Init(); } |
c71830c3 | 271 | wxSpinCtrl(wxWindow *parent, |
ca65c044 | 272 | wxWindowID id = wxID_ANY, |
c71830c3 VZ |
273 | const wxString& value = wxEmptyString, |
274 | const wxPoint& pos = wxDefaultPosition, | |
275 | const wxSize& size = wxDefaultSize, | |
f1ddb476 | 276 | long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT, |
c71830c3 | 277 | int min = 0, int max = 100, int initial = 0, |
9a83f860 | 278 | const wxString& name = wxT("wxSpinCtrl")) |
c71830c3 | 279 | { |
9e565667 VZ |
280 | Init(); |
281 | ||
c71830c3 VZ |
282 | Create(parent, id, value, pos, size, style, min, max, initial, name); |
283 | } | |
284 | ||
285 | bool Create(wxWindow *parent, | |
ca65c044 | 286 | wxWindowID id = wxID_ANY, |
c71830c3 VZ |
287 | const wxString& value = wxEmptyString, |
288 | const wxPoint& pos = wxDefaultPosition, | |
289 | const wxSize& size = wxDefaultSize, | |
f1ddb476 | 290 | long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT, |
c71830c3 | 291 | int min = 0, int max = 100, int initial = 0, |
9a83f860 | 292 | const wxString& name = wxT("wxSpinCtrl")) |
c71830c3 | 293 | { |
c218ccee VZ |
294 | return wxSpinCtrlGenericBase::Create(parent, id, value, pos, size, |
295 | style, min, max, initial, 1, name); | |
c71830c3 | 296 | } |
669a6e11 VZ |
297 | |
298 | // accessors | |
f0368d28 PC |
299 | int GetValue(wxSPINCTRL_GETVALUE_FIX) const { return int(DoGetValue()); } |
300 | int GetMin() const { return int(m_min); } | |
301 | int GetMax() const { return int(m_max); } | |
302 | int GetIncrement() const { return int(m_increment); } | |
8cd6a9ad VZ |
303 | |
304 | // operations | |
c218ccee VZ |
305 | void SetValue(const wxString& value) |
306 | { wxSpinCtrlGenericBase::SetValue(value); } | |
8cd6a9ad VZ |
307 | void SetValue( int value ) { DoSetValue(value); } |
308 | void SetRange( int minVal, int maxVal ) { DoSetRange(minVal, maxVal); } | |
92e164ff | 309 | void SetIncrement(int inc) { DoSetIncrement(inc); } |
8cd6a9ad | 310 | |
9e565667 VZ |
311 | virtual int GetBase() const { return m_base; } |
312 | virtual bool SetBase(int base); | |
313 | ||
8cd6a9ad VZ |
314 | protected: |
315 | virtual void DoSendEvent(); | |
316 | ||
62f96636 VZ |
317 | virtual bool DoTextToValue(const wxString& text, double *val); |
318 | virtual wxString DoValueToText(double val); | |
9e565667 VZ |
319 | |
320 | private: | |
321 | // Common part of all ctors. | |
322 | void Init() | |
323 | { | |
324 | m_base = 10; | |
325 | } | |
326 | ||
327 | int m_base; | |
328 | ||
8cd6a9ad VZ |
329 | DECLARE_DYNAMIC_CLASS(wxSpinCtrl) |
330 | }; | |
331 | ||
332 | #endif // wxHAS_NATIVE_SPINCTRL | |
333 | ||
334 | //----------------------------------------------------------------------------- | |
335 | // wxSpinCtrlDouble | |
336 | //----------------------------------------------------------------------------- | |
337 | ||
338 | class WXDLLIMPEXP_CORE wxSpinCtrlDouble : public wxSpinCtrlGenericBase | |
339 | { | |
340 | public: | |
62f96636 | 341 | wxSpinCtrlDouble() { Init(); } |
8cd6a9ad VZ |
342 | wxSpinCtrlDouble(wxWindow *parent, |
343 | wxWindowID id = wxID_ANY, | |
344 | const wxString& value = wxEmptyString, | |
345 | const wxPoint& pos = wxDefaultPosition, | |
346 | const wxSize& size = wxDefaultSize, | |
f1ddb476 | 347 | long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT, |
c218ccee VZ |
348 | double min = 0, double max = 100, double initial = 0, |
349 | double inc = 1, | |
9a83f860 | 350 | const wxString& name = wxT("wxSpinCtrlDouble")) |
669a6e11 | 351 | { |
62f96636 VZ |
352 | Init(); |
353 | ||
c218ccee VZ |
354 | Create(parent, id, value, pos, size, style, |
355 | min, max, initial, inc, name); | |
8cd6a9ad | 356 | } |
669a6e11 | 357 | |
8cd6a9ad VZ |
358 | bool Create(wxWindow *parent, |
359 | wxWindowID id = wxID_ANY, | |
360 | const wxString& value = wxEmptyString, | |
361 | const wxPoint& pos = wxDefaultPosition, | |
362 | const wxSize& size = wxDefaultSize, | |
f1ddb476 | 363 | long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT, |
c218ccee VZ |
364 | double min = 0, double max = 100, double initial = 0, |
365 | double inc = 1, | |
9a83f860 | 366 | const wxString& name = wxT("wxSpinCtrlDouble")) |
8cd6a9ad | 367 | { |
c218ccee VZ |
368 | return wxSpinCtrlGenericBase::Create(parent, id, value, pos, size, |
369 | style, min, max, initial, | |
370 | inc, name); | |
669a6e11 VZ |
371 | } |
372 | ||
8cd6a9ad VZ |
373 | // accessors |
374 | double GetValue(wxSPINCTRL_GETVALUE_FIX) const { return DoGetValue(); } | |
375 | double GetMin() const { return m_min; } | |
376 | double GetMax() const { return m_max; } | |
377 | double GetIncrement() const { return m_increment; } | |
378 | unsigned GetDigits() const { return m_digits; } | |
669a6e11 VZ |
379 | |
380 | // operations | |
c218ccee VZ |
381 | void SetValue(const wxString& value) |
382 | { wxSpinCtrlGenericBase::SetValue(value); } | |
8cd6a9ad VZ |
383 | void SetValue(double value) { DoSetValue(value); } |
384 | void SetRange(double minVal, double maxVal) { DoSetRange(minVal, maxVal); } | |
385 | void SetIncrement(double inc) { DoSetIncrement(inc); } | |
386 | void SetDigits(unsigned digits); | |
669a6e11 | 387 | |
9e565667 VZ |
388 | // We don't implement bases support for floating point numbers, this is not |
389 | // very useful in practice. | |
390 | virtual int GetBase() const { return 10; } | |
391 | virtual bool SetBase(int WXUNUSED(base)) { return 0; } | |
392 | ||
669a6e11 | 393 | protected: |
8cd6a9ad | 394 | virtual void DoSendEvent(); |
669a6e11 | 395 | |
62f96636 VZ |
396 | virtual bool DoTextToValue(const wxString& text, double *val); |
397 | virtual wxString DoValueToText(double val); | |
398 | ||
8cd6a9ad | 399 | unsigned m_digits; |
5fde6fcc | 400 | |
62f96636 VZ |
401 | private: |
402 | // Common part of all ctors. | |
403 | void Init() | |
404 | { | |
405 | m_digits = 0; | |
406 | m_format = wxS("%g"); | |
407 | } | |
408 | ||
409 | wxString m_format; | |
410 | ||
8cd6a9ad | 411 | DECLARE_DYNAMIC_CLASS(wxSpinCtrlDouble) |
669a6e11 VZ |
412 | }; |
413 | ||
414 | #endif // _WX_GENERIC_SPINCTRL_H_ |