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"
37 #include "wx/msw/private.h"
40 #include "wx/tooltip.h"
41 #endif // wxUSE_TOOLTIPS
43 #include <limits.h> // for INT_MIN
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 #if wxUSE_EXTENDED_RTTI
50 WX_DEFINE_FLAGS( wxSpinCtrlStyle
)
52 wxBEGIN_FLAGS( wxSpinCtrlStyle
)
53 // new style border flags, we put them first to
54 // use them for streaming out
55 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
56 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
57 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
58 wxFLAGS_MEMBER(wxBORDER_RAISED
)
59 wxFLAGS_MEMBER(wxBORDER_STATIC
)
60 wxFLAGS_MEMBER(wxBORDER_NONE
)
62 // old style border flags
63 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
64 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
65 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
66 wxFLAGS_MEMBER(wxRAISED_BORDER
)
67 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
68 wxFLAGS_MEMBER(wxBORDER
)
70 // standard window styles
71 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
72 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
73 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
74 wxFLAGS_MEMBER(wxWANTS_CHARS
)
75 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
76 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
77 wxFLAGS_MEMBER(wxVSCROLL
)
78 wxFLAGS_MEMBER(wxHSCROLL
)
80 wxFLAGS_MEMBER(wxSP_HORIZONTAL
)
81 wxFLAGS_MEMBER(wxSP_VERTICAL
)
82 wxFLAGS_MEMBER(wxSP_ARROW_KEYS
)
83 wxFLAGS_MEMBER(wxSP_WRAP
)
85 wxEND_FLAGS( wxSpinCtrlStyle
)
87 IMPLEMENT_DYNAMIC_CLASS_XTI(wxSpinCtrl
, wxControl
,"wx/spinbut.h")
89 wxBEGIN_PROPERTIES_TABLE(wxSpinCtrl
)
90 wxEVENT_RANGE_PROPERTY( Spin
, wxEVT_SCROLL_TOP
, wxEVT_SCROLL_ENDSCROLL
, wxSpinEvent
)
91 wxEVENT_PROPERTY( Updated
, wxEVT_COMMAND_SPINCTRL_UPDATED
, wxCommandEvent
)
92 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
93 wxEVENT_PROPERTY( TextEnter
, wxEVT_COMMAND_TEXT_ENTER
, wxCommandEvent
)
95 wxPROPERTY( ValueString
, wxString
, SetValue
, GetValue
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) ;
96 wxPROPERTY( Value
, int , SetValue
, GetValue
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
97 wxPROPERTY( Min
, int , SetMin
, GetMin
, 0, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
98 wxPROPERTY( Max
, int , SetMax
, GetMax
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
99 wxPROPERTY_FLAGS( WindowStyle
, wxSpinCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
102 style wxSP_ARROW_KEYS
104 wxEND_PROPERTIES_TABLE()
106 wxBEGIN_HANDLERS_TABLE(wxSpinCtrl
)
107 wxEND_HANDLERS_TABLE()
109 wxCONSTRUCTOR_6( wxSpinCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, ValueString
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
111 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
, wxControl
)
114 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxSpinButton
)
115 EVT_CHAR(wxSpinCtrl::OnChar
)
117 EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus
)
118 EVT_KILL_FOCUS(wxSpinCtrl::OnKillFocus
)
119 EVT_SPIN(wxID_ANY
, wxSpinCtrl::OnSpinChange
)
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;
133 // ============================================================================
135 // ============================================================================
137 wxArraySpins
wxSpinCtrl::ms_allSpins
;
139 // ----------------------------------------------------------------------------
140 // wnd proc for the buddy text ctrl
141 // ----------------------------------------------------------------------------
143 LRESULT APIENTRY _EXPORT
wxBuddyTextWndProc(HWND hwnd
,
148 wxSpinCtrl
*spin
= (wxSpinCtrl
*)wxGetWindowUserData(hwnd
);
150 // forward some messages (the key and focus ones only so far) to
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() )
166 spin
->MSWWindowProc(message
, wParam
, lParam
);
168 // The control may have been deleted at this point, so check.
169 if ( !::IsWindow(hwnd
) || wxGetWindowUserData(hwnd
) != spin
)
174 // we want to get WXK_RETURN in order to generate the event for it
175 return DLGC_WANTCHARS
;
178 return ::CallWindowProc(CASTWNDPROC spin
->GetBuddyWndProc(),
179 hwnd
, message
, wParam
, lParam
);
183 wxSpinCtrl
*wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy
)
185 wxSpinCtrl
*spin
= (wxSpinCtrl
*)wxGetWindowUserData((HWND
)hwndBuddy
);
187 int i
= ms_allSpins
.Index(spin
);
189 if ( i
== wxNOT_FOUND
)
193 wxASSERT_MSG( spin
->m_hwndBuddy
== hwndBuddy
,
194 _T("wxSpinCtrl has incorrect buddy HWND!") );
199 // process a WM_COMMAND generated by the buddy text control
200 bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd
, WXWORD
WXUNUSED(id
))
202 if ( cmd
== EN_CHANGE
)
204 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
205 event
.SetEventObject(this);
206 wxString val
= wxGetWindowText(m_hwndBuddy
);
207 event
.SetString(val
);
208 event
.SetInt(GetValue());
209 return GetEventHandler()->ProcessEvent(event
);
216 void wxSpinCtrl::OnChar(wxKeyEvent
& event
)
218 switch ( event
.GetKeyCode() )
222 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
223 InitCommandEvent(event
);
224 wxString val
= wxGetWindowText(m_hwndBuddy
);
225 event
.SetString(val
);
226 event
.SetInt(GetValue());
227 if ( GetEventHandler()->ProcessEvent(event
) )
233 // always produce navigation event - even if we process TAB
234 // ourselves the fact that we got here means that the user code
235 // decided to skip processing of this TAB - probably to let it
236 // do its default job.
238 wxNavigationKeyEvent eventNav
;
239 eventNav
.SetDirection(!event
.ShiftDown());
240 eventNav
.SetWindowChange(event
.ControlDown());
241 eventNav
.SetEventObject(this);
243 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
249 // no, we didn't process it
253 void wxSpinCtrl::OnKillFocus(wxFocusEvent
& event
)
255 // ensure that a correct value is shown by the control
260 void wxSpinCtrl::OnSetFocus(wxFocusEvent
& event
)
262 // when we get focus, give it to our buddy window as it needs it more than
264 ::SetFocus((HWND
)m_hwndBuddy
);
269 void wxSpinCtrl::NormalizeValue()
271 const int value
= GetValue();
272 const bool changed
= value
!= m_oldValue
;
274 // notice that we have to call SetValue() even if the value didn't change
275 // because otherwise we could be left with empty buddy control when value
276 // is 0, see comment in SetValue()
281 SendSpinUpdate(value
);
285 // ----------------------------------------------------------------------------
287 // ----------------------------------------------------------------------------
289 bool wxSpinCtrl::Create(wxWindow
*parent
,
291 const wxString
& value
,
295 int min
, int max
, int initial
,
296 const wxString
& name
)
298 // this should be in ctor/init function but I don't want to add one to 2.8
299 // to avoid problems with default ctor which can be inlined in the user
300 // code and so might not get this fix without recompilation
301 m_oldValue
= INT_MIN
;
303 // before using DoGetBestSize(), have to set style to let the base class
304 // know whether this is a horizontal or vertical control (we're always
306 style
|= wxSP_VERTICAL
;
308 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
310 style
|= wxBORDER_SIMPLE
;
312 style
|= wxBORDER_SUNKEN
;
315 SetWindowStyle(style
);
318 WXDWORD msStyle
= MSWGetStyle(GetWindowStyle(), & exStyle
) ;
320 // calculate the sizes: the size given is the toal size for both controls
321 // and we need to fit them both in the given width (height is the same)
322 wxSize
sizeText(size
), sizeBtn(size
);
323 sizeBtn
.x
= wxSpinButton::DoGetBestSize().x
;
324 if ( sizeText
.x
<= 0 )
326 // DEFAULT_ITEM_WIDTH is the default width for the text control
327 sizeText
.x
= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
+ sizeBtn
.x
;
330 sizeText
.x
-= sizeBtn
.x
+ MARGIN_BETWEEN
;
331 if ( sizeText
.x
<= 0 )
333 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
337 posBtn
.x
+= sizeText
.x
+ MARGIN_BETWEEN
;
339 // we must create the text control before the spin button for the purpose
340 // of the dialog navigation: if there is a static text just before the spin
341 // control, activating it by Alt-letter should give focus to the text
342 // control, not the spin and the dialog navigation code will give focus to
343 // the next control (at Windows level), not the one after it
345 // create the text window
347 m_hwndBuddy
= (WXHWND
)::CreateWindowEx
349 exStyle
, // sunken border
350 _T("EDIT"), // window class
351 NULL
, // no window title
352 msStyle
, // style (will be shown later)
353 pos
.x
, pos
.y
, // position
354 0, 0, // size (will be set later)
355 GetHwndOf(parent
), // parent
356 (HMENU
)-1, // control id
357 wxGetInstance(), // app instance
358 NULL
// unused client data
363 wxLogLastError(wxT("CreateWindow(buddy text window)"));
369 // create the spin button
370 if ( !wxSpinButton::Create(parent
, id
, posBtn
, sizeBtn
, style
, name
) )
375 wxSpinButtonBase::SetRange(min
, max
);
377 // subclass the text ctrl to be able to intercept some events
378 wxSetWindowUserData(GetBuddyHwnd(), this);
379 m_wndProcBuddy
= (WXFARPROC
)wxSetWindowProc(GetBuddyHwnd(),
382 // set up fonts and colours (This is nomally done in MSWCreateControl)
385 SetFont(GetDefaultAttributes().font
);
387 // set the size of the text window - can do it only now, because we
388 // couldn't call DoGetBestSize() before as font wasn't set
389 if ( sizeText
.y
<= 0 )
392 wxGetCharSize(GetHWND(), &cx
, &cy
, GetFont());
394 sizeText
.y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
397 SetInitialSize(size
);
399 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW
);
401 // associate the text window with the spin button
402 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY
, (WPARAM
)m_hwndBuddy
, 0);
406 // Set the range in the native control
409 // Send the initial event
410 SendSpinUpdate(initial
);
412 if ( !value
.empty() )
417 // do it after finishing with m_hwndBuddy creation to avoid generating
418 // initial wxEVT_COMMAND_TEXT_UPDATED message
419 ms_allSpins
.Add(this);
424 wxSpinCtrl::~wxSpinCtrl()
426 ms_allSpins
.Remove(this);
428 // This removes spurious memory leak reporting
429 if (ms_allSpins
.GetCount() == 0)
432 // destroy the buddy window because this pointer which wxBuddyTextWndProc
433 // uses will not soon be valid any more
434 ::DestroyWindow(GetBuddyHwnd());
437 // ----------------------------------------------------------------------------
438 // wxTextCtrl-like methods
439 // ----------------------------------------------------------------------------
441 void wxSpinCtrl::SetValue(const wxString
& text
)
443 if ( !::SetWindowText(GetBuddyHwnd(), text
.c_str()) )
445 wxLogLastError(wxT("SetWindowText(buddy)"));
449 void wxSpinCtrl::SetValue(int val
)
451 wxSpinButton::SetValue(val
);
453 // normally setting the value of the spin button is enough as it updates
454 // its buddy control automatically ...
455 if ( wxGetWindowText(m_hwndBuddy
).empty() )
457 // ... but sometimes it doesn't, notably when the value is 0 and the
458 // text control is currently empty, the spin button seems to be happy
459 // to leave it like this, while we really want to always show the
460 // current value in the control, so do it manually
461 ::SetWindowText(GetBuddyHwnd(), wxString::Format(_T("%d"), val
));
464 m_oldValue
= GetValue();
467 int wxSpinCtrl::GetValue() const
469 wxString val
= wxGetWindowText(m_hwndBuddy
);
472 if ( (wxSscanf(val
, wxT("%ld"), &n
) != 1) )
483 void wxSpinCtrl::SetSelection(long from
, long to
)
485 // if from and to are both -1, it means (in wxWidgets) that all text should
486 // be selected - translate into Windows convention
487 if ( (from
== -1) && (to
== -1) )
492 ::SendMessage(GetBuddyHwnd(), EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
495 // ----------------------------------------------------------------------------
496 // forward some methods to subcontrols
497 // ----------------------------------------------------------------------------
499 bool wxSpinCtrl::SetFont(const wxFont
& font
)
501 if ( !wxWindowBase::SetFont(font
) )
507 WXHANDLE hFont
= GetFont().GetResourceHandle();
508 (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT
, (WPARAM
)hFont
, TRUE
);
513 bool wxSpinCtrl::Show(bool show
)
515 if ( !wxControl::Show(show
) )
520 ::ShowWindow(GetBuddyHwnd(), show
? SW_SHOW
: SW_HIDE
);
525 bool wxSpinCtrl::Enable(bool enable
)
527 if ( !wxControl::Enable(enable
) )
532 ::EnableWindow(GetBuddyHwnd(), enable
);
537 void wxSpinCtrl::SetFocus()
539 ::SetFocus(GetBuddyHwnd());
544 void wxSpinCtrl::DoSetToolTip(wxToolTip
*tip
)
546 wxSpinButton::DoSetToolTip(tip
);
549 tip
->Add(m_hwndBuddy
);
552 #endif // wxUSE_TOOLTIPS
554 // ----------------------------------------------------------------------------
555 // events processing and generation
556 // ----------------------------------------------------------------------------
558 void wxSpinCtrl::SendSpinUpdate(int value
)
560 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, GetId());
561 event
.SetEventObject(this);
564 (void)GetEventHandler()->ProcessEvent(event
);
569 void wxSpinCtrl::OnSpinChange(wxSpinEvent
& eventSpin
)
571 const int value
= eventSpin
.GetPosition();
572 if ( value
!= m_oldValue
)
574 SendSpinUpdate(value
);
578 // ----------------------------------------------------------------------------
580 // ----------------------------------------------------------------------------
582 wxSize
wxSpinCtrl::DoGetBestSize() const
584 wxSize sizeBtn
= wxSpinButton::DoGetBestSize();
585 sizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
588 wxGetCharSize(GetHWND(), NULL
, &y
, GetFont());
589 y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
);
591 // JACS: we should always use the height calculated
592 // from above, because otherwise we'll get a spin control
593 // that's too big. So never use the height calculated
594 // from wxSpinButton::DoGetBestSize().
596 // if ( sizeBtn.y < y )
598 // make the text tall enough
605 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
607 int widthBtn
= wxSpinButton::DoGetBestSize().x
;
608 int widthText
= width
- widthBtn
- MARGIN_BETWEEN
;
609 if ( widthText
<= 0 )
611 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
614 // 1) The buddy window
615 DoMoveSibling(m_hwndBuddy
, x
, y
, widthText
, height
);
617 // 2) The button window
618 x
+= widthText
+ MARGIN_BETWEEN
;
619 wxSpinButton::DoMoveWindow(x
, y
, widthBtn
, height
);
622 // get total size of the control
623 void wxSpinCtrl::DoGetSize(int *x
, int *y
) const
625 RECT spinrect
, textrect
, ctrlrect
;
626 GetWindowRect(GetHwnd(), &spinrect
);
627 GetWindowRect(GetBuddyHwnd(), &textrect
);
628 UnionRect(&ctrlrect
,&textrect
, &spinrect
);
631 *x
= ctrlrect
.right
- ctrlrect
.left
;
633 *y
= ctrlrect
.bottom
- ctrlrect
.top
;
636 void wxSpinCtrl::DoGetPosition(int *x
, int *y
) const
638 // hack: pretend that our HWND is the text control just for a moment
639 WXHWND hWnd
= GetHWND();
640 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= m_hwndBuddy
;
642 wxSpinButton::DoGetPosition(x
, y
);
644 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= hWnd
;
647 #endif // wxUSE_SPINCTRL