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
50 static const wxCoord MARGIN
= 2;
52 #define SPINCTRLBUT_MAX 32000 // large to avoid wrap around trouble
54 // ----------------------------------------------------------------------------
55 // wxSpinCtrlTextGeneric: text control used by spin control
56 // ----------------------------------------------------------------------------
58 class wxSpinCtrlTextGeneric
: public wxTextCtrl
61 wxSpinCtrlTextGeneric(wxSpinCtrlGenericBase
*spin
, const wxString
& value
, long style
=0)
62 : wxTextCtrl(spin
->GetParent(), wxID_ANY
, value
, wxDefaultPosition
, wxDefaultSize
,
63 ( style
& wxALIGN_MASK
) | wxTE_NOHIDESEL
| wxTE_PROCESS_ENTER
)
67 // remove the default minsize, the spinctrl will have one instead
68 SetSizeHints(wxDefaultCoord
, wxDefaultCoord
);
71 virtual ~wxSpinCtrlTextGeneric()
73 // MSW sends extra kill focus event on destroy
75 m_spin
->m_textCtrl
= NULL
;
80 void OnTextEnter(wxCommandEvent
& event
)
83 m_spin
->OnTextEnter(event
);
86 void OnChar( wxKeyEvent
&event
)
89 m_spin
->OnTextChar(event
);
92 void OnKillFocus(wxFocusEvent
& event
)
96 if ( m_spin
->SyncSpinToText() )
97 m_spin
->DoSendEvent();
103 wxSpinCtrlGenericBase
*m_spin
;
106 DECLARE_EVENT_TABLE()
109 BEGIN_EVENT_TABLE(wxSpinCtrlTextGeneric
, wxTextCtrl
)
110 EVT_TEXT_ENTER(wxID_ANY
, wxSpinCtrlTextGeneric::OnTextEnter
)
112 EVT_CHAR(wxSpinCtrlTextGeneric::OnChar
)
114 EVT_KILL_FOCUS(wxSpinCtrlTextGeneric::OnKillFocus
)
117 // ----------------------------------------------------------------------------
118 // wxSpinCtrlButtonGeneric: spin button used by spin control
119 // ----------------------------------------------------------------------------
121 class wxSpinCtrlButtonGeneric
: public wxSpinButton
124 wxSpinCtrlButtonGeneric(wxSpinCtrlGenericBase
*spin
, int style
)
125 : wxSpinButton(spin
->GetParent(), wxID_ANY
, wxDefaultPosition
,
126 wxDefaultSize
, style
| wxSP_VERTICAL
)
130 SetRange(-SPINCTRLBUT_MAX
, SPINCTRLBUT_MAX
);
132 // remove the default minsize, the spinctrl will have one instead
133 SetSizeHints(wxDefaultCoord
, wxDefaultCoord
);
136 void OnSpinButton(wxSpinEvent
& event
)
139 m_spin
->OnSpinButton(event
);
142 wxSpinCtrlGenericBase
*m_spin
;
145 DECLARE_EVENT_TABLE()
148 BEGIN_EVENT_TABLE(wxSpinCtrlButtonGeneric
, wxSpinButton
)
149 EVT_SPIN_UP( wxID_ANY
, wxSpinCtrlButtonGeneric::OnSpinButton
)
150 EVT_SPIN_DOWN(wxID_ANY
, wxSpinCtrlButtonGeneric::OnSpinButton
)
153 // ============================================================================
154 // wxSpinCtrlGenericBase
155 // ============================================================================
157 // ----------------------------------------------------------------------------
158 // wxSpinCtrlGenericBase creation
159 // ----------------------------------------------------------------------------
161 void wxSpinCtrlGenericBase::Init()
167 m_snap_to_ticks
= false;
168 m_format
= wxS("%g");
176 bool wxSpinCtrlGenericBase::Create(wxWindow
*parent
,
178 const wxString
& value
,
179 const wxPoint
& pos
, const wxSize
& size
,
181 double min
, double max
, double initial
,
183 const wxString
& name
)
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 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
,
190 wxDefaultValidator
, name
) )
198 m_increment
= increment
;
200 m_textCtrl
= new wxSpinCtrlTextGeneric(this, value
, style
);
201 m_spinButton
= new wxSpinCtrlButtonGeneric(this, style
);
203 m_spin_value
= m_spinButton
->GetValue();
205 // the string value overrides the numeric one (for backwards compatibility
206 // reasons and also because it is simpler to satisfy the string value which
207 // comes much sooner in the list of arguments and leave the initial
208 // parameter unspecified)
209 if ( !value
.empty() )
212 if ( value
.ToDouble(&d
) )
215 m_textCtrl
->SetValue(wxString::Format(m_format
, m_value
));
219 SetInitialSize(size
);
222 // have to disable this window to avoid interfering it with message
223 // processing to the text and the button... but pretend it is enabled to
224 // make IsEnabled() return true
225 wxControl::Enable(false); // don't use non virtual Disable() here!
228 // we don't even need to show this window itself - and not doing it avoids
229 // that it overwrites the text control
230 wxControl::Show(false);
235 wxSpinCtrlGenericBase::~wxSpinCtrlGenericBase()
237 // delete the controls now, don't leave them alive even though they would
238 // still be eventually deleted by our parent - but it will be too late, the
239 // user code expects them to be gone now
243 // null this since MSW sends KILL_FOCUS on deletion, see ~wxSpinCtrlTextGeneric
244 wxDynamicCast(m_textCtrl
, wxSpinCtrlTextGeneric
)->m_spin
= NULL
;
246 wxSpinCtrlTextGeneric
*text
= (wxSpinCtrlTextGeneric
*)m_textCtrl
;
251 wxDELETE(m_spinButton
);
254 // ----------------------------------------------------------------------------
256 // ----------------------------------------------------------------------------
258 wxSize
wxSpinCtrlGenericBase::DoGetBestSize() const
260 wxSize sizeBtn
= m_spinButton
->GetBestSize(),
261 sizeText
= m_textCtrl
->GetBestSize();
263 return wxSize(sizeBtn
.x
+ sizeText
.x
+ MARGIN
, sizeText
.y
);
266 void wxSpinCtrlGenericBase::DoMoveWindow(int x
, int y
, int width
, int height
)
268 wxControl::DoMoveWindow(x
, y
, width
, height
);
270 // position the subcontrols inside the client area
271 wxSize sizeBtn
= m_spinButton
->GetSize();
273 wxCoord wText
= width
- sizeBtn
.x
;
274 m_textCtrl
->SetSize(x
, y
, wText
, height
);
275 m_spinButton
->SetSize(x
+ wText
+ MARGIN
, y
, wxDefaultCoord
, height
);
278 // ----------------------------------------------------------------------------
279 // operations forwarded to the subcontrols
280 // ----------------------------------------------------------------------------
282 void wxSpinCtrlGenericBase::SetFocus()
285 m_textCtrl
->SetFocus();
288 bool wxSpinCtrlGenericBase::Enable(bool enable
)
290 // Notice that we never enable this control itself, it must stay disabled
291 // to avoid interfering with the siblings event handling (see e.g. #12045
292 // for the kind of problems which arise otherwise).
293 if ( enable
== m_isEnabled
)
296 m_isEnabled
= enable
;
298 m_spinButton
->Enable(enable
);
299 m_textCtrl
->Enable(enable
);
304 bool wxSpinCtrlGenericBase::Show(bool show
)
306 if ( !wxControl::Show(show
) )
309 // under GTK Show() is called the first time before we are fully
313 m_spinButton
->Show(show
);
314 m_textCtrl
->Show(show
);
320 bool wxSpinCtrlGenericBase::Reparent(wxWindowBase
*newParent
)
324 m_spinButton
->Reparent(newParent
);
325 m_textCtrl
->Reparent(newParent
);
331 // ----------------------------------------------------------------------------
332 // Handle sub controls events
333 // ----------------------------------------------------------------------------
335 void wxSpinCtrlGenericBase::OnSpinButton(wxSpinEvent
& event
)
339 // Sync the textctrl since the user expects that the button will modify
340 // what they see in the textctrl.
343 int spin_value
= event
.GetPosition();
344 double step
= (event
.GetEventType() == wxEVT_SCROLL_LINEUP
) ? 1 : -1;
346 // Use the spinbutton's acceleration, if any, but not if wrapping around
347 if (((spin_value
>= 0) && (m_spin_value
>= 0)) || ((spin_value
<= 0) && (m_spin_value
<= 0)))
348 step
*= abs(spin_value
- m_spin_value
);
350 double value
= AdjustToFitInRange(m_value
+ step
*m_increment
);
352 // Ignore the edges when it wraps since the up/down event may be opposite
353 // They are in GTK and Mac
354 if (abs(spin_value
- m_spin_value
) > SPINCTRLBUT_MAX
)
356 m_spin_value
= spin_value
;
360 m_spin_value
= spin_value
;
362 if ( DoSetValue(value
) )
366 void wxSpinCtrlGenericBase::OnTextEnter(wxCommandEvent
& event
)
373 void wxSpinCtrlGenericBase::OnTextChar(wxKeyEvent
& event
)
375 if ( !HasFlag(wxSP_ARROW_KEYS
) )
381 double value
= m_value
;
382 switch ( event
.GetKeyCode() )
385 value
+= m_increment
;
389 value
-= m_increment
;
393 value
+= m_increment
* 10.0;
397 value
-= m_increment
* 10.0;
405 value
= AdjustToFitInRange(value
);
409 if ( DoSetValue(value
) )
413 // ----------------------------------------------------------------------------
414 // Textctrl functions
415 // ----------------------------------------------------------------------------
417 bool wxSpinCtrlGenericBase::SyncSpinToText()
419 if ( !m_textCtrl
|| !m_textCtrl
->IsModified() )
423 if ( m_textCtrl
->GetValue().ToDouble(&textValue
) )
425 if (textValue
> m_max
)
427 else if (textValue
< m_min
)
430 else // text contents is not a valid number at all
432 // replace its contents with the last valid value
436 // we must always set the value here, even if it's equal to m_value, as
437 // otherwise we could be left with an out of range value when leaving the
438 // text control and the current value is already m_max for example
439 return DoSetValue(textValue
);
442 // ----------------------------------------------------------------------------
443 // changing value and range
444 // ----------------------------------------------------------------------------
446 void wxSpinCtrlGenericBase::SetValue(const wxString
& text
)
448 wxCHECK_RET( m_textCtrl
, wxT("invalid call to wxSpinCtrl::SetValue") );
451 if ( text
.ToDouble(&val
) && InRange(val
) )
455 else // not a number at all or out of range
457 m_textCtrl
->SetValue(text
);
458 m_textCtrl
->SetSelection(0, -1);
459 m_textCtrl
->SetInsertionPointEnd();
463 bool wxSpinCtrlGenericBase::DoSetValue(double val
)
465 wxCHECK_MSG( m_textCtrl
, false, wxT("invalid call to wxSpinCtrl::SetValue") );
470 if ( m_snap_to_ticks
&& (m_increment
!= 0) )
472 double snap_value
= val
/ m_increment
;
474 if (wxFinite(snap_value
)) // FIXME what to do about a failure?
476 if ((snap_value
- floor(snap_value
)) < (ceil(snap_value
) - snap_value
))
477 val
= floor(snap_value
) * m_increment
;
479 val
= ceil(snap_value
) * m_increment
;
483 wxString
str(wxString::Format(m_format
.c_str(), val
));
485 if ((val
!= m_value
) || (str
!= m_textCtrl
->GetValue()))
488 str
.ToDouble( &m_value
); // wysiwyg for textctrl
489 m_textCtrl
->SetValue( str
);
490 m_textCtrl
->SetInsertionPointEnd();
491 m_textCtrl
->DiscardEdits();
498 double wxSpinCtrlGenericBase::AdjustToFitInRange(double value
) const
501 value
= HasFlag(wxSP_WRAP
) ? m_max
: m_min
;
503 value
= HasFlag(wxSP_WRAP
) ? m_min
: m_max
;
508 void wxSpinCtrlGenericBase::DoSetRange(double min
, double max
)
514 void wxSpinCtrlGenericBase::DoSetIncrement(double inc
)
519 void wxSpinCtrlGenericBase::SetSnapToTicks(bool snap_to_ticks
)
521 m_snap_to_ticks
= snap_to_ticks
;
525 void wxSpinCtrlGenericBase::SetSelection(long from
, long to
)
527 wxCHECK_RET( m_textCtrl
, wxT("invalid call to wxSpinCtrl::SetSelection") );
529 m_textCtrl
->SetSelection(from
, to
);
532 #ifndef wxHAS_NATIVE_SPINCTRL
534 //-----------------------------------------------------------------------------
536 //-----------------------------------------------------------------------------
538 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
, wxSpinCtrlGenericBase
)
540 void wxSpinCtrl::DoSendEvent()
542 wxSpinEvent
event( wxEVT_COMMAND_SPINCTRL_UPDATED
, GetId());
543 event
.SetEventObject( this );
544 event
.SetPosition((int)(m_value
+ 0.5)); // FIXME should be SetValue
545 event
.SetString(m_textCtrl
->GetValue());
546 GetEventHandler()->ProcessEvent( event
);
549 #endif // !wxHAS_NATIVE_SPINCTRL
551 //-----------------------------------------------------------------------------
553 //-----------------------------------------------------------------------------
555 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlDouble
, wxSpinCtrlGenericBase
)
557 void wxSpinCtrlDouble::DoSendEvent()
559 wxSpinDoubleEvent
event( wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED
, GetId());
560 event
.SetEventObject( this );
561 event
.SetValue(m_value
);
562 event
.SetString(m_textCtrl
->GetValue());
563 GetEventHandler()->ProcessEvent( event
);
566 void wxSpinCtrlDouble::SetDigits(unsigned digits
)
568 wxCHECK_RET( digits
<= 20, "too many digits for wxSpinCtrlDouble" );
570 if ( digits
== m_digits
)
575 m_format
.Printf(wxT("%%0.%ulf"), digits
);
580 #endif // wxUSE_SPINBTN
582 #endif // !wxPort-with-native-spinctrl
584 #endif // wxUSE_SPINCTRL