]> git.saurik.com Git - wxWidgets.git/blob - include/wx/generic/spinctlg.h
Make wxSpinCtrlGenericBase a wxCompositeWindow.
[wxWidgets.git] / include / wx / generic / spinctlg.h
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 #include "wx/compositewin.h"
26
27 class WXDLLIMPEXP_FWD_CORE wxSpinButton;
28 class WXDLLIMPEXP_FWD_CORE wxTextCtrl;
29
30 class wxSpinCtrlTextGeneric; // wxTextCtrl used for the wxSpinCtrlGenericBase
31
32 // The !wxUSE_SPINBTN version's GetValue() function conflicts with the
33 // wxTextCtrl's GetValue() and so you have to input a dummy int value.
34 #define wxSPINCTRL_GETVALUE_FIX
35
36 // ----------------------------------------------------------------------------
37 // wxSpinCtrlGeneric is a combination of wxTextCtrl and wxSpinButton
38 //
39 // This class manages a double valued generic spinctrl through the DoGet/SetXXX
40 // functions that are made public as Get/SetXXX functions for int or double
41 // for the wxSpinCtrl and wxSpinCtrlDouble classes respectively to avoid
42 // function ambiguity.
43 // ----------------------------------------------------------------------------
44
45 class WXDLLIMPEXP_CORE wxSpinCtrlGenericBase
46 : public wxCompositeWindow<wxSpinCtrlBase>
47 {
48 public:
49 wxSpinCtrlGenericBase() { Init(); }
50
51 bool Create(wxWindow *parent,
52 wxWindowID id = wxID_ANY,
53 const wxString& value = wxEmptyString,
54 const wxPoint& pos = wxDefaultPosition,
55 const wxSize& size = wxDefaultSize,
56 long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
57 double min = 0, double max = 100, double initial = 0,
58 double inc = 1,
59 const wxString& name = wxT("wxSpinCtrl"));
60
61 virtual ~wxSpinCtrlGenericBase();
62
63 // accessors
64 // T GetValue() const
65 // T GetMin() const
66 // T GetMax() const
67 // T GetIncrement() const
68 virtual bool GetSnapToTicks() const { return m_snap_to_ticks; }
69 // unsigned GetDigits() const - wxSpinCtrlDouble only
70
71 // operations
72 virtual void SetValue(const wxString& text);
73 // void SetValue(T val)
74 // void SetRange(T minVal, T maxVal)
75 // void SetIncrement(T inc)
76 virtual void SetSnapToTicks(bool snap_to_ticks);
77 // void SetDigits(unsigned digits) - wxSpinCtrlDouble only
78
79 // Select text in the textctrl
80 void SetSelection(long from, long to);
81
82 // implementation from now on
83
84 // forward these functions to all subcontrols
85 virtual bool Enable(bool enable = true);
86 virtual bool Show(bool show = true);
87 #if wxUSE_TOOLTIPS
88 virtual void DoSetToolTip(wxToolTip *tip);
89 #endif // wxUSE_TOOLTIPS
90
91 // get the subcontrols
92 wxTextCtrl *GetText() const { return m_textCtrl; }
93 wxSpinButton *GetSpinButton() const { return m_spinButton; }
94
95 // forwarded events from children windows
96 void OnSpinButton(wxSpinEvent& event);
97 void OnTextLostFocus(wxFocusEvent& event);
98 void OnTextChar(wxKeyEvent& event);
99
100 // this window itself is used only as a container for its sub windows so it
101 // shouldn't accept the focus at all and any attempts to explicitly set
102 // focus to it should give focus to its text constol part
103 virtual bool AcceptsFocus() const { return false; }
104 virtual void SetFocus();
105
106 friend class wxSpinCtrlTextGeneric;
107
108 protected:
109 // override the base class virtuals involved into geometry calculations
110 virtual wxSize DoGetBestSize() const;
111 virtual void DoMoveWindow(int x, int y, int width, int height);
112
113 #ifdef __WXMSW__
114 // and, for MSW, enabling this window itself
115 virtual void DoEnable(bool enable);
116 #endif // __WXMSW__
117
118 // generic double valued functions
119 double DoGetValue() const { return m_value; }
120 bool DoSetValue(double val);
121 void DoSetRange(double min_val, double max_val);
122 void DoSetIncrement(double inc);
123
124 // update our value to reflect the text control contents (if it has been
125 // modified by user, do nothing otherwise)
126 //
127 // can also change the text control if its value is invalid
128 //
129 // return true if our value has changed
130 bool SyncSpinToText();
131
132 // Send the correct event type
133 virtual void DoSendEvent() = 0;
134
135 // Convert the text to/from the corresponding value.
136 virtual bool DoTextToValue(const wxString& text, double *val) = 0;
137 virtual wxString DoValueToText(double val) = 0;
138
139 // check if the value is in range
140 bool InRange(double n) const { return (n >= m_min) && (n <= m_max); }
141
142 // ensure that the value is in range wrapping it round if necessary
143 double AdjustToFitInRange(double value) const;
144
145
146 double m_value;
147 double m_min;
148 double m_max;
149 double m_increment;
150 bool m_snap_to_ticks;
151
152 int m_spin_value;
153
154 // the subcontrols
155 wxTextCtrl *m_textCtrl;
156 wxSpinButton *m_spinButton;
157
158 private:
159 // common part of all ctors
160 void Init();
161
162 // Implement pure virtual function inherited from wxCompositeWindow.
163 virtual wxWindowList GetCompositeWindowParts() const;
164
165 DECLARE_EVENT_TABLE()
166 };
167
168 #else // !wxUSE_SPINBTN
169
170 #define wxSPINCTRL_GETVALUE_FIX int = 1
171
172 // ----------------------------------------------------------------------------
173 // wxSpinCtrl is just a text control
174 // ----------------------------------------------------------------------------
175
176 #include "wx/textctrl.h"
177
178 class WXDLLIMPEXP_CORE wxSpinCtrlGenericBase : public wxTextCtrl
179 {
180 public:
181 wxSpinCtrlGenericBase() : m_value(0), m_min(0), m_max(100),
182 m_increment(1), m_snap_to_ticks(false),
183 m_format(wxT("%g")) { }
184
185 bool Create(wxWindow *parent,
186 wxWindowID id = wxID_ANY,
187 const wxString& value = wxEmptyString,
188 const wxPoint& pos = wxDefaultPosition,
189 const wxSize& size = wxDefaultSize,
190 long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
191 double min = 0, double max = 100, double initial = 0,
192 double inc = 1,
193 const wxString& name = wxT("wxSpinCtrl"))
194 {
195 m_min = min;
196 m_max = max;
197 m_value = initial;
198 m_increment = inc;
199
200 bool ok = wxTextCtrl::Create(parent, id, value, pos, size, style,
201 wxDefaultValidator, name);
202 DoSetValue(initial);
203
204 return ok;
205 }
206
207 // accessors
208 // T GetValue() const
209 // T GetMin() const
210 // T GetMax() const
211 // T GetIncrement() const
212 virtual bool GetSnapToTicks() const { return m_snap_to_ticks; }
213 // unsigned GetDigits() const - wxSpinCtrlDouble only
214
215 // operations
216 virtual void SetValue(const wxString& text) { wxTextCtrl::SetValue(text); }
217 // void SetValue(T val)
218 // void SetRange(T minVal, T maxVal)
219 // void SetIncrement(T inc)
220 virtual void SetSnapToTicks(bool snap_to_ticks)
221 { m_snap_to_ticks = snap_to_ticks; }
222 // void SetDigits(unsigned digits) - wxSpinCtrlDouble only
223
224 // Select text in the textctrl
225 //void SetSelection(long from, long to);
226
227 protected:
228 // generic double valued
229 double DoGetValue() const
230 {
231 double n;
232 if ( (wxSscanf(wxTextCtrl::GetValue(), wxT("%lf"), &n) != 1) )
233 n = INT_MIN;
234
235 return n;
236 }
237
238 bool DoSetValue(double val)
239 {
240 wxTextCtrl::SetValue(wxString::Format(m_format.c_str(), val));
241 return true;
242 }
243 void DoSetRange(double min_val, double max_val)
244 {
245 m_min = min_val;
246 m_max = max_val;
247 }
248 void DoSetIncrement(double inc) { m_increment = inc; } // Note: unused
249
250 double m_value;
251 double m_min;
252 double m_max;
253 double m_increment;
254 bool m_snap_to_ticks;
255 wxString m_format;
256 };
257
258 #endif // wxUSE_SPINBTN/!wxUSE_SPINBTN
259
260 #if !defined(wxHAS_NATIVE_SPINCTRL)
261
262 //-----------------------------------------------------------------------------
263 // wxSpinCtrl
264 //-----------------------------------------------------------------------------
265
266 class WXDLLIMPEXP_CORE wxSpinCtrl : public wxSpinCtrlGenericBase
267 {
268 public:
269 wxSpinCtrl() { Init(); }
270 wxSpinCtrl(wxWindow *parent,
271 wxWindowID id = wxID_ANY,
272 const wxString& value = wxEmptyString,
273 const wxPoint& pos = wxDefaultPosition,
274 const wxSize& size = wxDefaultSize,
275 long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
276 int min = 0, int max = 100, int initial = 0,
277 const wxString& name = wxT("wxSpinCtrl"))
278 {
279 Init();
280
281 Create(parent, id, value, pos, size, style, min, max, initial, name);
282 }
283
284 bool Create(wxWindow *parent,
285 wxWindowID id = wxID_ANY,
286 const wxString& value = wxEmptyString,
287 const wxPoint& pos = wxDefaultPosition,
288 const wxSize& size = wxDefaultSize,
289 long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
290 int min = 0, int max = 100, int initial = 0,
291 const wxString& name = wxT("wxSpinCtrl"))
292 {
293 return wxSpinCtrlGenericBase::Create(parent, id, value, pos, size,
294 style, min, max, initial, 1, name);
295 }
296
297 // accessors
298 int GetValue(wxSPINCTRL_GETVALUE_FIX) const { return int(DoGetValue()); }
299 int GetMin() const { return int(m_min); }
300 int GetMax() const { return int(m_max); }
301 int GetIncrement() const { return int(m_increment); }
302
303 // operations
304 void SetValue(const wxString& value)
305 { wxSpinCtrlGenericBase::SetValue(value); }
306 void SetValue( int value ) { DoSetValue(value); }
307 void SetRange( int minVal, int maxVal ) { DoSetRange(minVal, maxVal); }
308 void SetIncrement(int inc) { DoSetIncrement(inc); }
309
310 virtual int GetBase() const { return m_base; }
311 virtual bool SetBase(int base);
312
313 protected:
314 virtual void DoSendEvent();
315
316 virtual bool DoTextToValue(const wxString& text, double *val);
317 virtual wxString DoValueToText(double val);
318
319 private:
320 // Common part of all ctors.
321 void Init()
322 {
323 m_base = 10;
324 }
325
326 int m_base;
327
328 DECLARE_DYNAMIC_CLASS(wxSpinCtrl)
329 };
330
331 #endif // wxHAS_NATIVE_SPINCTRL
332
333 //-----------------------------------------------------------------------------
334 // wxSpinCtrlDouble
335 //-----------------------------------------------------------------------------
336
337 class WXDLLIMPEXP_CORE wxSpinCtrlDouble : public wxSpinCtrlGenericBase
338 {
339 public:
340 wxSpinCtrlDouble() { Init(); }
341 wxSpinCtrlDouble(wxWindow *parent,
342 wxWindowID id = wxID_ANY,
343 const wxString& value = wxEmptyString,
344 const wxPoint& pos = wxDefaultPosition,
345 const wxSize& size = wxDefaultSize,
346 long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
347 double min = 0, double max = 100, double initial = 0,
348 double inc = 1,
349 const wxString& name = wxT("wxSpinCtrlDouble"))
350 {
351 Init();
352
353 Create(parent, id, value, pos, size, style,
354 min, max, initial, inc, name);
355 }
356
357 bool Create(wxWindow *parent,
358 wxWindowID id = wxID_ANY,
359 const wxString& value = wxEmptyString,
360 const wxPoint& pos = wxDefaultPosition,
361 const wxSize& size = wxDefaultSize,
362 long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
363 double min = 0, double max = 100, double initial = 0,
364 double inc = 1,
365 const wxString& name = wxT("wxSpinCtrlDouble"))
366 {
367 return wxSpinCtrlGenericBase::Create(parent, id, value, pos, size,
368 style, min, max, initial,
369 inc, name);
370 }
371
372 // accessors
373 double GetValue(wxSPINCTRL_GETVALUE_FIX) const { return DoGetValue(); }
374 double GetMin() const { return m_min; }
375 double GetMax() const { return m_max; }
376 double GetIncrement() const { return m_increment; }
377 unsigned GetDigits() const { return m_digits; }
378
379 // operations
380 void SetValue(const wxString& value)
381 { wxSpinCtrlGenericBase::SetValue(value); }
382 void SetValue(double value) { DoSetValue(value); }
383 void SetRange(double minVal, double maxVal) { DoSetRange(minVal, maxVal); }
384 void SetIncrement(double inc) { DoSetIncrement(inc); }
385 void SetDigits(unsigned digits);
386
387 // We don't implement bases support for floating point numbers, this is not
388 // very useful in practice.
389 virtual int GetBase() const { return 10; }
390 virtual bool SetBase(int WXUNUSED(base)) { return 0; }
391
392 protected:
393 virtual void DoSendEvent();
394
395 virtual bool DoTextToValue(const wxString& text, double *val);
396 virtual wxString DoValueToText(double val);
397
398 unsigned m_digits;
399
400 private:
401 // Common part of all ctors.
402 void Init()
403 {
404 m_digits = 0;
405 m_format = wxS("%g");
406 }
407
408 wxString m_format;
409
410 DECLARE_DYNAMIC_CLASS(wxSpinCtrlDouble)
411 };
412
413 #endif // _WX_GENERIC_SPINCTRL_H_