]> git.saurik.com Git - wxWidgets.git/blame - include/wx/generic/spinctlg.h
Just remove redundant wxDocument::SetDocumentTemplate() call.
[wxWidgets.git] / include / wx / generic / spinctlg.h
CommitLineData
669a6e11
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: wx/generic/spinctlg.h
3// Purpose: generic wxSpinCtrl class
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 28.10.99
669a6e11 7// Copyright: (c) Vadim Zeitlin
65571936 8// Licence: wxWindows licence
669a6e11
VZ
9/////////////////////////////////////////////////////////////////////////////
10
11#ifndef _WX_GENERIC_SPINCTRL_H_
12#define _WX_GENERIC_SPINCTRL_H_
13
1e6feb95
VZ
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
739555e3 22#if wxUSE_SPINBTN
1e6feb95 23
29604c85
VZ
24#include "wx/compositewin.h"
25
b5dbe15d
VS
26class WXDLLIMPEXP_FWD_CORE wxSpinButton;
27class WXDLLIMPEXP_FWD_CORE wxTextCtrl;
1e6feb95 28
405f0fef 29class wxSpinCtrlTextGeneric; // wxTextCtrl used for the wxSpinCtrlGenericBase
8cd6a9ad
VZ
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
1e6feb95 35// ----------------------------------------------------------------------------
8cd6a9ad
VZ
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.
1e6feb95
VZ
42// ----------------------------------------------------------------------------
43
29604c85 44class WXDLLIMPEXP_CORE wxSpinCtrlGenericBase
29cc4cc9 45 : public wxNavigationEnabled<wxCompositeWindow<wxSpinCtrlBase> >
1e6feb95
VZ
46{
47public:
8cd6a9ad 48 wxSpinCtrlGenericBase() { Init(); }
1e6feb95
VZ
49
50 bool Create(wxWindow *parent,
ca65c044 51 wxWindowID id = wxID_ANY,
1e6feb95
VZ
52 const wxString& value = wxEmptyString,
53 const wxPoint& pos = wxDefaultPosition,
54 const wxSize& size = wxDefaultSize,
f1ddb476 55 long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
c218ccee
VZ
56 double min = 0, double max = 100, double initial = 0,
57 double inc = 1,
9a83f860 58 const wxString& name = wxT("wxSpinCtrl"));
1e6feb95 59
8cd6a9ad
VZ
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
1e6feb95
VZ
69
70 // operations
8cd6a9ad
VZ
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
739555e3 79 void SetSelection(long from, long to);
1e6feb95 80
1e6feb95
VZ
81 // implementation from now on
82
83 // forward these functions to all subcontrols
ca65c044
WS
84 virtual bool Enable(bool enable = true);
85 virtual bool Show(bool show = true);
cb7ef329
VZ
86#if wxUSE_TOOLTIPS
87 virtual void DoSetToolTip(wxToolTip *tip);
88#endif // wxUSE_TOOLTIPS
1e6feb95 89
29cc4cc9
VZ
90 virtual bool SetBackgroundColour(const wxColour& colour);
91
1e6feb95 92 // get the subcontrols
8cd6a9ad
VZ
93 wxTextCtrl *GetText() const { return m_textCtrl; }
94 wxSpinButton *GetSpinButton() const { return m_spinButton; }
1e6feb95 95
8cd6a9ad
VZ
96 // forwarded events from children windows
97 void OnSpinButton(wxSpinEvent& event);
c8139480 98 void OnTextLostFocus(wxFocusEvent& event);
8cd6a9ad 99 void OnTextChar(wxKeyEvent& event);
1e6feb95 100
64b62afe 101 // this window itself is used only as a container for its sub windows so it
713c7336
VZ
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
64b62afe 104 virtual bool AcceptsFocus() const { return false; }
fda43a0e 105 virtual void SetFocus();
64b62afe 106
405f0fef 107 friend class wxSpinCtrlTextGeneric;
1e6feb95
VZ
108
109protected:
110 // override the base class virtuals involved into geometry calculations
dba00620 111 virtual wxSize DoGetBestSize() const;
40aa1a7e 112 virtual wxSize DoGetSizeFromTextSize(int xlen, int ylen = -1) const;
1e6feb95
VZ
113 virtual void DoMoveWindow(int x, int y, int width, int height);
114
a2fb9138
VZ
115#ifdef __WXMSW__
116 // and, for MSW, enabling this window itself
117 virtual void DoEnable(bool enable);
118#endif // __WXMSW__
119
b736d59e
VZ
120 enum SendEvent
121 {
122 SendEvent_None,
123 SendEvent_Text
124 };
125
8cd6a9ad
VZ
126 // generic double valued functions
127 double DoGetValue() const { return m_value; }
b736d59e 128 bool DoSetValue(double val, SendEvent sendEvent);
8cd6a9ad
VZ
129 void DoSetRange(double min_val, double max_val);
130 void DoSetIncrement(double inc);
131
3a712105
VZ
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
b736d59e 138 bool SyncSpinToText(SendEvent sendEvent);
8cd6a9ad
VZ
139
140 // Send the correct event type
141 virtual void DoSendEvent() = 0;
142
62f96636
VZ
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
70c14728 147 // check if the value is in range
8cd6a9ad
VZ
148 bool InRange(double n) const { return (n >= m_min) && (n <= m_max); }
149
70c14728
VZ
150 // ensure that the value is in range wrapping it round if necessary
151 double AdjustToFitInRange(double value) const;
152
153
8cd6a9ad
VZ
154 double m_value;
155 double m_min;
156 double m_max;
157 double m_increment;
158 bool m_snap_to_ticks;
8cd6a9ad
VZ
159
160 int m_spin_value;
1e6feb95 161
1e6feb95 162 // the subcontrols
8cd6a9ad
VZ
163 wxTextCtrl *m_textCtrl;
164 wxSpinButton *m_spinButton;
739555e3 165
6c0ba0d0 166private:
8cd6a9ad
VZ
167 // common part of all ctors
168 void Init();
c8139480 169
29604c85
VZ
170 // Implement pure virtual function inherited from wxCompositeWindow.
171 virtual wxWindowList GetCompositeWindowParts() const;
172
c8139480 173 DECLARE_EVENT_TABLE()
1e6feb95
VZ
174};
175
176#else // !wxUSE_SPINBTN
669a6e11 177
8cd6a9ad
VZ
178#define wxSPINCTRL_GETVALUE_FIX int = 1
179
669a6e11 180// ----------------------------------------------------------------------------
1e6feb95 181// wxSpinCtrl is just a text control
669a6e11
VZ
182// ----------------------------------------------------------------------------
183
1e6feb95
VZ
184#include "wx/textctrl.h"
185
53a2db12 186class WXDLLIMPEXP_CORE wxSpinCtrlGenericBase : public wxTextCtrl
669a6e11
VZ
187{
188public:
8cd6a9ad
VZ
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,
f1ddb476 198 long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
c218ccee
VZ
199 double min = 0, double max = 100, double initial = 0,
200 double inc = 1,
9a83f860 201 const wxString& name = wxT("wxSpinCtrl"))
8cd6a9ad
VZ
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);
b736d59e 210 DoSetValue(initial, SendEvent_None);
8cd6a9ad
VZ
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)
c218ccee
VZ
228 virtual void SetSnapToTicks(bool snap_to_ticks)
229 { m_snap_to_ticks = snap_to_ticks; }
8cd6a9ad 230 // void SetDigits(unsigned digits) - wxSpinCtrlDouble only
c71830c3 231
8cd6a9ad
VZ
232 // Select text in the textctrl
233 //void SetSelection(long from, long to);
234
235protected:
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
b736d59e 246 bool DoSetValue(double val, SendEvent sendEvent)
c218ccee 247 {
b736d59e
VZ
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
c218ccee
VZ
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 }
8cd6a9ad
VZ
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
285class WXDLLIMPEXP_CORE wxSpinCtrl : public wxSpinCtrlGenericBase
286{
287public:
9e565667 288 wxSpinCtrl() { Init(); }
c71830c3 289 wxSpinCtrl(wxWindow *parent,
ca65c044 290 wxWindowID id = wxID_ANY,
c71830c3
VZ
291 const wxString& value = wxEmptyString,
292 const wxPoint& pos = wxDefaultPosition,
293 const wxSize& size = wxDefaultSize,
f1ddb476 294 long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
c71830c3 295 int min = 0, int max = 100, int initial = 0,
9a83f860 296 const wxString& name = wxT("wxSpinCtrl"))
c71830c3 297 {
9e565667
VZ
298 Init();
299
c71830c3
VZ
300 Create(parent, id, value, pos, size, style, min, max, initial, name);
301 }
302
303 bool Create(wxWindow *parent,
ca65c044 304 wxWindowID id = wxID_ANY,
c71830c3
VZ
305 const wxString& value = wxEmptyString,
306 const wxPoint& pos = wxDefaultPosition,
307 const wxSize& size = wxDefaultSize,
f1ddb476 308 long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
c71830c3 309 int min = 0, int max = 100, int initial = 0,
9a83f860 310 const wxString& name = wxT("wxSpinCtrl"))
c71830c3 311 {
c218ccee
VZ
312 return wxSpinCtrlGenericBase::Create(parent, id, value, pos, size,
313 style, min, max, initial, 1, name);
c71830c3 314 }
669a6e11
VZ
315
316 // accessors
f0368d28
PC
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); }
8cd6a9ad
VZ
321
322 // operations
c218ccee
VZ
323 void SetValue(const wxString& value)
324 { wxSpinCtrlGenericBase::SetValue(value); }
b736d59e 325 void SetValue( int value ) { DoSetValue(value, SendEvent_None); }
8cd6a9ad 326 void SetRange( int minVal, int maxVal ) { DoSetRange(minVal, maxVal); }
92e164ff 327 void SetIncrement(int inc) { DoSetIncrement(inc); }
8cd6a9ad 328
9e565667
VZ
329 virtual int GetBase() const { return m_base; }
330 virtual bool SetBase(int base);
331
8cd6a9ad
VZ
332protected:
333 virtual void DoSendEvent();
334
62f96636
VZ
335 virtual bool DoTextToValue(const wxString& text, double *val);
336 virtual wxString DoValueToText(double val);
9e565667
VZ
337
338private:
339 // Common part of all ctors.
340 void Init()
341 {
342 m_base = 10;
343 }
344
345 int m_base;
346
8cd6a9ad
VZ
347 DECLARE_DYNAMIC_CLASS(wxSpinCtrl)
348};
349
350#endif // wxHAS_NATIVE_SPINCTRL
351
352//-----------------------------------------------------------------------------
353// wxSpinCtrlDouble
354//-----------------------------------------------------------------------------
355
356class WXDLLIMPEXP_CORE wxSpinCtrlDouble : public wxSpinCtrlGenericBase
357{
358public:
62f96636 359 wxSpinCtrlDouble() { Init(); }
8cd6a9ad
VZ
360 wxSpinCtrlDouble(wxWindow *parent,
361 wxWindowID id = wxID_ANY,
362 const wxString& value = wxEmptyString,
363 const wxPoint& pos = wxDefaultPosition,
364 const wxSize& size = wxDefaultSize,
f1ddb476 365 long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
c218ccee
VZ
366 double min = 0, double max = 100, double initial = 0,
367 double inc = 1,
9a83f860 368 const wxString& name = wxT("wxSpinCtrlDouble"))
669a6e11 369 {
62f96636
VZ
370 Init();
371
c218ccee
VZ
372 Create(parent, id, value, pos, size, style,
373 min, max, initial, inc, name);
8cd6a9ad 374 }
669a6e11 375
8cd6a9ad
VZ
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,
f1ddb476 381 long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
c218ccee
VZ
382 double min = 0, double max = 100, double initial = 0,
383 double inc = 1,
9a83f860 384 const wxString& name = wxT("wxSpinCtrlDouble"))
8cd6a9ad 385 {
c218ccee
VZ
386 return wxSpinCtrlGenericBase::Create(parent, id, value, pos, size,
387 style, min, max, initial,
388 inc, name);
669a6e11
VZ
389 }
390
8cd6a9ad
VZ
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; }
669a6e11
VZ
397
398 // operations
c218ccee
VZ
399 void SetValue(const wxString& value)
400 { wxSpinCtrlGenericBase::SetValue(value); }
b736d59e 401 void SetValue(double value) { DoSetValue(value, SendEvent_None); }
8cd6a9ad
VZ
402 void SetRange(double minVal, double maxVal) { DoSetRange(minVal, maxVal); }
403 void SetIncrement(double inc) { DoSetIncrement(inc); }
404 void SetDigits(unsigned digits);
669a6e11 405
9e565667
VZ
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
669a6e11 411protected:
8cd6a9ad 412 virtual void DoSendEvent();
669a6e11 413
62f96636
VZ
414 virtual bool DoTextToValue(const wxString& text, double *val);
415 virtual wxString DoValueToText(double val);
416
8cd6a9ad 417 unsigned m_digits;
5fde6fcc 418
62f96636
VZ
419private:
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
8cd6a9ad 429 DECLARE_DYNAMIC_CLASS(wxSpinCtrlDouble)
669a6e11
VZ
430};
431
432#endif // _WX_GENERIC_SPINCTRL_H_