]> git.saurik.com Git - wxWidgets.git/blob - include/wx/generic/spinctlg.h
addind nonowned window implementation
[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 WXDLLEXPORT 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(wxWindow *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 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;
123
124 // the subcontrols
125 wxTextCtrl *m_textCtrl;
126 wxSpinButton *m_spinButton;
127
128 private:
129 // common part of all ctors
130 void Init();
131 };
132
133 #else // !wxUSE_SPINBTN
134
135 #define wxSPINCTRL_GETVALUE_FIX int = 1
136
137 // ----------------------------------------------------------------------------
138 // wxSpinCtrl is just a text control
139 // ----------------------------------------------------------------------------
140
141 #include "wx/textctrl.h"
142
143 class WXDLLEXPORT wxSpinCtrlGenericBase : public wxTextCtrl
144 {
145 public:
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
186
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() {}
225 wxSpinCtrl(wxWindow *parent,
226 wxWindowID id = wxID_ANY,
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,
238 wxWindowID id = wxID_ANY,
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 {
246 return wxSpinCtrlGenericBase::Create(parent, id, value, pos, size, style, min, max, initial, 1, name);
247 }
248
249 // accessors
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"))
286 {
287 m_digits = 0;
288 Create(parent, id, value, pos, size, style, min, max, initial, inc, name);
289 }
290
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);
301 }
302
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; }
309
310 // operations
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);
316
317 protected:
318 virtual void DoSendEvent();
319
320 unsigned m_digits;
321
322 private:
323 DECLARE_DYNAMIC_CLASS(wxSpinCtrlDouble)
324 };
325
326 #endif // _WX_GENERIC_SPINCTRL_H_