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/hashmap.h"
33 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
35 #include "wx/textctrl.h"
36 #include "wx/wxcrtvararg.h"
39 #include "wx/msw/private.h"
42 #include "wx/tooltip.h"
43 #endif // wxUSE_TOOLTIPS
45 #include <limits.h> // for INT_MIN
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 #if wxUSE_EXTENDED_RTTI
52 WX_DEFINE_FLAGS( wxSpinCtrlStyle
)
54 wxBEGIN_FLAGS( wxSpinCtrlStyle
)
55 // new style border flags, we put them first to
56 // use them for streaming out
57 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
58 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
59 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
60 wxFLAGS_MEMBER(wxBORDER_RAISED
)
61 wxFLAGS_MEMBER(wxBORDER_STATIC
)
62 wxFLAGS_MEMBER(wxBORDER_NONE
)
64 // old style border flags
65 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
66 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
67 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
68 wxFLAGS_MEMBER(wxRAISED_BORDER
)
69 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
70 wxFLAGS_MEMBER(wxBORDER
)
72 // standard window styles
73 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
74 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
75 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
76 wxFLAGS_MEMBER(wxWANTS_CHARS
)
77 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
78 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
79 wxFLAGS_MEMBER(wxVSCROLL
)
80 wxFLAGS_MEMBER(wxHSCROLL
)
82 wxFLAGS_MEMBER(wxSP_HORIZONTAL
)
83 wxFLAGS_MEMBER(wxSP_VERTICAL
)
84 wxFLAGS_MEMBER(wxSP_ARROW_KEYS
)
85 wxFLAGS_MEMBER(wxSP_WRAP
)
87 wxEND_FLAGS( wxSpinCtrlStyle
)
89 IMPLEMENT_DYNAMIC_CLASS_XTI(wxSpinCtrl
, wxControl
,"wx/spinbut.h")
91 wxBEGIN_PROPERTIES_TABLE(wxSpinCtrl
)
92 wxEVENT_RANGE_PROPERTY( Spin
, wxEVT_SCROLL_TOP
, wxEVT_SCROLL_CHANGED
, wxSpinEvent
)
93 wxEVENT_PROPERTY( Updated
, wxEVT_COMMAND_SPINCTRL_UPDATED
, wxCommandEvent
)
94 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
95 wxEVENT_PROPERTY( TextEnter
, wxEVT_COMMAND_TEXT_ENTER
, wxCommandEvent
)
97 wxPROPERTY( ValueString
, wxString
, SetValue
, GetValue
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) ;
98 wxPROPERTY( Value
, int , SetValue
, GetValue
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
99 wxPROPERTY( Min
, int , SetMin
, GetMin
, 0, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
100 wxPROPERTY( Max
, int , SetMax
, GetMax
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
101 wxPROPERTY_FLAGS( WindowStyle
, wxSpinCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
104 style wxSP_ARROW_KEYS
106 wxEND_PROPERTIES_TABLE()
108 wxBEGIN_HANDLERS_TABLE(wxSpinCtrl
)
109 wxEND_HANDLERS_TABLE()
111 wxCONSTRUCTOR_6( wxSpinCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, ValueString
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
113 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
, wxControl
)
116 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxSpinButton
)
117 EVT_CHAR(wxSpinCtrl::OnChar
)
118 EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus
)
119 EVT_KILL_FOCUS(wxSpinCtrl::OnKillFocus
)
122 #define GetBuddyHwnd() (HWND)(m_hwndBuddy)
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
128 // the margin between the up-down control and its buddy (can be arbitrary,
129 // choose what you like - or may be decide during run-time depending on the
131 static const int MARGIN_BETWEEN
= 1;
134 // ---------------------------------------------------------------------------
136 // ---------------------------------------------------------------------------
141 // Global hash used to find the spin control corresponding to the given buddy
142 // text control HWND.
143 WX_DECLARE_HASH_MAP(HWND
, wxSpinCtrl
*,
144 wxPointerHash
, wxPointerEqual
,
147 SpinForTextCtrl gs_spinForTextCtrl
;
149 } // anonymous namespace
151 // ============================================================================
153 // ============================================================================
155 // ----------------------------------------------------------------------------
156 // wnd proc for the buddy text ctrl
157 // ----------------------------------------------------------------------------
159 LRESULT APIENTRY _EXPORT
wxBuddyTextWndProc(HWND hwnd
,
164 wxSpinCtrl
* const spin
= wxSpinCtrl::GetSpinForTextCtrl(hwnd
);
166 // forward some messages (mostly the key and focus ones) to the spin ctrl
170 // if the focus comes from the spin control itself, don't set it
171 // back to it -- we don't want to go into an infinite loop
172 if ( (WXHWND
)wParam
== spin
->GetHWND() )
182 // we need to forward WM_HELP too to ensure that the context help
183 // associated with wxSpinCtrl is shown when the text control part of it
184 // is clicked with the "?" cursor
187 spin
->MSWWindowProc(message
, wParam
, lParam
);
189 // The control may have been deleted at this point, so check.
190 if ( !::IsWindow(hwnd
) )
195 if ( spin
->HasFlag(wxTE_PROCESS_ENTER
) )
197 long dlgCode
= ::CallWindowProc
199 CASTWNDPROC spin
->GetBuddyWndProc(),
205 dlgCode
|= DLGC_WANTMESSAGE
;
211 return ::CallWindowProc(CASTWNDPROC spin
->GetBuddyWndProc(),
212 hwnd
, message
, wParam
, lParam
);
216 wxSpinCtrl
*wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy
)
218 const SpinForTextCtrl::const_iterator
219 it
= gs_spinForTextCtrl
.find(hwndBuddy
);
220 if ( it
== gs_spinForTextCtrl
.end() )
223 wxSpinCtrl
* const spin
= it
->second
;
226 wxASSERT_MSG( spin
->m_hwndBuddy
== hwndBuddy
,
227 wxT("wxSpinCtrl has incorrect buddy HWND!") );
232 // process a WM_COMMAND generated by the buddy text control
233 bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd
, WXWORD
WXUNUSED(id
))
235 if ( (cmd
== EN_CHANGE
) && (!m_blockEvent
))
237 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
238 event
.SetEventObject(this);
239 wxString val
= wxGetWindowText(m_hwndBuddy
);
240 event
.SetString(val
);
241 event
.SetInt(GetValue());
242 return HandleWindowEvent(event
);
249 void wxSpinCtrl::OnChar(wxKeyEvent
& event
)
251 switch ( event
.GetKeyCode() )
255 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
256 InitCommandEvent(event
);
257 wxString val
= wxGetWindowText(m_hwndBuddy
);
258 event
.SetString(val
);
259 event
.SetInt(GetValue());
260 if ( HandleWindowEvent(event
) )
266 // always produce navigation event - even if we process TAB
267 // ourselves the fact that we got here means that the user code
268 // decided to skip processing of this TAB - probably to let it
269 // do its default job.
271 wxNavigationKeyEvent eventNav
;
272 eventNav
.SetDirection(!event
.ShiftDown());
273 eventNav
.SetWindowChange(event
.ControlDown());
274 eventNav
.SetEventObject(this);
276 if ( GetParent()->HandleWindowEvent(eventNav
) )
282 // no, we didn't process it
286 void wxSpinCtrl::OnKillFocus(wxFocusEvent
& event
)
288 // ensure that a correct value is shown by the control
293 void wxSpinCtrl::OnSetFocus(wxFocusEvent
& event
)
295 // when we get focus, give it to our buddy window as it needs it more than
297 ::SetFocus((HWND
)m_hwndBuddy
);
302 void wxSpinCtrl::NormalizeValue()
304 const int value
= GetValue();
305 const bool changed
= value
!= m_oldValue
;
307 // notice that we have to call SetValue() even if the value didn't change
308 // because otherwise we could be left with empty buddy control when value
309 // is 0, see comment in SetValue()
314 SendSpinUpdate(value
);
318 // ----------------------------------------------------------------------------
320 // ----------------------------------------------------------------------------
322 bool wxSpinCtrl::Create(wxWindow
*parent
,
324 const wxString
& value
,
328 int min
, int max
, int initial
,
329 const wxString
& name
)
331 m_blockEvent
= false;
333 // this should be in ctor/init function but I don't want to add one to 2.8
334 // to avoid problems with default ctor which can be inlined in the user
335 // code and so might not get this fix without recompilation
336 m_oldValue
= INT_MIN
;
338 // before using DoGetBestSize(), have to set style to let the base class
339 // know whether this is a horizontal or vertical control (we're always
341 style
|= wxSP_VERTICAL
;
343 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
345 style
|= wxBORDER_SIMPLE
;
347 style
|= wxBORDER_SUNKEN
;
350 SetWindowStyle(style
);
353 WXDWORD msStyle
= MSWGetStyle(GetWindowStyle(), & exStyle
) ;
355 // propagate text alignment style to text ctrl
356 if ( style
& wxALIGN_RIGHT
)
358 else if ( style
& wxALIGN_CENTER
)
359 msStyle
|= ES_CENTER
;
361 // calculate the sizes: the size given is the total size for both controls
362 // and we need to fit them both in the given width (height is the same)
363 wxSize
sizeText(size
), sizeBtn(size
);
364 sizeBtn
.x
= wxSpinButton::DoGetBestSize().x
;
365 if ( sizeText
.x
<= 0 )
367 // DEFAULT_ITEM_WIDTH is the default width for the text control
368 sizeText
.x
= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
+ sizeBtn
.x
;
371 sizeText
.x
-= sizeBtn
.x
+ MARGIN_BETWEEN
;
372 if ( sizeText
.x
<= 0 )
374 wxLogDebug(wxT("not enough space for wxSpinCtrl!"));
378 posBtn
.x
+= sizeText
.x
+ MARGIN_BETWEEN
;
380 // we must create the text control before the spin button for the purpose
381 // of the dialog navigation: if there is a static text just before the spin
382 // control, activating it by Alt-letter should give focus to the text
383 // control, not the spin and the dialog navigation code will give focus to
384 // the next control (at Windows level), not the one after it
386 // create the text window
388 m_hwndBuddy
= (WXHWND
)::CreateWindowEx
390 exStyle
, // sunken border
391 wxT("EDIT"), // window class
392 NULL
, // no window title
393 msStyle
, // style (will be shown later)
394 pos
.x
, pos
.y
, // position
395 0, 0, // size (will be set later)
396 GetHwndOf(parent
), // parent
397 (HMENU
)-1, // control id
398 wxGetInstance(), // app instance
399 NULL
// unused client data
404 wxLogLastError(wxT("CreateWindow(buddy text window)"));
410 // create the spin button
411 if ( !wxSpinButton::Create(parent
, id
, posBtn
, sizeBtn
, style
, name
) )
416 wxSpinButtonBase::SetRange(min
, max
);
418 // subclass the text ctrl to be able to intercept some events
419 gs_spinForTextCtrl
[GetBuddyHwnd()] = this;
421 m_wndProcBuddy
= (WXFARPROC
)wxSetWindowProc(GetBuddyHwnd(),
424 // set up fonts and colours (This is nomally done in MSWCreateControl)
427 SetFont(GetDefaultAttributes().font
);
429 // set the size of the text window - can do it only now, because we
430 // couldn't call DoGetBestSize() before as font wasn't set
431 if ( sizeText
.y
<= 0 )
434 wxGetCharSize(GetHWND(), &cx
, &cy
, GetFont());
436 sizeText
.y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
439 SetInitialSize(size
);
441 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW
);
443 // associate the text window with the spin button
444 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY
, (WPARAM
)m_hwndBuddy
, 0);
448 // Set the range in the native control
451 if ( !value
.empty() )
454 m_oldValue
= (int) wxAtol(value
);
458 SetValue(wxString::Format(wxT("%d"), initial
));
459 m_oldValue
= initial
;
465 wxSpinCtrl::~wxSpinCtrl()
467 // destroy the buddy window because this pointer which wxBuddyTextWndProc
468 // uses will not soon be valid any more
469 ::DestroyWindow( GetBuddyHwnd() );
471 gs_spinForTextCtrl
.erase(GetBuddyHwnd());
474 // ----------------------------------------------------------------------------
475 // wxTextCtrl-like methods
476 // ----------------------------------------------------------------------------
478 void wxSpinCtrl::SetValue(const wxString
& text
)
480 if ( !::SetWindowText(GetBuddyHwnd(), text
.c_str()) )
482 wxLogLastError(wxT("SetWindowText(buddy)"));
486 void wxSpinCtrl::SetValue(int val
)
490 wxSpinButton::SetValue(val
);
492 // normally setting the value of the spin button is enough as it updates
493 // its buddy control automatically ...
494 if ( wxGetWindowText(m_hwndBuddy
).empty() )
496 // ... but sometimes it doesn't, notably when the value is 0 and the
497 // text control is currently empty, the spin button seems to be happy
498 // to leave it like this, while we really want to always show the
499 // current value in the control, so do it manually
500 ::SetWindowText(GetBuddyHwnd(),
501 wxString::Format(wxT("%d"), val
).wx_str());
504 m_oldValue
= GetValue();
506 m_blockEvent
= false;
509 int wxSpinCtrl::GetValue() const
511 wxString val
= wxGetWindowText(m_hwndBuddy
);
514 if ( (wxSscanf(val
, wxT("%ld"), &n
) != 1) )
525 void wxSpinCtrl::SetSelection(long from
, long to
)
527 // if from and to are both -1, it means (in wxWidgets) that all text should
528 // be selected - translate into Windows convention
529 if ( (from
== -1) && (to
== -1) )
534 ::SendMessage(GetBuddyHwnd(), EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
537 // ----------------------------------------------------------------------------
538 // wxSpinButton methods
539 // ----------------------------------------------------------------------------
541 void wxSpinCtrl::SetRange(int minVal
, int maxVal
)
543 wxSpinButton::SetRange(minVal
, maxVal
);
545 // this control is used for numeric entry so restrict the input to numeric
546 // keys only -- but only if we don't need to be able to enter "-" in it as
547 // otherwise this would become impossible
548 const DWORD styleOld
= ::GetWindowLong(GetBuddyHwnd(), GWL_STYLE
);
551 styleNew
= styleOld
& ~ES_NUMBER
;
553 styleNew
= styleOld
| ES_NUMBER
;
555 if ( styleNew
!= styleOld
)
556 ::SetWindowLong(GetBuddyHwnd(), GWL_STYLE
, styleNew
);
559 // ----------------------------------------------------------------------------
560 // forward some methods to subcontrols
561 // ----------------------------------------------------------------------------
563 bool wxSpinCtrl::SetFont(const wxFont
& font
)
565 if ( !wxWindowBase::SetFont(font
) )
571 WXHANDLE hFont
= GetFont().GetResourceHandle();
572 (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT
, (WPARAM
)hFont
, TRUE
);
577 bool wxSpinCtrl::Show(bool show
)
579 if ( !wxControl::Show(show
) )
584 ::ShowWindow(GetBuddyHwnd(), show
? SW_SHOW
: SW_HIDE
);
589 bool wxSpinCtrl::Reparent(wxWindowBase
*newParent
)
591 // Reparenting both the updown control and its buddy does not seem to work:
592 // they continue to be connected somehow, but visually there is no feedback
593 // on the buddy edit control. To avoid this problem, we reparent the buddy
594 // window normally, but we recreate the updown control and reassign its
597 // Get the position before changing the parent as it would be offset after
599 const wxRect rect
= GetRect();
601 if ( !wxWindowBase::Reparent(newParent
) )
604 newParent
->GetChildren().DeleteObject(this);
606 // destroy the old spin button after detaching it from this wxWindow object
607 // (notice that m_hWnd will be reset by UnsubclassWin() so save it first)
608 const HWND hwndOld
= GetHwnd();
610 if ( !::DestroyWindow(hwndOld
) )
612 wxLogLastError(wxT("DestroyWindow"));
615 // create and initialize the new one
616 if ( !wxSpinButton::Create(GetParent(), GetId(),
617 rect
.GetPosition(), rect
.GetSize(),
618 GetWindowStyle(), GetName()) )
621 // reapply our values to wxSpinButton
622 wxSpinButton::SetValue(GetValue());
623 SetRange(m_min
, m_max
);
625 // also set the size again with wxSIZE_ALLOW_MINUS_ONE flag: this is
626 // necessary if our original position used -1 for either x or y
627 SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
629 // associate it with the buddy control again
630 ::SetParent(GetBuddyHwnd(), GetHwndOf(GetParent()));
631 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY
, (WPARAM
)GetBuddyHwnd(), 0);
636 bool wxSpinCtrl::Enable(bool enable
)
638 if ( !wxControl::Enable(enable
) )
643 MSWEnableHWND(GetBuddyHwnd(), enable
);
648 void wxSpinCtrl::SetFocus()
650 ::SetFocus(GetBuddyHwnd());
655 void wxSpinCtrl::DoSetToolTip(wxToolTip
*tip
)
657 wxSpinButton::DoSetToolTip(tip
);
660 tip
->Add(m_hwndBuddy
);
663 #endif // wxUSE_TOOLTIPS
665 // ----------------------------------------------------------------------------
666 // events processing and generation
667 // ----------------------------------------------------------------------------
669 void wxSpinCtrl::SendSpinUpdate(int value
)
671 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, GetId());
672 event
.SetEventObject(this);
675 (void)HandleWindowEvent(event
);
680 bool wxSpinCtrl::MSWOnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
681 WXWORD pos
, WXHWND control
)
683 wxCHECK_MSG( control
, false, wxT("scrolling what?") );
685 if ( wParam
!= SB_THUMBPOSITION
)
687 // probable SB_ENDSCROLL - we don't react to it
691 int new_value
= (short) pos
;
692 if (m_oldValue
!= new_value
)
693 SendSpinUpdate( new_value
);
698 bool wxSpinCtrl::MSWOnNotify(int WXUNUSED(idCtrl
), WXLPARAM lParam
, WXLPARAM
*result
)
700 NM_UPDOWN
*lpnmud
= (NM_UPDOWN
*)lParam
;
702 if (lpnmud
->hdr
.hwndFrom
!= GetHwnd()) // make sure it is the right control
705 *result
= 0; // never reject UP and DOWN events
711 // ----------------------------------------------------------------------------
713 // ----------------------------------------------------------------------------
715 wxSize
wxSpinCtrl::DoGetBestSize() const
717 wxSize sizeBtn
= wxSpinButton::DoGetBestSize();
718 sizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
721 wxGetCharSize(GetHWND(), NULL
, &y
, GetFont());
722 y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
);
724 // JACS: we should always use the height calculated
725 // from above, because otherwise we'll get a spin control
726 // that's too big. So never use the height calculated
727 // from wxSpinButton::DoGetBestSize().
729 // if ( sizeBtn.y < y )
731 // make the text tall enough
738 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
740 int widthBtn
= wxSpinButton::DoGetBestSize().x
;
741 int widthText
= width
- widthBtn
- MARGIN_BETWEEN
;
742 if ( widthText
<= 0 )
744 wxLogDebug(wxT("not enough space for wxSpinCtrl!"));
747 // 1) The buddy window
748 DoMoveSibling(m_hwndBuddy
, x
, y
, widthText
, height
);
750 // 2) The button window
751 x
+= widthText
+ MARGIN_BETWEEN
;
752 wxSpinButton::DoMoveWindow(x
, y
, widthBtn
, height
);
755 // get total size of the control
756 void wxSpinCtrl::DoGetSize(int *x
, int *y
) const
758 RECT spinrect
, textrect
, ctrlrect
;
759 GetWindowRect(GetHwnd(), &spinrect
);
760 GetWindowRect(GetBuddyHwnd(), &textrect
);
761 UnionRect(&ctrlrect
,&textrect
, &spinrect
);
764 *x
= ctrlrect
.right
- ctrlrect
.left
;
766 *y
= ctrlrect
.bottom
- ctrlrect
.top
;
769 void wxSpinCtrl::DoGetClientSize(int *x
, int *y
) const
771 RECT spinrect
= wxGetClientRect(GetHwnd());
772 RECT textrect
= wxGetClientRect(GetBuddyHwnd());
774 UnionRect(&ctrlrect
,&textrect
, &spinrect
);
777 *x
= ctrlrect
.right
- ctrlrect
.left
;
779 *y
= ctrlrect
.bottom
- ctrlrect
.top
;
782 void wxSpinCtrl::DoGetPosition(int *x
, int *y
) const
784 // hack: pretend that our HWND is the text control just for a moment
785 WXHWND hWnd
= GetHWND();
786 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= m_hwndBuddy
;
788 wxSpinButton::DoGetPosition(x
, y
);
790 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= hWnd
;
793 #endif // wxUSE_SPINCTRL