wxMessageBox off the main thread lost result code.
[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 enum SendEvent
121 {
122 SendEvent_None,
123 SendEvent_Text
124 };
125
126 // generic double valued functions
127 double DoGetValue() const { return m_value; }
128 bool DoSetValue(double val, SendEvent sendEvent);
129 void DoSetRange(double min_val, double max_val);
130 void DoSetIncrement(double inc);
131
132 // update our value to reflect the text control contents (if it has been
133 // modified by user, do nothing otherwise)
134 //
135 // can also change the text control if its value is invalid
136 //
137 // return true if our value has changed
138 bool SyncSpinToText(SendEvent sendEvent);
139
140 // Send the correct event type
141 virtual void DoSendEvent() = 0;
142
143 // Convert the text to/from the corresponding value.
144 virtual bool DoTextToValue(const wxString& text, double *val) = 0;
145 virtual wxString DoValueToText(double val) = 0;
146
147 // check if the value is in range
148 bool InRange(double n) const { return (n >= m_min) && (n <= m_max); }
149
150 // ensure that the value is in range wrapping it round if necessary
151 double AdjustToFitInRange(double value) const;
152
153
154 double m_value;
155 double m_min;
156 double m_max;
157 double m_increment;
158 bool m_snap_to_ticks;
159
160 int m_spin_value;
161
162 // the subcontrols
163 wxTextCtrl *m_textCtrl;
164 wxSpinButton *m_spinButton;
165
166 private:
167 // common part of all ctors
168 void Init();
169
170 // Implement pure virtual function inherited from wxCompositeWindow.
171 virtual wxWindowList GetCompositeWindowParts() const;
172
173 DECLARE_EVENT_TABLE()
174 };
175
176 #else // !wxUSE_SPINBTN
177
178 #define wxSPINCTRL_GETVALUE_FIX int = 1
179
180 // ----------------------------------------------------------------------------
181 // wxSpinCtrl is just a text control
182 // ----------------------------------------------------------------------------
183
184 #include "wx/textctrl.h"
185
186 class WXDLLIMPEXP_CORE wxSpinCtrlGenericBase : public wxTextCtrl
187 {
188 public:
189 wxSpinCtrlGenericBase() : m_value(0), m_min(0), m_max(100),
190 m_increment(1), m_snap_to_ticks(false),
191 m_format(wxT("%g")) { }
192
193 bool Create(wxWindow *parent,
194 wxWindowID id = wxID_ANY,
195 const wxString& value = wxEmptyString,
196 const wxPoint& pos = wxDefaultPosition,
197 const wxSize& size = wxDefaultSize,
198 long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
199 double min = 0, double max = 100, double initial = 0,
200 double inc = 1,
201 const wxString& name = wxT("wxSpinCtrl"))
202 {
203 m_min = min;
204 m_max = max;
205 m_value = initial;
206 m_increment = inc;
207
208 bool ok = wxTextCtrl::Create(parent, id, value, pos, size, style,
209 wxDefaultValidator, name);
210 DoSetValue(initial, SendEvent_None);
211
212 return ok;
213 }
214
215 // accessors
216 // T GetValue() const
217 // T GetMin() const
218 // T GetMax() const
219 // T GetIncrement() const
220 virtual bool GetSnapToTicks() const { return m_snap_to_ticks; }
221 // unsigned GetDigits() const - wxSpinCtrlDouble only
222
223 // operations
224 virtual void SetValue(const wxString& text) { wxTextCtrl::SetValue(text); }
225 // void SetValue(T val)
226 // void SetRange(T minVal, T maxVal)
227 // void SetIncrement(T inc)
228 virtual void SetSnapToTicks(bool snap_to_ticks)
229 { m_snap_to_ticks = snap_to_ticks; }
230 // void SetDigits(unsigned digits) - wxSpinCtrlDouble only
231
232 // Select text in the textctrl
233 //void SetSelection(long from, long to);
234
235 protected:
236 // generic double valued
237 double DoGetValue() const
238 {
239 double n;
240 if ( (wxSscanf(wxTextCtrl::GetValue(), wxT("%lf"), &n) != 1) )
241 n = INT_MIN;
242
243 return n;
244 }
245
246 bool DoSetValue(double val, SendEvent sendEvent)
247 {
248 wxString str(wxString::Format(m_format, val));
249 switch ( sendEvent )
250 {
251 case SendEvent_None:
252 wxTextCtrl::ChangeValue(str);
253 break;
254
255 case SendEvent_Text:
256 wxTextCtrl::SetValue(str);
257 break;
258 }
259
260 return true;
261 }
262 void DoSetRange(double min_val, double max_val)
263 {
264 m_min = min_val;
265 m_max = max_val;
266 }
267 void DoSetIncrement(double inc) { m_increment = inc; } // Note: unused
268
269 double m_value;
270 double m_min;
271 double m_max;
272 double m_increment;
273 bool m_snap_to_ticks;
274 wxString m_format;
275 };
276
277 #endif // wxUSE_SPINBTN/!wxUSE_SPINBTN
278
279 #if !defined(wxHAS_NATIVE_SPINCTRL)
280
281 //-----------------------------------------------------------------------------
282 // wxSpinCtrl
283 //-----------------------------------------------------------------------------
284
285 class WXDLLIMPEXP_CORE wxSpinCtrl : public wxSpinCtrlGenericBase
286 {
287 public:
288 wxSpinCtrl() { Init(); }
289 wxSpinCtrl(wxWindow *parent,
290 wxWindowID id = wxID_ANY,
291 const wxString& value = wxEmptyString,
292 const wxPoint& pos = wxDefaultPosition,
293 const wxSize& size = wxDefaultSize,
294 long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
295 int min = 0, int max = 100, int initial = 0,
296 const wxString& name = wxT("wxSpinCtrl"))
297 {
298 Init();
299
300 Create(parent, id, value, pos, size, style, min, max, initial, name);
301 }
302
303 bool Create(wxWindow *parent,
304 wxWindowID id = wxID_ANY,
305 const wxString& value = wxEmptyString,
306 const wxPoint& pos = wxDefaultPosition,
307 const wxSize& size = wxDefaultSize,
308 long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
309 int min = 0, int max = 100, int initial = 0,
310 const wxString& name = wxT("wxSpinCtrl"))
311 {
312 return wxSpinCtrlGenericBase::Create(parent, id, value, pos, size,
313 style, min, max, initial, 1, name);
314 }
315
316 // accessors
317 int GetValue(wxSPINCTRL_GETVALUE_FIX) const { return int(DoGetValue()); }
318 int GetMin() const { return int(m_min); }
319 int GetMax() const { return int(m_max); }
320 int GetIncrement() const { return int(m_increment); }
321
322 // operations
323 void SetValue(const wxString& value)
324 { wxSpinCtrlGenericBase::SetValue(value); }
325 void SetValue( int value ) { DoSetValue(value, SendEvent_None); }
326 void SetRange( int minVal, int maxVal ) { DoSetRange(minVal, maxVal); }
327 void SetIncrement(int inc) { DoSetIncrement(inc); }
328
329 virtual int GetBase() const { return m_base; }
330 virtual bool SetBase(int base);
331
332 protected:
333 virtual void DoSendEvent();
334
335 virtual bool DoTextToValue(const wxString& text, double *val);
336 virtual wxString DoValueToText(double val);
337
338 private:
339 // Common part of all ctors.
340 void Init()
341 {
342 m_base = 10;
343 }
344
345 int m_base;
346
347 DECLARE_DYNAMIC_CLASS(wxSpinCtrl)
348 };
349
350 #endif // wxHAS_NATIVE_SPINCTRL
351
352 //-----------------------------------------------------------------------------
353 // wxSpinCtrlDouble
354 //-----------------------------------------------------------------------------
355
356 class WXDLLIMPEXP_CORE wxSpinCtrlDouble : public wxSpinCtrlGenericBase
357 {
358 public:
359 wxSpinCtrlDouble() { Init(); }
360 wxSpinCtrlDouble(wxWindow *parent,
361 wxWindowID id = wxID_ANY,
362 const wxString& value = wxEmptyString,
363 const wxPoint& pos = wxDefaultPosition,
364 const wxSize& size = wxDefaultSize,
365 long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
366 double min = 0, double max = 100, double initial = 0,
367 double inc = 1,
368 const wxString& name = wxT("wxSpinCtrlDouble"))
369 {
370 Init();
371
372 Create(parent, id, value, pos, size, style,
373 min, max, initial, inc, name);
374 }
375
376 bool Create(wxWindow *parent,
377 wxWindowID id = wxID_ANY,
378 const wxString& value = wxEmptyString,
379 const wxPoint& pos = wxDefaultPosition,
380 const wxSize& size = wxDefaultSize,
381 long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
382 double min = 0, double max = 100, double initial = 0,
383 double inc = 1,
384 const wxString& name = wxT("wxSpinCtrlDouble"))
385 {
386 return wxSpinCtrlGenericBase::Create(parent, id, value, pos, size,
387 style, min, max, initial,
388 inc, name);
389 }
390
391 // accessors
392 double GetValue(wxSPINCTRL_GETVALUE_FIX) const { return DoGetValue(); }
393 double GetMin() const { return m_min; }
394 double GetMax() const { return m_max; }
395 double GetIncrement() const { return m_increment; }
396 unsigned GetDigits() const { return m_digits; }
397
398 // operations
399 void SetValue(const wxString& value)
400 { wxSpinCtrlGenericBase::SetValue(value); }
401 void SetValue(double value) { DoSetValue(value, SendEvent_None); }
402 void SetRange(double minVal, double maxVal) { DoSetRange(minVal, maxVal); }
403 void SetIncrement(double inc) { DoSetIncrement(inc); }
404 void SetDigits(unsigned digits);
405
406 // We don't implement bases support for floating point numbers, this is not
407 // very useful in practice.
408 virtual int GetBase() const { return 10; }
409 virtual bool SetBase(int WXUNUSED(base)) { return 0; }
410
411 protected:
412 virtual void DoSendEvent();
413
414 virtual bool DoTextToValue(const wxString& text, double *val);
415 virtual wxString DoValueToText(double val);
416
417 unsigned m_digits;
418
419 private:
420 // Common part of all ctors.
421 void Init()
422 {
423 m_digits = 0;
424 m_format = wxS("%g");
425 }
426
427 wxString m_format;
428
429 DECLARE_DYNAMIC_CLASS(wxSpinCtrlDouble)
430 };
431
432 #endif // _WX_GENERIC_SPINCTRL_H_