]> git.saurik.com Git - wxWidgets.git/blame - include/wx/generic/spinctlg.h
Implement HasSelection for the OSX WebKit backend.
[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
7// RCS-ID: $Id$
8// Copyright: (c) Vadim Zeitlin
65571936 9// Licence: wxWindows licence
669a6e11
VZ
10/////////////////////////////////////////////////////////////////////////////
11
12#ifndef _WX_GENERIC_SPINCTRL_H_
13#define _WX_GENERIC_SPINCTRL_H_
14
1e6feb95
VZ
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
739555e3 23#if wxUSE_SPINBTN
1e6feb95 24
b5dbe15d
VS
25class WXDLLIMPEXP_FWD_CORE wxSpinButton;
26class WXDLLIMPEXP_FWD_CORE wxTextCtrl;
1e6feb95 27
405f0fef 28class wxSpinCtrlTextGeneric; // wxTextCtrl used for the wxSpinCtrlGenericBase
8cd6a9ad
VZ
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
1e6feb95 34// ----------------------------------------------------------------------------
8cd6a9ad
VZ
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.
1e6feb95
VZ
41// ----------------------------------------------------------------------------
42
53a2db12 43class WXDLLIMPEXP_CORE wxSpinCtrlGenericBase : public wxSpinCtrlBase
1e6feb95
VZ
44{
45public:
8cd6a9ad 46 wxSpinCtrlGenericBase() { Init(); }
1e6feb95
VZ
47
48 bool Create(wxWindow *parent,
ca65c044 49 wxWindowID id = wxID_ANY,
1e6feb95
VZ
50 const wxString& value = wxEmptyString,
51 const wxPoint& pos = wxDefaultPosition,
52 const wxSize& size = wxDefaultSize,
f1ddb476 53 long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
c218ccee
VZ
54 double min = 0, double max = 100, double initial = 0,
55 double inc = 1,
9a83f860 56 const wxString& name = wxT("wxSpinCtrl"));
1e6feb95 57
8cd6a9ad
VZ
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
1e6feb95
VZ
67
68 // operations
8cd6a9ad
VZ
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
739555e3 77 void SetSelection(long from, long to);
1e6feb95 78
1e6feb95
VZ
79 // implementation from now on
80
81 // forward these functions to all subcontrols
ca65c044
WS
82 virtual bool Enable(bool enable = true);
83 virtual bool Show(bool show = true);
7f0cbaaa 84 virtual bool Reparent(wxWindowBase *newParent);
cb7ef329
VZ
85#if wxUSE_TOOLTIPS
86 virtual void DoSetToolTip(wxToolTip *tip);
87#endif // wxUSE_TOOLTIPS
1e6feb95
VZ
88
89 // get the subcontrols
8cd6a9ad
VZ
90 wxTextCtrl *GetText() const { return m_textCtrl; }
91 wxSpinButton *GetSpinButton() const { return m_spinButton; }
1e6feb95 92
8cd6a9ad
VZ
93 // forwarded events from children windows
94 void OnSpinButton(wxSpinEvent& event);
c8139480 95 void OnTextLostFocus(wxFocusEvent& event);
8cd6a9ad 96 void OnTextChar(wxKeyEvent& event);
1e6feb95 97
64b62afe 98 // this window itself is used only as a container for its sub windows so it
713c7336
VZ
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
64b62afe 101 virtual bool AcceptsFocus() const { return false; }
fda43a0e 102 virtual void SetFocus();
64b62afe 103
405f0fef 104 friend class wxSpinCtrlTextGeneric;
1e6feb95
VZ
105
106protected:
107 // override the base class virtuals involved into geometry calculations
dba00620 108 virtual wxSize DoGetBestSize() const;
1e6feb95
VZ
109 virtual void DoMoveWindow(int x, int y, int width, int height);
110
8cd6a9ad
VZ
111 // generic double valued functions
112 double DoGetValue() const { return m_value; }
113 bool DoSetValue(double val);
114 void DoSetRange(double min_val, double max_val);
115 void DoSetIncrement(double inc);
116
3a712105
VZ
117 // update our value to reflect the text control contents (if it has been
118 // modified by user, do nothing otherwise)
119 //
120 // can also change the text control if its value is invalid
121 //
122 // return true if our value has changed
123 bool SyncSpinToText();
8cd6a9ad
VZ
124
125 // Send the correct event type
126 virtual void DoSendEvent() = 0;
127
70c14728 128 // check if the value is in range
8cd6a9ad
VZ
129 bool InRange(double n) const { return (n >= m_min) && (n <= m_max); }
130
70c14728
VZ
131 // ensure that the value is in range wrapping it round if necessary
132 double AdjustToFitInRange(double value) const;
133
134
8cd6a9ad
VZ
135 double m_value;
136 double m_min;
137 double m_max;
138 double m_increment;
139 bool m_snap_to_ticks;
140 wxString m_format;
141
142 int m_spin_value;
1e6feb95 143
1e6feb95 144 // the subcontrols
8cd6a9ad
VZ
145 wxTextCtrl *m_textCtrl;
146 wxSpinButton *m_spinButton;
739555e3 147
6c0ba0d0 148private:
8cd6a9ad
VZ
149 // common part of all ctors
150 void Init();
c8139480
VS
151
152 DECLARE_EVENT_TABLE()
1e6feb95
VZ
153};
154
155#else // !wxUSE_SPINBTN
669a6e11 156
8cd6a9ad
VZ
157#define wxSPINCTRL_GETVALUE_FIX int = 1
158
669a6e11 159// ----------------------------------------------------------------------------
1e6feb95 160// wxSpinCtrl is just a text control
669a6e11
VZ
161// ----------------------------------------------------------------------------
162
1e6feb95
VZ
163#include "wx/textctrl.h"
164
53a2db12 165class WXDLLIMPEXP_CORE wxSpinCtrlGenericBase : public wxTextCtrl
669a6e11
VZ
166{
167public:
8cd6a9ad
VZ
168 wxSpinCtrlGenericBase() : m_value(0), m_min(0), m_max(100),
169 m_increment(1), m_snap_to_ticks(false),
170 m_format(wxT("%g")) { }
171
172 bool Create(wxWindow *parent,
173 wxWindowID id = wxID_ANY,
174 const wxString& value = wxEmptyString,
175 const wxPoint& pos = wxDefaultPosition,
176 const wxSize& size = wxDefaultSize,
f1ddb476 177 long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
c218ccee
VZ
178 double min = 0, double max = 100, double initial = 0,
179 double inc = 1,
9a83f860 180 const wxString& name = wxT("wxSpinCtrl"))
8cd6a9ad
VZ
181 {
182 m_min = min;
183 m_max = max;
184 m_value = initial;
185 m_increment = inc;
186
187 bool ok = wxTextCtrl::Create(parent, id, value, pos, size, style,
188 wxDefaultValidator, name);
189 DoSetValue(initial);
190
191 return ok;
192 }
193
194 // accessors
195 // T GetValue() const
196 // T GetMin() const
197 // T GetMax() const
198 // T GetIncrement() const
199 virtual bool GetSnapToTicks() const { return m_snap_to_ticks; }
200 // unsigned GetDigits() const - wxSpinCtrlDouble only
201
202 // operations
203 virtual void SetValue(const wxString& text) { wxTextCtrl::SetValue(text); }
204 // void SetValue(T val)
205 // void SetRange(T minVal, T maxVal)
206 // void SetIncrement(T inc)
c218ccee
VZ
207 virtual void SetSnapToTicks(bool snap_to_ticks)
208 { m_snap_to_ticks = snap_to_ticks; }
8cd6a9ad 209 // void SetDigits(unsigned digits) - wxSpinCtrlDouble only
c71830c3 210
8cd6a9ad
VZ
211 // Select text in the textctrl
212 //void SetSelection(long from, long to);
213
214protected:
215 // generic double valued
216 double DoGetValue() const
217 {
218 double n;
219 if ( (wxSscanf(wxTextCtrl::GetValue(), wxT("%lf"), &n) != 1) )
220 n = INT_MIN;
221
222 return n;
223 }
224
c218ccee
VZ
225 bool DoSetValue(double val)
226 {
227 wxTextCtrl::SetValue(wxString::Format(m_format.c_str(), val));
228 return true;
229 }
230 void DoSetRange(double min_val, double max_val)
231 {
232 m_min = min_val;
233 m_max = max_val;
234 }
8cd6a9ad
VZ
235 void DoSetIncrement(double inc) { m_increment = inc; } // Note: unused
236
237 double m_value;
238 double m_min;
239 double m_max;
240 double m_increment;
241 bool m_snap_to_ticks;
242 wxString m_format;
243};
244
245#endif // wxUSE_SPINBTN/!wxUSE_SPINBTN
246
247#if !defined(wxHAS_NATIVE_SPINCTRL)
248
249//-----------------------------------------------------------------------------
250// wxSpinCtrl
251//-----------------------------------------------------------------------------
252
253class WXDLLIMPEXP_CORE wxSpinCtrl : public wxSpinCtrlGenericBase
254{
255public:
256 wxSpinCtrl() {}
c71830c3 257 wxSpinCtrl(wxWindow *parent,
ca65c044 258 wxWindowID id = wxID_ANY,
c71830c3
VZ
259 const wxString& value = wxEmptyString,
260 const wxPoint& pos = wxDefaultPosition,
261 const wxSize& size = wxDefaultSize,
f1ddb476 262 long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
c71830c3 263 int min = 0, int max = 100, int initial = 0,
9a83f860 264 const wxString& name = wxT("wxSpinCtrl"))
c71830c3
VZ
265 {
266 Create(parent, id, value, pos, size, style, min, max, initial, name);
267 }
268
269 bool Create(wxWindow *parent,
ca65c044 270 wxWindowID id = wxID_ANY,
c71830c3
VZ
271 const wxString& value = wxEmptyString,
272 const wxPoint& pos = wxDefaultPosition,
273 const wxSize& size = wxDefaultSize,
f1ddb476 274 long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
c71830c3 275 int min = 0, int max = 100, int initial = 0,
9a83f860 276 const wxString& name = wxT("wxSpinCtrl"))
c71830c3 277 {
c218ccee
VZ
278 return wxSpinCtrlGenericBase::Create(parent, id, value, pos, size,
279 style, min, max, initial, 1, name);
c71830c3 280 }
669a6e11
VZ
281
282 // accessors
f0368d28
PC
283 int GetValue(wxSPINCTRL_GETVALUE_FIX) const { return int(DoGetValue()); }
284 int GetMin() const { return int(m_min); }
285 int GetMax() const { return int(m_max); }
286 int GetIncrement() const { return int(m_increment); }
8cd6a9ad
VZ
287
288 // operations
c218ccee
VZ
289 void SetValue(const wxString& value)
290 { wxSpinCtrlGenericBase::SetValue(value); }
8cd6a9ad
VZ
291 void SetValue( int value ) { DoSetValue(value); }
292 void SetRange( int minVal, int maxVal ) { DoSetRange(minVal, maxVal); }
92e164ff 293 void SetIncrement(int inc) { DoSetIncrement(inc); }
8cd6a9ad
VZ
294
295protected:
296 virtual void DoSendEvent();
297
8cd6a9ad
VZ
298 DECLARE_DYNAMIC_CLASS(wxSpinCtrl)
299};
300
301#endif // wxHAS_NATIVE_SPINCTRL
302
303//-----------------------------------------------------------------------------
304// wxSpinCtrlDouble
305//-----------------------------------------------------------------------------
306
307class WXDLLIMPEXP_CORE wxSpinCtrlDouble : public wxSpinCtrlGenericBase
308{
309public:
310 wxSpinCtrlDouble() : m_digits(0) { }
311 wxSpinCtrlDouble(wxWindow *parent,
312 wxWindowID id = wxID_ANY,
313 const wxString& value = wxEmptyString,
314 const wxPoint& pos = wxDefaultPosition,
315 const wxSize& size = wxDefaultSize,
f1ddb476 316 long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
c218ccee
VZ
317 double min = 0, double max = 100, double initial = 0,
318 double inc = 1,
9a83f860 319 const wxString& name = wxT("wxSpinCtrlDouble"))
669a6e11 320 {
8cd6a9ad 321 m_digits = 0;
c218ccee
VZ
322 Create(parent, id, value, pos, size, style,
323 min, max, initial, inc, name);
8cd6a9ad 324 }
669a6e11 325
8cd6a9ad
VZ
326 bool Create(wxWindow *parent,
327 wxWindowID id = wxID_ANY,
328 const wxString& value = wxEmptyString,
329 const wxPoint& pos = wxDefaultPosition,
330 const wxSize& size = wxDefaultSize,
f1ddb476 331 long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
c218ccee
VZ
332 double min = 0, double max = 100, double initial = 0,
333 double inc = 1,
9a83f860 334 const wxString& name = wxT("wxSpinCtrlDouble"))
8cd6a9ad 335 {
c218ccee
VZ
336 return wxSpinCtrlGenericBase::Create(parent, id, value, pos, size,
337 style, min, max, initial,
338 inc, name);
669a6e11
VZ
339 }
340
8cd6a9ad
VZ
341 // accessors
342 double GetValue(wxSPINCTRL_GETVALUE_FIX) const { return DoGetValue(); }
343 double GetMin() const { return m_min; }
344 double GetMax() const { return m_max; }
345 double GetIncrement() const { return m_increment; }
346 unsigned GetDigits() const { return m_digits; }
669a6e11
VZ
347
348 // operations
c218ccee
VZ
349 void SetValue(const wxString& value)
350 { wxSpinCtrlGenericBase::SetValue(value); }
8cd6a9ad
VZ
351 void SetValue(double value) { DoSetValue(value); }
352 void SetRange(double minVal, double maxVal) { DoSetRange(minVal, maxVal); }
353 void SetIncrement(double inc) { DoSetIncrement(inc); }
354 void SetDigits(unsigned digits);
669a6e11
VZ
355
356protected:
8cd6a9ad 357 virtual void DoSendEvent();
669a6e11 358
8cd6a9ad 359 unsigned m_digits;
5fde6fcc 360
8cd6a9ad 361 DECLARE_DYNAMIC_CLASS(wxSpinCtrlDouble)
669a6e11
VZ
362};
363
364#endif // _WX_GENERIC_SPINCTRL_H_