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
)
112 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxSpinButton
)
113 EVT_CHAR(wxSpinCtrl::OnChar
)
115 EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus
)
117 EVT_SPIN(wxID_ANY
, wxSpinCtrl::OnSpinChange
)
120 #define GetBuddyHwnd() (HWND)(m_hwndBuddy)
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
126 // the margin between the up-down control and its buddy (can be arbitrary,
127 // choose what you like - or may be decide during run-time depending on the
129 static const int MARGIN_BETWEEN
= 1;
131 // ============================================================================
133 // ============================================================================
135 wxArraySpins
wxSpinCtrl::ms_allSpins
;
137 // ----------------------------------------------------------------------------
138 // wnd proc for the buddy text ctrl
139 // ----------------------------------------------------------------------------
141 LRESULT APIENTRY _EXPORT
wxBuddyTextWndProc(HWND hwnd
,
146 wxSpinCtrl
*spin
= (wxSpinCtrl
*)wxGetWindowUserData(hwnd
);
148 // forward some messages (the key and focus ones only so far) to
153 // if the focus comes from the spin control itself, don't set it
154 // back to it -- we don't want to go into an infinite loop
155 if ( (WXHWND
)wParam
== spin
->GetHWND() )
164 spin
->MSWWindowProc(message
, wParam
, lParam
);
166 // The control may have been deleted at this point, so check.
167 if ( !::IsWindow(hwnd
) || wxGetWindowUserData(hwnd
) != spin
)
172 // we want to get WXK_RETURN in order to generate the event for it
173 return DLGC_WANTCHARS
;
176 return ::CallWindowProc(CASTWNDPROC spin
->GetBuddyWndProc(),
177 hwnd
, message
, wParam
, lParam
);
181 wxSpinCtrl
*wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy
)
183 wxSpinCtrl
*spin
= (wxSpinCtrl
*)wxGetWindowUserData((HWND
)hwndBuddy
);
185 int i
= ms_allSpins
.Index(spin
);
187 if ( i
== wxNOT_FOUND
)
191 wxASSERT_MSG( spin
->m_hwndBuddy
== hwndBuddy
,
192 _T("wxSpinCtrl has incorrect buddy HWND!") );
197 // process a WM_COMMAND generated by the buddy text control
198 bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd
, WXWORD
WXUNUSED(id
))
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
);
214 wxFocusEvent
event(cmd
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
217 event
.SetEventObject( this );
218 return GetEventHandler()->ProcessEvent(event
);
228 void wxSpinCtrl::OnChar(wxKeyEvent
& event
)
230 switch ( event
.GetKeyCode() )
234 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
235 InitCommandEvent(event
);
236 wxString val
= wxGetWindowText(m_hwndBuddy
);
237 event
.SetString(val
);
238 event
.SetInt(GetValue());
239 if ( GetEventHandler()->ProcessEvent(event
) )
245 // always produce navigation event - even if we process TAB
246 // ourselves the fact that we got here means that the user code
247 // decided to skip processing of this TAB - probably to let it
248 // do its default job.
250 wxNavigationKeyEvent eventNav
;
251 eventNav
.SetDirection(!event
.ShiftDown());
252 eventNav
.SetWindowChange(event
.ControlDown());
253 eventNav
.SetEventObject(this);
255 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
261 // no, we didn't process it
265 void wxSpinCtrl::OnSetFocus(wxFocusEvent
& event
)
267 // when we get focus, give it to our buddy window as it needs it more than
269 ::SetFocus((HWND
)m_hwndBuddy
);
274 // ----------------------------------------------------------------------------
276 // ----------------------------------------------------------------------------
278 bool wxSpinCtrl::Create(wxWindow
*parent
,
280 const wxString
& value
,
284 int min
, int max
, int initial
,
285 const wxString
& name
)
287 // before using DoGetBestSize(), have to set style to let the base class
288 // know whether this is a horizontal or vertical control (we're always
290 style
|= wxSP_VERTICAL
;
292 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
294 style
|= wxBORDER_SIMPLE
;
296 style
|= wxBORDER_SUNKEN
;
299 SetWindowStyle(style
);
302 WXDWORD msStyle
= MSWGetStyle(GetWindowStyle(), & exStyle
) ;
304 // calculate the sizes: the size given is the toal size for both controls
305 // and we need to fit them both in the given width (height is the same)
306 wxSize
sizeText(size
), sizeBtn(size
);
307 sizeBtn
.x
= wxSpinButton::DoGetBestSize().x
;
308 if ( sizeText
.x
<= 0 )
310 // DEFAULT_ITEM_WIDTH is the default width for the text control
311 sizeText
.x
= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
+ sizeBtn
.x
;
314 sizeText
.x
-= sizeBtn
.x
+ MARGIN_BETWEEN
;
315 if ( sizeText
.x
<= 0 )
317 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
321 posBtn
.x
+= sizeText
.x
+ MARGIN_BETWEEN
;
323 // we must create the text control before the spin button for the purpose
324 // of the dialog navigation: if there is a static text just before the spin
325 // control, activating it by Alt-letter should give focus to the text
326 // control, not the spin and the dialog navigation code will give focus to
327 // the next control (at Windows level), not the one after it
329 // create the text window
331 m_hwndBuddy
= (WXHWND
)::CreateWindowEx
333 exStyle
, // sunken border
334 _T("EDIT"), // window class
335 NULL
, // no window title
336 msStyle
, // style (will be shown later)
337 pos
.x
, pos
.y
, // position
338 0, 0, // size (will be set later)
339 GetHwndOf(parent
), // parent
340 (HMENU
)-1, // control id
341 wxGetInstance(), // app instance
342 NULL
// unused client data
347 wxLogLastError(wxT("CreateWindow(buddy text window)"));
353 // create the spin button
354 if ( !wxSpinButton::Create(parent
, id
, posBtn
, sizeBtn
, style
, name
) )
362 // subclass the text ctrl to be able to intercept some events
363 wxSetWindowUserData(GetBuddyHwnd(), this);
364 m_wndProcBuddy
= (WXFARPROC
)wxSetWindowProc(GetBuddyHwnd(),
367 // set up fonts and colours (This is nomally done in MSWCreateControl)
370 SetFont(GetDefaultAttributes().font
);
372 // set the size of the text window - can do it only now, because we
373 // couldn't call DoGetBestSize() before as font wasn't set
374 if ( sizeText
.y
<= 0 )
377 wxGetCharSize(GetHWND(), &cx
, &cy
, GetFont());
379 sizeText
.y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
384 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW
);
386 // associate the text window with the spin button
387 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY
, (WPARAM
)m_hwndBuddy
, 0);
389 if ( !value
.empty() )
394 // do it after finishing with m_hwndBuddy creation to avoid generating
395 // initial wxEVT_COMMAND_TEXT_UPDATED message
396 ms_allSpins
.Add(this);
401 wxSpinCtrl::~wxSpinCtrl()
403 ms_allSpins
.Remove(this);
405 // This removes spurious memory leak reporting
406 if (ms_allSpins
.GetCount() == 0)
409 // destroy the buddy window because this pointer which wxBuddyTextWndProc
410 // uses will not soon be valid any more
411 ::DestroyWindow(GetBuddyHwnd());
414 // ----------------------------------------------------------------------------
415 // wxTextCtrl-like methods
416 // ----------------------------------------------------------------------------
418 void wxSpinCtrl::SetValue(const wxString
& text
)
420 if ( !::SetWindowText(GetBuddyHwnd(), text
.c_str()) )
422 wxLogLastError(wxT("SetWindowText(buddy)"));
426 int wxSpinCtrl::GetValue() const
428 wxString val
= wxGetWindowText(m_hwndBuddy
);
431 if ( (wxSscanf(val
, wxT("%lu"), &n
) != 1) )
434 if (n
< m_min
) n
= m_min
;
435 if (n
> m_max
) n
= m_max
;
440 void wxSpinCtrl::SetSelection(long from
, long to
)
442 // if from and to are both -1, it means (in wxWidgets) that all text should
443 // be selected - translate into Windows convention
444 if ( (from
== -1) && (to
== -1) )
449 ::SendMessage(GetBuddyHwnd(), EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
452 // ----------------------------------------------------------------------------
453 // forward some methods to subcontrols
454 // ----------------------------------------------------------------------------
456 bool wxSpinCtrl::SetFont(const wxFont
& font
)
458 if ( !wxWindowBase::SetFont(font
) )
464 WXHANDLE hFont
= GetFont().GetResourceHandle();
465 (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT
, (WPARAM
)hFont
, TRUE
);
470 bool wxSpinCtrl::Show(bool show
)
472 if ( !wxControl::Show(show
) )
477 ::ShowWindow(GetBuddyHwnd(), show
? SW_SHOW
: SW_HIDE
);
482 bool wxSpinCtrl::Enable(bool enable
)
484 if ( !wxControl::Enable(enable
) )
489 ::EnableWindow(GetBuddyHwnd(), enable
);
494 void wxSpinCtrl::SetFocus()
496 ::SetFocus(GetBuddyHwnd());
501 void wxSpinCtrl::DoSetToolTip(wxToolTip
*tip
)
503 wxSpinButton::DoSetToolTip(tip
);
506 tip
->Add(m_hwndBuddy
);
509 #endif // wxUSE_TOOLTIPS
511 // ----------------------------------------------------------------------------
513 // ----------------------------------------------------------------------------
515 void wxSpinCtrl::OnSpinChange(wxSpinEvent
& eventSpin
)
517 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, GetId());
518 event
.SetEventObject(this);
519 event
.SetInt(eventSpin
.GetPosition());
521 (void)GetEventHandler()->ProcessEvent(event
);
523 if ( eventSpin
.GetSkipped() )
529 // ----------------------------------------------------------------------------
531 // ----------------------------------------------------------------------------
533 wxSize
wxSpinCtrl::DoGetBestSize() const
535 wxSize sizeBtn
= wxSpinButton::DoGetBestSize();
536 sizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
539 wxGetCharSize(GetHWND(), NULL
, &y
, GetFont());
540 y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
);
542 // JACS: we should always use the height calculated
543 // from above, because otherwise we'll get a spin control
544 // that's too big. So never use the height calculated
545 // from wxSpinButton::DoGetBestSize().
547 // if ( sizeBtn.y < y )
549 // make the text tall enough
556 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
558 int widthBtn
= wxSpinButton::DoGetBestSize().x
;
559 int widthText
= width
- widthBtn
- MARGIN_BETWEEN
;
560 if ( widthText
<= 0 )
562 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
565 // 1) The buddy window
566 DoMoveSibling(m_hwndBuddy
, x
, y
, widthText
, height
);
568 // 2) The button window
569 x
+= widthText
+ MARGIN_BETWEEN
;
570 wxSpinButton::DoMoveWindow(x
, y
, widthBtn
, height
);
573 // get total size of the control
574 void wxSpinCtrl::DoGetSize(int *x
, int *y
) const
576 RECT spinrect
, textrect
, ctrlrect
;
577 GetWindowRect(GetHwnd(), &spinrect
);
578 GetWindowRect(GetBuddyHwnd(), &textrect
);
579 UnionRect(&ctrlrect
,&textrect
, &spinrect
);
582 *x
= ctrlrect
.right
- ctrlrect
.left
;
584 *y
= ctrlrect
.bottom
- ctrlrect
.top
;
587 void wxSpinCtrl::DoGetPosition(int *x
, int *y
) const
589 // hack: pretend that our HWND is the text control just for a moment
590 WXHWND hWnd
= GetHWND();
591 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= m_hwndBuddy
;
593 wxSpinButton::DoGetPosition(x
, y
);
595 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= hWnd
;
598 #endif // wxUSE_SPINCTRL