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
)
118 EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus
)
119 EVT_KILL_FOCUS(wxSpinCtrl::OnKillFocus
)
120 EVT_SPIN(wxID_ANY
, wxSpinCtrl::OnSpinChange
)
123 #define GetBuddyHwnd() (HWND)(m_hwndBuddy)
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 // the margin between the up-down control and its buddy (can be arbitrary,
130 // choose what you like - or may be decide during run-time depending on the
132 static const int MARGIN_BETWEEN
= 1;
134 // ============================================================================
136 // ============================================================================
138 wxArraySpins
wxSpinCtrl::ms_allSpins
;
140 // ----------------------------------------------------------------------------
141 // wnd proc for the buddy text ctrl
142 // ----------------------------------------------------------------------------
144 LRESULT APIENTRY _EXPORT
wxBuddyTextWndProc(HWND hwnd
,
149 wxSpinCtrl
*spin
= (wxSpinCtrl
*)wxGetWindowUserData(hwnd
);
151 // forward some messages (mostly the key and focus ones) to the spin ctrl
155 // if the focus comes from the spin control itself, don't set it
156 // back to it -- we don't want to go into an infinite loop
157 if ( (WXHWND
)wParam
== spin
->GetHWND() )
167 // we need to forward WM_HELP too to ensure that the context help
168 // associated with wxSpinCtrl is shown when the text control part of it
169 // is clicked with the "?" cursor
172 spin
->MSWWindowProc(message
, wParam
, lParam
);
174 // The control may have been deleted at this point, so check.
175 if ( !::IsWindow(hwnd
) || wxGetWindowUserData(hwnd
) != spin
)
180 // we want to get WXK_RETURN in order to generate the event for it
181 return DLGC_WANTALLKEYS
;
184 return ::CallWindowProc(CASTWNDPROC spin
->GetBuddyWndProc(),
185 hwnd
, message
, wParam
, lParam
);
189 wxSpinCtrl
*wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy
)
191 wxSpinCtrl
*spin
= (wxSpinCtrl
*)wxGetWindowUserData((HWND
)hwndBuddy
);
193 int i
= ms_allSpins
.Index(spin
);
195 if ( i
== wxNOT_FOUND
)
199 wxASSERT_MSG( spin
->m_hwndBuddy
== hwndBuddy
,
200 _T("wxSpinCtrl has incorrect buddy HWND!") );
205 // process a WM_COMMAND generated by the buddy text control
206 bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd
, WXWORD
WXUNUSED(id
))
208 if ( (cmd
== EN_CHANGE
) && (!m_blockEvent
))
210 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
211 event
.SetEventObject(this);
212 wxString val
= wxGetWindowText(m_hwndBuddy
);
213 event
.SetString(val
);
214 event
.SetInt(GetValue());
215 return HandleWindowEvent(event
);
222 void wxSpinCtrl::OnChar(wxKeyEvent
& event
)
224 switch ( event
.GetKeyCode() )
228 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
229 InitCommandEvent(event
);
230 wxString val
= wxGetWindowText(m_hwndBuddy
);
231 event
.SetString(val
);
232 event
.SetInt(GetValue());
233 if ( HandleWindowEvent(event
) )
239 // always produce navigation event - even if we process TAB
240 // ourselves the fact that we got here means that the user code
241 // decided to skip processing of this TAB - probably to let it
242 // do its default job.
244 wxNavigationKeyEvent eventNav
;
245 eventNav
.SetDirection(!event
.ShiftDown());
246 eventNav
.SetWindowChange(event
.ControlDown());
247 eventNav
.SetEventObject(this);
249 if ( GetParent()->HandleWindowEvent(eventNav
) )
255 // no, we didn't process it
259 void wxSpinCtrl::OnKillFocus(wxFocusEvent
& event
)
261 // ensure that a correct value is shown by the control
266 void wxSpinCtrl::OnSetFocus(wxFocusEvent
& event
)
268 // when we get focus, give it to our buddy window as it needs it more than
270 ::SetFocus((HWND
)m_hwndBuddy
);
275 void wxSpinCtrl::NormalizeValue()
277 const int value
= GetValue();
278 const bool changed
= value
!= m_oldValue
;
280 // notice that we have to call SetValue() even if the value didn't change
281 // because otherwise we could be left with empty buddy control when value
282 // is 0, see comment in SetValue()
287 SendSpinUpdate(value
);
291 // ----------------------------------------------------------------------------
293 // ----------------------------------------------------------------------------
295 bool wxSpinCtrl::Create(wxWindow
*parent
,
297 const wxString
& value
,
301 int min
, int max
, int initial
,
302 const wxString
& name
)
304 m_blockEvent
= false;
306 // this should be in ctor/init function but I don't want to add one to 2.8
307 // to avoid problems with default ctor which can be inlined in the user
308 // code and so might not get this fix without recompilation
309 m_oldValue
= INT_MIN
;
311 // before using DoGetBestSize(), have to set style to let the base class
312 // know whether this is a horizontal or vertical control (we're always
314 style
|= wxSP_VERTICAL
;
316 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
318 style
|= wxBORDER_SIMPLE
;
320 style
|= wxBORDER_SUNKEN
;
323 SetWindowStyle(style
);
326 WXDWORD msStyle
= MSWGetStyle(GetWindowStyle(), & exStyle
) ;
328 // this control is used for numeric entry so normally using these flags by
329 // default shouldn't be a problem, if it is we can always add a style such
330 // as wxSP_NON_NUMERIC later
331 msStyle
|= ES_RIGHT
| ES_NUMBER
;
333 // calculate the sizes: the size given is the total size for both controls
334 // and we need to fit them both in the given width (height is the same)
335 wxSize
sizeText(size
), sizeBtn(size
);
336 sizeBtn
.x
= wxSpinButton::DoGetBestSize().x
;
337 if ( sizeText
.x
<= 0 )
339 // DEFAULT_ITEM_WIDTH is the default width for the text control
340 sizeText
.x
= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
+ sizeBtn
.x
;
343 sizeText
.x
-= sizeBtn
.x
+ MARGIN_BETWEEN
;
344 if ( sizeText
.x
<= 0 )
346 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
350 posBtn
.x
+= sizeText
.x
+ MARGIN_BETWEEN
;
352 // we must create the text control before the spin button for the purpose
353 // of the dialog navigation: if there is a static text just before the spin
354 // control, activating it by Alt-letter should give focus to the text
355 // control, not the spin and the dialog navigation code will give focus to
356 // the next control (at Windows level), not the one after it
358 // create the text window
360 m_hwndBuddy
= (WXHWND
)::CreateWindowEx
362 exStyle
, // sunken border
363 _T("EDIT"), // window class
364 NULL
, // no window title
365 msStyle
, // style (will be shown later)
366 pos
.x
, pos
.y
, // position
367 0, 0, // size (will be set later)
368 GetHwndOf(parent
), // parent
369 (HMENU
)-1, // control id
370 wxGetInstance(), // app instance
371 NULL
// unused client data
376 wxLogLastError(wxT("CreateWindow(buddy text window)"));
382 // create the spin button
383 if ( !wxSpinButton::Create(parent
, id
, posBtn
, sizeBtn
, style
, name
) )
388 wxSpinButtonBase::SetRange(min
, max
);
390 // subclass the text ctrl to be able to intercept some events
391 wxSetWindowUserData(GetBuddyHwnd(), this);
392 m_wndProcBuddy
= (WXFARPROC
)wxSetWindowProc(GetBuddyHwnd(),
395 // set up fonts and colours (This is nomally done in MSWCreateControl)
398 SetFont(GetDefaultAttributes().font
);
400 // set the size of the text window - can do it only now, because we
401 // couldn't call DoGetBestSize() before as font wasn't set
402 if ( sizeText
.y
<= 0 )
405 wxGetCharSize(GetHWND(), &cx
, &cy
, GetFont());
407 sizeText
.y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
410 SetInitialSize(size
);
412 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW
);
414 // associate the text window with the spin button
415 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY
, (WPARAM
)m_hwndBuddy
, 0);
419 // Set the range in the native control
422 if ( !value
.empty() )
425 m_oldValue
= (int) wxAtol(value
);
429 SetValue(wxString::Format(wxT("%d"), initial
));
430 m_oldValue
= initial
;
433 // do it after finishing with m_hwndBuddy creation to avoid generating
434 // initial wxEVT_COMMAND_TEXT_UPDATED message
435 ms_allSpins
.Add(this);
440 wxSpinCtrl::~wxSpinCtrl()
442 ms_allSpins
.Remove(this);
444 // This removes spurious memory leak reporting
445 if (ms_allSpins
.GetCount() == 0)
448 // destroy the buddy window because this pointer which wxBuddyTextWndProc
449 // uses will not soon be valid any more
450 ::DestroyWindow(GetBuddyHwnd());
453 // ----------------------------------------------------------------------------
454 // wxTextCtrl-like methods
455 // ----------------------------------------------------------------------------
457 void wxSpinCtrl::SetValue(const wxString
& text
)
459 if ( !::SetWindowText(GetBuddyHwnd(), text
.c_str()) )
461 wxLogLastError(wxT("SetWindowText(buddy)"));
465 void wxSpinCtrl::SetValue(int val
)
469 wxSpinButton::SetValue(val
);
471 // normally setting the value of the spin button is enough as it updates
472 // its buddy control automatically ...
473 if ( wxGetWindowText(m_hwndBuddy
).empty() )
475 // ... but sometimes it doesn't, notably when the value is 0 and the
476 // text control is currently empty, the spin button seems to be happy
477 // to leave it like this, while we really want to always show the
478 // current value in the control, so do it manually
479 ::SetWindowText(GetBuddyHwnd(),
480 wxString::Format(_T("%d"), val
).wx_str());
483 m_oldValue
= GetValue();
485 m_blockEvent
= false;
488 int wxSpinCtrl::GetValue() const
490 wxString val
= wxGetWindowText(m_hwndBuddy
);
493 if ( (wxSscanf(val
, wxT("%ld"), &n
) != 1) )
504 void wxSpinCtrl::SetSelection(long from
, long to
)
506 // if from and to are both -1, it means (in wxWidgets) that all text should
507 // be selected - translate into Windows convention
508 if ( (from
== -1) && (to
== -1) )
513 ::SendMessage(GetBuddyHwnd(), EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
516 // ----------------------------------------------------------------------------
517 // forward some methods to subcontrols
518 // ----------------------------------------------------------------------------
520 bool wxSpinCtrl::SetFont(const wxFont
& font
)
522 if ( !wxWindowBase::SetFont(font
) )
528 WXHANDLE hFont
= GetFont().GetResourceHandle();
529 (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT
, (WPARAM
)hFont
, TRUE
);
534 bool wxSpinCtrl::Show(bool show
)
536 if ( !wxControl::Show(show
) )
541 ::ShowWindow(GetBuddyHwnd(), show
? SW_SHOW
: SW_HIDE
);
546 bool wxSpinCtrl::Reparent(wxWindowBase
*newParent
)
548 // Reparenting both the updown control and its buddy does not seem to work:
549 // they continue to be connected somehow, but visually there is no feedback
550 // on the buddy edit control. To avoid this problem, we reparent the buddy
551 // window normally, but we recreate the updown control and reassign its
554 if ( !wxWindowBase::Reparent(newParent
) )
557 newParent
->GetChildren().DeleteObject(this);
559 // preserve the old values
560 const wxSize size
= GetSize();
561 int value
= GetValue();
562 const wxRect btnRect
= wxRectFromRECT(wxGetWindowRect(GetHwnd()));
564 // destroy the old spin button
566 if ( !::DestroyWindow(GetHwnd()) )
567 wxLogLastError(wxT("DestroyWindow"));
569 // create and initialize the new one
570 if ( !wxSpinButton::Create(GetParent(), GetId(),
571 btnRect
.GetPosition(), btnRect
.GetSize(),
572 GetWindowStyle(), GetName()) )
576 SetRange(m_min
, m_max
);
577 SetInitialSize(size
);
579 // associate it with the buddy control again
580 ::SetParent(GetBuddyHwnd(), GetHwndOf(GetParent()));
581 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY
, (WPARAM
)GetBuddyHwnd(), 0);
586 bool wxSpinCtrl::Enable(bool enable
)
588 if ( !wxControl::Enable(enable
) )
593 ::EnableWindow(GetBuddyHwnd(), enable
);
598 void wxSpinCtrl::SetFocus()
600 ::SetFocus(GetBuddyHwnd());
605 void wxSpinCtrl::DoSetToolTip(wxToolTip
*tip
)
607 wxSpinButton::DoSetToolTip(tip
);
610 tip
->Add(m_hwndBuddy
);
613 #endif // wxUSE_TOOLTIPS
615 // ----------------------------------------------------------------------------
616 // events processing and generation
617 // ----------------------------------------------------------------------------
619 void wxSpinCtrl::SendSpinUpdate(int value
)
621 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, GetId());
622 event
.SetEventObject(this);
625 (void)HandleWindowEvent(event
);
630 void wxSpinCtrl::OnSpinChange(wxSpinEvent
& eventSpin
)
632 const int value
= eventSpin
.GetPosition();
633 if ( value
!= m_oldValue
)
635 SendSpinUpdate(value
);
639 // ----------------------------------------------------------------------------
641 // ----------------------------------------------------------------------------
643 wxSize
wxSpinCtrl::DoGetBestSize() const
645 wxSize sizeBtn
= wxSpinButton::DoGetBestSize();
646 sizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
649 wxGetCharSize(GetHWND(), NULL
, &y
, GetFont());
650 y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
);
652 // JACS: we should always use the height calculated
653 // from above, because otherwise we'll get a spin control
654 // that's too big. So never use the height calculated
655 // from wxSpinButton::DoGetBestSize().
657 // if ( sizeBtn.y < y )
659 // make the text tall enough
666 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
668 int widthBtn
= wxSpinButton::DoGetBestSize().x
;
669 int widthText
= width
- widthBtn
- MARGIN_BETWEEN
;
670 if ( widthText
<= 0 )
672 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
675 // 1) The buddy window
676 DoMoveSibling(m_hwndBuddy
, x
, y
, widthText
, height
);
678 // 2) The button window
679 x
+= widthText
+ MARGIN_BETWEEN
;
680 wxSpinButton::DoMoveWindow(x
, y
, widthBtn
, height
);
683 // get total size of the control
684 void wxSpinCtrl::DoGetSize(int *x
, int *y
) const
686 RECT spinrect
, textrect
, ctrlrect
;
687 GetWindowRect(GetHwnd(), &spinrect
);
688 GetWindowRect(GetBuddyHwnd(), &textrect
);
689 UnionRect(&ctrlrect
,&textrect
, &spinrect
);
692 *x
= ctrlrect
.right
- ctrlrect
.left
;
694 *y
= ctrlrect
.bottom
- ctrlrect
.top
;
697 void wxSpinCtrl::DoGetClientSize(int *x
, int *y
) const
699 RECT spinrect
= wxGetClientRect(GetHwnd());
700 RECT textrect
= wxGetClientRect(GetBuddyHwnd());
702 UnionRect(&ctrlrect
,&textrect
, &spinrect
);
705 *x
= ctrlrect
.right
- ctrlrect
.left
;
707 *y
= ctrlrect
.bottom
- ctrlrect
.top
;
710 void wxSpinCtrl::DoGetPosition(int *x
, int *y
) const
712 // hack: pretend that our HWND is the text control just for a moment
713 WXHWND hWnd
= GetHWND();
714 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= m_hwndBuddy
;
716 wxSpinButton::DoGetPosition(x
, y
);
718 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= hWnd
;
721 #endif // wxUSE_SPINCTRL