]> git.saurik.com Git - wxWidgets.git/blob - include/wx/generic/spinctlg.h
Use correct reload flags on all platforms to avoid compilation errors
[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 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 virtual bool Reparent(wxWindowBase *newParent);
85 #if wxUSE_TOOLTIPS
86 virtual void DoSetToolTip(wxToolTip *tip);
87 #endif // wxUSE_TOOLTIPS
88
89 // get the subcontrols
90 wxTextCtrl *GetText() const { return m_textCtrl; }
91 wxSpinButton *GetSpinButton() const { return m_spinButton; }
92
93 // forwarded events from children windows
94 void OnSpinButton(wxSpinEvent& event);
95 void OnTextLostFocus();
96 void OnTextChar(wxKeyEvent& event);
97
98 // this window itself is used only as a container for its sub windows so it
99 // shouldn't accept the focus at all and any attempts to explicitly set
100 // focus to it should give focus to its text constol part
101 virtual bool AcceptsFocus() const { return false; }
102 virtual void SetFocus();
103
104 friend class wxSpinCtrlTextGeneric;
105
106 protected:
107 // override the base class virtuals involved into geometry calculations
108 virtual wxSize DoGetBestSize() const;
109 virtual void DoMoveWindow(int x, int y, int width, int height);
110
111 // generic double valued functions
112 double DoGetValue() const { return m_value; }
113 bool DoSetValue(double val);
114 void DoSetRange(double min_val, double max_val);
115 void DoSetIncrement(double inc);
116
117 // update our value to reflect the text control contents (if it has been
118 // modified by user, do nothing otherwise)
119 //
120 // can also change the text control if its value is invalid
121 //
122 // return true if our value has changed
123 bool SyncSpinToText();
124
125 // Send the correct event type
126 virtual void DoSendEvent() = 0;
127
128 // check if the value is in range
129 bool InRange(double n) const { return (n >= m_min) && (n <= m_max); }
130
131 // ensure that the value is in range wrapping it round if necessary
132 double AdjustToFitInRange(double value) const;
133
134
135 double m_value;
136 double m_min;
137 double m_max;
138 double m_increment;
139 bool m_snap_to_ticks;
140 wxString m_format;
141
142 int m_spin_value;
143
144 // the subcontrols
145 wxTextCtrl *m_textCtrl;
146 wxSpinButton *m_spinButton;
147
148 private:
149 // common part of all ctors
150 void Init();
151 };
152
153 #else // !wxUSE_SPINBTN
154
155 #define wxSPINCTRL_GETVALUE_FIX int = 1
156
157 // ----------------------------------------------------------------------------
158 // wxSpinCtrl is just a text control
159 // ----------------------------------------------------------------------------
160
161 #include "wx/textctrl.h"
162
163 class WXDLLIMPEXP_CORE wxSpinCtrlGenericBase : public wxTextCtrl
164 {
165 public:
166 wxSpinCtrlGenericBase() : m_value(0), m_min(0), m_max(100),
167 m_increment(1), m_snap_to_ticks(false),
168 m_format(wxT("%g")) { }
169
170 bool Create(wxWindow *parent,
171 wxWindowID id = wxID_ANY,
172 const wxString& value = wxEmptyString,
173 const wxPoint& pos = wxDefaultPosition,
174 const wxSize& size = wxDefaultSize,
175 long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
176 double min = 0, double max = 100, double initial = 0,
177 double inc = 1,
178 const wxString& name = wxT("wxSpinCtrl"))
179 {
180 m_min = min;
181 m_max = max;
182 m_value = initial;
183 m_increment = inc;
184
185 bool ok = wxTextCtrl::Create(parent, id, value, pos, size, style,
186 wxDefaultValidator, name);
187 DoSetValue(initial);
188
189 return ok;
190 }
191
192 // accessors
193 // T GetValue() const
194 // T GetMin() const
195 // T GetMax() const
196 // T GetIncrement() const
197 virtual bool GetSnapToTicks() const { return m_snap_to_ticks; }
198 // unsigned GetDigits() const - wxSpinCtrlDouble only
199
200 // operations
201 virtual void SetValue(const wxString& text) { wxTextCtrl::SetValue(text); }
202 // void SetValue(T val)
203 // void SetRange(T minVal, T maxVal)
204 // void SetIncrement(T inc)
205 virtual void SetSnapToTicks(bool snap_to_ticks)
206 { m_snap_to_ticks = snap_to_ticks; }
207 // void SetDigits(unsigned digits) - wxSpinCtrlDouble only
208
209 // Select text in the textctrl
210 //void SetSelection(long from, long to);
211
212 protected:
213 // generic double valued
214 double DoGetValue() const
215 {
216 double n;
217 if ( (wxSscanf(wxTextCtrl::GetValue(), wxT("%lf"), &n) != 1) )
218 n = INT_MIN;
219
220 return n;
221 }
222
223 bool DoSetValue(double val)
224 {
225 wxTextCtrl::SetValue(wxString::Format(m_format.c_str(), val));
226 return true;
227 }
228 void DoSetRange(double min_val, double max_val)
229 {
230 m_min = min_val;
231 m_max = max_val;
232 }
233 void DoSetIncrement(double inc) { m_increment = inc; } // Note: unused
234
235 double m_value;
236 double m_min;
237 double m_max;
238 double m_increment;
239 bool m_snap_to_ticks;
240 wxString m_format;
241 };
242
243 #endif // wxUSE_SPINBTN/!wxUSE_SPINBTN
244
245 #if !defined(wxHAS_NATIVE_SPINCTRL)
246
247 //-----------------------------------------------------------------------------
248 // wxSpinCtrl
249 //-----------------------------------------------------------------------------
250
251 class WXDLLIMPEXP_CORE wxSpinCtrl : public wxSpinCtrlGenericBase
252 {
253 public:
254 wxSpinCtrl() {}
255 wxSpinCtrl(wxWindow *parent,
256 wxWindowID id = wxID_ANY,
257 const wxString& value = wxEmptyString,
258 const wxPoint& pos = wxDefaultPosition,
259 const wxSize& size = wxDefaultSize,
260 long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
261 int min = 0, int max = 100, int initial = 0,
262 const wxString& name = wxT("wxSpinCtrl"))
263 {
264 Create(parent, id, value, pos, size, style, min, max, initial, name);
265 }
266
267 bool Create(wxWindow *parent,
268 wxWindowID id = wxID_ANY,
269 const wxString& value = wxEmptyString,
270 const wxPoint& pos = wxDefaultPosition,
271 const wxSize& size = wxDefaultSize,
272 long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
273 int min = 0, int max = 100, int initial = 0,
274 const wxString& name = wxT("wxSpinCtrl"))
275 {
276 return wxSpinCtrlGenericBase::Create(parent, id, value, pos, size,
277 style, min, max, initial, 1, name);
278 }
279
280 // accessors
281 int GetValue(wxSPINCTRL_GETVALUE_FIX) const { return int(DoGetValue()); }
282 int GetMin() const { return int(m_min); }
283 int GetMax() const { return int(m_max); }
284 int GetIncrement() const { return int(m_increment); }
285
286 // operations
287 void SetValue(const wxString& value)
288 { wxSpinCtrlGenericBase::SetValue(value); }
289 void SetValue( int value ) { DoSetValue(value); }
290 void SetRange( int minVal, int maxVal ) { DoSetRange(minVal, maxVal); }
291 void SetIncrement(int inc) { DoSetIncrement(inc); }
292
293 protected:
294 virtual void DoSendEvent();
295
296 DECLARE_DYNAMIC_CLASS(wxSpinCtrl)
297 };
298
299 #endif // wxHAS_NATIVE_SPINCTRL
300
301 //-----------------------------------------------------------------------------
302 // wxSpinCtrlDouble
303 //-----------------------------------------------------------------------------
304
305 class WXDLLIMPEXP_CORE wxSpinCtrlDouble : public wxSpinCtrlGenericBase
306 {
307 public:
308 wxSpinCtrlDouble() : m_digits(0) { }
309 wxSpinCtrlDouble(wxWindow *parent,
310 wxWindowID id = wxID_ANY,
311 const wxString& value = wxEmptyString,
312 const wxPoint& pos = wxDefaultPosition,
313 const wxSize& size = wxDefaultSize,
314 long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
315 double min = 0, double max = 100, double initial = 0,
316 double inc = 1,
317 const wxString& name = wxT("wxSpinCtrlDouble"))
318 {
319 m_digits = 0;
320 Create(parent, id, value, pos, size, style,
321 min, max, initial, inc, name);
322 }
323
324 bool Create(wxWindow *parent,
325 wxWindowID id = wxID_ANY,
326 const wxString& value = wxEmptyString,
327 const wxPoint& pos = wxDefaultPosition,
328 const wxSize& size = wxDefaultSize,
329 long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
330 double min = 0, double max = 100, double initial = 0,
331 double inc = 1,
332 const wxString& name = wxT("wxSpinCtrlDouble"))
333 {
334 return wxSpinCtrlGenericBase::Create(parent, id, value, pos, size,
335 style, min, max, initial,
336 inc, name);
337 }
338
339 // accessors
340 double GetValue(wxSPINCTRL_GETVALUE_FIX) const { return DoGetValue(); }
341 double GetMin() const { return m_min; }
342 double GetMax() const { return m_max; }
343 double GetIncrement() const { return m_increment; }
344 unsigned GetDigits() const { return m_digits; }
345
346 // operations
347 void SetValue(const wxString& value)
348 { wxSpinCtrlGenericBase::SetValue(value); }
349 void SetValue(double value) { DoSetValue(value); }
350 void SetRange(double minVal, double maxVal) { DoSetRange(minVal, maxVal); }
351 void SetIncrement(double inc) { DoSetIncrement(inc); }
352 void SetDigits(unsigned digits);
353
354 protected:
355 virtual void DoSendEvent();
356
357 unsigned m_digits;
358
359 DECLARE_DYNAMIC_CLASS(wxSpinCtrlDouble)
360 };
361
362 #endif // _WX_GENERIC_SPINCTRL_H_