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