add full 32 bit range support to wxSpinButton/Ctrl
[wxWidgets.git] / src / msw / spinbutt.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: spinbutt.cpp
3 // Purpose: wxSpinButton
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "spinbutt.h"
22 #pragma implementation "spinbutbase.h"
23 #endif
24
25 // For compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
27
28 #ifdef __BORLANDC__
29 #pragma hdrstop
30 #endif
31
32 #ifndef WX_PRECOMP
33 #include "wx/wx.h"
34 #endif
35
36 // Can't resolve reference to CreateUpDownControl in
37 // TWIN32, but could probably use normal CreateWindow instead.
38
39 #if wxUSE_SPINBTN
40
41 #include "wx/spinbutt.h"
42
43 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxNotifyEvent)
44
45 #if defined(__WIN95__) && !defined(__TWIN32__)
46
47 #include "wx/msw/private.h"
48
49 #if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
50 #include <commctrl.h>
51 #endif
52
53 // ============================================================================
54 // implementation
55 // ============================================================================
56
57 // ----------------------------------------------------------------------------
58 // wxWin macros
59 // ----------------------------------------------------------------------------
60
61 IMPLEMENT_DYNAMIC_CLASS(wxSpinButton, wxControl)
62
63 // ----------------------------------------------------------------------------
64 // wxSpinButton
65 // ----------------------------------------------------------------------------
66
67 bool wxSpinButton::Create(wxWindow *parent,
68 wxWindowID id,
69 const wxPoint& pos,
70 const wxSize& size,
71 long style,
72 const wxString& name)
73 {
74 // basic initialization
75 InitBase();
76
77 m_windowId = (id == -1) ? NewControlId() : id;
78
79 m_backgroundColour = parent->GetBackgroundColour() ;
80 m_foregroundColour = parent->GetForegroundColour() ;
81
82 SetName(name);
83
84 int x = pos.x;
85 int y = pos.y;
86 int width = size.x;
87 int height = size.y;
88
89 m_windowStyle = style;
90
91 SetParent(parent);
92
93 // get the right size for the control
94 if ( width <= 0 || height <= 0 )
95 {
96 wxSize size = DoGetBestSize();
97 if ( width <= 0 )
98 width = size.x;
99 if ( height <= 0 )
100 height = size.y;
101 }
102
103 if ( x < 0 )
104 x = 0;
105 if ( y < 0 )
106 y = 0;
107
108 // translate the styles
109 DWORD wstyle = WS_VISIBLE | WS_CHILD | WS_TABSTOP | /* WS_CLIPSIBLINGS | */
110 UDS_NOTHOUSANDS | // never useful, sometimes harmful
111 UDS_SETBUDDYINT; // it doesn't harm if we don't have buddy
112
113 if ( m_windowStyle & wxCLIP_SIBLINGS )
114 wstyle |= WS_CLIPSIBLINGS;
115 if ( m_windowStyle & wxSP_HORIZONTAL )
116 wstyle |= UDS_HORZ;
117 if ( m_windowStyle & wxSP_ARROW_KEYS )
118 wstyle |= UDS_ARROWKEYS;
119 if ( m_windowStyle & wxSP_WRAP )
120 wstyle |= UDS_WRAP;
121
122 // create the UpDown control.
123 m_hWnd = (WXHWND)CreateUpDownControl
124 (
125 wstyle,
126 x, y, width, height,
127 GetHwndOf(parent),
128 m_windowId,
129 wxGetInstance(),
130 NULL, // no buddy
131 m_max, m_min,
132 m_min // initial position
133 );
134
135 if ( !m_hWnd )
136 {
137 wxLogLastError(wxT("CreateUpDownControl"));
138
139 return FALSE;
140 }
141
142 if ( parent )
143 {
144 parent->AddChild(this);
145 }
146
147 SubclassWin(m_hWnd);
148
149 return TRUE;
150 }
151
152 wxSpinButton::~wxSpinButton()
153 {
154 }
155
156 // ----------------------------------------------------------------------------
157 // size calculation
158 // ----------------------------------------------------------------------------
159
160 wxSize wxSpinButton::DoGetBestSize() const
161 {
162 if ( (GetWindowStyle() & wxSP_VERTICAL) != 0 )
163 {
164 // vertical control
165 return wxSize(GetSystemMetrics(SM_CXVSCROLL),
166 2*GetSystemMetrics(SM_CYVSCROLL));
167 }
168 else
169 {
170 // horizontal control
171 return wxSize(2*GetSystemMetrics(SM_CXHSCROLL),
172 GetSystemMetrics(SM_CYHSCROLL));
173 }
174 }
175
176 // ----------------------------------------------------------------------------
177 // Attributes
178 // ----------------------------------------------------------------------------
179
180 int wxSpinButton::GetValue() const
181 {
182 #ifdef UDM_GETPOS32
183 if ( wxTheApp->GetComCtl32Version() >= 580 )
184 {
185 // use the full 32 bit range if available
186 return ::SendMessage(GetHwnd(), UDM_GETPOS32, 0, 0);
187 }
188 #endif // UDM_GETPOS32
189
190 // we're limited to 16 bit
191 return (short)LOWORD(::SendMessage(GetHwnd(), UDM_GETPOS, 0, 0));
192 }
193
194 void wxSpinButton::SetValue(int val)
195 {
196 // wxSpinButtonBase::SetValue(val); -- no, it is pure virtual
197
198 #ifdef UDM_SETPOS32
199 if ( wxTheApp->GetComCtl32Version() >= 580 )
200 {
201 // use the full 32 bit range if available
202 ::SendMessage(GetHwnd(), UDM_SETPOS32, 0, val);
203 }
204 else // we're limited to 16 bit
205 #endif // UDM_SETPOS32
206 {
207 ::SendMessage(GetHwnd(), UDM_SETPOS, 0, MAKELONG((short) val, 0));
208 }
209 }
210
211 void wxSpinButton::SetRange(int minVal, int maxVal)
212 {
213 wxSpinButtonBase::SetRange(minVal, maxVal);
214
215 #ifdef UDM_SETRANGE32
216 if ( wxTheApp->GetComCtl32Version() >= 471 )
217 {
218 // use the full 32 bit range if available
219 ::SendMessage(GetHwnd(), UDM_SETRANGE32, minVal, maxVal);
220 }
221 else // we're limited to 16 bit
222 #endif // UDM_SETRANGE32
223 {
224 ::SendMessage(GetHwnd(), UDM_SETRANGE, 0,
225 (LPARAM) MAKELONG((short)maxVal, (short)minVal));
226 }
227 }
228
229 bool wxSpinButton::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
230 WXWORD pos, WXHWND control)
231 {
232 wxCHECK_MSG( control, FALSE, wxT("scrolling what?") )
233
234 if ( wParam != SB_THUMBPOSITION )
235 {
236 // probable SB_ENDSCROLL - we don't react to it
237 return FALSE;
238 }
239
240 wxSpinEvent event(wxEVT_SCROLL_THUMBTRACK, m_windowId);
241 event.SetPosition((short)pos); // cast is important for negative values!
242 event.SetEventObject(this);
243
244 return GetEventHandler()->ProcessEvent(event);
245 }
246
247 bool wxSpinButton::MSWOnNotify(int WXUNUSED(idCtrl), WXLPARAM lParam, WXLPARAM *result)
248 {
249 LPNM_UPDOWN lpnmud = (LPNM_UPDOWN)lParam;
250
251 if (lpnmud->hdr.hwndFrom != GetHwnd()) // make sure it is the right control
252 return FALSE;
253
254 wxSpinEvent event(lpnmud->iDelta > 0 ? wxEVT_SCROLL_LINEUP
255 : wxEVT_SCROLL_LINEDOWN,
256 m_windowId);
257 event.SetPosition(lpnmud->iPos + lpnmud->iDelta);
258 event.SetEventObject(this);
259
260 bool processed = GetEventHandler()->ProcessEvent(event);
261
262 *result = event.IsAllowed() ? 0 : 1;
263
264 return processed;
265 }
266
267 bool wxSpinButton::MSWCommand(WXUINT WXUNUSED(cmd), WXWORD WXUNUSED(id))
268 {
269 // No command messages
270 return FALSE;
271 }
272
273 #endif // __WIN95__
274
275 #endif
276 // wxUSE_SPINCTN
277