]>
Commit | Line | Data |
---|---|---|
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 | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifndef _WX_GENERIC_SPINCTRL_H_ | |
13 | #define _WX_GENERIC_SPINCTRL_H_ | |
14 | ||
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 | ||
23 | #if wxUSE_SPINBTN | |
24 | ||
25 | class WXDLLIMPEXP_FWD_CORE wxSpinButton; | |
26 | class WXDLLIMPEXP_FWD_CORE wxTextCtrl; | |
27 | ||
28 | class wxSpinCtrlTextGeneric; // wxTextCtrl used for the wxSpinCtrlGenericBase | |
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 | ||
34 | // ---------------------------------------------------------------------------- | |
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. | |
41 | // ---------------------------------------------------------------------------- | |
42 | ||
43 | class WXDLLIMPEXP_CORE wxSpinCtrlGenericBase : public wxSpinCtrlBase | |
44 | { | |
45 | public: | |
46 | wxSpinCtrlGenericBase() { Init(); } | |
47 | ||
48 | bool Create(wxWindow *parent, | |
49 | wxWindowID id = wxID_ANY, | |
50 | const wxString& value = wxEmptyString, | |
51 | const wxPoint& pos = wxDefaultPosition, | |
52 | const wxSize& size = wxDefaultSize, | |
53 | long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT, | |
54 | double min = 0, double max = 100, double initial = 0, | |
55 | double inc = 1, | |
56 | const wxString& name = wxT("wxSpinCtrl")); | |
57 | ||
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 | |
67 | ||
68 | // operations | |
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 | |
77 | void SetSelection(long from, long to); | |
78 | ||
79 | // implementation from now on | |
80 | ||
81 | // forward these functions to all subcontrols | |
82 | virtual bool Enable(bool enable = true); | |
83 | virtual bool Show(bool show = true); | |
84 | #if wxUSE_TOOLTIPS | |
85 | virtual void DoSetToolTip(wxToolTip *tip); | |
86 | #endif // wxUSE_TOOLTIPS | |
87 | ||
88 | // get the subcontrols | |
89 | wxTextCtrl *GetText() const { return m_textCtrl; } | |
90 | wxSpinButton *GetSpinButton() const { return m_spinButton; } | |
91 | ||
92 | // forwarded events from children windows | |
93 | void OnSpinButton(wxSpinEvent& event); | |
94 | void OnTextLostFocus(wxFocusEvent& event); | |
95 | void OnTextChar(wxKeyEvent& event); | |
96 | ||
97 | // this window itself is used only as a container for its sub windows so it | |
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 | |
100 | virtual bool AcceptsFocus() const { return false; } | |
101 | virtual void SetFocus(); | |
102 | ||
103 | friend class wxSpinCtrlTextGeneric; | |
104 | ||
105 | protected: | |
106 | // override the base class virtuals involved into geometry calculations | |
107 | virtual wxSize DoGetBestSize() const; | |
108 | virtual void DoMoveWindow(int x, int y, int width, int height); | |
109 | ||
110 | #ifdef __WXMSW__ | |
111 | // and, for MSW, enabling this window itself | |
112 | virtual void DoEnable(bool enable); | |
113 | #endif // __WXMSW__ | |
114 | ||
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 | ||
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(); | |
128 | ||
129 | // Send the correct event type | |
130 | virtual void DoSendEvent() = 0; | |
131 | ||
132 | // check if the value is in range | |
133 | bool InRange(double n) const { return (n >= m_min) && (n <= m_max); } | |
134 | ||
135 | // ensure that the value is in range wrapping it round if necessary | |
136 | double AdjustToFitInRange(double value) const; | |
137 | ||
138 | ||
139 | double m_value; | |
140 | double m_min; | |
141 | double m_max; | |
142 | double m_increment; | |
143 | bool m_snap_to_ticks; | |
144 | wxString m_format; | |
145 | ||
146 | int m_spin_value; | |
147 | ||
148 | // the subcontrols | |
149 | wxTextCtrl *m_textCtrl; | |
150 | wxSpinButton *m_spinButton; | |
151 | ||
152 | private: | |
153 | // common part of all ctors | |
154 | void Init(); | |
155 | ||
156 | DECLARE_EVENT_TABLE() | |
157 | }; | |
158 | ||
159 | #else // !wxUSE_SPINBTN | |
160 | ||
161 | #define wxSPINCTRL_GETVALUE_FIX int = 1 | |
162 | ||
163 | // ---------------------------------------------------------------------------- | |
164 | // wxSpinCtrl is just a text control | |
165 | // ---------------------------------------------------------------------------- | |
166 | ||
167 | #include "wx/textctrl.h" | |
168 | ||
169 | class WXDLLIMPEXP_CORE wxSpinCtrlGenericBase : public wxTextCtrl | |
170 | { | |
171 | public: | |
172 | wxSpinCtrlGenericBase() : m_value(0), m_min(0), m_max(100), | |
173 | m_increment(1), m_snap_to_ticks(false), | |
174 | m_format(wxT("%g")) { } | |
175 | ||
176 | bool Create(wxWindow *parent, | |
177 | wxWindowID id = wxID_ANY, | |
178 | const wxString& value = wxEmptyString, | |
179 | const wxPoint& pos = wxDefaultPosition, | |
180 | const wxSize& size = wxDefaultSize, | |
181 | long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT, | |
182 | double min = 0, double max = 100, double initial = 0, | |
183 | double inc = 1, | |
184 | const wxString& name = wxT("wxSpinCtrl")) | |
185 | { | |
186 | m_min = min; | |
187 | m_max = max; | |
188 | m_value = initial; | |
189 | m_increment = inc; | |
190 | ||
191 | bool ok = wxTextCtrl::Create(parent, id, value, pos, size, style, | |
192 | wxDefaultValidator, name); | |
193 | DoSetValue(initial); | |
194 | ||
195 | return ok; | |
196 | } | |
197 | ||
198 | // accessors | |
199 | // T GetValue() const | |
200 | // T GetMin() const | |
201 | // T GetMax() const | |
202 | // T GetIncrement() const | |
203 | virtual bool GetSnapToTicks() const { return m_snap_to_ticks; } | |
204 | // unsigned GetDigits() const - wxSpinCtrlDouble only | |
205 | ||
206 | // operations | |
207 | virtual void SetValue(const wxString& text) { wxTextCtrl::SetValue(text); } | |
208 | // void SetValue(T val) | |
209 | // void SetRange(T minVal, T maxVal) | |
210 | // void SetIncrement(T inc) | |
211 | virtual void SetSnapToTicks(bool snap_to_ticks) | |
212 | { m_snap_to_ticks = snap_to_ticks; } | |
213 | // void SetDigits(unsigned digits) - wxSpinCtrlDouble only | |
214 | ||
215 | // Select text in the textctrl | |
216 | //void SetSelection(long from, long to); | |
217 | ||
218 | protected: | |
219 | // generic double valued | |
220 | double DoGetValue() const | |
221 | { | |
222 | double n; | |
223 | if ( (wxSscanf(wxTextCtrl::GetValue(), wxT("%lf"), &n) != 1) ) | |
224 | n = INT_MIN; | |
225 | ||
226 | return n; | |
227 | } | |
228 | ||
229 | bool DoSetValue(double val) | |
230 | { | |
231 | wxTextCtrl::SetValue(wxString::Format(m_format.c_str(), val)); | |
232 | return true; | |
233 | } | |
234 | void DoSetRange(double min_val, double max_val) | |
235 | { | |
236 | m_min = min_val; | |
237 | m_max = max_val; | |
238 | } | |
239 | void DoSetIncrement(double inc) { m_increment = inc; } // Note: unused | |
240 | ||
241 | double m_value; | |
242 | double m_min; | |
243 | double m_max; | |
244 | double m_increment; | |
245 | bool m_snap_to_ticks; | |
246 | wxString m_format; | |
247 | }; | |
248 | ||
249 | #endif // wxUSE_SPINBTN/!wxUSE_SPINBTN | |
250 | ||
251 | #if !defined(wxHAS_NATIVE_SPINCTRL) | |
252 | ||
253 | //----------------------------------------------------------------------------- | |
254 | // wxSpinCtrl | |
255 | //----------------------------------------------------------------------------- | |
256 | ||
257 | class WXDLLIMPEXP_CORE wxSpinCtrl : public wxSpinCtrlGenericBase | |
258 | { | |
259 | public: | |
260 | wxSpinCtrl() {} | |
261 | wxSpinCtrl(wxWindow *parent, | |
262 | wxWindowID id = wxID_ANY, | |
263 | const wxString& value = wxEmptyString, | |
264 | const wxPoint& pos = wxDefaultPosition, | |
265 | const wxSize& size = wxDefaultSize, | |
266 | long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT, | |
267 | int min = 0, int max = 100, int initial = 0, | |
268 | const wxString& name = wxT("wxSpinCtrl")) | |
269 | { | |
270 | Create(parent, id, value, pos, size, style, min, max, initial, name); | |
271 | } | |
272 | ||
273 | bool Create(wxWindow *parent, | |
274 | wxWindowID id = wxID_ANY, | |
275 | const wxString& value = wxEmptyString, | |
276 | const wxPoint& pos = wxDefaultPosition, | |
277 | const wxSize& size = wxDefaultSize, | |
278 | long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT, | |
279 | int min = 0, int max = 100, int initial = 0, | |
280 | const wxString& name = wxT("wxSpinCtrl")) | |
281 | { | |
282 | return wxSpinCtrlGenericBase::Create(parent, id, value, pos, size, | |
283 | style, min, max, initial, 1, name); | |
284 | } | |
285 | ||
286 | // accessors | |
287 | int GetValue(wxSPINCTRL_GETVALUE_FIX) const { return int(DoGetValue()); } | |
288 | int GetMin() const { return int(m_min); } | |
289 | int GetMax() const { return int(m_max); } | |
290 | int GetIncrement() const { return int(m_increment); } | |
291 | ||
292 | // operations | |
293 | void SetValue(const wxString& value) | |
294 | { wxSpinCtrlGenericBase::SetValue(value); } | |
295 | void SetValue( int value ) { DoSetValue(value); } | |
296 | void SetRange( int minVal, int maxVal ) { DoSetRange(minVal, maxVal); } | |
297 | void SetIncrement(int inc) { DoSetIncrement(inc); } | |
298 | ||
299 | protected: | |
300 | virtual void DoSendEvent(); | |
301 | ||
302 | DECLARE_DYNAMIC_CLASS(wxSpinCtrl) | |
303 | }; | |
304 | ||
305 | #endif // wxHAS_NATIVE_SPINCTRL | |
306 | ||
307 | //----------------------------------------------------------------------------- | |
308 | // wxSpinCtrlDouble | |
309 | //----------------------------------------------------------------------------- | |
310 | ||
311 | class WXDLLIMPEXP_CORE wxSpinCtrlDouble : public wxSpinCtrlGenericBase | |
312 | { | |
313 | public: | |
314 | wxSpinCtrlDouble() : m_digits(0) { } | |
315 | wxSpinCtrlDouble(wxWindow *parent, | |
316 | wxWindowID id = wxID_ANY, | |
317 | const wxString& value = wxEmptyString, | |
318 | const wxPoint& pos = wxDefaultPosition, | |
319 | const wxSize& size = wxDefaultSize, | |
320 | long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT, | |
321 | double min = 0, double max = 100, double initial = 0, | |
322 | double inc = 1, | |
323 | const wxString& name = wxT("wxSpinCtrlDouble")) | |
324 | { | |
325 | m_digits = 0; | |
326 | Create(parent, id, value, pos, size, style, | |
327 | min, max, initial, inc, name); | |
328 | } | |
329 | ||
330 | bool Create(wxWindow *parent, | |
331 | wxWindowID id = wxID_ANY, | |
332 | const wxString& value = wxEmptyString, | |
333 | const wxPoint& pos = wxDefaultPosition, | |
334 | const wxSize& size = wxDefaultSize, | |
335 | long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT, | |
336 | double min = 0, double max = 100, double initial = 0, | |
337 | double inc = 1, | |
338 | const wxString& name = wxT("wxSpinCtrlDouble")) | |
339 | { | |
340 | return wxSpinCtrlGenericBase::Create(parent, id, value, pos, size, | |
341 | style, min, max, initial, | |
342 | inc, name); | |
343 | } | |
344 | ||
345 | // accessors | |
346 | double GetValue(wxSPINCTRL_GETVALUE_FIX) const { return DoGetValue(); } | |
347 | double GetMin() const { return m_min; } | |
348 | double GetMax() const { return m_max; } | |
349 | double GetIncrement() const { return m_increment; } | |
350 | unsigned GetDigits() const { return m_digits; } | |
351 | ||
352 | // operations | |
353 | void SetValue(const wxString& value) | |
354 | { wxSpinCtrlGenericBase::SetValue(value); } | |
355 | void SetValue(double value) { DoSetValue(value); } | |
356 | void SetRange(double minVal, double maxVal) { DoSetRange(minVal, maxVal); } | |
357 | void SetIncrement(double inc) { DoSetIncrement(inc); } | |
358 | void SetDigits(unsigned digits); | |
359 | ||
360 | protected: | |
361 | virtual void DoSendEvent(); | |
362 | ||
363 | unsigned m_digits; | |
364 | ||
365 | DECLARE_DYNAMIC_CLASS(wxSpinCtrlDouble) | |
366 | }; | |
367 | ||
368 | #endif // _WX_GENERIC_SPINCTRL_H_ |