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