1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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"
33 #include "wx/spinctrl.h"
34 #include "wx/msw/private.h"
35 #include "wx/msw/wrapcctl.h"
38 #include "wx/tooltip.h"
39 #endif // wxUSE_TOOLTIPS
41 #include <limits.h> // for INT_MIN
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 #if wxUSE_EXTENDED_RTTI
48 WX_DEFINE_FLAGS( wxSpinCtrlStyle
)
50 wxBEGIN_FLAGS( wxSpinCtrlStyle
)
51 // new style border flags, we put them first to
52 // use them for streaming out
53 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
54 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
55 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
56 wxFLAGS_MEMBER(wxBORDER_RAISED
)
57 wxFLAGS_MEMBER(wxBORDER_STATIC
)
58 wxFLAGS_MEMBER(wxBORDER_NONE
)
60 // old style border flags
61 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
62 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
63 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
64 wxFLAGS_MEMBER(wxRAISED_BORDER
)
65 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
66 wxFLAGS_MEMBER(wxBORDER
)
68 // standard window styles
69 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
70 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
71 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
72 wxFLAGS_MEMBER(wxWANTS_CHARS
)
73 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
74 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
75 wxFLAGS_MEMBER(wxVSCROLL
)
76 wxFLAGS_MEMBER(wxHSCROLL
)
78 wxFLAGS_MEMBER(wxSP_HORIZONTAL
)
79 wxFLAGS_MEMBER(wxSP_VERTICAL
)
80 wxFLAGS_MEMBER(wxSP_ARROW_KEYS
)
81 wxFLAGS_MEMBER(wxSP_WRAP
)
83 wxEND_FLAGS( wxSpinCtrlStyle
)
85 IMPLEMENT_DYNAMIC_CLASS_XTI(wxSpinCtrl
, wxControl
,"wx/spinbut.h")
87 wxBEGIN_PROPERTIES_TABLE(wxSpinCtrl
)
88 wxEVENT_RANGE_PROPERTY( Spin
, wxEVT_SCROLL_TOP
, wxEVT_SCROLL_ENDSCROLL
, wxSpinEvent
)
89 wxEVENT_PROPERTY( Updated
, wxEVT_COMMAND_SPINCTRL_UPDATED
, wxCommandEvent
)
90 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
91 wxEVENT_PROPERTY( TextEnter
, wxEVT_COMMAND_TEXT_ENTER
, wxCommandEvent
)
93 wxPROPERTY( ValueString
, wxString
, SetValue
, GetValue
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) ;
94 wxPROPERTY( Value
, int , SetValue
, GetValue
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
95 wxPROPERTY( Min
, int , SetMin
, GetMin
, 0, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
96 wxPROPERTY( Max
, int , SetMax
, GetMax
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
97 wxPROPERTY_FLAGS( WindowStyle
, wxSpinCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
100 style wxSP_ARROW_KEYS
102 wxEND_PROPERTIES_TABLE()
104 wxBEGIN_HANDLERS_TABLE(wxSpinCtrl
)
105 wxEND_HANDLERS_TABLE()
107 wxCONSTRUCTOR_6( wxSpinCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, ValueString
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
109 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
, wxControl
)
113 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxSpinButton
)
114 EVT_CHAR(wxSpinCtrl::OnChar
)
116 EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus
)
117 EVT_KILL_FOCUS(wxSpinCtrl::OnKillFocus
)
118 EVT_SPIN(wxID_ANY
, wxSpinCtrl::OnSpinChange
)
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 (the key and focus ones only so far) to
154 // if the focus comes from the spin control itself, don't set it
155 // back to it -- we don't want to go into an infinite loop
156 if ( (WXHWND
)wParam
== spin
->GetHWND() )
165 spin
->MSWWindowProc(message
, wParam
, lParam
);
167 // The control may have been deleted at this point, so check.
168 if ( !::IsWindow(hwnd
) || wxGetWindowUserData(hwnd
) != spin
)
173 // we want to get WXK_RETURN in order to generate the event for it
174 return DLGC_WANTCHARS
;
177 return ::CallWindowProc(CASTWNDPROC spin
->GetBuddyWndProc(),
178 hwnd
, message
, wParam
, lParam
);
182 wxSpinCtrl
*wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy
)
184 wxSpinCtrl
*spin
= (wxSpinCtrl
*)wxGetWindowUserData((HWND
)hwndBuddy
);
186 int i
= ms_allSpins
.Index(spin
);
188 if ( i
== wxNOT_FOUND
)
192 wxASSERT_MSG( spin
->m_hwndBuddy
== hwndBuddy
,
193 _T("wxSpinCtrl has incorrect buddy HWND!") );
198 // process a WM_COMMAND generated by the buddy text control
199 bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd
, WXWORD
WXUNUSED(id
))
205 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
206 event
.SetEventObject(this);
207 wxString val
= wxGetWindowText(m_hwndBuddy
);
208 event
.SetString(val
);
209 event
.SetInt(GetValue());
210 return GetEventHandler()->ProcessEvent(event
);
215 wxFocusEvent
event(cmd
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
218 event
.SetEventObject( this );
219 return GetEventHandler()->ProcessEvent(event
);
229 void wxSpinCtrl::OnChar(wxKeyEvent
& event
)
231 switch ( event
.GetKeyCode() )
235 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
236 InitCommandEvent(event
);
237 wxString val
= wxGetWindowText(m_hwndBuddy
);
238 event
.SetString(val
);
239 event
.SetInt(GetValue());
240 if ( GetEventHandler()->ProcessEvent(event
) )
246 // always produce navigation event - even if we process TAB
247 // ourselves the fact that we got here means that the user code
248 // decided to skip processing of this TAB - probably to let it
249 // do its default job.
251 wxNavigationKeyEvent eventNav
;
252 eventNav
.SetDirection(!event
.ShiftDown());
253 eventNav
.SetWindowChange(event
.ControlDown());
254 eventNav
.SetEventObject(this);
256 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
262 // no, we didn't process it
266 void wxSpinCtrl::OnKillFocus(wxFocusEvent
& event
)
268 // ensure that the value is shown correctly
269 SetValue(GetValue()) ;
273 void wxSpinCtrl::OnSetFocus(wxFocusEvent
& event
)
275 // when we get focus, give it to our buddy window as it needs it more than
277 ::SetFocus((HWND
)m_hwndBuddy
);
282 // ----------------------------------------------------------------------------
284 // ----------------------------------------------------------------------------
286 bool wxSpinCtrl::Create(wxWindow
*parent
,
288 const wxString
& value
,
292 int min
, int max
, int initial
,
293 const wxString
& name
)
295 // before using DoGetBestSize(), have to set style to let the base class
296 // know whether this is a horizontal or vertical control (we're always
298 style
|= wxSP_VERTICAL
;
300 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
302 style
|= wxBORDER_SIMPLE
;
304 style
|= wxBORDER_SUNKEN
;
307 SetWindowStyle(style
);
310 WXDWORD msStyle
= MSWGetStyle(GetWindowStyle(), & exStyle
) ;
312 // calculate the sizes: the size given is the toal size for both controls
313 // and we need to fit them both in the given width (height is the same)
314 wxSize
sizeText(size
), sizeBtn(size
);
315 sizeBtn
.x
= wxSpinButton::DoGetBestSize().x
;
316 if ( sizeText
.x
<= 0 )
318 // DEFAULT_ITEM_WIDTH is the default width for the text control
319 sizeText
.x
= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
+ sizeBtn
.x
;
322 sizeText
.x
-= sizeBtn
.x
+ MARGIN_BETWEEN
;
323 if ( sizeText
.x
<= 0 )
325 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
329 posBtn
.x
+= sizeText
.x
+ MARGIN_BETWEEN
;
331 // we must create the text control before the spin button for the purpose
332 // of the dialog navigation: if there is a static text just before the spin
333 // control, activating it by Alt-letter should give focus to the text
334 // control, not the spin and the dialog navigation code will give focus to
335 // the next control (at Windows level), not the one after it
337 // create the text window
339 m_hwndBuddy
= (WXHWND
)::CreateWindowEx
341 exStyle
, // sunken border
342 _T("EDIT"), // window class
343 NULL
, // no window title
344 msStyle
, // style (will be shown later)
345 pos
.x
, pos
.y
, // position
346 0, 0, // size (will be set later)
347 GetHwndOf(parent
), // parent
348 (HMENU
)-1, // control id
349 wxGetInstance(), // app instance
350 NULL
// unused client data
355 wxLogLastError(wxT("CreateWindow(buddy text window)"));
361 // create the spin button
362 if ( !wxSpinButton::Create(parent
, id
, posBtn
, sizeBtn
, style
, name
) )
370 // subclass the text ctrl to be able to intercept some events
371 wxSetWindowUserData(GetBuddyHwnd(), this);
372 m_wndProcBuddy
= (WXFARPROC
)wxSetWindowProc(GetBuddyHwnd(),
375 // set up fonts and colours (This is nomally done in MSWCreateControl)
378 SetFont(GetDefaultAttributes().font
);
380 // set the size of the text window - can do it only now, because we
381 // couldn't call DoGetBestSize() before as font wasn't set
382 if ( sizeText
.y
<= 0 )
385 wxGetCharSize(GetHWND(), &cx
, &cy
, GetFont());
387 sizeText
.y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
392 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW
);
394 // associate the text window with the spin button
395 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY
, (WPARAM
)m_hwndBuddy
, 0);
397 if ( !value
.empty() )
402 // do it after finishing with m_hwndBuddy creation to avoid generating
403 // initial wxEVT_COMMAND_TEXT_UPDATED message
404 ms_allSpins
.Add(this);
409 wxSpinCtrl::~wxSpinCtrl()
411 ms_allSpins
.Remove(this);
413 // This removes spurious memory leak reporting
414 if (ms_allSpins
.GetCount() == 0)
417 // destroy the buddy window because this pointer which wxBuddyTextWndProc
418 // uses will not soon be valid any more
419 ::DestroyWindow(GetBuddyHwnd());
422 // ----------------------------------------------------------------------------
423 // wxTextCtrl-like methods
424 // ----------------------------------------------------------------------------
426 void wxSpinCtrl::SetValue(const wxString
& text
)
428 if ( !::SetWindowText(GetBuddyHwnd(), text
.c_str()) )
430 wxLogLastError(wxT("SetWindowText(buddy)"));
434 void wxSpinCtrl::SetValue(int val
)
436 wxSpinButton::SetValue(val
);
438 // normally setting the value of the spin button is enough as it updates
439 // its buddy control automatically ...
440 if ( wxGetWindowText(m_hwndBuddy
).empty() )
442 // ... but sometimes it doesn't, notably when the value is 0 and the
443 // text control is currently empty, the spin button seems to be happy
444 // to leave it like this, while we really want to always show the
445 // current value in the control, so do it manually
446 ::SetWindowText(GetBuddyHwnd(), wxString::Format(_T("%ld"), val
));
450 int wxSpinCtrl::GetValue() const
452 wxString val
= wxGetWindowText(m_hwndBuddy
);
455 if ( (wxSscanf(val
, wxT("%ld"), &n
) != 1) )
466 void wxSpinCtrl::SetSelection(long from
, long to
)
468 // if from and to are both -1, it means (in wxWidgets) that all text should
469 // be selected - translate into Windows convention
470 if ( (from
== -1) && (to
== -1) )
475 ::SendMessage(GetBuddyHwnd(), EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
478 // ----------------------------------------------------------------------------
479 // forward some methods to subcontrols
480 // ----------------------------------------------------------------------------
482 bool wxSpinCtrl::SetFont(const wxFont
& font
)
484 if ( !wxWindowBase::SetFont(font
) )
490 WXHANDLE hFont
= GetFont().GetResourceHandle();
491 (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT
, (WPARAM
)hFont
, TRUE
);
496 bool wxSpinCtrl::Show(bool show
)
498 if ( !wxControl::Show(show
) )
503 ::ShowWindow(GetBuddyHwnd(), show
? SW_SHOW
: SW_HIDE
);
508 bool wxSpinCtrl::Enable(bool enable
)
510 if ( !wxControl::Enable(enable
) )
515 ::EnableWindow(GetBuddyHwnd(), enable
);
520 void wxSpinCtrl::SetFocus()
522 ::SetFocus(GetBuddyHwnd());
527 void wxSpinCtrl::DoSetToolTip(wxToolTip
*tip
)
529 wxSpinButton::DoSetToolTip(tip
);
532 tip
->Add(m_hwndBuddy
);
535 #endif // wxUSE_TOOLTIPS
537 // ----------------------------------------------------------------------------
539 // ----------------------------------------------------------------------------
541 void wxSpinCtrl::OnSpinChange(wxSpinEvent
& eventSpin
)
543 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, GetId());
544 event
.SetEventObject(this);
545 event
.SetInt(eventSpin
.GetPosition());
547 (void)GetEventHandler()->ProcessEvent(event
);
549 if ( eventSpin
.GetSkipped() )
555 // ----------------------------------------------------------------------------
557 // ----------------------------------------------------------------------------
559 wxSize
wxSpinCtrl::DoGetBestSize() const
561 wxSize sizeBtn
= wxSpinButton::DoGetBestSize();
562 sizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
565 wxGetCharSize(GetHWND(), NULL
, &y
, GetFont());
566 y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
);
568 // JACS: we should always use the height calculated
569 // from above, because otherwise we'll get a spin control
570 // that's too big. So never use the height calculated
571 // from wxSpinButton::DoGetBestSize().
573 // if ( sizeBtn.y < y )
575 // make the text tall enough
582 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
584 int widthBtn
= wxSpinButton::DoGetBestSize().x
;
585 int widthText
= width
- widthBtn
- MARGIN_BETWEEN
;
586 if ( widthText
<= 0 )
588 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
591 // 1) The buddy window
592 DoMoveSibling(m_hwndBuddy
, x
, y
, widthText
, height
);
594 // 2) The button window
595 x
+= widthText
+ MARGIN_BETWEEN
;
596 wxSpinButton::DoMoveWindow(x
, y
, widthBtn
, height
);
599 // get total size of the control
600 void wxSpinCtrl::DoGetSize(int *x
, int *y
) const
602 RECT spinrect
, textrect
, ctrlrect
;
603 GetWindowRect(GetHwnd(), &spinrect
);
604 GetWindowRect(GetBuddyHwnd(), &textrect
);
605 UnionRect(&ctrlrect
,&textrect
, &spinrect
);
608 *x
= ctrlrect
.right
- ctrlrect
.left
;
610 *y
= ctrlrect
.bottom
- ctrlrect
.top
;
613 void wxSpinCtrl::DoGetPosition(int *x
, int *y
) const
615 // hack: pretend that our HWND is the text control just for a moment
616 WXHWND hWnd
= GetHWND();
617 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= m_hwndBuddy
;
619 wxSpinButton::DoGetPosition(x
, y
);
621 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= hWnd
;
624 #endif // wxUSE_SPINCTRL