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