1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/spinctrl.cpp
3 // Purpose: wxSpinCtrl class implementation for Win32
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1999-2005 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/spinctrl.h"
32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
34 #include "wx/textctrl.h"
35 #include "wx/wxcrtvararg.h"
38 #include "wx/msw/private.h"
41 #include "wx/tooltip.h"
42 #endif // wxUSE_TOOLTIPS
44 #include <limits.h> // for INT_MIN
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 #if wxUSE_EXTENDED_RTTI
51 WX_DEFINE_FLAGS( wxSpinCtrlStyle
)
53 wxBEGIN_FLAGS( wxSpinCtrlStyle
)
54 // new style border flags, we put them first to
55 // use them for streaming out
56 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
57 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
58 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
59 wxFLAGS_MEMBER(wxBORDER_RAISED
)
60 wxFLAGS_MEMBER(wxBORDER_STATIC
)
61 wxFLAGS_MEMBER(wxBORDER_NONE
)
63 // old style border flags
64 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
65 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
66 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
67 wxFLAGS_MEMBER(wxRAISED_BORDER
)
68 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
69 wxFLAGS_MEMBER(wxBORDER
)
71 // standard window styles
72 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
73 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
74 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
75 wxFLAGS_MEMBER(wxWANTS_CHARS
)
76 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
77 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
78 wxFLAGS_MEMBER(wxVSCROLL
)
79 wxFLAGS_MEMBER(wxHSCROLL
)
81 wxFLAGS_MEMBER(wxSP_HORIZONTAL
)
82 wxFLAGS_MEMBER(wxSP_VERTICAL
)
83 wxFLAGS_MEMBER(wxSP_ARROW_KEYS
)
84 wxFLAGS_MEMBER(wxSP_WRAP
)
86 wxEND_FLAGS( wxSpinCtrlStyle
)
88 IMPLEMENT_DYNAMIC_CLASS_XTI(wxSpinCtrl
, wxControl
,"wx/spinbut.h")
90 wxBEGIN_PROPERTIES_TABLE(wxSpinCtrl
)
91 wxEVENT_RANGE_PROPERTY( Spin
, wxEVT_SCROLL_TOP
, wxEVT_SCROLL_CHANGED
, wxSpinEvent
)
92 wxEVENT_PROPERTY( Updated
, wxEVT_COMMAND_SPINCTRL_UPDATED
, wxCommandEvent
)
93 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
94 wxEVENT_PROPERTY( TextEnter
, wxEVT_COMMAND_TEXT_ENTER
, wxCommandEvent
)
96 wxPROPERTY( ValueString
, wxString
, SetValue
, GetValue
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) ;
97 wxPROPERTY( Value
, int , SetValue
, GetValue
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
98 wxPROPERTY( Min
, int , SetMin
, GetMin
, 0, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
99 wxPROPERTY( Max
, int , SetMax
, GetMax
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
100 wxPROPERTY_FLAGS( WindowStyle
, wxSpinCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
103 style wxSP_ARROW_KEYS
105 wxEND_PROPERTIES_TABLE()
107 wxBEGIN_HANDLERS_TABLE(wxSpinCtrl
)
108 wxEND_HANDLERS_TABLE()
110 wxCONSTRUCTOR_6( wxSpinCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, ValueString
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
112 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
, wxControl
)
115 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxSpinButton
)
116 EVT_CHAR(wxSpinCtrl::OnChar
)
117 EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus
)
118 EVT_KILL_FOCUS(wxSpinCtrl::OnKillFocus
)
121 #define GetBuddyHwnd() (HWND)(m_hwndBuddy)
123 // ----------------------------------------------------------------------------
125 // ----------------------------------------------------------------------------
127 // the margin between the up-down control and its buddy (can be arbitrary,
128 // choose what you like - or may be decide during run-time depending on the
130 static const int MARGIN_BETWEEN
= 1;
132 // ============================================================================
134 // ============================================================================
136 wxArraySpins
wxSpinCtrl::ms_allSpins
;
138 // ----------------------------------------------------------------------------
139 // wnd proc for the buddy text ctrl
140 // ----------------------------------------------------------------------------
142 LRESULT APIENTRY _EXPORT
wxBuddyTextWndProc(HWND hwnd
,
147 wxSpinCtrl
*spin
= (wxSpinCtrl
*)wxGetWindowUserData(hwnd
);
149 // forward some messages (mostly the key and focus ones) to the spin ctrl
153 // if the focus comes from the spin control itself, don't set it
154 // back to it -- we don't want to go into an infinite loop
155 if ( (WXHWND
)wParam
== spin
->GetHWND() )
165 // we need to forward WM_HELP too to ensure that the context help
166 // associated with wxSpinCtrl is shown when the text control part of it
167 // is clicked with the "?" cursor
170 spin
->MSWWindowProc(message
, wParam
, lParam
);
172 // The control may have been deleted at this point, so check.
173 if ( !::IsWindow(hwnd
) || wxGetWindowUserData(hwnd
) != spin
)
178 if ( spin
->HasFlag(wxTE_PROCESS_ENTER
) )
180 long dlgCode
= ::CallWindowProc
182 CASTWNDPROC spin
->GetBuddyWndProc(),
188 dlgCode
|= DLGC_WANTMESSAGE
;
194 return ::CallWindowProc(CASTWNDPROC spin
->GetBuddyWndProc(),
195 hwnd
, message
, wParam
, lParam
);
199 wxSpinCtrl
*wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy
)
201 wxSpinCtrl
*spin
= (wxSpinCtrl
*)wxGetWindowUserData((HWND
)hwndBuddy
);
203 int i
= ms_allSpins
.Index(spin
);
205 if ( i
== wxNOT_FOUND
)
209 wxASSERT_MSG( spin
->m_hwndBuddy
== hwndBuddy
,
210 _T("wxSpinCtrl has incorrect buddy HWND!") );
215 // process a WM_COMMAND generated by the buddy text control
216 bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd
, WXWORD
WXUNUSED(id
))
218 if ( (cmd
== EN_CHANGE
) && (!m_blockEvent
))
220 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
221 event
.SetEventObject(this);
222 wxString val
= wxGetWindowText(m_hwndBuddy
);
223 event
.SetString(val
);
224 event
.SetInt(GetValue());
225 return HandleWindowEvent(event
);
232 void wxSpinCtrl::OnChar(wxKeyEvent
& event
)
234 switch ( event
.GetKeyCode() )
238 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
239 InitCommandEvent(event
);
240 wxString val
= wxGetWindowText(m_hwndBuddy
);
241 event
.SetString(val
);
242 event
.SetInt(GetValue());
243 if ( HandleWindowEvent(event
) )
249 // always produce navigation event - even if we process TAB
250 // ourselves the fact that we got here means that the user code
251 // decided to skip processing of this TAB - probably to let it
252 // do its default job.
254 wxNavigationKeyEvent eventNav
;
255 eventNav
.SetDirection(!event
.ShiftDown());
256 eventNav
.SetWindowChange(event
.ControlDown());
257 eventNav
.SetEventObject(this);
259 if ( GetParent()->HandleWindowEvent(eventNav
) )
265 // no, we didn't process it
269 void wxSpinCtrl::OnKillFocus(wxFocusEvent
& event
)
271 // ensure that a correct value is shown by the control
276 void wxSpinCtrl::OnSetFocus(wxFocusEvent
& event
)
278 // when we get focus, give it to our buddy window as it needs it more than
280 ::SetFocus((HWND
)m_hwndBuddy
);
285 void wxSpinCtrl::NormalizeValue()
287 const int value
= GetValue();
288 const bool changed
= value
!= m_oldValue
;
290 // notice that we have to call SetValue() even if the value didn't change
291 // because otherwise we could be left with empty buddy control when value
292 // is 0, see comment in SetValue()
297 SendSpinUpdate(value
);
301 // ----------------------------------------------------------------------------
303 // ----------------------------------------------------------------------------
305 bool wxSpinCtrl::Create(wxWindow
*parent
,
307 const wxString
& value
,
311 int min
, int max
, int initial
,
312 const wxString
& name
)
314 m_blockEvent
= false;
316 // this should be in ctor/init function but I don't want to add one to 2.8
317 // to avoid problems with default ctor which can be inlined in the user
318 // code and so might not get this fix without recompilation
319 m_oldValue
= INT_MIN
;
321 // before using DoGetBestSize(), have to set style to let the base class
322 // know whether this is a horizontal or vertical control (we're always
324 style
|= wxSP_VERTICAL
;
326 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
328 style
|= wxBORDER_SIMPLE
;
330 style
|= wxBORDER_SUNKEN
;
333 SetWindowStyle(style
);
336 WXDWORD msStyle
= MSWGetStyle(GetWindowStyle(), & exStyle
) ;
338 // propagate text alignment style to text ctrl
339 if ( style
& wxALIGN_RIGHT
)
341 else if ( style
& wxALIGN_CENTER
)
342 msStyle
|= ES_CENTER
;
344 // this control is used for numeric entry so normally using these flags by
345 // default shouldn't be a problem, if it is we can always add a style such
346 // as wxSP_NON_NUMERIC later
347 msStyle
|= ES_NUMBER
;
349 // calculate the sizes: the size given is the total size for both controls
350 // and we need to fit them both in the given width (height is the same)
351 wxSize
sizeText(size
), sizeBtn(size
);
352 sizeBtn
.x
= wxSpinButton::DoGetBestSize().x
;
353 if ( sizeText
.x
<= 0 )
355 // DEFAULT_ITEM_WIDTH is the default width for the text control
356 sizeText
.x
= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
+ sizeBtn
.x
;
359 sizeText
.x
-= sizeBtn
.x
+ MARGIN_BETWEEN
;
360 if ( sizeText
.x
<= 0 )
362 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
366 posBtn
.x
+= sizeText
.x
+ MARGIN_BETWEEN
;
368 // we must create the text control before the spin button for the purpose
369 // of the dialog navigation: if there is a static text just before the spin
370 // control, activating it by Alt-letter should give focus to the text
371 // control, not the spin and the dialog navigation code will give focus to
372 // the next control (at Windows level), not the one after it
374 // create the text window
376 m_hwndBuddy
= (WXHWND
)::CreateWindowEx
378 exStyle
, // sunken border
379 _T("EDIT"), // window class
380 NULL
, // no window title
381 msStyle
, // style (will be shown later)
382 pos
.x
, pos
.y
, // position
383 0, 0, // size (will be set later)
384 GetHwndOf(parent
), // parent
385 (HMENU
)-1, // control id
386 wxGetInstance(), // app instance
387 NULL
// unused client data
392 wxLogLastError(wxT("CreateWindow(buddy text window)"));
398 // create the spin button
399 if ( !wxSpinButton::Create(parent
, id
, posBtn
, sizeBtn
, style
, name
) )
404 wxSpinButtonBase::SetRange(min
, max
);
406 // subclass the text ctrl to be able to intercept some events
407 wxSetWindowUserData(GetBuddyHwnd(), this);
408 m_wndProcBuddy
= (WXFARPROC
)wxSetWindowProc(GetBuddyHwnd(),
411 // set up fonts and colours (This is nomally done in MSWCreateControl)
414 SetFont(GetDefaultAttributes().font
);
416 // set the size of the text window - can do it only now, because we
417 // couldn't call DoGetBestSize() before as font wasn't set
418 if ( sizeText
.y
<= 0 )
421 wxGetCharSize(GetHWND(), &cx
, &cy
, GetFont());
423 sizeText
.y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
426 SetInitialSize(size
);
428 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW
);
430 // associate the text window with the spin button
431 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY
, (WPARAM
)m_hwndBuddy
, 0);
435 // Set the range in the native control
438 if ( !value
.empty() )
441 m_oldValue
= (int) wxAtol(value
);
445 SetValue(wxString::Format(wxT("%d"), initial
));
446 m_oldValue
= initial
;
449 // do it after finishing with m_hwndBuddy creation to avoid generating
450 // initial wxEVT_COMMAND_TEXT_UPDATED message
451 ms_allSpins
.Add(this);
456 wxSpinCtrl::~wxSpinCtrl()
458 ms_allSpins
.Remove(this);
460 // This removes spurious memory leak reporting
461 if (ms_allSpins
.GetCount() == 0)
464 // destroy the buddy window because this pointer which wxBuddyTextWndProc
465 // uses will not soon be valid any more
466 ::DestroyWindow(GetBuddyHwnd());
469 // ----------------------------------------------------------------------------
470 // wxTextCtrl-like methods
471 // ----------------------------------------------------------------------------
473 void wxSpinCtrl::SetValue(const wxString
& text
)
475 if ( !::SetWindowText(GetBuddyHwnd(), text
.c_str()) )
477 wxLogLastError(wxT("SetWindowText(buddy)"));
481 void wxSpinCtrl::SetValue(int val
)
485 wxSpinButton::SetValue(val
);
487 // normally setting the value of the spin button is enough as it updates
488 // its buddy control automatically ...
489 if ( wxGetWindowText(m_hwndBuddy
).empty() )
491 // ... but sometimes it doesn't, notably when the value is 0 and the
492 // text control is currently empty, the spin button seems to be happy
493 // to leave it like this, while we really want to always show the
494 // current value in the control, so do it manually
495 ::SetWindowText(GetBuddyHwnd(),
496 wxString::Format(_T("%d"), val
).wx_str());
499 m_oldValue
= GetValue();
501 m_blockEvent
= false;
504 int wxSpinCtrl::GetValue() const
506 wxString val
= wxGetWindowText(m_hwndBuddy
);
509 if ( (wxSscanf(val
, wxT("%ld"), &n
) != 1) )
520 void wxSpinCtrl::SetSelection(long from
, long to
)
522 // if from and to are both -1, it means (in wxWidgets) that all text should
523 // be selected - translate into Windows convention
524 if ( (from
== -1) && (to
== -1) )
529 ::SendMessage(GetBuddyHwnd(), EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
532 // ----------------------------------------------------------------------------
533 // forward some methods to subcontrols
534 // ----------------------------------------------------------------------------
536 bool wxSpinCtrl::SetFont(const wxFont
& font
)
538 if ( !wxWindowBase::SetFont(font
) )
544 WXHANDLE hFont
= GetFont().GetResourceHandle();
545 (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT
, (WPARAM
)hFont
, TRUE
);
550 bool wxSpinCtrl::Show(bool show
)
552 if ( !wxControl::Show(show
) )
557 ::ShowWindow(GetBuddyHwnd(), show
? SW_SHOW
: SW_HIDE
);
562 bool wxSpinCtrl::Reparent(wxWindowBase
*newParent
)
564 // Reparenting both the updown control and its buddy does not seem to work:
565 // they continue to be connected somehow, but visually there is no feedback
566 // on the buddy edit control. To avoid this problem, we reparent the buddy
567 // window normally, but we recreate the updown control and reassign its
570 if ( !wxWindowBase::Reparent(newParent
) )
573 newParent
->GetChildren().DeleteObject(this);
575 // preserve the old values
576 const wxSize size
= GetSize();
577 int value
= GetValue();
578 const wxRect btnRect
= wxRectFromRECT(wxGetWindowRect(GetHwnd()));
580 // destroy the old spin button
582 if ( !::DestroyWindow(GetHwnd()) )
583 wxLogLastError(wxT("DestroyWindow"));
585 // create and initialize the new one
586 if ( !wxSpinButton::Create(GetParent(), GetId(),
587 btnRect
.GetPosition(), btnRect
.GetSize(),
588 GetWindowStyle(), GetName()) )
592 SetRange(m_min
, m_max
);
593 SetInitialSize(size
);
595 // associate it with the buddy control again
596 ::SetParent(GetBuddyHwnd(), GetHwndOf(GetParent()));
597 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY
, (WPARAM
)GetBuddyHwnd(), 0);
602 bool wxSpinCtrl::Enable(bool enable
)
604 if ( !wxControl::Enable(enable
) )
609 ::EnableWindow(GetBuddyHwnd(), enable
);
614 void wxSpinCtrl::SetFocus()
616 ::SetFocus(GetBuddyHwnd());
621 void wxSpinCtrl::DoSetToolTip(wxToolTip
*tip
)
623 wxSpinButton::DoSetToolTip(tip
);
626 tip
->Add(m_hwndBuddy
);
629 #endif // wxUSE_TOOLTIPS
631 // ----------------------------------------------------------------------------
632 // events processing and generation
633 // ----------------------------------------------------------------------------
635 void wxSpinCtrl::SendSpinUpdate(int value
)
637 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, GetId());
638 event
.SetEventObject(this);
641 (void)HandleWindowEvent(event
);
646 bool wxSpinCtrl::MSWOnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
647 WXWORD pos
, WXHWND control
)
649 wxCHECK_MSG( control
, false, wxT("scrolling what?") );
651 if ( wParam
!= SB_THUMBPOSITION
)
653 // probable SB_ENDSCROLL - we don't react to it
657 int new_value
= (short) pos
;
658 if (m_oldValue
!= new_value
)
659 SendSpinUpdate( new_value
);
664 bool wxSpinCtrl::MSWOnNotify(int WXUNUSED(idCtrl
), WXLPARAM lParam
, WXLPARAM
*result
)
666 NM_UPDOWN
*lpnmud
= (NM_UPDOWN
*)lParam
;
668 if (lpnmud
->hdr
.hwndFrom
!= GetHwnd()) // make sure it is the right control
671 *result
= 0; // never reject UP and DOWN events
677 // ----------------------------------------------------------------------------
679 // ----------------------------------------------------------------------------
681 wxSize
wxSpinCtrl::DoGetBestSize() const
683 wxSize sizeBtn
= wxSpinButton::DoGetBestSize();
684 sizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
687 wxGetCharSize(GetHWND(), NULL
, &y
, GetFont());
688 y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
);
690 // JACS: we should always use the height calculated
691 // from above, because otherwise we'll get a spin control
692 // that's too big. So never use the height calculated
693 // from wxSpinButton::DoGetBestSize().
695 // if ( sizeBtn.y < y )
697 // make the text tall enough
704 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
706 int widthBtn
= wxSpinButton::DoGetBestSize().x
;
707 int widthText
= width
- widthBtn
- MARGIN_BETWEEN
;
708 if ( widthText
<= 0 )
710 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
713 // 1) The buddy window
714 DoMoveSibling(m_hwndBuddy
, x
, y
, widthText
, height
);
716 // 2) The button window
717 x
+= widthText
+ MARGIN_BETWEEN
;
718 wxSpinButton::DoMoveWindow(x
, y
, widthBtn
, height
);
721 // get total size of the control
722 void wxSpinCtrl::DoGetSize(int *x
, int *y
) const
724 RECT spinrect
, textrect
, ctrlrect
;
725 GetWindowRect(GetHwnd(), &spinrect
);
726 GetWindowRect(GetBuddyHwnd(), &textrect
);
727 UnionRect(&ctrlrect
,&textrect
, &spinrect
);
730 *x
= ctrlrect
.right
- ctrlrect
.left
;
732 *y
= ctrlrect
.bottom
- ctrlrect
.top
;
735 void wxSpinCtrl::DoGetClientSize(int *x
, int *y
) const
737 RECT spinrect
= wxGetClientRect(GetHwnd());
738 RECT textrect
= wxGetClientRect(GetBuddyHwnd());
740 UnionRect(&ctrlrect
,&textrect
, &spinrect
);
743 *x
= ctrlrect
.right
- ctrlrect
.left
;
745 *y
= ctrlrect
.bottom
- ctrlrect
.top
;
748 void wxSpinCtrl::DoGetPosition(int *x
, int *y
) const
750 // hack: pretend that our HWND is the text control just for a moment
751 WXHWND hWnd
= GetHWND();
752 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= m_hwndBuddy
;
754 wxSpinButton::DoGetPosition(x
, y
);
756 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= hWnd
;
759 #endif // wxUSE_SPINCTRL