]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/spinctlg.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/spinctlg.cpp
3 // Purpose: implements wxSpinCtrl as a composite control
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
28 #include "wx/textctrl.h"
31 #include "wx/spinctrl.h"
32 #include "wx/tooltip.h"
36 IMPLEMENT_DYNAMIC_CLASS(wxSpinDoubleEvent
, wxNotifyEvent
)
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)
42 #include "wx/spinbutt.h"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
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;
57 #define SPINCTRLBUT_MAX 32000 // large to avoid wrap around trouble
59 // ----------------------------------------------------------------------------
60 // wxSpinCtrlTextGeneric: text control used by spin control
61 // ----------------------------------------------------------------------------
63 class wxSpinCtrlTextGeneric
: public wxTextCtrl
66 wxSpinCtrlTextGeneric(wxSpinCtrlGenericBase
*spin
, const wxString
& value
, long style
=0)
67 : wxTextCtrl(spin
, 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
)
76 // remove the default minsize, the spinctrl will have one instead
77 SetSizeHints(wxDefaultCoord
, wxDefaultCoord
);
80 virtual ~wxSpinCtrlTextGeneric()
82 // MSW sends extra kill focus event on destroy
84 m_spin
->m_textCtrl
= NULL
;
89 void OnChar( wxKeyEvent
&event
)
91 if ( !m_spin
->ProcessWindowEvent(event
) )
95 void OnTextEvent(wxCommandEvent
& event
)
97 wxCommandEvent
eventCopy(event
);
98 eventCopy
.SetEventObject(m_spin
);
99 eventCopy
.SetId(m_spin
->GetId());
100 m_spin
->ProcessWindowEvent(eventCopy
);
103 void OnKillFocus(wxFocusEvent
& event
)
106 m_spin
->ProcessWindowEvent(event
);
111 wxSpinCtrlGenericBase
*m_spin
;
114 DECLARE_EVENT_TABLE()
117 BEGIN_EVENT_TABLE(wxSpinCtrlTextGeneric
, wxTextCtrl
)
118 EVT_CHAR(wxSpinCtrlTextGeneric::OnChar
)
120 // Forward the text events to wxSpinCtrl itself adjusting them slightly in
122 EVT_TEXT(wxID_ANY
, wxSpinCtrlTextGeneric::OnTextEvent
)
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
)
129 EVT_KILL_FOCUS(wxSpinCtrlTextGeneric::OnKillFocus
)
132 // ----------------------------------------------------------------------------
133 // wxSpinCtrlButtonGeneric: spin button used by spin control
134 // ----------------------------------------------------------------------------
136 class wxSpinCtrlButtonGeneric
: public wxSpinButton
139 wxSpinCtrlButtonGeneric(wxSpinCtrlGenericBase
*spin
, int style
)
140 : wxSpinButton(spin
, wxID_ANY
, wxDefaultPosition
,
141 wxDefaultSize
, style
| wxSP_VERTICAL
)
145 SetRange(-SPINCTRLBUT_MAX
, SPINCTRLBUT_MAX
);
147 // remove the default minsize, the spinctrl will have one instead
148 SetSizeHints(wxDefaultCoord
, wxDefaultCoord
);
151 void OnSpinButton(wxSpinEvent
& event
)
154 m_spin
->OnSpinButton(event
);
157 wxSpinCtrlGenericBase
*m_spin
;
160 DECLARE_EVENT_TABLE()
163 BEGIN_EVENT_TABLE(wxSpinCtrlButtonGeneric
, wxSpinButton
)
164 EVT_SPIN_UP( wxID_ANY
, wxSpinCtrlButtonGeneric::OnSpinButton
)
165 EVT_SPIN_DOWN(wxID_ANY
, wxSpinCtrlButtonGeneric::OnSpinButton
)
168 // ============================================================================
169 // wxSpinCtrlGenericBase
170 // ============================================================================
172 // ----------------------------------------------------------------------------
173 // wxSpinCtrlGenericBase creation
174 // ----------------------------------------------------------------------------
176 void wxSpinCtrlGenericBase::Init()
182 m_snap_to_ticks
= false;
190 bool wxSpinCtrlGenericBase::Create(wxWindow
*parent
,
192 const wxString
& value
,
193 const wxPoint
& pos
, const wxSize
& size
,
195 double min
, double max
, double initial
,
197 const wxString
& name
)
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
) )
212 m_increment
= increment
;
214 m_textCtrl
= new wxSpinCtrlTextGeneric(this, value
, style
);
215 m_spinButton
= new wxSpinCtrlButtonGeneric(this, style
);
218 m_textCtrl
->SetToolTip(GetToolTipText());
219 m_spinButton
->SetToolTip(GetToolTipText());
220 #endif // wxUSE_TOOLTIPS
222 m_spin_value
= m_spinButton
->GetValue();
224 // the string value overrides the numeric one (for backwards compatibility
225 // reasons and also because it is simpler to satisfy the string value which
226 // comes much sooner in the list of arguments and leave the initial
227 // parameter unspecified)
228 if ( !value
.empty() )
231 if ( DoTextToValue(value
, &d
) )
234 m_textCtrl
->SetValue(DoValueToText(m_value
));
238 SetInitialSize(size
);
244 wxSpinCtrlGenericBase::~wxSpinCtrlGenericBase()
246 // delete the controls now, don't leave them alive even though they would
247 // still be eventually deleted by our parent - but it will be too late, the
248 // user code expects them to be gone now
252 // null this since MSW sends KILL_FOCUS on deletion, see ~wxSpinCtrlTextGeneric
253 wxDynamicCast(m_textCtrl
, wxSpinCtrlTextGeneric
)->m_spin
= NULL
;
255 wxSpinCtrlTextGeneric
*text
= (wxSpinCtrlTextGeneric
*)m_textCtrl
;
260 wxDELETE(m_spinButton
);
263 wxWindowList
wxSpinCtrlGenericBase::GetCompositeWindowParts() const
266 parts
.push_back(m_textCtrl
);
267 parts
.push_back(m_spinButton
);
271 // ----------------------------------------------------------------------------
273 // ----------------------------------------------------------------------------
275 wxSize
wxSpinCtrlGenericBase::DoGetBestSize() const
277 return DoGetSizeFromTextSize(m_textCtrl
->GetBestSize().x
, -1);
280 wxSize
wxSpinCtrlGenericBase::DoGetSizeFromTextSize(int xlen
, int ylen
) const
282 wxSize sizeBtn
= m_spinButton
->GetBestSize();
283 wxSize
totalS( m_textCtrl
->GetBestSize() );
285 wxSize
tsize(xlen
+ sizeBtn
.x
+ MARGIN
, totalS
.y
);
286 #if defined(__WXMSW__)
287 tsize
.IncBy(0.4 * totalS
.y
+ 4, 0);
288 #elif defined(__WXGTK__)
289 tsize
.IncBy(totalS
.y
+ 10, 0);
292 // Check if the user requested a non-standard height.
294 tsize
.IncBy(0, ylen
- GetCharHeight());
299 void wxSpinCtrlGenericBase::DoMoveWindow(int x
, int y
, int width
, int height
)
301 wxControl::DoMoveWindow(x
, y
, width
, height
);
303 // position the subcontrols inside the client area
304 wxSize sizeBtn
= m_spinButton
->GetSize();
306 wxCoord wText
= width
- sizeBtn
.x
- MARGIN
;
307 m_textCtrl
->SetSize(0, 0, wText
, height
);
308 m_spinButton
->SetSize(0 + wText
+ MARGIN
, 0, wxDefaultCoord
, height
);
311 // ----------------------------------------------------------------------------
312 // operations forwarded to the subcontrols
313 // ----------------------------------------------------------------------------
315 void wxSpinCtrlGenericBase::SetFocus()
318 m_textCtrl
->SetFocus();
323 void wxSpinCtrlGenericBase::DoEnable(bool enable
)
325 wxSpinCtrlBase::DoEnable(enable
);
330 bool wxSpinCtrlGenericBase::Enable(bool enable
)
332 if ( !wxSpinCtrlBase::Enable(enable
) )
335 m_spinButton
->Enable(enable
);
336 m_textCtrl
->Enable(enable
);
341 bool wxSpinCtrlGenericBase::Show(bool show
)
343 if ( !wxControl::Show(show
) )
346 // under GTK Show() is called the first time before we are fully
350 m_spinButton
->Show(show
);
351 m_textCtrl
->Show(show
);
358 void wxSpinCtrlGenericBase::DoSetToolTip(wxToolTip
*tip
)
360 // Notice that we must check for the subcontrols not being NULL (as they
361 // could be if we were created with the default ctor and this is called
362 // before Create() for some reason) and that we can't call SetToolTip(tip)
363 // because this would take ownership of the wxToolTip object (twice).
367 m_textCtrl
->SetToolTip(tip
->GetTip());
369 m_textCtrl
->SetToolTip(NULL
);
375 m_spinButton
->SetToolTip(tip
->GetTip());
377 m_spinButton
->SetToolTip(NULL
);
380 wxWindowBase::DoSetToolTip(tip
);
382 #endif // wxUSE_TOOLTIPS
384 bool wxSpinCtrlGenericBase::SetBackgroundColour(const wxColour
& colour
)
386 // We need to provide this otherwise the entire composite window
387 // background and therefore the between component spaces
390 return m_textCtrl
->SetBackgroundColour(colour
);
395 // ----------------------------------------------------------------------------
396 // Handle sub controls events
397 // ----------------------------------------------------------------------------
399 BEGIN_EVENT_TABLE(wxSpinCtrlGenericBase
, wxSpinCtrlBase
)
400 EVT_CHAR(wxSpinCtrlGenericBase::OnTextChar
)
401 EVT_KILL_FOCUS(wxSpinCtrlGenericBase::OnTextLostFocus
)
404 void wxSpinCtrlGenericBase::OnSpinButton(wxSpinEvent
& event
)
408 // Sync the textctrl since the user expects that the button will modify
409 // what they see in the textctrl.
412 int spin_value
= event
.GetPosition();
413 double step
= (event
.GetEventType() == wxEVT_SCROLL_LINEUP
) ? 1 : -1;
415 // Use the spinbutton's acceleration, if any, but not if wrapping around
416 if (((spin_value
>= 0) && (m_spin_value
>= 0)) || ((spin_value
<= 0) && (m_spin_value
<= 0)))
417 step
*= abs(spin_value
- m_spin_value
);
419 double value
= AdjustToFitInRange(m_value
+ step
*m_increment
);
421 // Ignore the edges when it wraps since the up/down event may be opposite
422 // They are in GTK and Mac
423 if (abs(spin_value
- m_spin_value
) > SPINCTRLBUT_MAX
)
425 m_spin_value
= spin_value
;
429 m_spin_value
= spin_value
;
431 if ( DoSetValue(value
) )
435 void wxSpinCtrlGenericBase::OnTextLostFocus(wxFocusEvent
& event
)
443 void wxSpinCtrlGenericBase::OnTextChar(wxKeyEvent
& event
)
445 if ( !HasFlag(wxSP_ARROW_KEYS
) )
451 double value
= m_value
;
452 switch ( event
.GetKeyCode() )
455 value
+= m_increment
;
459 value
-= m_increment
;
463 value
+= m_increment
* 10.0;
467 value
-= m_increment
* 10.0;
475 value
= AdjustToFitInRange(value
);
479 if ( DoSetValue(value
) )
483 // ----------------------------------------------------------------------------
484 // Textctrl functions
485 // ----------------------------------------------------------------------------
487 bool wxSpinCtrlGenericBase::SyncSpinToText()
489 if ( !m_textCtrl
|| !m_textCtrl
->IsModified() )
493 if ( DoTextToValue(m_textCtrl
->GetValue(), &textValue
) )
495 if (textValue
> m_max
)
497 else if (textValue
< m_min
)
500 else // text contents is not a valid number at all
502 // replace its contents with the last valid value
506 // we must always set the value here, even if it's equal to m_value, as
507 // otherwise we could be left with an out of range value when leaving the
508 // text control and the current value is already m_max for example
509 return DoSetValue(textValue
);
512 // ----------------------------------------------------------------------------
513 // changing value and range
514 // ----------------------------------------------------------------------------
516 void wxSpinCtrlGenericBase::SetValue(const wxString
& text
)
518 wxCHECK_RET( m_textCtrl
, wxT("invalid call to wxSpinCtrl::SetValue") );
521 if ( DoTextToValue(text
, &val
) && InRange(val
) )
525 else // not a number at all or out of range
527 m_textCtrl
->SetValue(text
);
528 m_textCtrl
->SelectAll();
532 bool wxSpinCtrlGenericBase::DoSetValue(double val
)
534 wxCHECK_MSG( m_textCtrl
, false, wxT("invalid call to wxSpinCtrl::SetValue") );
539 if ( m_snap_to_ticks
&& (m_increment
!= 0) )
541 double snap_value
= val
/ m_increment
;
543 if (wxFinite(snap_value
)) // FIXME what to do about a failure?
545 if ((snap_value
- floor(snap_value
)) < (ceil(snap_value
) - snap_value
))
546 val
= floor(snap_value
) * m_increment
;
548 val
= ceil(snap_value
) * m_increment
;
552 wxString
str(DoValueToText(val
));
554 if ((val
!= m_value
) || (str
!= m_textCtrl
->GetValue()))
556 if ( !DoTextToValue(str
, &m_value
) ) // wysiwyg for textctrl
558 m_textCtrl
->SetValue( str
);
559 m_textCtrl
->SelectAll();
560 m_textCtrl
->DiscardEdits();
567 double wxSpinCtrlGenericBase::AdjustToFitInRange(double value
) const
570 value
= HasFlag(wxSP_WRAP
) ? m_max
: m_min
;
572 value
= HasFlag(wxSP_WRAP
) ? m_min
: m_max
;
577 void wxSpinCtrlGenericBase::DoSetRange(double min
, double max
)
583 void wxSpinCtrlGenericBase::DoSetIncrement(double inc
)
588 void wxSpinCtrlGenericBase::SetSnapToTicks(bool snap_to_ticks
)
590 m_snap_to_ticks
= snap_to_ticks
;
594 void wxSpinCtrlGenericBase::SetSelection(long from
, long to
)
596 wxCHECK_RET( m_textCtrl
, wxT("invalid call to wxSpinCtrl::SetSelection") );
598 m_textCtrl
->SetSelection(from
, to
);
601 #ifndef wxHAS_NATIVE_SPINCTRL
603 //-----------------------------------------------------------------------------
605 //-----------------------------------------------------------------------------
607 bool wxSpinCtrl::SetBase(int base
)
609 // Currently we only support base 10 and 16. We could add support for base
610 // 8 quite easily but wxMSW doesn't support it natively so don't bother.
611 if ( base
!= 10 && base
!= 16 )
614 if ( base
== m_base
)
617 // Update the current control contents to show in the new base: be careful
618 // to call DoTextToValue() before changing the base...
620 const bool hasValidVal
= DoTextToValue(m_textCtrl
->GetValue(), &val
);
624 // ... but DoValueToText() after doing it.
626 m_textCtrl
->SetValue(DoValueToText(val
));
631 void wxSpinCtrl::DoSendEvent()
633 wxSpinEvent
event( wxEVT_SPINCTRL
, GetId());
634 event
.SetEventObject( this );
635 event
.SetPosition((int)(m_value
+ 0.5)); // FIXME should be SetValue
636 event
.SetString(m_textCtrl
->GetValue());
637 GetEventHandler()->ProcessEvent( event
);
640 bool wxSpinCtrl::DoTextToValue(const wxString
& text
, double *val
)
643 if ( !text
.ToLong(&lval
, GetBase()) )
646 *val
= static_cast<double>(lval
);
651 wxString
wxSpinCtrl::DoValueToText(double val
)
656 return wxPrivate::wxSpinCtrlFormatAsHex(static_cast<long>(val
),
660 wxFAIL_MSG( wxS("Unsupported spin control base") );
664 return wxString::Format("%ld", static_cast<long>(val
));
668 #endif // !wxHAS_NATIVE_SPINCTRL
670 //-----------------------------------------------------------------------------
672 //-----------------------------------------------------------------------------
674 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlDouble
, wxSpinCtrlGenericBase
)
676 void wxSpinCtrlDouble::DoSendEvent()
678 wxSpinDoubleEvent
event( wxEVT_SPINCTRLDOUBLE
, GetId());
679 event
.SetEventObject( this );
680 event
.SetValue(m_value
);
681 event
.SetString(m_textCtrl
->GetValue());
682 GetEventHandler()->ProcessEvent( event
);
685 bool wxSpinCtrlDouble::DoTextToValue(const wxString
& text
, double *val
)
687 return text
.ToDouble(val
);
690 wxString
wxSpinCtrlDouble::DoValueToText(double val
)
692 return wxString::Format(m_format
, val
);
695 void wxSpinCtrlDouble::SetDigits(unsigned digits
)
697 wxCHECK_RET( digits
<= 20, "too many digits for wxSpinCtrlDouble" );
699 if ( digits
== m_digits
)
704 m_format
.Printf(wxT("%%0.%ulf"), digits
);
709 #endif // wxUSE_SPINBTN
711 #endif // !wxPort-with-native-spinctrl
713 #endif // wxUSE_SPINCTRL