1
/////////////////////////////////////////////////////////////////////////////
2
// Name: wx/generic/spinctlg.h
3
// Purpose: generic wxSpinCtrl class
4
// Author: Vadim Zeitlin
7
// Copyright: (c) Vadim Zeitlin
8
// Licence: wxWindows licence
9
/////////////////////////////////////////////////////////////////////////////
11
#ifndef _WX_GENERIC_SPINCTRL_H_
12
#define _WX_GENERIC_SPINCTRL_H_
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
// ----------------------------------------------------------------------------
24
#include "wx/compositewin.h"
26
class WXDLLIMPEXP_FWD_CORE wxSpinButton;
27
class WXDLLIMPEXP_FWD_CORE wxTextCtrl;
29
class wxSpinCtrlTextGeneric; // wxTextCtrl used for the wxSpinCtrlGenericBase
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
35
// ----------------------------------------------------------------------------
36
// wxSpinCtrlGeneric is a combination of wxTextCtrl and wxSpinButton
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
// ----------------------------------------------------------------------------
44
class WXDLLIMPEXP_CORE wxSpinCtrlGenericBase
45
: public wxNavigationEnabled<wxCompositeWindow<wxSpinCtrlBase> >
48
wxSpinCtrlGenericBase() { Init(); }
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,
58
const wxString& name = wxT("wxSpinCtrl"));
60
virtual ~wxSpinCtrlGenericBase();
66
// T GetIncrement() const
67
virtual bool GetSnapToTicks() const { return m_snap_to_ticks; }
68
// unsigned GetDigits() const - wxSpinCtrlDouble only
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
78
// Select text in the textctrl
79
void SetSelection(long from, long to);
81
// implementation from now on
83
// forward these functions to all subcontrols
84
virtual bool Enable(bool enable = true);
85
virtual bool Show(bool show = true);
87
virtual void DoSetToolTip(wxToolTip *tip);
88
#endif // wxUSE_TOOLTIPS
90
virtual bool SetBackgroundColour(const wxColour& colour);
92
// get the subcontrols
93
wxTextCtrl *GetText() const { return m_textCtrl; }
94
wxSpinButton *GetSpinButton() const { return m_spinButton; }
96
// forwarded events from children windows
97
void OnSpinButton(wxSpinEvent& event);
98
void OnTextLostFocus(wxFocusEvent& event);
99
void OnTextChar(wxKeyEvent& event);
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();
107
friend class wxSpinCtrlTextGeneric;
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);
116
// and, for MSW, enabling this window itself
117
virtual void DoEnable(bool enable);
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);
132
// update our value to reflect the text control contents (if it has been
133
// modified by user, do nothing otherwise)
135
// can also change the text control if its value is invalid
137
// return true if our value has changed
138
bool SyncSpinToText(SendEvent sendEvent);
140
// Send the correct event type
141
virtual void DoSendEvent() = 0;
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;
147
// check if the value is in range
148
bool InRange(double n) const { return (n >= m_min) && (n <= m_max); }
150
// ensure that the value is in range wrapping it round if necessary
151
double AdjustToFitInRange(double value) const;
158
bool m_snap_to_ticks;
163
wxTextCtrl *m_textCtrl;
164
wxSpinButton *m_spinButton;
167
// common part of all ctors
170
// Implement pure virtual function inherited from wxCompositeWindow.
171
virtual wxWindowList GetCompositeWindowParts() const;
173
DECLARE_EVENT_TABLE()
176
#else // !wxUSE_SPINBTN
178
#define wxSPINCTRL_GETVALUE_FIX int = 1
180
// ----------------------------------------------------------------------------
181
// wxSpinCtrl is just a text control
182
// ----------------------------------------------------------------------------
184
#include "wx/textctrl.h"
186
class WXDLLIMPEXP_CORE wxSpinCtrlGenericBase : public wxTextCtrl
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")) { }
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,
201
const wxString& name = wxT("wxSpinCtrl"))
208
bool ok = wxTextCtrl::Create(parent, id, value, pos, size, style,
209
wxDefaultValidator, name);
210
DoSetValue(initial, SendEvent_None);
216
// T GetValue() const
219
// T GetIncrement() const
220
virtual bool GetSnapToTicks() const { return m_snap_to_ticks; }
221
// unsigned GetDigits() const - wxSpinCtrlDouble only
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
232
// Select text in the textctrl
233
//void SetSelection(long from, long to);
236
// generic double valued
237
double DoGetValue() const
240
if ( (wxSscanf(wxTextCtrl::GetValue(), wxT("%lf"), &n) != 1) )
246
bool DoSetValue(double val, SendEvent sendEvent)
248
wxString str(wxString::Format(m_format, val));
252
wxTextCtrl::ChangeValue(str);
256
wxTextCtrl::SetValue(str);
262
void DoSetRange(double min_val, double max_val)
267
void DoSetIncrement(double inc) { m_increment = inc; } // Note: unused
273
bool m_snap_to_ticks;
277
#endif // wxUSE_SPINBTN/!wxUSE_SPINBTN
279
#if !defined(wxHAS_NATIVE_SPINCTRL)
281
//-----------------------------------------------------------------------------
283
//-----------------------------------------------------------------------------
285
class WXDLLIMPEXP_CORE wxSpinCtrl : public wxSpinCtrlGenericBase
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"))
300
Create(parent, id, value, pos, size, style, min, max, initial, name);
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"))
312
return wxSpinCtrlGenericBase::Create(parent, id, value, pos, size,
313
style, min, max, initial, 1, name);
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); }
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); }
329
virtual int GetBase() const { return m_base; }
330
virtual bool SetBase(int base);
333
virtual void DoSendEvent();
335
virtual bool DoTextToValue(const wxString& text, double *val);
336
virtual wxString DoValueToText(double val);
339
// Common part of all ctors.
347
DECLARE_DYNAMIC_CLASS(wxSpinCtrl)
350
#endif // wxHAS_NATIVE_SPINCTRL
352
//-----------------------------------------------------------------------------
354
//-----------------------------------------------------------------------------
356
class WXDLLIMPEXP_CORE wxSpinCtrlDouble : public wxSpinCtrlGenericBase
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,
368
const wxString& name = wxT("wxSpinCtrlDouble"))
372
Create(parent, id, value, pos, size, style,
373
min, max, initial, inc, name);
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,
384
const wxString& name = wxT("wxSpinCtrlDouble"))
386
return wxSpinCtrlGenericBase::Create(parent, id, value, pos, size,
387
style, min, max, initial,
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; }
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);
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; }
412
virtual void DoSendEvent();
414
virtual bool DoTextToValue(const wxString& text, double *val);
415
virtual wxString DoValueToText(double val);
420
// Common part of all ctors.
424
m_format = wxS("%g");
429
DECLARE_DYNAMIC_CLASS(wxSpinCtrlDouble)
432
#endif // _WX_GENERIC_SPINCTRL_H_