]> git.saurik.com Git - wxWidgets.git/blob - src/generic/spinctlg.cpp
0041895fd34547dc8fa00dda4f9273d916d2cebf
[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 // Licence: 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 #include "wx/tooltip.h"
33
34 #if wxUSE_SPINCTRL
35
36 IMPLEMENT_DYNAMIC_CLASS(wxSpinDoubleEvent, wxNotifyEvent)
37
38 // There are port-specific versions for the wxSpinCtrl, so exclude the
39 // contents of this file in those cases
40 #if !defined(wxHAS_NATIVE_SPINCTRL) || !defined(wxHAS_NATIVE_SPINCTRLDOUBLE)
41
42 #include "wx/spinbutt.h"
43
44 #if wxUSE_SPINBTN
45
46 // ----------------------------------------------------------------------------
47 // constants
48 // ----------------------------------------------------------------------------
49
50 // The margin between the text control and the spin: the value here is the same
51 // as the margin between the spin button and its "buddy" text control in wxMSW
52 // so the generic control looks similarly to the native one there, we might
53 // need to use different value for the other platforms (and maybe even
54 // determine it dynamically?).
55 static const wxCoord MARGIN = 1;
56
57 #define SPINCTRLBUT_MAX 32000 // large to avoid wrap around trouble
58
59 // ----------------------------------------------------------------------------
60 // wxSpinCtrlTextGeneric: text control used by spin control
61 // ----------------------------------------------------------------------------
62
63 class wxSpinCtrlTextGeneric : public wxTextCtrl
64 {
65 public:
66 wxSpinCtrlTextGeneric(wxSpinCtrlGenericBase *spin, const wxString& value, long style=0)
67 : wxTextCtrl(spin->GetParent(), wxID_ANY, value, wxDefaultPosition, wxDefaultSize,
68 // This is tricky: we want to honour any alignment flags
69 // but not wxALIGN_CENTER_VERTICAL because it's the same
70 // as wxTE_PASSWORD and we definitely don't want to show
71 // asterisks in spin control.
72 style & (wxALIGN_MASK | wxTE_PROCESS_ENTER) & ~wxTE_PASSWORD)
73 {
74 m_spin = spin;
75
76 // remove the default minsize, the spinctrl will have one instead
77 SetSizeHints(wxDefaultCoord, wxDefaultCoord);
78 }
79
80 virtual ~wxSpinCtrlTextGeneric()
81 {
82 // MSW sends extra kill focus event on destroy
83 if (m_spin)
84 m_spin->m_textCtrl = NULL;
85
86 m_spin = NULL;
87 }
88
89 void OnChar( wxKeyEvent &event )
90 {
91 if ( !m_spin->ProcessWindowEvent(event) )
92 event.Skip();
93 }
94
95 void OnTextEvent(wxCommandEvent& event)
96 {
97 wxCommandEvent eventCopy(event);
98 eventCopy.SetEventObject(m_spin);
99 eventCopy.SetId(m_spin->GetId());
100 m_spin->ProcessWindowEvent(eventCopy);
101 }
102
103 void OnKillFocus(wxFocusEvent& event)
104 {
105 if (m_spin)
106 m_spin->ProcessWindowEvent(event);
107
108 event.Skip();
109 }
110
111 wxSpinCtrlGenericBase *m_spin;
112
113 private:
114 DECLARE_EVENT_TABLE()
115 };
116
117 BEGIN_EVENT_TABLE(wxSpinCtrlTextGeneric, wxTextCtrl)
118 EVT_CHAR(wxSpinCtrlTextGeneric::OnChar)
119
120 // Forward the text events to wxSpinCtrl itself adjusting them slightly in
121 // the process.
122 EVT_TEXT(wxID_ANY, wxSpinCtrlTextGeneric::OnTextEvent)
123
124 // And we need to forward this one too as wxSpinCtrl is supposed to
125 // generate it if wxTE_PROCESS_ENTER is used with it (and if it isn't,
126 // we're never going to get EVT_TEXT_ENTER in the first place).
127 EVT_TEXT_ENTER(wxID_ANY, wxSpinCtrlTextGeneric::OnTextEvent)
128
129 EVT_KILL_FOCUS(wxSpinCtrlTextGeneric::OnKillFocus)
130 END_EVENT_TABLE()
131
132 // ----------------------------------------------------------------------------
133 // wxSpinCtrlButtonGeneric: spin button used by spin control
134 // ----------------------------------------------------------------------------
135
136 class wxSpinCtrlButtonGeneric : public wxSpinButton
137 {
138 public:
139 wxSpinCtrlButtonGeneric(wxSpinCtrlGenericBase *spin, int style)
140 : wxSpinButton(spin->GetParent(), wxID_ANY, wxDefaultPosition,
141 wxDefaultSize, style | wxSP_VERTICAL)
142 {
143 m_spin = spin;
144
145 SetRange(-SPINCTRLBUT_MAX, SPINCTRLBUT_MAX);
146
147 // remove the default minsize, the spinctrl will have one instead
148 SetSizeHints(wxDefaultCoord, wxDefaultCoord);
149 }
150
151 void OnSpinButton(wxSpinEvent& event)
152 {
153 if (m_spin)
154 m_spin->OnSpinButton(event);
155 }
156
157 wxSpinCtrlGenericBase *m_spin;
158
159 private:
160 DECLARE_EVENT_TABLE()
161 };
162
163 BEGIN_EVENT_TABLE(wxSpinCtrlButtonGeneric, wxSpinButton)
164 EVT_SPIN_UP( wxID_ANY, wxSpinCtrlButtonGeneric::OnSpinButton)
165 EVT_SPIN_DOWN(wxID_ANY, wxSpinCtrlButtonGeneric::OnSpinButton)
166 END_EVENT_TABLE()
167
168 // ============================================================================
169 // wxSpinCtrlGenericBase
170 // ============================================================================
171
172 // ----------------------------------------------------------------------------
173 // wxSpinCtrlGenericBase creation
174 // ----------------------------------------------------------------------------
175
176 void wxSpinCtrlGenericBase::Init()
177 {
178 m_value = 0;
179 m_min = 0;
180 m_max = 100;
181 m_increment = 1;
182 m_snap_to_ticks = false;
183
184 m_spin_value = 0;
185
186 m_textCtrl = NULL;
187 m_spinButton = NULL;
188 }
189
190 bool wxSpinCtrlGenericBase::Create(wxWindow *parent,
191 wxWindowID id,
192 const wxString& value,
193 const wxPoint& pos, const wxSize& size,
194 long style,
195 double min, double max, double initial,
196 double increment,
197 const wxString& name)
198 {
199 // don't use borders for this control itself, it wouldn't look good with
200 // the text control borders (but we might want to use style border bits to
201 // select the text control style)
202 if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize,
203 (style & ~wxBORDER_MASK) | wxBORDER_NONE,
204 wxDefaultValidator, name) )
205 {
206 return false;
207 }
208
209 m_value = initial;
210 m_min = min;
211 m_max = max;
212 m_increment = increment;
213
214 m_textCtrl = new wxSpinCtrlTextGeneric(this, value, style);
215 m_spinButton = new wxSpinCtrlButtonGeneric(this, style);
216 #if wxUSE_TOOLTIPS
217 m_textCtrl->SetToolTip(GetToolTipText());
218 m_spinButton->SetToolTip(GetToolTipText());
219 #endif // wxUSE_TOOLTIPS
220
221 m_spin_value = m_spinButton->GetValue();
222
223 // the string value overrides the numeric one (for backwards compatibility
224 // reasons and also because it is simpler to satisfy the string value which
225 // comes much sooner in the list of arguments and leave the initial
226 // parameter unspecified)
227 if ( !value.empty() )
228 {
229 double d;
230 if ( DoTextToValue(value, &d) )
231 {
232 m_value = d;
233 m_textCtrl->SetValue(DoValueToText(m_value));
234 }
235 }
236
237 SetInitialSize(size);
238 Move(pos);
239
240 // have to disable this window to avoid interfering it with message
241 // processing to the text and the button... but pretend it is enabled to
242 // make IsEnabled() return true
243 wxControl::Enable(false); // don't use non virtual Disable() here!
244 m_isEnabled = true;
245
246 // we don't even need to show this window itself - and not doing it avoids
247 // that it overwrites the text control
248 wxControl::Show(false);
249 m_isShown = true;
250 return true;
251 }
252
253 wxSpinCtrlGenericBase::~wxSpinCtrlGenericBase()
254 {
255 // delete the controls now, don't leave them alive even though they would
256 // still be eventually deleted by our parent - but it will be too late, the
257 // user code expects them to be gone now
258
259 if (m_textCtrl)
260 {
261 // null this since MSW sends KILL_FOCUS on deletion, see ~wxSpinCtrlTextGeneric
262 wxDynamicCast(m_textCtrl, wxSpinCtrlTextGeneric)->m_spin = NULL;
263
264 wxSpinCtrlTextGeneric *text = (wxSpinCtrlTextGeneric*)m_textCtrl;
265 m_textCtrl = NULL;
266 delete text;
267 }
268
269 wxDELETE(m_spinButton);
270 }
271
272 wxWindowList wxSpinCtrlGenericBase::GetCompositeWindowParts() const
273 {
274 wxWindowList parts;
275 parts.push_back(m_textCtrl);
276 parts.push_back(m_spinButton);
277 return parts;
278 }
279
280 // ----------------------------------------------------------------------------
281 // geometry
282 // ----------------------------------------------------------------------------
283
284 wxSize wxSpinCtrlGenericBase::DoGetBestSize() const
285 {
286 wxSize sizeBtn = m_spinButton->GetBestSize(),
287 sizeText = m_textCtrl->GetBestSize();
288
289 return wxSize(sizeBtn.x + sizeText.x + MARGIN, sizeText.y);
290 }
291
292 void wxSpinCtrlGenericBase::DoMoveWindow(int x, int y, int width, int height)
293 {
294 wxControl::DoMoveWindow(x, y, width, height);
295
296 // position the subcontrols inside the client area
297 wxSize sizeBtn = m_spinButton->GetSize();
298
299 wxCoord wText = width - sizeBtn.x - MARGIN;
300 m_textCtrl->SetSize(x, y, wText, height);
301 m_spinButton->SetSize(x + wText + MARGIN, y, wxDefaultCoord, height);
302 }
303
304 // ----------------------------------------------------------------------------
305 // operations forwarded to the subcontrols
306 // ----------------------------------------------------------------------------
307
308 void wxSpinCtrlGenericBase::SetFocus()
309 {
310 if ( m_textCtrl )
311 m_textCtrl->SetFocus();
312 }
313
314 #ifdef __WXMSW__
315
316 void wxSpinCtrlGenericBase::DoEnable(bool enable)
317 {
318 // We never enable this control itself, it must stay disabled to avoid
319 // interfering with the siblings event handling (see e.g. #12045 for the
320 // kind of problems which arise otherwise).
321 if ( !enable )
322 wxSpinCtrlBase::DoEnable(enable);
323 }
324
325 #endif // __WXMSW__
326
327 bool wxSpinCtrlGenericBase::Enable(bool enable)
328 {
329 if ( !wxSpinCtrlBase::Enable(enable) )
330 return false;
331
332 m_spinButton->Enable(enable);
333 m_textCtrl->Enable(enable);
334
335 return true;
336 }
337
338 bool wxSpinCtrlGenericBase::Show(bool show)
339 {
340 if ( !wxControl::Show(show) )
341 return false;
342
343 // under GTK Show() is called the first time before we are fully
344 // constructed
345 if ( m_spinButton )
346 {
347 m_spinButton->Show(show);
348 m_textCtrl->Show(show);
349 }
350
351 return true;
352 }
353
354 #if wxUSE_TOOLTIPS
355 void wxSpinCtrlGenericBase::DoSetToolTip(wxToolTip *tip)
356 {
357 // Notice that we must check for the subcontrols not being NULL (as they
358 // could be if we were created with the default ctor and this is called
359 // before Create() for some reason) and that we can't call SetToolTip(tip)
360 // because this would take ownership of the wxToolTip object (twice).
361 if ( m_textCtrl )
362 {
363 if ( tip )
364 m_textCtrl->SetToolTip(tip->GetTip());
365 else
366 m_textCtrl->SetToolTip(NULL);
367 }
368
369 if ( m_spinButton )
370 {
371 if( tip )
372 m_spinButton->SetToolTip(tip->GetTip());
373 else
374 m_spinButton->SetToolTip(NULL);
375 }
376
377 wxWindowBase::DoSetToolTip(tip);
378 }
379 #endif // wxUSE_TOOLTIPS
380
381 // ----------------------------------------------------------------------------
382 // Handle sub controls events
383 // ----------------------------------------------------------------------------
384
385 BEGIN_EVENT_TABLE(wxSpinCtrlGenericBase, wxSpinCtrlBase)
386 EVT_CHAR(wxSpinCtrlGenericBase::OnTextChar)
387 EVT_KILL_FOCUS(wxSpinCtrlGenericBase::OnTextLostFocus)
388 END_EVENT_TABLE()
389
390 void wxSpinCtrlGenericBase::OnSpinButton(wxSpinEvent& event)
391 {
392 event.Skip();
393
394 // Sync the textctrl since the user expects that the button will modify
395 // what they see in the textctrl.
396 SyncSpinToText();
397
398 int spin_value = event.GetPosition();
399 double step = (event.GetEventType() == wxEVT_SCROLL_LINEUP) ? 1 : -1;
400
401 // Use the spinbutton's acceleration, if any, but not if wrapping around
402 if (((spin_value >= 0) && (m_spin_value >= 0)) || ((spin_value <= 0) && (m_spin_value <= 0)))
403 step *= abs(spin_value - m_spin_value);
404
405 double value = AdjustToFitInRange(m_value + step*m_increment);
406
407 // Ignore the edges when it wraps since the up/down event may be opposite
408 // They are in GTK and Mac
409 if (abs(spin_value - m_spin_value) > SPINCTRLBUT_MAX)
410 {
411 m_spin_value = spin_value;
412 return;
413 }
414
415 m_spin_value = spin_value;
416
417 if ( DoSetValue(value) )
418 DoSendEvent();
419 }
420
421 void wxSpinCtrlGenericBase::OnTextLostFocus(wxFocusEvent& event)
422 {
423 SyncSpinToText();
424 DoSendEvent();
425
426 event.Skip();
427 }
428
429 void wxSpinCtrlGenericBase::OnTextChar(wxKeyEvent& event)
430 {
431 if ( !HasFlag(wxSP_ARROW_KEYS) )
432 {
433 event.Skip();
434 return;
435 }
436
437 double value = m_value;
438 switch ( event.GetKeyCode() )
439 {
440 case WXK_UP :
441 value += m_increment;
442 break;
443
444 case WXK_DOWN :
445 value -= m_increment;
446 break;
447
448 case WXK_PAGEUP :
449 value += m_increment * 10.0;
450 break;
451
452 case WXK_PAGEDOWN :
453 value -= m_increment * 10.0;
454 break;
455
456 default:
457 event.Skip();
458 return;
459 }
460
461 value = AdjustToFitInRange(value);
462
463 SyncSpinToText();
464
465 if ( DoSetValue(value) )
466 DoSendEvent();
467 }
468
469 // ----------------------------------------------------------------------------
470 // Textctrl functions
471 // ----------------------------------------------------------------------------
472
473 bool wxSpinCtrlGenericBase::SyncSpinToText()
474 {
475 if ( !m_textCtrl || !m_textCtrl->IsModified() )
476 return false;
477
478 double textValue;
479 if ( DoTextToValue(m_textCtrl->GetValue(), &textValue) )
480 {
481 if (textValue > m_max)
482 textValue = m_max;
483 else if (textValue < m_min)
484 textValue = m_min;
485 }
486 else // text contents is not a valid number at all
487 {
488 // replace its contents with the last valid value
489 textValue = m_value;
490 }
491
492 // we must always set the value here, even if it's equal to m_value, as
493 // otherwise we could be left with an out of range value when leaving the
494 // text control and the current value is already m_max for example
495 return DoSetValue(textValue);
496 }
497
498 // ----------------------------------------------------------------------------
499 // changing value and range
500 // ----------------------------------------------------------------------------
501
502 void wxSpinCtrlGenericBase::SetValue(const wxString& text)
503 {
504 wxCHECK_RET( m_textCtrl, wxT("invalid call to wxSpinCtrl::SetValue") );
505
506 double val;
507 if ( DoTextToValue(text, &val) && InRange(val) )
508 {
509 DoSetValue(val);
510 }
511 else // not a number at all or out of range
512 {
513 m_textCtrl->SetValue(text);
514 m_textCtrl->SetSelection(0, -1);
515 m_textCtrl->SetInsertionPointEnd();
516 }
517 }
518
519 bool wxSpinCtrlGenericBase::DoSetValue(double val)
520 {
521 wxCHECK_MSG( m_textCtrl, false, wxT("invalid call to wxSpinCtrl::SetValue") );
522
523 if (!InRange(val))
524 return false;
525
526 if ( m_snap_to_ticks && (m_increment != 0) )
527 {
528 double snap_value = val / m_increment;
529
530 if (wxFinite(snap_value)) // FIXME what to do about a failure?
531 {
532 if ((snap_value - floor(snap_value)) < (ceil(snap_value) - snap_value))
533 val = floor(snap_value) * m_increment;
534 else
535 val = ceil(snap_value) * m_increment;
536 }
537 }
538
539 wxString str(DoValueToText(val));
540
541 if ((val != m_value) || (str != m_textCtrl->GetValue()))
542 {
543 if ( !DoTextToValue(str, &m_value ) ) // wysiwyg for textctrl
544 m_value = val;
545 m_textCtrl->SetValue( str );
546 m_textCtrl->SetInsertionPointEnd();
547 m_textCtrl->DiscardEdits();
548 return true;
549 }
550
551 return false;
552 }
553
554 double wxSpinCtrlGenericBase::AdjustToFitInRange(double value) const
555 {
556 if (value < m_min)
557 value = HasFlag(wxSP_WRAP) ? m_max : m_min;
558 if (value > m_max)
559 value = HasFlag(wxSP_WRAP) ? m_min : m_max;
560
561 return value;
562 }
563
564 void wxSpinCtrlGenericBase::DoSetRange(double min, double max)
565 {
566 m_min = min;
567 m_max = max;
568 }
569
570 void wxSpinCtrlGenericBase::DoSetIncrement(double inc)
571 {
572 m_increment = inc;
573 }
574
575 void wxSpinCtrlGenericBase::SetSnapToTicks(bool snap_to_ticks)
576 {
577 m_snap_to_ticks = snap_to_ticks;
578 DoSetValue(m_value);
579 }
580
581 void wxSpinCtrlGenericBase::SetSelection(long from, long to)
582 {
583 wxCHECK_RET( m_textCtrl, wxT("invalid call to wxSpinCtrl::SetSelection") );
584
585 m_textCtrl->SetSelection(from, to);
586 }
587
588 #ifndef wxHAS_NATIVE_SPINCTRL
589
590 //-----------------------------------------------------------------------------
591 // wxSpinCtrl
592 //-----------------------------------------------------------------------------
593
594 bool wxSpinCtrl::SetBase(int base)
595 {
596 // Currently we only support base 10 and 16. We could add support for base
597 // 8 quite easily but wxMSW doesn't support it natively so don't bother.
598 if ( base != 10 && base != 16 )
599 return false;
600
601 if ( base == m_base )
602 return true;
603
604 // Update the current control contents to show in the new base: be careful
605 // to call DoTextToValue() before changing the base...
606 double val;
607 const bool hasValidVal = DoTextToValue(m_textCtrl->GetValue(), &val);
608
609 m_base = base;
610
611 // ... but DoValueToText() after doing it.
612 if ( hasValidVal )
613 m_textCtrl->SetValue(DoValueToText(val));
614
615 return true;
616 }
617
618 void wxSpinCtrl::DoSendEvent()
619 {
620 wxSpinEvent event( wxEVT_COMMAND_SPINCTRL_UPDATED, GetId());
621 event.SetEventObject( this );
622 event.SetPosition((int)(m_value + 0.5)); // FIXME should be SetValue
623 event.SetString(m_textCtrl->GetValue());
624 GetEventHandler()->ProcessEvent( event );
625 }
626
627 bool wxSpinCtrl::DoTextToValue(const wxString& text, double *val)
628 {
629 long lval;
630 if ( !text.ToLong(&lval, GetBase()) )
631 return false;
632
633 *val = static_cast<double>(lval);
634
635 return true;
636 }
637
638 wxString wxSpinCtrl::DoValueToText(double val)
639 {
640 switch ( GetBase() )
641 {
642 case 16:
643 return wxPrivate::wxSpinCtrlFormatAsHex(static_cast<long>(val),
644 GetMax());
645
646 default:
647 wxFAIL_MSG( wxS("Unsupported spin control base") );
648 // Fall through
649
650 case 10:
651 return wxString::Format("%ld", static_cast<long>(val));
652 }
653 }
654
655 #endif // !wxHAS_NATIVE_SPINCTRL
656
657 //-----------------------------------------------------------------------------
658 // wxSpinCtrlDouble
659 //-----------------------------------------------------------------------------
660
661 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlDouble, wxSpinCtrlGenericBase)
662
663 void wxSpinCtrlDouble::DoSendEvent()
664 {
665 wxSpinDoubleEvent event( wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED, GetId());
666 event.SetEventObject( this );
667 event.SetValue(m_value);
668 event.SetString(m_textCtrl->GetValue());
669 GetEventHandler()->ProcessEvent( event );
670 }
671
672 bool wxSpinCtrlDouble::DoTextToValue(const wxString& text, double *val)
673 {
674 return text.ToDouble(val);
675 }
676
677 wxString wxSpinCtrlDouble::DoValueToText(double val)
678 {
679 return wxString::Format(m_format, val);
680 }
681
682 void wxSpinCtrlDouble::SetDigits(unsigned digits)
683 {
684 wxCHECK_RET( digits <= 20, "too many digits for wxSpinCtrlDouble" );
685
686 if ( digits == m_digits )
687 return;
688
689 m_digits = digits;
690
691 m_format.Printf(wxT("%%0.%ulf"), digits);
692
693 DoSetValue(m_value);
694 }
695
696 #endif // wxUSE_SPINBTN
697
698 #endif // !wxPort-with-native-spinctrl
699
700 #endif // wxUSE_SPINCTRL