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