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
->GetParent(), wxID_ANY
, value
, wxDefaultPosition
, wxDefaultSize
,
72 // remove the default minsize, the spinctrl will have one instead
73 SetSizeHints(wxDefaultCoord
, wxDefaultCoord
);
76 virtual ~wxSpinCtrlTextGeneric()
78 // MSW sends extra kill focus event on destroy
80 m_spin
->m_textCtrl
= NULL
;
85 void OnChar( wxKeyEvent
&event
)
88 m_spin
->ProcessWindowEvent(event
);
91 void OnKillFocus(wxFocusEvent
& event
)
94 m_spin
->ProcessWindowEvent(event
);
99 wxSpinCtrlGenericBase
*m_spin
;
102 DECLARE_EVENT_TABLE()
105 BEGIN_EVENT_TABLE(wxSpinCtrlTextGeneric
, wxTextCtrl
)
106 EVT_CHAR(wxSpinCtrlTextGeneric::OnChar
)
108 EVT_KILL_FOCUS(wxSpinCtrlTextGeneric::OnKillFocus
)
111 // ----------------------------------------------------------------------------
112 // wxSpinCtrlButtonGeneric: spin button used by spin control
113 // ----------------------------------------------------------------------------
115 class wxSpinCtrlButtonGeneric
: public wxSpinButton
118 wxSpinCtrlButtonGeneric(wxSpinCtrlGenericBase
*spin
, int style
)
119 : wxSpinButton(spin
->GetParent(), wxID_ANY
, wxDefaultPosition
,
120 wxDefaultSize
, style
| wxSP_VERTICAL
)
124 SetRange(-SPINCTRLBUT_MAX
, SPINCTRLBUT_MAX
);
126 // remove the default minsize, the spinctrl will have one instead
127 SetSizeHints(wxDefaultCoord
, wxDefaultCoord
);
130 void OnSpinButton(wxSpinEvent
& event
)
133 m_spin
->OnSpinButton(event
);
136 wxSpinCtrlGenericBase
*m_spin
;
139 DECLARE_EVENT_TABLE()
142 BEGIN_EVENT_TABLE(wxSpinCtrlButtonGeneric
, wxSpinButton
)
143 EVT_SPIN_UP( wxID_ANY
, wxSpinCtrlButtonGeneric::OnSpinButton
)
144 EVT_SPIN_DOWN(wxID_ANY
, wxSpinCtrlButtonGeneric::OnSpinButton
)
147 // ============================================================================
148 // wxSpinCtrlGenericBase
149 // ============================================================================
151 // ----------------------------------------------------------------------------
152 // wxSpinCtrlGenericBase creation
153 // ----------------------------------------------------------------------------
155 void wxSpinCtrlGenericBase::Init()
161 m_snap_to_ticks
= false;
162 m_format
= wxS("%g");
170 bool wxSpinCtrlGenericBase::Create(wxWindow
*parent
,
172 const wxString
& value
,
173 const wxPoint
& pos
, const wxSize
& size
,
175 double min
, double max
, double initial
,
177 const wxString
& name
)
179 // don't use borders for this control itself, it wouldn't look good with
180 // the text control borders (but we might want to use style border bits to
181 // select the text control style)
182 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
,
183 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
,
184 wxDefaultValidator
, name
) )
192 m_increment
= increment
;
194 m_textCtrl
= new wxSpinCtrlTextGeneric(this, value
, style
);
195 m_spinButton
= new wxSpinCtrlButtonGeneric(this, style
);
197 m_textCtrl
->SetToolTip(GetToolTipText());
198 m_spinButton
->SetToolTip(GetToolTipText());
199 #endif // wxUSE_TOOLTIPS
201 m_spin_value
= m_spinButton
->GetValue();
203 // the string value overrides the numeric one (for backwards compatibility
204 // reasons and also because it is simpler to satisfy the string value which
205 // comes much sooner in the list of arguments and leave the initial
206 // parameter unspecified)
207 if ( !value
.empty() )
210 if ( value
.ToDouble(&d
) )
213 m_textCtrl
->SetValue(wxString::Format(m_format
, m_value
));
217 SetInitialSize(size
);
220 // have to disable this window to avoid interfering it with message
221 // processing to the text and the button... but pretend it is enabled to
222 // make IsEnabled() return true
223 wxControl::Enable(false); // don't use non virtual Disable() here!
226 // we don't even need to show this window itself - and not doing it avoids
227 // that it overwrites the text control
228 wxControl::Show(false);
233 wxSpinCtrlGenericBase::~wxSpinCtrlGenericBase()
235 // delete the controls now, don't leave them alive even though they would
236 // still be eventually deleted by our parent - but it will be too late, the
237 // user code expects them to be gone now
241 // null this since MSW sends KILL_FOCUS on deletion, see ~wxSpinCtrlTextGeneric
242 wxDynamicCast(m_textCtrl
, wxSpinCtrlTextGeneric
)->m_spin
= NULL
;
244 wxSpinCtrlTextGeneric
*text
= (wxSpinCtrlTextGeneric
*)m_textCtrl
;
249 wxDELETE(m_spinButton
);
252 // ----------------------------------------------------------------------------
254 // ----------------------------------------------------------------------------
256 wxSize
wxSpinCtrlGenericBase::DoGetBestSize() const
258 wxSize sizeBtn
= m_spinButton
->GetBestSize(),
259 sizeText
= m_textCtrl
->GetBestSize();
261 return wxSize(sizeBtn
.x
+ sizeText
.x
+ MARGIN
, sizeText
.y
);
264 void wxSpinCtrlGenericBase::DoMoveWindow(int x
, int y
, int width
, int height
)
266 wxControl::DoMoveWindow(x
, y
, width
, height
);
268 // position the subcontrols inside the client area
269 wxSize sizeBtn
= m_spinButton
->GetSize();
271 wxCoord wText
= width
- sizeBtn
.x
- MARGIN
;
272 m_textCtrl
->SetSize(x
, y
, wText
, height
);
273 m_spinButton
->SetSize(x
+ wText
+ MARGIN
, y
, wxDefaultCoord
, height
);
276 // ----------------------------------------------------------------------------
277 // operations forwarded to the subcontrols
278 // ----------------------------------------------------------------------------
280 void wxSpinCtrlGenericBase::SetFocus()
283 m_textCtrl
->SetFocus();
288 void wxSpinCtrlGenericBase::DoEnable(bool enable
)
290 // We never enable this control itself, it must stay disabled to avoid
291 // interfering with the siblings event handling (see e.g. #12045 for the
292 // kind of problems which arise otherwise).
294 wxSpinCtrlBase::DoEnable(enable
);
299 bool wxSpinCtrlGenericBase::Enable(bool enable
)
301 if ( !wxSpinCtrlBase::Enable(enable
) )
304 m_spinButton
->Enable(enable
);
305 m_textCtrl
->Enable(enable
);
310 bool wxSpinCtrlGenericBase::Show(bool show
)
312 if ( !wxControl::Show(show
) )
315 // under GTK Show() is called the first time before we are fully
319 m_spinButton
->Show(show
);
320 m_textCtrl
->Show(show
);
327 void wxSpinCtrlGenericBase::DoSetToolTip(wxToolTip
*tip
)
329 // Notice that we must check for the subcontrols not being NULL (as they
330 // could be if we were created with the default ctor and this is called
331 // before Create() for some reason) and that we can't call SetToolTip(tip)
332 // because this would take ownership of the wxToolTip object (twice).
336 m_textCtrl
->SetToolTip(tip
->GetTip());
338 m_textCtrl
->SetToolTip(NULL
);
344 m_spinButton
->SetToolTip(tip
->GetTip());
346 m_spinButton
->SetToolTip(NULL
);
349 wxWindowBase::DoSetToolTip(tip
);
351 #endif // wxUSE_TOOLTIPS
353 // ----------------------------------------------------------------------------
354 // Handle sub controls events
355 // ----------------------------------------------------------------------------
357 BEGIN_EVENT_TABLE(wxSpinCtrlGenericBase
, wxSpinCtrlBase
)
358 EVT_CHAR(wxSpinCtrlGenericBase::OnTextChar
)
359 EVT_KILL_FOCUS(wxSpinCtrlGenericBase::OnTextLostFocus
)
362 void wxSpinCtrlGenericBase::OnSpinButton(wxSpinEvent
& event
)
366 // Sync the textctrl since the user expects that the button will modify
367 // what they see in the textctrl.
370 int spin_value
= event
.GetPosition();
371 double step
= (event
.GetEventType() == wxEVT_SCROLL_LINEUP
) ? 1 : -1;
373 // Use the spinbutton's acceleration, if any, but not if wrapping around
374 if (((spin_value
>= 0) && (m_spin_value
>= 0)) || ((spin_value
<= 0) && (m_spin_value
<= 0)))
375 step
*= abs(spin_value
- m_spin_value
);
377 double value
= AdjustToFitInRange(m_value
+ step
*m_increment
);
379 // Ignore the edges when it wraps since the up/down event may be opposite
380 // They are in GTK and Mac
381 if (abs(spin_value
- m_spin_value
) > SPINCTRLBUT_MAX
)
383 m_spin_value
= spin_value
;
387 m_spin_value
= spin_value
;
389 if ( DoSetValue(value
) )
393 void wxSpinCtrlGenericBase::OnTextLostFocus(wxFocusEvent
& event
)
401 void wxSpinCtrlGenericBase::OnTextChar(wxKeyEvent
& event
)
403 if ( !HasFlag(wxSP_ARROW_KEYS
) )
409 double value
= m_value
;
410 switch ( event
.GetKeyCode() )
413 value
+= m_increment
;
417 value
-= m_increment
;
421 value
+= m_increment
* 10.0;
425 value
-= m_increment
* 10.0;
433 value
= AdjustToFitInRange(value
);
437 if ( DoSetValue(value
) )
441 // ----------------------------------------------------------------------------
442 // Textctrl functions
443 // ----------------------------------------------------------------------------
445 bool wxSpinCtrlGenericBase::SyncSpinToText()
447 if ( !m_textCtrl
|| !m_textCtrl
->IsModified() )
451 if ( m_textCtrl
->GetValue().ToDouble(&textValue
) )
453 if (textValue
> m_max
)
455 else if (textValue
< m_min
)
458 else // text contents is not a valid number at all
460 // replace its contents with the last valid value
464 // we must always set the value here, even if it's equal to m_value, as
465 // otherwise we could be left with an out of range value when leaving the
466 // text control and the current value is already m_max for example
467 return DoSetValue(textValue
);
470 // ----------------------------------------------------------------------------
471 // changing value and range
472 // ----------------------------------------------------------------------------
474 void wxSpinCtrlGenericBase::SetValue(const wxString
& text
)
476 wxCHECK_RET( m_textCtrl
, wxT("invalid call to wxSpinCtrl::SetValue") );
479 if ( text
.ToDouble(&val
) && InRange(val
) )
483 else // not a number at all or out of range
485 m_textCtrl
->SetValue(text
);
486 m_textCtrl
->SetSelection(0, -1);
487 m_textCtrl
->SetInsertionPointEnd();
491 bool wxSpinCtrlGenericBase::DoSetValue(double val
)
493 wxCHECK_MSG( m_textCtrl
, false, wxT("invalid call to wxSpinCtrl::SetValue") );
498 if ( m_snap_to_ticks
&& (m_increment
!= 0) )
500 double snap_value
= val
/ m_increment
;
502 if (wxFinite(snap_value
)) // FIXME what to do about a failure?
504 if ((snap_value
- floor(snap_value
)) < (ceil(snap_value
) - snap_value
))
505 val
= floor(snap_value
) * m_increment
;
507 val
= ceil(snap_value
) * m_increment
;
511 wxString
str(wxString::Format(m_format
.c_str(), val
));
513 if ((val
!= m_value
) || (str
!= m_textCtrl
->GetValue()))
516 str
.ToDouble( &m_value
); // wysiwyg for textctrl
517 m_textCtrl
->SetValue( str
);
518 m_textCtrl
->SetInsertionPointEnd();
519 m_textCtrl
->DiscardEdits();
526 double wxSpinCtrlGenericBase::AdjustToFitInRange(double value
) const
529 value
= HasFlag(wxSP_WRAP
) ? m_max
: m_min
;
531 value
= HasFlag(wxSP_WRAP
) ? m_min
: m_max
;
536 void wxSpinCtrlGenericBase::DoSetRange(double min
, double max
)
542 void wxSpinCtrlGenericBase::DoSetIncrement(double inc
)
547 void wxSpinCtrlGenericBase::SetSnapToTicks(bool snap_to_ticks
)
549 m_snap_to_ticks
= snap_to_ticks
;
553 void wxSpinCtrlGenericBase::SetSelection(long from
, long to
)
555 wxCHECK_RET( m_textCtrl
, wxT("invalid call to wxSpinCtrl::SetSelection") );
557 m_textCtrl
->SetSelection(from
, to
);
560 #ifndef wxHAS_NATIVE_SPINCTRL
562 //-----------------------------------------------------------------------------
564 //-----------------------------------------------------------------------------
566 void wxSpinCtrl::DoSendEvent()
568 wxSpinEvent
event( wxEVT_COMMAND_SPINCTRL_UPDATED
, GetId());
569 event
.SetEventObject( this );
570 event
.SetPosition((int)(m_value
+ 0.5)); // FIXME should be SetValue
571 event
.SetString(m_textCtrl
->GetValue());
572 GetEventHandler()->ProcessEvent( event
);
575 #endif // !wxHAS_NATIVE_SPINCTRL
577 //-----------------------------------------------------------------------------
579 //-----------------------------------------------------------------------------
581 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlDouble
, wxSpinCtrlGenericBase
)
583 void wxSpinCtrlDouble::DoSendEvent()
585 wxSpinDoubleEvent
event( wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED
, GetId());
586 event
.SetEventObject( this );
587 event
.SetValue(m_value
);
588 event
.SetString(m_textCtrl
->GetValue());
589 GetEventHandler()->ProcessEvent( event
);
592 void wxSpinCtrlDouble::SetDigits(unsigned digits
)
594 wxCHECK_RET( digits
<= 20, "too many digits for wxSpinCtrlDouble" );
596 if ( digits
== m_digits
)
601 m_format
.Printf(wxT("%%0.%ulf"), digits
);
606 #endif // wxUSE_SPINBTN
608 #endif // !wxPort-with-native-spinctrl
610 #endif // wxUSE_SPINCTRL