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
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 #if wxUSE_EXTENDED_RTTI
55 WX_DEFINE_FLAGS( wxSpinCtrlStyle
)
57 wxBEGIN_FLAGS( wxSpinCtrlStyle
)
58 // new style border flags, we put them first to
59 // use them for streaming out
60 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
61 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
62 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
63 wxFLAGS_MEMBER(wxBORDER_RAISED
)
64 wxFLAGS_MEMBER(wxBORDER_STATIC
)
65 wxFLAGS_MEMBER(wxBORDER_NONE
)
67 // old style border flags
68 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
69 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
70 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
71 wxFLAGS_MEMBER(wxRAISED_BORDER
)
72 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
73 wxFLAGS_MEMBER(wxBORDER
)
75 // standard window styles
76 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
77 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
78 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
79 wxFLAGS_MEMBER(wxWANTS_CHARS
)
80 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
81 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
82 wxFLAGS_MEMBER(wxVSCROLL
)
83 wxFLAGS_MEMBER(wxHSCROLL
)
85 wxFLAGS_MEMBER(wxSP_HORIZONTAL
)
86 wxFLAGS_MEMBER(wxSP_VERTICAL
)
87 wxFLAGS_MEMBER(wxSP_ARROW_KEYS
)
88 wxFLAGS_MEMBER(wxSP_WRAP
)
90 wxEND_FLAGS( wxSpinCtrlStyle
)
92 IMPLEMENT_DYNAMIC_CLASS_XTI(wxSpinCtrl
, wxControl
,"wx/spinbut.h")
94 wxBEGIN_PROPERTIES_TABLE(wxSpinCtrl
)
95 wxEVENT_RANGE_PROPERTY( Spin
, wxEVT_SCROLL_TOP
, wxEVT_SCROLL_ENDSCROLL
, wxSpinEvent
)
96 wxEVENT_PROPERTY( Updated
, wxEVT_COMMAND_SPINCTRL_UPDATED
, wxCommandEvent
)
97 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
98 wxEVENT_PROPERTY( TextEnter
, wxEVT_COMMAND_TEXT_ENTER
, wxCommandEvent
)
100 wxPROPERTY( ValueString
, wxString
, SetValue
, GetValue
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) ;
101 wxPROPERTY( Value
, int , SetValue
, GetValue
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
102 wxPROPERTY( Min
, int , SetMin
, GetMin
, 0, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
103 wxPROPERTY( Max
, int , SetMax
, GetMax
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
104 wxPROPERTY_FLAGS( WindowStyle
, wxSpinCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
107 style wxSP_ARROW_KEYS
109 wxEND_PROPERTIES_TABLE()
111 wxBEGIN_HANDLERS_TABLE(wxSpinCtrl
)
112 wxEND_HANDLERS_TABLE()
114 wxCONSTRUCTOR_6( wxSpinCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, ValueString
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
116 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
, wxControl
)
119 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxSpinButton
)
120 EVT_CHAR(wxSpinCtrl
::OnChar
)
122 EVT_SET_FOCUS(wxSpinCtrl
::OnSetFocus
)
124 EVT_SPIN(wxID_ANY
, wxSpinCtrl
::OnSpinChange
)
127 #define GetBuddyHwnd() (HWND)(m_hwndBuddy)
129 // ----------------------------------------------------------------------------
131 // ----------------------------------------------------------------------------
133 // the margin between the up-down control and its buddy (can be arbitrary,
134 // choose what you like - or may be decide during run-time depending on the
136 static const int MARGIN_BETWEEN
= 1;
138 // ============================================================================
140 // ============================================================================
142 wxArraySpins wxSpinCtrl
::ms_allSpins
;
144 // ----------------------------------------------------------------------------
145 // wnd proc for the buddy text ctrl
146 // ----------------------------------------------------------------------------
148 LRESULT APIENTRY _EXPORT
wxBuddyTextWndProc(HWND hwnd
,
153 wxSpinCtrl
*spin
= (wxSpinCtrl
*)wxGetWindowUserData(hwnd
);
155 // forward some messages (the key and focus ones only so far) to
160 // if the focus comes from the spin control itself, don't set it
161 // back to it -- we don't want to go into an infinite loop
162 if ( (WXHWND
)wParam
== spin
->GetHWND() )
171 spin
->MSWWindowProc(message
, wParam
, lParam
);
173 // The control may have been deleted at this point, so check.
174 if ( !::IsWindow(hwnd
) || wxGetWindowUserData(hwnd
) != spin
)
179 // we want to get WXK_RETURN in order to generate the event for it
180 return DLGC_WANTCHARS
;
183 return ::CallWindowProc(CASTWNDPROC spin
->GetBuddyWndProc(),
184 hwnd
, message
, wParam
, lParam
);
188 wxSpinCtrl
*wxSpinCtrl
::GetSpinForTextCtrl(WXHWND hwndBuddy
)
190 wxSpinCtrl
*spin
= (wxSpinCtrl
*)wxGetWindowUserData((HWND
)hwndBuddy
);
192 int i
= ms_allSpins
.Index(spin
);
194 if ( i
== wxNOT_FOUND
)
198 wxASSERT_MSG( spin
->m_hwndBuddy
== hwndBuddy
,
199 _T("wxSpinCtrl has incorrect buddy HWND!") );
204 // process a WM_COMMAND generated by the buddy text control
205 bool wxSpinCtrl
::ProcessTextCommand(WXWORD cmd
, WXWORD
WXUNUSED(id
))
211 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
212 event
.SetEventObject(this);
213 wxString val
= wxGetWindowText(m_hwndBuddy
);
214 event
.SetString(val
);
215 event
.SetInt(GetValue());
216 return GetEventHandler()->ProcessEvent(event
);
221 wxFocusEvent
event(cmd
== EN_KILLFOCUS ? wxEVT_KILL_FOCUS
224 event
.SetEventObject( this );
225 return GetEventHandler()->ProcessEvent(event
);
235 void wxSpinCtrl
::OnChar(wxKeyEvent
& event
)
237 switch ( event
.GetKeyCode() )
241 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
242 InitCommandEvent(event
);
243 wxString val
= wxGetWindowText(m_hwndBuddy
);
244 event
.SetString(val
);
245 event
.SetInt(GetValue());
246 if ( GetEventHandler()->ProcessEvent(event
) )
252 // always produce navigation event - even if we process TAB
253 // ourselves the fact that we got here means that the user code
254 // decided to skip processing of this TAB - probably to let it
255 // do its default job.
257 wxNavigationKeyEvent eventNav
;
258 eventNav
.SetDirection(!event
.ShiftDown());
259 eventNav
.SetWindowChange(event
.ControlDown());
260 eventNav
.SetEventObject(this);
262 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
268 // no, we didn't process it
272 void wxSpinCtrl
::OnSetFocus(wxFocusEvent
& event
)
274 // when we get focus, give it to our buddy window as it needs it more than
276 ::SetFocus((HWND
)m_hwndBuddy
);
281 // ----------------------------------------------------------------------------
283 // ----------------------------------------------------------------------------
285 bool wxSpinCtrl
::Create(wxWindow
*parent
,
287 const wxString
& value
,
291 int min
, int max
, int initial
,
292 const wxString
& name
)
294 // before using DoGetBestSize(), have to set style to let the base class
295 // know whether this is a horizontal or vertical control (we're always
297 style
|= wxSP_VERTICAL
;
299 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
301 style
|= wxBORDER_SIMPLE
;
303 style
|= wxBORDER_SUNKEN
;
306 SetWindowStyle(style
);
309 WXDWORD msStyle
= MSWGetStyle(GetWindowStyle(), & exStyle
) ;
311 // calculate the sizes: the size given is the toal size for both controls
312 // and we need to fit them both in the given width (height is the same)
313 wxSize
sizeText(size
), sizeBtn(size
);
314 sizeBtn
.x
= wxSpinButton
::DoGetBestSize().x
;
315 if ( sizeText
.x
<= 0 )
317 // DEFAULT_ITEM_WIDTH is the default width for the text control
318 sizeText
.x
= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
+ sizeBtn
.x
;
321 sizeText
.x
-= sizeBtn
.x
+ MARGIN_BETWEEN
;
322 if ( sizeText
.x
<= 0 )
324 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
328 posBtn
.x
+= sizeText
.x
+ MARGIN_BETWEEN
;
330 // we must create the text control before the spin button for the purpose
331 // of the dialog navigation: if there is a static text just before the spin
332 // control, activating it by Alt-letter should give focus to the text
333 // control, not the spin and the dialog navigation code will give focus to
334 // the next control (at Windows level), not the one after it
336 // create the text window
338 m_hwndBuddy
= (WXHWND
)::CreateWindowEx
340 exStyle
, // sunken border
341 _T("EDIT"), // window class
342 NULL
, // no window title
343 msStyle
, // style (will be shown later)
344 pos
.x
, pos
.y
, // position
345 0, 0, // size (will be set later)
346 GetHwndOf(parent
), // parent
347 (HMENU
)-1, // control id
348 wxGetInstance(), // app instance
349 NULL
// unused client data
354 wxLogLastError(wxT("CreateWindow(buddy text window)"));
360 // create the spin button
361 if ( !wxSpinButton
::Create(parent
, id
, posBtn
, sizeBtn
, style
, name
) )
369 // subclass the text ctrl to be able to intercept some events
370 wxSetWindowUserData(GetBuddyHwnd(), this);
371 m_wndProcBuddy
= (WXFARPROC
)wxSetWindowProc(GetBuddyHwnd(),
374 // set up fonts and colours (This is nomally done in MSWCreateControl)
377 SetFont(GetDefaultAttributes().font
);
379 // set the size of the text window - can do it only now, because we
380 // couldn't call DoGetBestSize() before as font wasn't set
381 if ( sizeText
.y
<= 0 )
384 wxGetCharSize(GetHWND(), &cx
, &cy
, GetFont());
386 sizeText
.y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
391 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW
);
393 // associate the text window with the spin button
394 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY
, (WPARAM
)m_hwndBuddy
, 0);
396 if ( !value
.IsEmpty() )
401 // do it after finishing with m_hwndBuddy creation to avoid generating
402 // initial wxEVT_COMMAND_TEXT_UPDATED message
403 ms_allSpins
.Add(this);
408 wxSpinCtrl
::~wxSpinCtrl()
410 ms_allSpins
.Remove(this);
412 // This removes spurious memory leak reporting
413 if (ms_allSpins
.GetCount() == 0)
416 // destroy the buddy window because this pointer which wxBuddyTextWndProc
417 // uses will not soon be valid any more
418 ::DestroyWindow(GetBuddyHwnd());
421 // ----------------------------------------------------------------------------
422 // wxTextCtrl-like methods
423 // ----------------------------------------------------------------------------
425 void wxSpinCtrl
::SetValue(const wxString
& text
)
427 if ( !::SetWindowText(GetBuddyHwnd(), text
.c_str()) )
429 wxLogLastError(wxT("SetWindowText(buddy)"));
433 int wxSpinCtrl
::GetValue() const
435 wxString val
= wxGetWindowText(m_hwndBuddy
);
438 if ( (wxSscanf(val
, wxT("%lu"), &n
) != 1) )
441 if (n
< m_min
) n
= m_min
;
442 if (n
> m_max
) n
= m_max
;
447 void wxSpinCtrl
::SetSelection(long from
, long to
)
449 // if from and to are both -1, it means (in wxWidgets) that all text should
450 // be selected - translate into Windows convention
451 if ( (from
== -1) && (to
== -1) )
456 ::SendMessage((HWND
)m_hwndBuddy
, EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
459 // ----------------------------------------------------------------------------
460 // forward some methods to subcontrols
461 // ----------------------------------------------------------------------------
463 bool wxSpinCtrl
::SetFont(const wxFont
& font
)
465 if ( !wxWindowBase
::SetFont(font
) )
471 WXHANDLE hFont
= GetFont().GetResourceHandle();
472 (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT
, (WPARAM
)hFont
, TRUE
);
477 bool wxSpinCtrl
::Show(bool show
)
479 if ( !wxControl
::Show(show
) )
484 ::ShowWindow(GetBuddyHwnd(), show ? SW_SHOW
: SW_HIDE
);
489 bool wxSpinCtrl
::Enable(bool enable
)
491 if ( !wxControl
::Enable(enable
) )
496 ::EnableWindow(GetBuddyHwnd(), enable
);
501 void wxSpinCtrl
::SetFocus()
503 ::SetFocus(GetBuddyHwnd());
508 void wxSpinCtrl
::DoSetToolTip(wxToolTip
*tip
)
510 wxSpinButton
::DoSetToolTip(tip
);
513 tip
->Add(m_hwndBuddy
);
516 #endif // wxUSE_TOOLTIPS
518 // ----------------------------------------------------------------------------
520 // ----------------------------------------------------------------------------
522 void wxSpinCtrl
::OnSpinChange(wxSpinEvent
& eventSpin
)
524 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, GetId());
525 event
.SetEventObject(this);
526 event
.SetInt(eventSpin
.GetPosition());
528 (void)GetEventHandler()->ProcessEvent(event
);
530 if ( eventSpin
.GetSkipped() )
536 // ----------------------------------------------------------------------------
538 // ----------------------------------------------------------------------------
540 wxSize wxSpinCtrl
::DoGetBestSize() const
542 wxSize sizeBtn
= wxSpinButton
::DoGetBestSize();
543 sizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
546 wxGetCharSize(GetHWND(), NULL
, &y
, GetFont());
547 y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
);
549 // JACS: we should always use the height calculated
550 // from above, because otherwise we'll get a spin control
551 // that's too big. So never use the height calculated
552 // from wxSpinButton::DoGetBestSize().
554 // if ( sizeBtn.y < y )
556 // make the text tall enough
563 void wxSpinCtrl
::DoMoveWindow(int x
, int y
, int width
, int height
)
565 int widthBtn
= wxSpinButton
::DoGetBestSize().x
;
566 int widthText
= width
- widthBtn
- MARGIN_BETWEEN
;
567 if ( widthText
<= 0 )
569 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
572 // if our parent had prepared a defer window handle for us, use it (unless
573 // we are a top level window)
574 wxWindowMSW
*parent
= GetParent();
577 #if USE_DEFERRED_SIZING
578 HDWP hdwp
= parent
&& !IsTopLevel() ?
(HDWP
)parent
->m_hDWP
: NULL
;
583 // 1) The buddy window
584 wxMoveWindowDeferred(hdwp
, this, GetBuddyHwnd(),
585 x
, y
, widthText
, height
);
587 // 2) The button window
588 x
+= widthText
+ MARGIN_BETWEEN
;
589 wxMoveWindowDeferred(hdwp
, this, GetHwnd(),
590 x
, y
, widthBtn
, height
);
594 // Store the size so we can report it accurately
595 wxExtraWindowData
* extraData
= (wxExtraWindowData
*) m_windowReserved
;
598 extraData
= new wxExtraWindowData
;
599 m_windowReserved
= (void*) extraData
;
601 extraData
->m_pos
= wxPoint(originalX
, y
);
602 extraData
->m_size
= wxSize(width
, height
);
603 extraData
->m_deferring
= true;
605 // hdwp must be updated as it may have been changed
606 parent
->m_hDWP
= (WXHANDLE
)hdwp
;
610 // get total size of the control
611 void wxSpinCtrl
::DoGetSize(int *x
, int *y
) const
613 wxExtraWindowData
* extraData
= (wxExtraWindowData
*) m_windowReserved
;
614 if (extraData
&& extraData
->m_deferring
&& GetParent() && GetParent()->m_hDWP
)
616 *x
= extraData
->m_size
.x
;
617 *y
= extraData
->m_size
.y
;
621 RECT spinrect
, textrect
, ctrlrect
;
622 GetWindowRect(GetHwnd(), &spinrect
);
623 GetWindowRect(GetBuddyHwnd(), &textrect
);
624 UnionRect(&ctrlrect
,&textrect
, &spinrect
);
627 *x
= ctrlrect
.right
- ctrlrect
.left
;
629 *y
= ctrlrect
.bottom
- ctrlrect
.top
;
632 void wxSpinCtrl
::DoGetPosition(int *x
, int *y
) const
634 wxExtraWindowData
* extraData
= (wxExtraWindowData
*) m_windowReserved
;
635 if (extraData
&& extraData
->m_deferring
&& GetParent() && GetParent()->m_hDWP
)
637 *x
= extraData
->m_pos
.x
;
638 *y
= extraData
->m_pos
.y
;
642 // hack: pretend that our HWND is the text control just for a moment
643 WXHWND hWnd
= GetHWND();
644 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= m_hwndBuddy
;
646 wxSpinButton
::DoGetPosition(x
, y
);
648 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= hWnd
;
651 #endif // wxUSE_SPINCTRL