]>
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, | |
54 | double min = 0, double max = 100, double initial = 0, double inc = 1, | |
55 | const wxString& name = _T("wxSpinCtrl")); | |
56 | ||
57 | virtual ~wxSpinCtrlGenericBase(); | |
58 | ||
59 | // accessors | |
60 | // T GetValue() const | |
61 | // T GetMin() const | |
62 | // T GetMax() const | |
63 | // T GetIncrement() const | |
64 | virtual bool GetSnapToTicks() const { return m_snap_to_ticks; } | |
65 | // unsigned GetDigits() const - wxSpinCtrlDouble only | |
66 | ||
67 | // operations | |
68 | virtual void SetValue(const wxString& text); | |
69 | // void SetValue(T val) | |
70 | // void SetRange(T minVal, T maxVal) | |
71 | // void SetIncrement(T inc) | |
72 | virtual void SetSnapToTicks(bool snap_to_ticks); | |
73 | // void SetDigits(unsigned digits) - wxSpinCtrlDouble only | |
74 | ||
75 | // Select text in the textctrl | |
76 | void SetSelection(long from, long to); | |
77 | ||
78 | // implementation from now on | |
79 | ||
80 | // forward these functions to all subcontrols | |
81 | virtual bool Enable(bool enable = true); | |
82 | virtual bool Show(bool show = true); | |
83 | virtual bool Reparent(wxWindowBase *newParent); | |
84 | ||
85 | // get the subcontrols | |
86 | wxTextCtrl *GetText() const { return m_textCtrl; } | |
87 | wxSpinButton *GetSpinButton() const { return m_spinButton; } | |
88 | ||
89 | // forwarded events from children windows | |
90 | void OnSpinButton(wxSpinEvent& event); | |
91 | void OnTextEnter(wxCommandEvent& event); | |
92 | void OnTextChar(wxKeyEvent& event); | |
93 | ||
94 | friend class wxSpinCtrlTextGeneric; | |
95 | ||
96 | protected: | |
97 | // override the base class virtuals involved into geometry calculations | |
98 | virtual wxSize DoGetBestSize() const; | |
99 | virtual void DoMoveWindow(int x, int y, int width, int height); | |
100 | ||
101 | // generic double valued functions | |
102 | double DoGetValue() const { return m_value; } | |
103 | bool DoSetValue(double val); | |
104 | void DoSetRange(double min_val, double max_val); | |
105 | void DoSetIncrement(double inc); | |
106 | ||
107 | // Ensure that the textctrl shows correct value | |
108 | void SyncSpinToText(); | |
109 | ||
110 | // Send the correct event type | |
111 | virtual void DoSendEvent() = 0; | |
112 | ||
113 | // check if the value is in range | |
114 | bool InRange(double n) const { return (n >= m_min) && (n <= m_max); } | |
115 | ||
116 | // ensure that the value is in range wrapping it round if necessary | |
117 | double AdjustToFitInRange(double value) const; | |
118 | ||
119 | ||
120 | double m_value; | |
121 | double m_min; | |
122 | double m_max; | |
123 | double m_increment; | |
124 | bool m_snap_to_ticks; | |
125 | wxString m_format; | |
126 | ||
127 | int m_spin_value; | |
128 | ||
129 | // the subcontrols | |
130 | wxTextCtrl *m_textCtrl; | |
131 | wxSpinButton *m_spinButton; | |
132 | ||
133 | private: | |
134 | // common part of all ctors | |
135 | void Init(); | |
136 | }; | |
137 | ||
138 | #else // !wxUSE_SPINBTN | |
139 | ||
140 | #define wxSPINCTRL_GETVALUE_FIX int = 1 | |
141 | ||
142 | // ---------------------------------------------------------------------------- | |
143 | // wxSpinCtrl is just a text control | |
144 | // ---------------------------------------------------------------------------- | |
145 | ||
146 | #include "wx/textctrl.h" | |
147 | ||
148 | class WXDLLIMPEXP_CORE wxSpinCtrlGenericBase : public wxTextCtrl | |
149 | { | |
150 | public: | |
151 | wxSpinCtrlGenericBase() : m_value(0), m_min(0), m_max(100), | |
152 | m_increment(1), m_snap_to_ticks(false), | |
153 | m_format(wxT("%g")) { } | |
154 | ||
155 | bool Create(wxWindow *parent, | |
156 | wxWindowID id = wxID_ANY, | |
157 | const wxString& value = wxEmptyString, | |
158 | const wxPoint& pos = wxDefaultPosition, | |
159 | const wxSize& size = wxDefaultSize, | |
160 | long style = wxSP_ARROW_KEYS, | |
161 | double min = 0, double max = 100, double initial = 0, double inc = 1, | |
162 | const wxString& name = _T("wxSpinCtrl")) | |
163 | { | |
164 | m_min = min; | |
165 | m_max = max; | |
166 | m_value = initial; | |
167 | m_increment = inc; | |
168 | ||
169 | bool ok = wxTextCtrl::Create(parent, id, value, pos, size, style, | |
170 | wxDefaultValidator, name); | |
171 | DoSetValue(initial); | |
172 | ||
173 | return ok; | |
174 | } | |
175 | ||
176 | // accessors | |
177 | // T GetValue() const | |
178 | // T GetMin() const | |
179 | // T GetMax() const | |
180 | // T GetIncrement() const | |
181 | virtual bool GetSnapToTicks() const { return m_snap_to_ticks; } | |
182 | // unsigned GetDigits() const - wxSpinCtrlDouble only | |
183 | ||
184 | // operations | |
185 | virtual void SetValue(const wxString& text) { wxTextCtrl::SetValue(text); } | |
186 | // void SetValue(T val) | |
187 | // void SetRange(T minVal, T maxVal) | |
188 | // void SetIncrement(T inc) | |
189 | virtual void SetSnapToTicks(bool snap_to_ticks) { m_snap_to_ticks = snap_to_ticks; } | |
190 | // void SetDigits(unsigned digits) - wxSpinCtrlDouble only | |
191 | ||
192 | // Select text in the textctrl | |
193 | //void SetSelection(long from, long to); | |
194 | ||
195 | protected: | |
196 | // generic double valued | |
197 | double DoGetValue() const | |
198 | { | |
199 | double n; | |
200 | if ( (wxSscanf(wxTextCtrl::GetValue(), wxT("%lf"), &n) != 1) ) | |
201 | n = INT_MIN; | |
202 | ||
203 | return n; | |
204 | } | |
205 | ||
206 | bool DoSetValue(double val) { wxTextCtrl::SetValue(wxString::Format(m_format.c_str(), val)); return true; } | |
207 | void DoSetRange(double min_val, double max_val) { m_min = min_val; m_max = max_val; } | |
208 | void DoSetIncrement(double inc) { m_increment = inc; } // Note: unused | |
209 | ||
210 | double m_value; | |
211 | double m_min; | |
212 | double m_max; | |
213 | double m_increment; | |
214 | bool m_snap_to_ticks; | |
215 | wxString m_format; | |
216 | }; | |
217 | ||
218 | #endif // wxUSE_SPINBTN/!wxUSE_SPINBTN | |
219 | ||
220 | #if !defined(wxHAS_NATIVE_SPINCTRL) | |
221 | ||
222 | //----------------------------------------------------------------------------- | |
223 | // wxSpinCtrl | |
224 | //----------------------------------------------------------------------------- | |
225 | ||
226 | class WXDLLIMPEXP_CORE wxSpinCtrl : public wxSpinCtrlGenericBase | |
227 | { | |
228 | public: | |
229 | wxSpinCtrl() {} | |
230 | wxSpinCtrl(wxWindow *parent, | |
231 | wxWindowID id = wxID_ANY, | |
232 | const wxString& value = wxEmptyString, | |
233 | const wxPoint& pos = wxDefaultPosition, | |
234 | const wxSize& size = wxDefaultSize, | |
235 | long style = wxSP_ARROW_KEYS, | |
236 | int min = 0, int max = 100, int initial = 0, | |
237 | const wxString& name = _T("wxSpinCtrl")) | |
238 | { | |
239 | Create(parent, id, value, pos, size, style, min, max, initial, name); | |
240 | } | |
241 | ||
242 | bool Create(wxWindow *parent, | |
243 | wxWindowID id = wxID_ANY, | |
244 | const wxString& value = wxEmptyString, | |
245 | const wxPoint& pos = wxDefaultPosition, | |
246 | const wxSize& size = wxDefaultSize, | |
247 | long style = wxSP_ARROW_KEYS, | |
248 | int min = 0, int max = 100, int initial = 0, | |
249 | const wxString& name = _T("wxSpinCtrl")) | |
250 | { | |
251 | return wxSpinCtrlGenericBase::Create(parent, id, value, pos, size, style, min, max, initial, 1, name); | |
252 | } | |
253 | ||
254 | // accessors | |
255 | int GetValue(wxSPINCTRL_GETVALUE_FIX) const { return wxRound( DoGetValue() ); } | |
256 | int GetMin() const { return wxRound( m_min ); } | |
257 | int GetMax() const { return wxRound( m_max ); } | |
258 | int GetIncrement() const { return wxRound( m_increment ); } | |
259 | ||
260 | // operations | |
261 | void SetValue(const wxString& value) { wxSpinCtrlGenericBase::SetValue(value); } // visibility problem w/ gcc | |
262 | void SetValue( int value ) { DoSetValue(value); } | |
263 | void SetRange( int minVal, int maxVal ) { DoSetRange(minVal, maxVal); } | |
264 | void SetIncrement( double inc ) { DoSetIncrement(inc); } | |
265 | ||
266 | protected: | |
267 | virtual void DoSendEvent(); | |
268 | ||
269 | private: | |
270 | DECLARE_DYNAMIC_CLASS(wxSpinCtrl) | |
271 | }; | |
272 | ||
273 | #endif // wxHAS_NATIVE_SPINCTRL | |
274 | ||
275 | //----------------------------------------------------------------------------- | |
276 | // wxSpinCtrlDouble | |
277 | //----------------------------------------------------------------------------- | |
278 | ||
279 | class WXDLLIMPEXP_CORE wxSpinCtrlDouble : public wxSpinCtrlGenericBase | |
280 | { | |
281 | public: | |
282 | wxSpinCtrlDouble() : m_digits(0) { } | |
283 | wxSpinCtrlDouble(wxWindow *parent, | |
284 | wxWindowID id = wxID_ANY, | |
285 | const wxString& value = wxEmptyString, | |
286 | const wxPoint& pos = wxDefaultPosition, | |
287 | const wxSize& size = wxDefaultSize, | |
288 | long style = wxSP_ARROW_KEYS, | |
289 | double min = 0, double max = 100, double initial = 0, double inc = 1, | |
290 | const wxString& name = _T("wxSpinCtrlDouble")) | |
291 | { | |
292 | m_digits = 0; | |
293 | Create(parent, id, value, pos, size, style, min, max, initial, inc, name); | |
294 | } | |
295 | ||
296 | bool Create(wxWindow *parent, | |
297 | wxWindowID id = wxID_ANY, | |
298 | const wxString& value = wxEmptyString, | |
299 | const wxPoint& pos = wxDefaultPosition, | |
300 | const wxSize& size = wxDefaultSize, | |
301 | long style = wxSP_ARROW_KEYS, | |
302 | double min = 0, double max = 100, double initial = 0, double inc = 1, | |
303 | const wxString& name = _T("wxSpinCtrlDouble")) | |
304 | { | |
305 | return wxSpinCtrlGenericBase::Create(parent, id, value, pos, size, style, min, max, initial, inc, name); | |
306 | } | |
307 | ||
308 | // accessors | |
309 | double GetValue(wxSPINCTRL_GETVALUE_FIX) const { return DoGetValue(); } | |
310 | double GetMin() const { return m_min; } | |
311 | double GetMax() const { return m_max; } | |
312 | double GetIncrement() const { return m_increment; } | |
313 | unsigned GetDigits() const { return m_digits; } | |
314 | ||
315 | // operations | |
316 | void SetValue(const wxString& value) { wxSpinCtrlGenericBase::SetValue(value); } // visibility problem w/ gcc | |
317 | void SetValue(double value) { DoSetValue(value); } | |
318 | void SetRange(double minVal, double maxVal) { DoSetRange(minVal, maxVal); } | |
319 | void SetIncrement(double inc) { DoSetIncrement(inc); } | |
320 | void SetDigits(unsigned digits); | |
321 | ||
322 | protected: | |
323 | virtual void DoSendEvent(); | |
324 | ||
325 | unsigned m_digits; | |
326 | ||
327 | private: | |
328 | DECLARE_DYNAMIC_CLASS(wxSpinCtrlDouble) | |
329 | }; | |
330 | ||
331 | #endif // _WX_GENERIC_SPINCTRL_H_ |