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