Honous initial position for wxSpinCtrl.
[wxWidgets.git] / src / generic / spinctlg.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/spinctlg.cpp
3 // Purpose: implements wxSpinCtrl as a composite control
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 29.01.01
7 // RCS-ID: $Id$
8 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "spinctlg.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 // There are port-specific versions for MSW, GTK, OS/2 and Mac, so exclude the
32 // contents of this file in those cases
33 #if !(defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXPM__) || \
34 defined(__WXMAC__)) || defined(__WXUNIVERSAL__)
35
36 #ifndef WX_PRECOMP
37 #include "wx/textctrl.h"
38 #endif //WX_PRECOMP
39
40 #if wxUSE_SPINCTRL
41
42 #include "wx/spinbutt.h"
43 #include "wx/spinctrl.h"
44
45 // ----------------------------------------------------------------------------
46 // constants
47 // ----------------------------------------------------------------------------
48
49 // the margin between the text control and the spin
50 static const wxCoord MARGIN = 2;
51
52 // ----------------------------------------------------------------------------
53 // wxSpinCtrlText: text control used by spin control
54 // ----------------------------------------------------------------------------
55
56 class wxSpinCtrlText : public wxTextCtrl
57 {
58 public:
59 wxSpinCtrlText(wxSpinCtrl *spin, const wxString& value)
60 : wxTextCtrl(spin->GetParent(), wxID_ANY, value)
61 {
62 m_spin = spin;
63
64 // remove the default minsize, the spinctrl will have one instead
65 SetSizeHints(wxDefaultCoord,wxDefaultCoord);
66 }
67
68 protected:
69 void OnTextChange(wxCommandEvent& event)
70 {
71 int val;
72 if ( m_spin->GetTextValue(&val) )
73 {
74 m_spin->GetSpinButton()->SetValue(val);
75 }
76
77 event.Skip();
78 }
79
80 bool ProcessEvent(wxEvent &event)
81 {
82 // Hand button down events to wxSpinCtrl. Doesn't work.
83 if (event.GetEventType() == wxEVT_LEFT_DOWN && m_spin->ProcessEvent( event ))
84 return true;
85
86 return wxTextCtrl::ProcessEvent( event );
87 }
88
89 private:
90 wxSpinCtrl *m_spin;
91
92 DECLARE_EVENT_TABLE()
93 };
94
95 BEGIN_EVENT_TABLE(wxSpinCtrlText, wxTextCtrl)
96 EVT_TEXT(wxID_ANY, wxSpinCtrlText::OnTextChange)
97 END_EVENT_TABLE()
98
99 // ----------------------------------------------------------------------------
100 // wxSpinCtrlButton: spin button used by spin control
101 // ----------------------------------------------------------------------------
102
103 class wxSpinCtrlButton : public wxSpinButton
104 {
105 public:
106 wxSpinCtrlButton(wxSpinCtrl *spin, int style)
107 : wxSpinButton(spin->GetParent())
108 {
109 m_spin = spin;
110
111 SetWindowStyle(style | wxSP_VERTICAL);
112
113 // remove the default minsize, the spinctrl will have one instead
114 SetSizeHints(wxDefaultCoord,wxDefaultCoord);
115 }
116
117 protected:
118 void OnSpinButton(wxSpinEvent& eventSpin)
119 {
120 m_spin->SetTextValue(eventSpin.GetPosition());
121
122 wxCommandEvent event(wxEVT_COMMAND_SPINCTRL_UPDATED, m_spin->GetId());
123 event.SetEventObject(m_spin);
124 event.SetInt(eventSpin.GetPosition());
125
126 m_spin->GetEventHandler()->ProcessEvent(event);
127
128 eventSpin.Skip();
129 }
130
131 private:
132 wxSpinCtrl *m_spin;
133
134 DECLARE_EVENT_TABLE()
135 };
136
137 BEGIN_EVENT_TABLE(wxSpinCtrlButton, wxSpinButton)
138 EVT_SPIN(wxID_ANY, wxSpinCtrlButton::OnSpinButton)
139 END_EVENT_TABLE()
140
141 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxControl)
142
143 // ============================================================================
144 // implementation
145 // ============================================================================
146
147 // ----------------------------------------------------------------------------
148 // wxSpinCtrl creation
149 // ----------------------------------------------------------------------------
150
151 void wxSpinCtrl::Init()
152 {
153 m_text = NULL;
154 m_btn = NULL;
155 }
156
157 bool wxSpinCtrl::Create(wxWindow *parent,
158 wxWindowID id,
159 const wxString& value,
160 const wxPoint& pos,
161 const wxSize& size,
162 long style,
163 int min,
164 int max,
165 int initial,
166 const wxString& name)
167 {
168 if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, style,
169 wxDefaultValidator, name) )
170 {
171 return false;
172 }
173
174 // the string value overrides the numeric one (for backwards compatibility
175 // reasons and also because it is simpler to satisfy the string value which
176 // comes much sooner in the list of arguments and leave the initial
177 // parameter unspecified)
178 if ( !value.empty() )
179 {
180 long l;
181 if ( value.ToLong(&l) )
182 initial = l;
183 }
184
185 m_text = new wxSpinCtrlText(this, value);
186 m_btn = new wxSpinCtrlButton(this, style);
187
188 m_btn->SetRange(min, max);
189 m_btn->SetValue(initial);
190 SetBestSize(size);
191 Move(pos);
192
193 // have to disable this window to avoid interfering it with message
194 // processing to the text and the button... but pretend it is enabled to
195 // make IsEnabled() return true
196 wxControl::Enable(false); // don't use non virtual Disable() here!
197 m_isEnabled = true;
198
199 // we don't even need to show this window itself - and not doing it avoids
200 // that it overwrites the text control
201 wxControl::Show(false);
202 m_isShown = true;
203 return true;
204 }
205
206 wxSpinCtrl::~wxSpinCtrl()
207 {
208 // delete the controls now, don't leave them alive even though they would
209 // still be eventually deleted by our parent - but it will be too late, the
210 // user code expects them to be gone now
211 delete m_text;
212 m_text = NULL ;
213 delete m_btn;
214 m_btn = NULL ;
215 }
216
217 // ----------------------------------------------------------------------------
218 // geometry
219 // ----------------------------------------------------------------------------
220
221 wxSize wxSpinCtrl::DoGetBestSize() const
222 {
223 wxSize sizeBtn = m_btn->GetBestSize(),
224 sizeText = m_text->GetBestSize();
225
226 return wxSize(sizeBtn.x + sizeText.x + MARGIN, sizeText.y);
227 }
228
229 void wxSpinCtrl::DoMoveWindow(int x, int y, int width, int height)
230 {
231 wxControl::DoMoveWindow(x, y, width, height);
232
233 // position the subcontrols inside the client area
234 wxSize sizeBtn = m_btn->GetSize();
235
236 wxCoord wText = width - sizeBtn.x;
237 m_text->SetSize(x, y, wText, height);
238 m_btn->SetSize(x + wText + MARGIN, y, wxDefaultCoord, height);
239 }
240
241 // ----------------------------------------------------------------------------
242 // operations forwarded to the subcontrols
243 // ----------------------------------------------------------------------------
244
245 bool wxSpinCtrl::Enable(bool enable)
246 {
247 if ( !wxControl::Enable(enable) )
248 return false;
249
250 m_btn->Enable(enable);
251 m_text->Enable(enable);
252
253 return true;
254 }
255
256 bool wxSpinCtrl::Show(bool show)
257 {
258 if ( !wxControl::Show(show) )
259 return false;
260
261 // under GTK Show() is called the first time before we are fully
262 // constructed
263 if ( m_btn )
264 {
265 m_btn->Show(show);
266 m_text->Show(show);
267 }
268
269 return true;
270 }
271
272 // ----------------------------------------------------------------------------
273 // value and range access
274 // ----------------------------------------------------------------------------
275
276 bool wxSpinCtrl::GetTextValue(int *val) const
277 {
278 long l;
279 if ( !m_text->GetValue().ToLong(&l) )
280 {
281 // not a number at all
282 return false;
283 }
284
285 if ( l < GetMin() || l > GetMax() )
286 {
287 // out of range
288 return false;
289 }
290
291 *val = l;
292
293 return true;
294 }
295
296 int wxSpinCtrl::GetValue() const
297 {
298 return m_btn ? m_btn->GetValue() : 0;
299 }
300
301 int wxSpinCtrl::GetMin() const
302 {
303 return m_btn ? m_btn->GetMin() : 0;
304 }
305
306 int wxSpinCtrl::GetMax() const
307 {
308 return m_btn ? m_btn->GetMax() : 0;
309 }
310
311 // ----------------------------------------------------------------------------
312 // changing value and range
313 // ----------------------------------------------------------------------------
314
315 void wxSpinCtrl::SetTextValue(int val)
316 {
317 wxCHECK_RET( m_text, _T("invalid call to wxSpinCtrl::SetTextValue") );
318
319 m_text->SetValue(wxString::Format(_T("%d"), val));
320
321 // select all text
322 m_text->SetSelection(0, -1);
323
324 // and give focus to the control!
325 // m_text->SetFocus(); Why???? TODO.
326 }
327
328 void wxSpinCtrl::SetValue(int val)
329 {
330 wxCHECK_RET( m_btn, _T("invalid call to wxSpinCtrl::SetValue") );
331
332 SetTextValue(val);
333
334 m_btn->SetValue(val);
335 }
336
337 void wxSpinCtrl::SetValue(const wxString& text)
338 {
339 wxCHECK_RET( m_text, _T("invalid call to wxSpinCtrl::SetValue") );
340
341 long val;
342 if ( text.ToLong(&val) && ((val > INT_MIN) && (val < INT_MAX)) )
343 {
344 SetValue((int)val);
345 }
346 else // not a number at all or out of range
347 {
348 m_text->SetValue(text);
349 m_text->SetSelection(0, -1);
350 }
351 }
352
353 void wxSpinCtrl::SetRange(int min, int max)
354 {
355 wxCHECK_RET( m_btn, _T("invalid call to wxSpinCtrl::SetRange") );
356
357 m_btn->SetRange(min, max);
358 }
359
360 void wxSpinCtrl::SetSelection(long from, long to)
361 {
362 wxCHECK_RET( m_text, _T("invalid call to wxSpinCtrl::SetSelection") );
363
364 m_text->SetSelection(from, to);
365 }
366
367 #endif // wxUSE_SPINCTRL
368 #endif // !wxPort-with-native-spinctrl