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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
17 #pragma implementation "spinctrlbase.h"
18 #pragma implementation "spinctrl.h"
21 // ----------------------------------------------------------------------------
23 // ----------------------------------------------------------------------------
25 // for compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
38 #include "wx/spinctrl.h"
39 #include "wx/msw/private.h"
40 #include "wx/msw/wrapcctl.h"
43 #include "wx/tooltip.h"
44 #endif // wxUSE_TOOLTIPS
46 #include <limits.h> // for INT_MIN
48 #define USE_DEFERRED_SIZING 1
49 #define USE_DEFER_BUG_WORKAROUND 0
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 #if wxUSE_EXTENDED_RTTI
56 WX_DEFINE_FLAGS( wxSpinCtrlStyle
)
58 wxBEGIN_FLAGS( wxSpinCtrlStyle
)
59 // new style border flags, we put them first to
60 // use them for streaming out
61 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
62 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
63 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
64 wxFLAGS_MEMBER(wxBORDER_RAISED
)
65 wxFLAGS_MEMBER(wxBORDER_STATIC
)
66 wxFLAGS_MEMBER(wxBORDER_NONE
)
68 // old style border flags
69 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
70 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
71 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
72 wxFLAGS_MEMBER(wxRAISED_BORDER
)
73 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
74 wxFLAGS_MEMBER(wxBORDER
)
76 // standard window styles
77 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
78 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
79 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
80 wxFLAGS_MEMBER(wxWANTS_CHARS
)
81 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
82 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
83 wxFLAGS_MEMBER(wxVSCROLL
)
84 wxFLAGS_MEMBER(wxHSCROLL
)
86 wxFLAGS_MEMBER(wxSP_HORIZONTAL
)
87 wxFLAGS_MEMBER(wxSP_VERTICAL
)
88 wxFLAGS_MEMBER(wxSP_ARROW_KEYS
)
89 wxFLAGS_MEMBER(wxSP_WRAP
)
91 wxEND_FLAGS( wxSpinCtrlStyle
)
93 IMPLEMENT_DYNAMIC_CLASS_XTI(wxSpinCtrl
, wxControl
,"wx/spinbut.h")
95 wxBEGIN_PROPERTIES_TABLE(wxSpinCtrl
)
96 wxEVENT_RANGE_PROPERTY( Spin
, wxEVT_SCROLL_TOP
, wxEVT_SCROLL_ENDSCROLL
, wxSpinEvent
)
97 wxEVENT_PROPERTY( Updated
, wxEVT_COMMAND_SPINCTRL_UPDATED
, wxCommandEvent
)
98 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
99 wxEVENT_PROPERTY( TextEnter
, wxEVT_COMMAND_TEXT_ENTER
, wxCommandEvent
)
101 wxPROPERTY( ValueString
, wxString
, SetValue
, GetValue
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) ;
102 wxPROPERTY( Value
, int , SetValue
, GetValue
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
103 wxPROPERTY( Min
, int , SetMin
, GetMin
, 0, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
104 wxPROPERTY( Max
, int , SetMax
, GetMax
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
105 wxPROPERTY_FLAGS( WindowStyle
, wxSpinCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
108 style wxSP_ARROW_KEYS
110 wxEND_PROPERTIES_TABLE()
112 wxBEGIN_HANDLERS_TABLE(wxSpinCtrl
)
113 wxEND_HANDLERS_TABLE()
115 wxCONSTRUCTOR_6( wxSpinCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, ValueString
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
117 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
, wxControl
)
120 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxSpinButton
)
121 EVT_CHAR(wxSpinCtrl::OnChar
)
123 EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus
)
125 EVT_SPIN(wxID_ANY
, wxSpinCtrl::OnSpinChange
)
128 #define GetBuddyHwnd() (HWND)(m_hwndBuddy)
130 // ----------------------------------------------------------------------------
132 // ----------------------------------------------------------------------------
134 // the margin between the up-down control and its buddy (can be arbitrary,
135 // choose what you like - or may be decide during run-time depending on the
137 static const int MARGIN_BETWEEN
= 1;
139 // ============================================================================
141 // ============================================================================
143 wxArraySpins
wxSpinCtrl::ms_allSpins
;
145 // ----------------------------------------------------------------------------
146 // wnd proc for the buddy text ctrl
147 // ----------------------------------------------------------------------------
149 LRESULT APIENTRY _EXPORT
wxBuddyTextWndProc(HWND hwnd
,
154 wxSpinCtrl
*spin
= (wxSpinCtrl
*)wxGetWindowUserData(hwnd
);
156 // forward some messages (the key and focus ones only so far) to
161 // if the focus comes from the spin control itself, don't set it
162 // back to it -- we don't want to go into an infinite loop
163 if ( (WXHWND
)wParam
== spin
->GetHWND() )
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_WANTCHARS
;
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
))
212 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
213 event
.SetEventObject(this);
214 wxString val
= wxGetWindowText(m_hwndBuddy
);
215 event
.SetString(val
);
216 event
.SetInt(GetValue());
217 return GetEventHandler()->ProcessEvent(event
);
222 wxFocusEvent
event(cmd
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
225 event
.SetEventObject( this );
226 return GetEventHandler()->ProcessEvent(event
);
236 void wxSpinCtrl::OnChar(wxKeyEvent
& event
)
238 switch ( event
.GetKeyCode() )
242 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
243 InitCommandEvent(event
);
244 wxString val
= wxGetWindowText(m_hwndBuddy
);
245 event
.SetString(val
);
246 event
.SetInt(GetValue());
247 if ( GetEventHandler()->ProcessEvent(event
) )
253 // always produce navigation event - even if we process TAB
254 // ourselves the fact that we got here means that the user code
255 // decided to skip processing of this TAB - probably to let it
256 // do its default job.
258 wxNavigationKeyEvent eventNav
;
259 eventNav
.SetDirection(!event
.ShiftDown());
260 eventNav
.SetWindowChange(event
.ControlDown());
261 eventNav
.SetEventObject(this);
263 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
269 // no, we didn't process it
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
.IsEmpty() )
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 int wxSpinCtrl::GetValue() const
436 wxString val
= wxGetWindowText(m_hwndBuddy
);
439 if ( (wxSscanf(val
, wxT("%lu"), &n
) != 1) )
442 if (n
< m_min
) n
= m_min
;
443 if (n
> m_max
) n
= m_max
;
448 void wxSpinCtrl::SetSelection(long from
, long to
)
450 // if from and to are both -1, it means (in wxWidgets) that all text should
451 // be selected - translate into Windows convention
452 if ( (from
== -1) && (to
== -1) )
457 ::SendMessage((HWND
)m_hwndBuddy
, EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
460 // ----------------------------------------------------------------------------
461 // forward some methods to subcontrols
462 // ----------------------------------------------------------------------------
464 bool wxSpinCtrl::SetFont(const wxFont
& font
)
466 if ( !wxWindowBase::SetFont(font
) )
472 WXHANDLE hFont
= GetFont().GetResourceHandle();
473 (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT
, (WPARAM
)hFont
, TRUE
);
478 bool wxSpinCtrl::Show(bool show
)
480 if ( !wxControl::Show(show
) )
485 ::ShowWindow(GetBuddyHwnd(), show
? SW_SHOW
: SW_HIDE
);
490 bool wxSpinCtrl::Enable(bool enable
)
492 if ( !wxControl::Enable(enable
) )
497 ::EnableWindow(GetBuddyHwnd(), enable
);
502 void wxSpinCtrl::SetFocus()
504 ::SetFocus(GetBuddyHwnd());
509 void wxSpinCtrl::DoSetToolTip(wxToolTip
*tip
)
511 wxSpinButton::DoSetToolTip(tip
);
514 tip
->Add(m_hwndBuddy
);
517 #endif // wxUSE_TOOLTIPS
519 // ----------------------------------------------------------------------------
521 // ----------------------------------------------------------------------------
523 void wxSpinCtrl::OnSpinChange(wxSpinEvent
& eventSpin
)
525 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, GetId());
526 event
.SetEventObject(this);
527 event
.SetInt(eventSpin
.GetPosition());
529 (void)GetEventHandler()->ProcessEvent(event
);
531 if ( eventSpin
.GetSkipped() )
537 // ----------------------------------------------------------------------------
539 // ----------------------------------------------------------------------------
541 wxSize
wxSpinCtrl::DoGetBestSize() const
543 wxSize sizeBtn
= wxSpinButton::DoGetBestSize();
544 sizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
547 wxGetCharSize(GetHWND(), NULL
, &y
, GetFont());
548 y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
);
550 // JACS: we should always use the height calculated
551 // from above, because otherwise we'll get a spin control
552 // that's too big. So never use the height calculated
553 // from wxSpinButton::DoGetBestSize().
555 // if ( sizeBtn.y < y )
557 // make the text tall enough
564 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
566 int widthBtn
= wxSpinButton::DoGetBestSize().x
;
567 int widthText
= width
- widthBtn
- MARGIN_BETWEEN
;
568 if ( widthText
<= 0 )
570 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
573 // if our parent had prepared a defer window handle for us, use it (unless
574 // we are a top level window)
575 wxWindowMSW
*parent
= GetParent();
578 #if USE_DEFERRED_SIZING
579 HDWP hdwp
= parent
&& !IsTopLevel() ? (HDWP
)parent
->m_hDWP
: NULL
;
584 // 1) The buddy window
585 wxMoveWindowDeferred(hdwp
, this, GetBuddyHwnd(),
586 x
, y
, widthText
, height
);
588 // 2) The button window
589 x
+= widthText
+ MARGIN_BETWEEN
;
590 wxMoveWindowDeferred(hdwp
, this, GetHwnd(),
591 x
, y
, widthBtn
, height
);
593 #if USE_DEFERRED_SIZING
596 // hdwp must be updated as it may have been changed
597 parent
->m_hDWP
= (WXHANDLE
)hdwp
;
602 // get total size of the control
603 void wxSpinCtrl::DoGetSize(int *x
, int *y
) const
605 RECT spinrect
, textrect
, ctrlrect
;
606 GetWindowRect(GetHwnd(), &spinrect
);
607 GetWindowRect(GetBuddyHwnd(), &textrect
);
608 UnionRect(&ctrlrect
,&textrect
, &spinrect
);
611 *x
= ctrlrect
.right
- ctrlrect
.left
;
613 *y
= ctrlrect
.bottom
- ctrlrect
.top
;
616 void wxSpinCtrl::DoGetPosition(int *x
, int *y
) const
618 // hack: pretend that our HWND is the text control just for a moment
619 WXHWND hWnd
= GetHWND();
620 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= m_hwndBuddy
;
622 wxSpinButton::DoGetPosition(x
, y
);
624 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= hWnd
;
627 #endif // wxUSE_SPINCTRL