1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/msw/spinbutt.cpp
3 // Purpose:     wxSpinButton
4 // Author:      Julian Smart
5 // Modified by:
6 // Created:     04/01/98
7 // Copyright:   (c) Julian Smart
8 // Licence:     wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10 
11 // ============================================================================
12 // declarations
13 // ============================================================================
14 
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18 
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21 
22 
23 #ifndef WX_PRECOMP
24     #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
25     #include "wx/app.h"
26 #endif
27 
28 #if wxUSE_SPINBTN
29 
30 #include "wx/spinbutt.h"
31 
32 #include "wx/msw/private.h"
33 
34 #ifndef UDM_SETRANGE32
35     #define UDM_SETRANGE32 (WM_USER+111)
36 #endif
37 
38 #ifndef UDM_SETPOS32
39     #define UDM_SETPOS32 (WM_USER+113)
40     #define UDM_GETPOS32 (WM_USER+114)
41 #endif
42 
43 // ============================================================================
44 // implementation
45 // ============================================================================
46 
47 // ----------------------------------------------------------------------------
48 // wxWin macros
49 // ----------------------------------------------------------------------------
50 
51 // ----------------------------------------------------------------------------
52 // wxSpinButton
53 // ----------------------------------------------------------------------------
54 
Create(wxWindow * parent,wxWindowID id,const wxPoint & pos,const wxSize & size,long style,const wxString & name)55 bool wxSpinButton::Create(wxWindow *parent,
56                           wxWindowID id,
57                           const wxPoint& pos,
58                           const wxSize& size,
59                           long style,
60                           const wxString& name)
61 {
62     // basic initialization
63     m_windowId = (id == wxID_ANY) ? NewControlId() : id;
64 
65     SetName(name);
66 
67     int x = pos.x;
68     int y = pos.y;
69     int width = size.x;
70     int height = size.y;
71 
72     m_windowStyle = style;
73 
74     SetParent(parent);
75 
76     // get the right size for the control
77     if ( width <= 0 || height <= 0 )
78     {
79         wxSize bestSize = DoGetBestSize();
80         if ( width <= 0 )
81             width = bestSize.x;
82         if ( height <= 0 )
83             height = bestSize.y;
84     }
85 
86     if ( x < 0 )
87         x = 0;
88     if ( y < 0 )
89         y = 0;
90 
91     // translate the styles
92     DWORD wstyle = WS_VISIBLE | WS_CHILD | WS_TABSTOP | /*  WS_CLIPSIBLINGS | */
93                    UDS_NOTHOUSANDS | // never useful, sometimes harmful
94                    UDS_ALIGNRIGHT  | // these styles are effectively used only
95                    UDS_SETBUDDYINT;  //  by wxSpinCtrl but do no harm otherwise
96 
97     if ( m_windowStyle & wxCLIP_SIBLINGS )
98         wstyle |= WS_CLIPSIBLINGS;
99     if ( m_windowStyle & wxSP_HORIZONTAL )
100         wstyle |= UDS_HORZ;
101     if ( m_windowStyle & wxSP_ARROW_KEYS )
102         wstyle |= UDS_ARROWKEYS;
103     if ( m_windowStyle & wxSP_WRAP )
104         wstyle |= UDS_WRAP;
105 
106     // create the UpDown control.
107     m_hWnd = (WXHWND)CreateUpDownControl
108                      (
109                        wstyle,
110                        x, y, width, height,
111                        GetHwndOf(parent),
112                        m_windowId,
113                        wxGetInstance(),
114                        NULL, // no buddy
115                        m_max, m_min,
116                        m_min // initial position
117                      );
118 
119     if ( !m_hWnd )
120     {
121         wxLogLastError(wxT("CreateUpDownControl"));
122 
123         return false;
124     }
125 
126     if ( parent )
127     {
128         parent->AddChild(this);
129     }
130 
131     SubclassWin(m_hWnd);
132 
133     SetInitialSize(size);
134 
135     return true;
136 }
137 
~wxSpinButton()138 wxSpinButton::~wxSpinButton()
139 {
140 }
141 
142 // ----------------------------------------------------------------------------
143 // size calculation
144 // ----------------------------------------------------------------------------
145 
DoGetBestSize() const146 wxSize wxSpinButton::DoGetBestSize() const
147 {
148     const bool vert = HasFlag(wxSP_VERTICAL);
149 
150     wxSize bestSize(wxGetSystemMetrics(vert ? SM_CXVSCROLL : SM_CXHSCROLL, m_parent),
151                     wxGetSystemMetrics(vert ? SM_CYVSCROLL : SM_CYHSCROLL, m_parent));
152 
153     if ( vert )
154         bestSize.y *= 2;
155     else
156         bestSize.x *= 2;
157 
158     return bestSize;
159 }
160 
161 // ----------------------------------------------------------------------------
162 // Attributes
163 // ----------------------------------------------------------------------------
164 
GetValue() const165 int wxSpinButton::GetValue() const
166 {
167     int n;
168 #ifdef UDM_GETPOS32
169     // use the full 32 bit range if available
170     n = ::SendMessage(GetHwnd(), UDM_GETPOS32, 0, 0);
171 #else
172     // we're limited to 16 bit
173     n = (short)LOWORD(::SendMessage(GetHwnd(), UDM_GETPOS, 0, 0));
174 #endif // UDM_GETPOS32
175 
176     if (n < m_min) n = m_min;
177     if (n > m_max) n = m_max;
178 
179     return n;
180 }
181 
SetValue(int val)182 void wxSpinButton::SetValue(int val)
183 {
184     // wxSpinButtonBase::SetValue(val); -- no, it is pure virtual
185 
186 #ifdef UDM_SETPOS32
187     // use the full 32 bit range if available
188     ::SendMessage(GetHwnd(), UDM_SETPOS32, 0, val);
189 #else
190     ::SendMessage(GetHwnd(), UDM_SETPOS, 0, MAKELONG((short) val, 0));
191 #endif // UDM_SETPOS32
192 }
193 
NormalizeValue()194 void wxSpinButton::NormalizeValue()
195 {
196     SetValue( GetValue() );
197 }
198 
SetRange(int minVal,int maxVal)199 void wxSpinButton::SetRange(int minVal, int maxVal)
200 {
201     const bool hadRange = m_min < m_max;
202 
203     wxSpinButtonBase::SetRange(minVal, maxVal);
204 
205 #ifdef UDM_SETRANGE32
206     // use the full 32 bit range if available
207     ::SendMessage(GetHwnd(), UDM_SETRANGE32, minVal, maxVal);
208 #else
209     // we're limited to 16 bit
210     ::SendMessage(GetHwnd(), UDM_SETRANGE, 0,
211                   (LPARAM) MAKELONG((short)maxVal, (short)minVal));
212 #endif // UDM_SETRANGE32
213 
214     // the current value might be out of the new range, force it to be in it
215     NormalizeValue();
216 
217     // if range was valid but becomes degenerated (min == max) now or vice
218     // versa then the spin buttons are automatically disabled/enabled back
219     // but don't update themselves for some reason, so do it manually
220     if ( hadRange != (m_min < m_max) )
221     {
222         // update the visual state of the button
223         Refresh();
224     }
225 }
226 
MSWOnScroll(int WXUNUSED (orientation),WXWORD wParam,WXWORD WXUNUSED (pos),WXHWND control)227 bool wxSpinButton::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
228                                WXWORD WXUNUSED(pos), WXHWND control)
229 {
230     wxCHECK_MSG( control, false, wxT("scrolling what?") );
231 
232     if ( wParam != SB_THUMBPOSITION )
233     {
234         // probable SB_ENDSCROLL - we don't react to it
235         return false;
236     }
237 
238     wxSpinEvent event(wxEVT_SCROLL_THUMBTRACK, m_windowId);
239     // We can't use 16 bit position provided in this message for spin buttons
240     // using 32 bit range.
241     event.SetPosition(GetValue());
242     event.SetEventObject(this);
243 
244     return HandleWindowEvent(event);
245 }
246 
MSWOnNotify(int WXUNUSED (idCtrl),WXLPARAM lParam,WXLPARAM * result)247 bool wxSpinButton::MSWOnNotify(int WXUNUSED(idCtrl), WXLPARAM lParam, WXLPARAM *result)
248 {
249     NM_UPDOWN *lpnmud = (NM_UPDOWN *)lParam;
250 
251     if ( lpnmud->hdr.hwndFrom != GetHwnd() || // make sure it is the right control
252          lpnmud->hdr.code != UDN_DELTAPOS )   // and the right notification
253         return false;
254 
255     int newVal = lpnmud->iPos + lpnmud->iDelta;
256     if ( newVal < m_min )
257     {
258         newVal = HasFlag(wxSP_WRAP) ? m_max : m_min;
259     }
260     else if ( newVal > m_max )
261     {
262         newVal = HasFlag(wxSP_WRAP) ? m_min : m_max;
263     }
264 
265     // Don't send an event if the value hasn't actually changed (for compatibility with wxGTK and wxOSX).
266     if ( newVal == lpnmud->iPos )
267     {
268         *result = 1;
269         return true;
270     }
271 
272     wxSpinEvent event(lpnmud->iDelta > 0 ? wxEVT_SCROLL_LINEUP
273                                          : wxEVT_SCROLL_LINEDOWN,
274                       m_windowId);
275     event.SetPosition(newVal);
276     event.SetEventObject(this);
277 
278     bool processed = HandleWindowEvent(event);
279 
280     *result = event.IsAllowed() ? 0 : 1;
281 
282     return processed;
283 }
284 
MSWCommand(WXUINT WXUNUSED (cmd),WXWORD WXUNUSED (id))285 bool wxSpinButton::MSWCommand(WXUINT WXUNUSED(cmd), WXWORD WXUNUSED(id))
286 {
287     // No command messages
288     return false;
289 }
290 
291 #endif // wxUSE_SPINBTN
292