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