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"
35 IMPLEMENT_DYNAMIC_CLASS(wxSpinDoubleEvent
, wxNotifyEvent
)
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)
41 #include "wx/spinbutt.h"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 // The margin between the text control and the spin: the value here is the same
50 // as the margin between the spin button and its "buddy" text control in wxMSW
51 // so the generic control looks similarly to the native one there, we might
52 // need to use different value for the other platforms (and maybe even
53 // determine it dynamically?).
54 static const wxCoord MARGIN
= 1;
56 #define SPINCTRLBUT_MAX 32000 // large to avoid wrap around trouble
58 // ----------------------------------------------------------------------------
59 // wxSpinCtrlTextGeneric: text control used by spin control
60 // ----------------------------------------------------------------------------
62 class wxSpinCtrlTextGeneric
: public wxTextCtrl
65 wxSpinCtrlTextGeneric(wxSpinCtrlGenericBase
*spin
, const wxString
& value
, long style
=0)
66 : wxTextCtrl(spin
->GetParent(), wxID_ANY
, value
, wxDefaultPosition
, wxDefaultSize
,
67 ( style
& wxALIGN_MASK
) | wxTE_NOHIDESEL
| wxTE_PROCESS_ENTER
)
71 // remove the default minsize, the spinctrl will have one instead
72 SetSizeHints(wxDefaultCoord
, wxDefaultCoord
);
75 virtual ~wxSpinCtrlTextGeneric()
77 // MSW sends extra kill focus event on destroy
79 m_spin
->m_textCtrl
= NULL
;
84 void OnTextEnter(wxCommandEvent
& event
)
87 m_spin
->OnTextEnter(event
);
90 void OnChar( wxKeyEvent
&event
)
93 m_spin
->OnTextChar(event
);
96 void OnKillFocus(wxFocusEvent
& event
)
100 if ( m_spin
->SyncSpinToText() )
101 m_spin
->DoSendEvent();
107 wxSpinCtrlGenericBase
*m_spin
;
110 DECLARE_EVENT_TABLE()
113 BEGIN_EVENT_TABLE(wxSpinCtrlTextGeneric
, wxTextCtrl
)
114 EVT_TEXT_ENTER(wxID_ANY
, wxSpinCtrlTextGeneric::OnTextEnter
)
116 EVT_CHAR(wxSpinCtrlTextGeneric::OnChar
)
118 EVT_KILL_FOCUS(wxSpinCtrlTextGeneric::OnKillFocus
)
121 // ----------------------------------------------------------------------------
122 // wxSpinCtrlButtonGeneric: spin button used by spin control
123 // ----------------------------------------------------------------------------
125 class wxSpinCtrlButtonGeneric
: public wxSpinButton
128 wxSpinCtrlButtonGeneric(wxSpinCtrlGenericBase
*spin
, int style
)
129 : wxSpinButton(spin
->GetParent(), wxID_ANY
, wxDefaultPosition
,
130 wxDefaultSize
, style
| wxSP_VERTICAL
)
134 SetRange(-SPINCTRLBUT_MAX
, SPINCTRLBUT_MAX
);
136 // remove the default minsize, the spinctrl will have one instead
137 SetSizeHints(wxDefaultCoord
, wxDefaultCoord
);
140 void OnSpinButton(wxSpinEvent
& event
)
143 m_spin
->OnSpinButton(event
);
146 wxSpinCtrlGenericBase
*m_spin
;
149 DECLARE_EVENT_TABLE()
152 BEGIN_EVENT_TABLE(wxSpinCtrlButtonGeneric
, wxSpinButton
)
153 EVT_SPIN_UP( wxID_ANY
, wxSpinCtrlButtonGeneric::OnSpinButton
)
154 EVT_SPIN_DOWN(wxID_ANY
, wxSpinCtrlButtonGeneric::OnSpinButton
)
157 // ============================================================================
158 // wxSpinCtrlGenericBase
159 // ============================================================================
161 // ----------------------------------------------------------------------------
162 // wxSpinCtrlGenericBase creation
163 // ----------------------------------------------------------------------------
165 void wxSpinCtrlGenericBase::Init()
171 m_snap_to_ticks
= false;
172 m_format
= wxS("%g");
180 bool wxSpinCtrlGenericBase::Create(wxWindow
*parent
,
182 const wxString
& value
,
183 const wxPoint
& pos
, const wxSize
& size
,
185 double min
, double max
, double initial
,
187 const wxString
& name
)
189 // don't use borders for this control itself, it wouldn't look good with
190 // the text control borders (but we might want to use style border bits to
191 // select the text control style)
192 if ( !wxControl::Create(parent
, id
, wxDefaultPosition
, wxDefaultSize
,
193 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
,
194 wxDefaultValidator
, name
) )
202 m_increment
= increment
;
204 m_textCtrl
= new wxSpinCtrlTextGeneric(this, value
, style
);
205 m_spinButton
= new wxSpinCtrlButtonGeneric(this, style
);
207 m_spin_value
= m_spinButton
->GetValue();
209 // the string value overrides the numeric one (for backwards compatibility
210 // reasons and also because it is simpler to satisfy the string value which
211 // comes much sooner in the list of arguments and leave the initial
212 // parameter unspecified)
213 if ( !value
.empty() )
216 if ( value
.ToDouble(&d
) )
219 m_textCtrl
->SetValue(wxString::Format(m_format
, m_value
));
223 SetInitialSize(size
);
226 // have to disable this window to avoid interfering it with message
227 // processing to the text and the button... but pretend it is enabled to
228 // make IsEnabled() return true
229 wxControl::Enable(false); // don't use non virtual Disable() here!
232 // we don't even need to show this window itself - and not doing it avoids
233 // that it overwrites the text control
234 wxControl::Show(false);
239 wxSpinCtrlGenericBase::~wxSpinCtrlGenericBase()
241 // delete the controls now, don't leave them alive even though they would
242 // still be eventually deleted by our parent - but it will be too late, the
243 // user code expects them to be gone now
247 // null this since MSW sends KILL_FOCUS on deletion, see ~wxSpinCtrlTextGeneric
248 wxDynamicCast(m_textCtrl
, wxSpinCtrlTextGeneric
)->m_spin
= NULL
;
250 wxSpinCtrlTextGeneric
*text
= (wxSpinCtrlTextGeneric
*)m_textCtrl
;
255 wxDELETE(m_spinButton
);
258 // ----------------------------------------------------------------------------
260 // ----------------------------------------------------------------------------
262 wxSize
wxSpinCtrlGenericBase::DoGetBestSize() const
264 wxSize sizeBtn
= m_spinButton
->GetBestSize(),
265 sizeText
= m_textCtrl
->GetBestSize();
267 return wxSize(sizeBtn
.x
+ sizeText
.x
+ MARGIN
, sizeText
.y
);
270 void wxSpinCtrlGenericBase::DoMoveWindow(int x
, int y
, int width
, int height
)
272 wxControl::DoMoveWindow(x
, y
, width
, height
);
274 // position the subcontrols inside the client area
275 wxSize sizeBtn
= m_spinButton
->GetSize();
277 wxCoord wText
= width
- sizeBtn
.x
- MARGIN
;
278 m_textCtrl
->SetSize(x
, y
, wText
, height
);
279 m_spinButton
->SetSize(x
+ wText
+ MARGIN
, y
, wxDefaultCoord
, height
);
282 // ----------------------------------------------------------------------------
283 // operations forwarded to the subcontrols
284 // ----------------------------------------------------------------------------
286 void wxSpinCtrlGenericBase::SetFocus()
289 m_textCtrl
->SetFocus();
292 bool wxSpinCtrlGenericBase::Enable(bool enable
)
294 // Notice that we never enable this control itself, it must stay disabled
295 // to avoid interfering with the siblings event handling (see e.g. #12045
296 // for the kind of problems which arise otherwise).
297 if ( enable
== m_isEnabled
)
300 m_isEnabled
= enable
;
302 m_spinButton
->Enable(enable
);
303 m_textCtrl
->Enable(enable
);
308 bool wxSpinCtrlGenericBase::Show(bool show
)
310 if ( !wxControl::Show(show
) )
313 // under GTK Show() is called the first time before we are fully
317 m_spinButton
->Show(show
);
318 m_textCtrl
->Show(show
);
324 bool wxSpinCtrlGenericBase::Reparent(wxWindowBase
*newParent
)
328 m_spinButton
->Reparent(newParent
);
329 m_textCtrl
->Reparent(newParent
);
335 // ----------------------------------------------------------------------------
336 // Handle sub controls events
337 // ----------------------------------------------------------------------------
339 void wxSpinCtrlGenericBase::OnSpinButton(wxSpinEvent
& event
)
343 // Sync the textctrl since the user expects that the button will modify
344 // what they see in the textctrl.
347 int spin_value
= event
.GetPosition();
348 double step
= (event
.GetEventType() == wxEVT_SCROLL_LINEUP
) ? 1 : -1;
350 // Use the spinbutton's acceleration, if any, but not if wrapping around
351 if (((spin_value
>= 0) && (m_spin_value
>= 0)) || ((spin_value
<= 0) && (m_spin_value
<= 0)))
352 step
*= abs(spin_value
- m_spin_value
);
354 double value
= AdjustToFitInRange(m_value
+ step
*m_increment
);
356 // Ignore the edges when it wraps since the up/down event may be opposite
357 // They are in GTK and Mac
358 if (abs(spin_value
- m_spin_value
) > SPINCTRLBUT_MAX
)
360 m_spin_value
= spin_value
;
364 m_spin_value
= spin_value
;
366 if ( DoSetValue(value
) )
370 void wxSpinCtrlGenericBase::OnTextEnter(wxCommandEvent
& event
)
377 void wxSpinCtrlGenericBase::OnTextChar(wxKeyEvent
& event
)
379 if ( !HasFlag(wxSP_ARROW_KEYS
) )
385 double value
= m_value
;
386 switch ( event
.GetKeyCode() )
389 value
+= m_increment
;
393 value
-= m_increment
;
397 value
+= m_increment
* 10.0;
401 value
-= m_increment
* 10.0;
409 value
= AdjustToFitInRange(value
);
413 if ( DoSetValue(value
) )
417 // ----------------------------------------------------------------------------
418 // Textctrl functions
419 // ----------------------------------------------------------------------------
421 bool wxSpinCtrlGenericBase::SyncSpinToText()
423 if ( !m_textCtrl
|| !m_textCtrl
->IsModified() )
427 if ( m_textCtrl
->GetValue().ToDouble(&textValue
) )
429 if (textValue
> m_max
)
431 else if (textValue
< m_min
)
434 else // text contents is not a valid number at all
436 // replace its contents with the last valid value
440 // we must always set the value here, even if it's equal to m_value, as
441 // otherwise we could be left with an out of range value when leaving the
442 // text control and the current value is already m_max for example
443 return DoSetValue(textValue
);
446 // ----------------------------------------------------------------------------
447 // changing value and range
448 // ----------------------------------------------------------------------------
450 void wxSpinCtrlGenericBase::SetValue(const wxString
& text
)
452 wxCHECK_RET( m_textCtrl
, wxT("invalid call to wxSpinCtrl::SetValue") );
455 if ( text
.ToDouble(&val
) && InRange(val
) )
459 else // not a number at all or out of range
461 m_textCtrl
->SetValue(text
);
462 m_textCtrl
->SetSelection(0, -1);
463 m_textCtrl
->SetInsertionPointEnd();
467 bool wxSpinCtrlGenericBase::DoSetValue(double val
)
469 wxCHECK_MSG( m_textCtrl
, false, wxT("invalid call to wxSpinCtrl::SetValue") );
474 if ( m_snap_to_ticks
&& (m_increment
!= 0) )
476 double snap_value
= val
/ m_increment
;
478 if (wxFinite(snap_value
)) // FIXME what to do about a failure?
480 if ((snap_value
- floor(snap_value
)) < (ceil(snap_value
) - snap_value
))
481 val
= floor(snap_value
) * m_increment
;
483 val
= ceil(snap_value
) * m_increment
;
487 wxString
str(wxString::Format(m_format
.c_str(), val
));
489 if ((val
!= m_value
) || (str
!= m_textCtrl
->GetValue()))
492 str
.ToDouble( &m_value
); // wysiwyg for textctrl
493 m_textCtrl
->SetValue( str
);
494 m_textCtrl
->SetInsertionPointEnd();
495 m_textCtrl
->DiscardEdits();
502 double wxSpinCtrlGenericBase::AdjustToFitInRange(double value
) const
505 value
= HasFlag(wxSP_WRAP
) ? m_max
: m_min
;
507 value
= HasFlag(wxSP_WRAP
) ? m_min
: m_max
;
512 void wxSpinCtrlGenericBase::DoSetRange(double min
, double max
)
518 void wxSpinCtrlGenericBase::DoSetIncrement(double inc
)
523 void wxSpinCtrlGenericBase::SetSnapToTicks(bool snap_to_ticks
)
525 m_snap_to_ticks
= snap_to_ticks
;
529 void wxSpinCtrlGenericBase::SetSelection(long from
, long to
)
531 wxCHECK_RET( m_textCtrl
, wxT("invalid call to wxSpinCtrl::SetSelection") );
533 m_textCtrl
->SetSelection(from
, to
);
536 #ifndef wxHAS_NATIVE_SPINCTRL
538 //-----------------------------------------------------------------------------
540 //-----------------------------------------------------------------------------
542 void wxSpinCtrl::DoSendEvent()
544 wxSpinEvent
event( wxEVT_COMMAND_SPINCTRL_UPDATED
, GetId());
545 event
.SetEventObject( this );
546 event
.SetPosition((int)(m_value
+ 0.5)); // FIXME should be SetValue
547 event
.SetString(m_textCtrl
->GetValue());
548 GetEventHandler()->ProcessEvent( event
);
551 #endif // !wxHAS_NATIVE_SPINCTRL
553 //-----------------------------------------------------------------------------
555 //-----------------------------------------------------------------------------
557 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlDouble
, wxSpinCtrlGenericBase
)
559 void wxSpinCtrlDouble::DoSendEvent()
561 wxSpinDoubleEvent
event( wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED
, GetId());
562 event
.SetEventObject( this );
563 event
.SetValue(m_value
);
564 event
.SetString(m_textCtrl
->GetValue());
565 GetEventHandler()->ProcessEvent( event
);
568 void wxSpinCtrlDouble::SetDigits(unsigned digits
)
570 wxCHECK_RET( digits
<= 20, "too many digits for wxSpinCtrlDouble" );
572 if ( digits
== m_digits
)
577 m_format
.Printf(wxT("%%0.%ulf"), digits
);
582 #endif // wxUSE_SPINBTN
584 #endif // !wxPort-with-native-spinctrl
586 #endif // wxUSE_SPINCTRL