]> git.saurik.com Git - wxWidgets.git/blob - src/generic/spinctlg.cpp
8be2e5d720ad420d9002b7dc41b573017ab3d481
[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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/textctrl.h"
29 #endif //WX_PRECOMP
30
31 #include "wx/spinctrl.h"
32
33 #if wxUSE_SPINCTRL
34
35 IMPLEMENT_DYNAMIC_CLASS(wxSpinDoubleEvent, wxNotifyEvent)
36
37 // There are port-specific versions for the wxSpinCtrl, so exclude the
38 // contents of this file in those cases
39 #if !defined(wxHAS_NATIVE_SPINCTRL) || !defined(wxHAS_NATIVE_SPINCTRLDOUBLE)
40
41 #include "wx/spinbutt.h"
42
43 #if wxUSE_SPINBTN
44
45 // ----------------------------------------------------------------------------
46 // constants
47 // ----------------------------------------------------------------------------
48
49 // the margin between the text control and the spin
50 static const wxCoord MARGIN = 2;
51
52 #define SPINCTRLBUT_MAX 32000 // large to avoid wrap around trouble
53
54 // ----------------------------------------------------------------------------
55 // wxSpinCtrlTextGeneric: text control used by spin control
56 // ----------------------------------------------------------------------------
57
58 class wxSpinCtrlTextGeneric : public wxTextCtrl
59 {
60 public:
61 wxSpinCtrlTextGeneric(wxSpinCtrlGenericBase *spin, const wxString& value)
62 : wxTextCtrl(spin->GetParent(), wxID_ANY, value, wxDefaultPosition,
63 wxDefaultSize, wxTE_NOHIDESEL|wxTE_PROCESS_ENTER)
64 {
65 m_spin = spin;
66
67 // remove the default minsize, the spinctrl will have one instead
68 SetSizeHints(wxDefaultCoord, wxDefaultCoord);
69 }
70
71 virtual ~wxSpinCtrlTextGeneric()
72 {
73 // MSW sends extra kill focus event on destroy
74 if (m_spin)
75 m_spin->m_textCtrl = NULL;
76
77 m_spin = NULL;
78 }
79
80 void OnTextEnter(wxCommandEvent& event)
81 {
82 if (m_spin)
83 m_spin->OnTextEnter(event);
84 }
85
86 void OnChar( wxKeyEvent &event )
87 {
88 if (m_spin)
89 m_spin->OnTextChar(event);
90 }
91
92 void OnKillFocus(wxFocusEvent& event)
93 {
94 if (m_spin)
95 {
96 m_spin->SyncSpinToText();
97 m_spin->DoSendEvent();
98 }
99
100 event.Skip();
101 }
102
103 wxSpinCtrlGenericBase *m_spin;
104
105 private:
106 DECLARE_EVENT_TABLE()
107 };
108
109 BEGIN_EVENT_TABLE(wxSpinCtrlTextGeneric, wxTextCtrl)
110 EVT_TEXT_ENTER(wxID_ANY, wxSpinCtrlTextGeneric::OnTextEnter)
111
112 EVT_CHAR(wxSpinCtrlTextGeneric::OnChar)
113
114 EVT_KILL_FOCUS(wxSpinCtrlTextGeneric::OnKillFocus)
115 END_EVENT_TABLE()
116
117 // ----------------------------------------------------------------------------
118 // wxSpinCtrlButtonGeneric: spin button used by spin control
119 // ----------------------------------------------------------------------------
120
121 class wxSpinCtrlButtonGeneric : public wxSpinButton
122 {
123 public:
124 wxSpinCtrlButtonGeneric(wxSpinCtrlGenericBase *spin, int style)
125 : wxSpinButton(spin->GetParent(), wxID_ANY, wxDefaultPosition,
126 wxDefaultSize, style | wxSP_VERTICAL)
127 {
128 m_spin = spin;
129
130 SetRange(-SPINCTRLBUT_MAX, SPINCTRLBUT_MAX);
131
132 // remove the default minsize, the spinctrl will have one instead
133 SetSizeHints(wxDefaultCoord, wxDefaultCoord);
134 }
135
136 void OnSpinButton(wxSpinEvent& event)
137 {
138 if (m_spin)
139 m_spin->OnSpinButton(event);
140 }
141
142 wxSpinCtrlGenericBase *m_spin;
143
144 private:
145 DECLARE_EVENT_TABLE()
146 };
147
148 BEGIN_EVENT_TABLE(wxSpinCtrlButtonGeneric, wxSpinButton)
149 EVT_SPIN_UP( wxID_ANY, wxSpinCtrlButtonGeneric::OnSpinButton)
150 EVT_SPIN_DOWN(wxID_ANY, wxSpinCtrlButtonGeneric::OnSpinButton)
151 END_EVENT_TABLE()
152
153 // ============================================================================
154 // wxSpinCtrlGenericBase
155 // ============================================================================
156
157 // ----------------------------------------------------------------------------
158 // wxSpinCtrlGenericBase creation
159 // ----------------------------------------------------------------------------
160
161 void wxSpinCtrlGenericBase::Init()
162 {
163 m_value = 0;
164 m_min = 0;
165 m_max = 100;
166 m_increment = 1;
167 m_snap_to_ticks = false;
168 m_format = wxS("%g");
169
170 m_spin_value = 0;
171
172 m_textCtrl = NULL;
173 m_spinButton = NULL;
174 }
175
176 bool wxSpinCtrlGenericBase::Create(wxWindow *parent,
177 wxWindowID id,
178 const wxString& value,
179 const wxPoint& pos, const wxSize& size,
180 long style,
181 double min, double max, double initial,
182 double increment,
183 const wxString& name)
184 {
185 // don't use borders for this control itself, it wouldn't look good with
186 // the text control borders (but we might want to use style border bits to
187 // select the text control style)
188 if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize,
189 wxBORDER_NONE, wxDefaultValidator, name) )
190 {
191 return false;
192 }
193
194 m_value = initial;
195 m_min = min;
196 m_max = max;
197 m_increment = increment;
198
199 m_textCtrl = new wxSpinCtrlTextGeneric(this, value);
200 m_spinButton = new wxSpinCtrlButtonGeneric(this, style);
201
202 m_spin_value = m_spinButton->GetValue();
203
204 // the string value overrides the numeric one (for backwards compatibility
205 // reasons and also because it is simpler to satisfy the string value which
206 // comes much sooner in the list of arguments and leave the initial
207 // parameter unspecified)
208 if ( !value.empty() )
209 {
210 double d;
211 if ( value.ToDouble(&d) )
212 {
213 m_value = d;
214 m_textCtrl->SetValue(wxString::Format(m_format, m_value));
215 }
216 }
217
218 SetInitialSize(size);
219 Move(pos);
220
221 // have to disable this window to avoid interfering it with message
222 // processing to the text and the button... but pretend it is enabled to
223 // make IsEnabled() return true
224 wxControl::Enable(false); // don't use non virtual Disable() here!
225 m_isEnabled = true;
226
227 // we don't even need to show this window itself - and not doing it avoids
228 // that it overwrites the text control
229 wxControl::Show(false);
230 m_isShown = true;
231 return true;
232 }
233
234 wxSpinCtrlGenericBase::~wxSpinCtrlGenericBase()
235 {
236 // delete the controls now, don't leave them alive even though they would
237 // still be eventually deleted by our parent - but it will be too late, the
238 // user code expects them to be gone now
239
240 if (m_textCtrl)
241 {
242 // null this since MSW sends KILL_FOCUS on deletion, see ~wxSpinCtrlTextGeneric
243 wxDynamicCast(m_textCtrl, wxSpinCtrlTextGeneric)->m_spin = NULL;
244
245 wxSpinCtrlTextGeneric *text = (wxSpinCtrlTextGeneric*)m_textCtrl;
246 m_textCtrl = NULL;
247 delete text;
248 }
249
250 delete m_spinButton;
251 m_spinButton = NULL;
252 }
253
254 // ----------------------------------------------------------------------------
255 // geometry
256 // ----------------------------------------------------------------------------
257
258 wxSize wxSpinCtrlGenericBase::DoGetBestSize() const
259 {
260 wxSize sizeBtn = m_spinButton->GetBestSize(),
261 sizeText = m_textCtrl->GetBestSize();
262
263 return wxSize(sizeBtn.x + sizeText.x + MARGIN, sizeText.y);
264 }
265
266 void wxSpinCtrlGenericBase::DoMoveWindow(int x, int y, int width, int height)
267 {
268 wxControl::DoMoveWindow(x, y, width, height);
269
270 // position the subcontrols inside the client area
271 wxSize sizeBtn = m_spinButton->GetSize();
272
273 wxCoord wText = width - sizeBtn.x;
274 m_textCtrl->SetSize(x, y, wText, height);
275 m_spinButton->SetSize(x + wText + MARGIN, y, wxDefaultCoord, height);
276 }
277
278 // ----------------------------------------------------------------------------
279 // operations forwarded to the subcontrols
280 // ----------------------------------------------------------------------------
281
282 bool wxSpinCtrlGenericBase::Enable(bool enable)
283 {
284 if ( !wxControl::Enable(enable) )
285 return false;
286
287 m_spinButton->Enable(enable);
288 m_textCtrl->Enable(enable);
289
290 return true;
291 }
292
293 bool wxSpinCtrlGenericBase::Show(bool show)
294 {
295 if ( !wxControl::Show(show) )
296 return false;
297
298 // under GTK Show() is called the first time before we are fully
299 // constructed
300 if ( m_spinButton )
301 {
302 m_spinButton->Show(show);
303 m_textCtrl->Show(show);
304 }
305
306 return true;
307 }
308
309 bool wxSpinCtrlGenericBase::Reparent(wxWindowBase *newParent)
310 {
311 if ( m_spinButton )
312 {
313 m_spinButton->Reparent(newParent);
314 m_textCtrl->Reparent(newParent);
315 }
316
317 return true;
318 }
319
320 // ----------------------------------------------------------------------------
321 // Handle sub controls events
322 // ----------------------------------------------------------------------------
323
324 void wxSpinCtrlGenericBase::OnSpinButton(wxSpinEvent& event)
325 {
326 event.Skip();
327
328 // Sync the textctrl since the user expects that the button will modify
329 // what they see in the textctrl.
330 if ( m_textCtrl && m_textCtrl->IsModified() )
331 SyncSpinToText();
332
333 int spin_value = event.GetPosition();
334 double step = (event.GetEventType() == wxEVT_SCROLL_LINEUP) ? 1 : -1;
335
336 // Use the spinbutton's acceleration, if any, but not if wrapping around
337 if (((spin_value >= 0) && (m_spin_value >= 0)) || ((spin_value <= 0) && (m_spin_value <= 0)))
338 step *= abs(spin_value - m_spin_value);
339
340 double value = m_value + step*m_increment;
341
342 // Check for over/underflow wrapping around if necessary
343 if (value < m_min)
344 value = HasFlag(wxSP_WRAP) ? m_max : m_min;
345 if (value > m_max)
346 value = HasFlag(wxSP_WRAP) ? m_min : m_max;
347
348 // Ignore the edges when it wraps since the up/down event may be opposite
349 // They are in GTK and Mac
350 if (abs(spin_value - m_spin_value) > SPINCTRLBUT_MAX)
351 {
352 m_spin_value = spin_value;
353 return;
354 }
355
356 m_spin_value = spin_value;
357
358 if (InRange(value) && DoSetValue(value))
359 DoSendEvent();
360 }
361
362 void wxSpinCtrlGenericBase::OnTextEnter(wxCommandEvent& event)
363 {
364 SyncSpinToText();
365 DoSendEvent();
366 event.Skip();
367 }
368
369 void wxSpinCtrlGenericBase::OnTextChar(wxKeyEvent& event)
370 {
371 double value = m_value;
372 switch ( event.GetKeyCode() )
373 {
374 case WXK_UP :
375 value += m_increment;
376 break;
377
378 case WXK_DOWN :
379 value -= m_increment;
380 break;
381
382 case WXK_PAGEUP :
383 value += m_increment * 10.0;
384 break;
385
386 case WXK_PAGEDOWN :
387 value -= m_increment * 10.0;
388 break;
389
390 default:
391 event.Skip();
392 return;
393 }
394
395 if ( m_textCtrl && m_textCtrl->IsModified() )
396 SyncSpinToText();
397
398 if ( DoSetValue(value) )
399 DoSendEvent();
400 }
401
402 // ----------------------------------------------------------------------------
403 // Textctrl functions
404 // ----------------------------------------------------------------------------
405
406 void wxSpinCtrlGenericBase::SyncSpinToText()
407 {
408 if (!m_textCtrl)
409 return;
410
411 double textValue;
412 if ( m_textCtrl->GetValue().ToDouble(&textValue) )
413 {
414 if (textValue > m_max)
415 textValue = m_max;
416 else if (textValue < m_min)
417 textValue = m_min;
418
419 if (m_value != textValue)
420 {
421 DoSetValue(textValue);
422 }
423 }
424 else
425 {
426 // textctrl is out of sync, discard and reset
427 DoSetValue(m_value);
428 }
429 }
430
431 // ----------------------------------------------------------------------------
432 // changing value and range
433 // ----------------------------------------------------------------------------
434
435 void wxSpinCtrlGenericBase::SetValue(const wxString& text)
436 {
437 wxCHECK_RET( m_textCtrl, _T("invalid call to wxSpinCtrl::SetValue") );
438
439 double val;
440 if ( text.ToDouble(&val) && InRange(val) )
441 {
442 DoSetValue(val);
443 }
444 else // not a number at all or out of range
445 {
446 m_textCtrl->SetValue(text);
447 m_textCtrl->SetSelection(0, -1);
448 }
449 }
450
451 bool wxSpinCtrlGenericBase::DoSetValue(double val)
452 {
453 wxCHECK_MSG( m_textCtrl, false, _T("invalid call to wxSpinCtrl::SetValue") );
454
455 if (!InRange(val))
456 return false;
457
458 if ( m_snap_to_ticks && (m_increment != 0) )
459 {
460 double snap_value = val / m_increment;
461
462 if (wxFinite(snap_value)) // FIXME what to do about a failure?
463 {
464 if ((snap_value - floor(snap_value)) < (ceil(snap_value) - snap_value))
465 val = floor(snap_value) * m_increment;
466 else
467 val = ceil(snap_value) * m_increment;
468 }
469 }
470
471 wxString str(wxString::Format(m_format.c_str(), val));
472
473 if ((val != m_value) || (str != m_textCtrl->GetValue()))
474 {
475 m_value = val;
476 str.ToDouble( &m_value ); // wysiwyg for textctrl
477 m_textCtrl->SetValue( str );
478 m_textCtrl->DiscardEdits();
479 return true;
480 }
481
482 return false;
483 }
484
485 void wxSpinCtrlGenericBase::DoSetRange(double min, double max)
486 {
487 m_min = min;
488 m_max = max;
489 }
490
491 void wxSpinCtrlGenericBase::DoSetIncrement(double inc)
492 {
493 m_increment = inc;
494 }
495
496 void wxSpinCtrlGenericBase::SetSnapToTicks(bool snap_to_ticks)
497 {
498 m_snap_to_ticks = snap_to_ticks;
499 DoSetValue(m_value);
500 }
501
502 void wxSpinCtrlGenericBase::SetSelection(long from, long to)
503 {
504 wxCHECK_RET( m_textCtrl, _T("invalid call to wxSpinCtrl::SetSelection") );
505
506 m_textCtrl->SetSelection(from, to);
507 }
508
509 #ifndef wxHAS_NATIVE_SPINCTRL
510
511 //-----------------------------------------------------------------------------
512 // wxSpinCtrl
513 //-----------------------------------------------------------------------------
514
515 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxSpinCtrlGenericBase)
516
517 void wxSpinCtrl::DoSendEvent()
518 {
519 wxSpinEvent event( wxEVT_COMMAND_SPINCTRL_UPDATED, GetId());
520 event.SetEventObject( this );
521 event.SetPosition((int)(m_value + 0.5)); // FIXME should be SetValue
522 event.SetString(m_textCtrl->GetValue());
523 GetEventHandler()->ProcessEvent( event );
524 }
525
526 #endif // !wxHAS_NATIVE_SPINCTRL
527
528 //-----------------------------------------------------------------------------
529 // wxSpinCtrlDouble
530 //-----------------------------------------------------------------------------
531
532 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlDouble, wxSpinCtrlGenericBase)
533
534 void wxSpinCtrlDouble::DoSendEvent()
535 {
536 wxSpinDoubleEvent event( wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED, GetId());
537 event.SetEventObject( this );
538 event.SetValue(m_value);
539 event.SetString(m_textCtrl->GetValue());
540 GetEventHandler()->ProcessEvent( event );
541 }
542
543 void wxSpinCtrlDouble::SetDigits(unsigned digits)
544 {
545 wxCHECK_RET( digits <= 20, "too many digits for wxSpinCtrlDouble" );
546
547 m_format.Printf(wxT("%%0.%ulf"), digits);
548
549 DoSetValue(m_value);
550 }
551
552 #endif // wxUSE_SPINBTN
553
554 #endif // !wxPort-with-native-spinctrl
555
556 #endif // wxUSE_SPINCTRL