Check for range on wxSpinCtrl and wxSpinButton's GetValue()
[wxWidgets.git] / src / msw / spinbutt.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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/app.h"
34 #endif
35
36 #if wxUSE_SPINBTN
37
38 #include "wx/spinbutt.h"
39
40 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxNotifyEvent)
41
42 #if defined(__WIN95__)
43
44 #include "wx/msw/private.h"
45
46 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
47 #include <commctrl.h>
48 #endif
49
50 // ============================================================================
51 // implementation
52 // ============================================================================
53
54 // ----------------------------------------------------------------------------
55 // wxWin macros
56 // ----------------------------------------------------------------------------
57
58
59 #if wxUSE_EXTENDED_RTTI
60 WX_DEFINE_FLAGS( wxSpinButtonStyle )
61
62 wxBEGIN_FLAGS( wxSpinButtonStyle )
63 // new style border flags, we put them first to
64 // use them for streaming out
65 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
66 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
67 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
68 wxFLAGS_MEMBER(wxBORDER_RAISED)
69 wxFLAGS_MEMBER(wxBORDER_STATIC)
70 wxFLAGS_MEMBER(wxBORDER_NONE)
71
72 // old style border flags
73 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
74 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
75 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
76 wxFLAGS_MEMBER(wxRAISED_BORDER)
77 wxFLAGS_MEMBER(wxSTATIC_BORDER)
78 wxFLAGS_MEMBER(wxBORDER)
79
80 // standard window styles
81 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
82 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
83 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
84 wxFLAGS_MEMBER(wxWANTS_CHARS)
85 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
86 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
87 wxFLAGS_MEMBER(wxVSCROLL)
88 wxFLAGS_MEMBER(wxHSCROLL)
89
90 wxFLAGS_MEMBER(wxSP_HORIZONTAL)
91 wxFLAGS_MEMBER(wxSP_VERTICAL)
92 wxFLAGS_MEMBER(wxSP_ARROW_KEYS)
93 wxFLAGS_MEMBER(wxSP_WRAP)
94
95 wxEND_FLAGS( wxSpinButtonStyle )
96
97 IMPLEMENT_DYNAMIC_CLASS_XTI(wxSpinButton, wxControl,"wx/spinbut.h")
98
99 wxBEGIN_PROPERTIES_TABLE(wxSpinButton)
100 wxEVENT_RANGE_PROPERTY( Spin , wxEVT_SCROLL_TOP , wxEVT_SCROLL_ENDSCROLL , wxSpinEvent )
101
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 , wxSpinButtonStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
106 wxEND_PROPERTIES_TABLE()
107
108 wxBEGIN_HANDLERS_TABLE(wxSpinButton)
109 wxEND_HANDLERS_TABLE()
110
111 wxCONSTRUCTOR_5( wxSpinButton , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle )
112 #else
113 IMPLEMENT_DYNAMIC_CLASS(wxSpinButton, wxControl)
114 #endif
115
116
117
118 // ----------------------------------------------------------------------------
119 // wxSpinButton
120 // ----------------------------------------------------------------------------
121
122 bool wxSpinButton::Create(wxWindow *parent,
123 wxWindowID id,
124 const wxPoint& pos,
125 const wxSize& size,
126 long style,
127 const wxString& name)
128 {
129 // basic initialization
130 m_windowId = (id == wxID_ANY) ? NewControlId() : id;
131
132 SetName(name);
133
134 int x = pos.x;
135 int y = pos.y;
136 int width = size.x;
137 int height = size.y;
138
139 m_windowStyle = style;
140
141 SetParent(parent);
142
143 // get the right size for the control
144 if ( width <= 0 || height <= 0 )
145 {
146 wxSize size = DoGetBestSize();
147 if ( width <= 0 )
148 width = size.x;
149 if ( height <= 0 )
150 height = size.y;
151 }
152
153 if ( x < 0 )
154 x = 0;
155 if ( y < 0 )
156 y = 0;
157
158 // translate the styles
159 DWORD wstyle = WS_VISIBLE | WS_CHILD | WS_TABSTOP | /* WS_CLIPSIBLINGS | */
160 UDS_NOTHOUSANDS | // never useful, sometimes harmful
161 UDS_SETBUDDYINT; // it doesn't harm if we don't have buddy
162
163 if ( m_windowStyle & wxCLIP_SIBLINGS )
164 wstyle |= WS_CLIPSIBLINGS;
165 if ( m_windowStyle & wxSP_HORIZONTAL )
166 wstyle |= UDS_HORZ;
167 if ( m_windowStyle & wxSP_ARROW_KEYS )
168 wstyle |= UDS_ARROWKEYS;
169 if ( m_windowStyle & wxSP_WRAP )
170 wstyle |= UDS_WRAP;
171
172 // create the UpDown control.
173 m_hWnd = (WXHWND)CreateUpDownControl
174 (
175 wstyle,
176 x, y, width, height,
177 GetHwndOf(parent),
178 m_windowId,
179 wxGetInstance(),
180 NULL, // no buddy
181 m_max, m_min,
182 m_min // initial position
183 );
184
185 if ( !m_hWnd )
186 {
187 wxLogLastError(wxT("CreateUpDownControl"));
188
189 return false;
190 }
191
192 if ( parent )
193 {
194 parent->AddChild(this);
195 }
196
197 SubclassWin(m_hWnd);
198
199 SetBestSize(size);
200
201 return true;
202 }
203
204 wxSpinButton::~wxSpinButton()
205 {
206 }
207
208 // ----------------------------------------------------------------------------
209 // size calculation
210 // ----------------------------------------------------------------------------
211
212 wxSize wxSpinButton::DoGetBestSize() const
213 {
214 return GetBestSpinerSize( (GetWindowStyle() & wxSP_VERTICAL) != 0 );
215 }
216
217 // ----------------------------------------------------------------------------
218 // Attributes
219 // ----------------------------------------------------------------------------
220
221 int wxSpinButton::GetValue() const
222 {
223 int n;
224 #ifdef UDM_GETPOS32
225 if ( wxTheApp->GetComCtl32Version() >= 580 )
226 {
227 // use the full 32 bit range if available
228 n = ::SendMessage(GetHwnd(), UDM_GETPOS32, 0, 0);
229 }
230 #endif // UDM_GETPOS32
231
232 // we're limited to 16 bit
233 n = (short)LOWORD(::SendMessage(GetHwnd(), UDM_GETPOS, 0, 0));
234
235 if (n < m_min) n = m_min;
236 if (n > m_max) n = m_max;
237
238 return n;
239 }
240
241 void wxSpinButton::SetValue(int val)
242 {
243 // wxSpinButtonBase::SetValue(val); -- no, it is pure virtual
244
245 #ifdef UDM_SETPOS32
246 if ( wxTheApp->GetComCtl32Version() >= 580 )
247 {
248 // use the full 32 bit range if available
249 ::SendMessage(GetHwnd(), UDM_SETPOS32, 0, val);
250 }
251 else // we're limited to 16 bit
252 #endif // UDM_SETPOS32
253 {
254 ::SendMessage(GetHwnd(), UDM_SETPOS, 0, MAKELONG((short) val, 0));
255 }
256 }
257
258 void wxSpinButton::SetRange(int minVal, int maxVal)
259 {
260 wxSpinButtonBase::SetRange(minVal, maxVal);
261
262 #ifdef UDM_SETRANGE32
263 if ( wxTheApp->GetComCtl32Version() >= 471 )
264 {
265 // use the full 32 bit range if available
266 ::SendMessage(GetHwnd(), UDM_SETRANGE32, minVal, maxVal);
267 }
268 else // we're limited to 16 bit
269 #endif // UDM_SETRANGE32
270 {
271 ::SendMessage(GetHwnd(), UDM_SETRANGE, 0,
272 (LPARAM) MAKELONG((short)maxVal, (short)minVal));
273 }
274 }
275
276 bool wxSpinButton::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
277 WXWORD pos, WXHWND control)
278 {
279 wxCHECK_MSG( control, false, wxT("scrolling what?") )
280
281 if ( wParam != SB_THUMBPOSITION )
282 {
283 // probable SB_ENDSCROLL - we don't react to it
284 return false;
285 }
286
287 wxSpinEvent event(wxEVT_SCROLL_THUMBTRACK, m_windowId);
288 event.SetPosition((short)pos); // cast is important for negative values!
289 event.SetEventObject(this);
290
291 return GetEventHandler()->ProcessEvent(event);
292 }
293
294 bool wxSpinButton::MSWOnNotify(int WXUNUSED(idCtrl), WXLPARAM lParam, WXLPARAM *result)
295 {
296 NM_UPDOWN *lpnmud = (NM_UPDOWN *)lParam;
297
298 if (lpnmud->hdr.hwndFrom != GetHwnd()) // make sure it is the right control
299 return false;
300
301 wxSpinEvent event(lpnmud->iDelta > 0 ? wxEVT_SCROLL_LINEUP
302 : wxEVT_SCROLL_LINEDOWN,
303 m_windowId);
304 event.SetPosition(lpnmud->iPos + lpnmud->iDelta);
305 event.SetEventObject(this);
306
307 bool processed = GetEventHandler()->ProcessEvent(event);
308
309 *result = event.IsAllowed() ? 0 : 1;
310
311 return processed;
312 }
313
314 bool wxSpinButton::MSWCommand(WXUINT WXUNUSED(cmd), WXWORD WXUNUSED(id))
315 {
316 // No command messages
317 return false;
318 }
319
320 #endif // __WIN95__
321
322 #endif // wxUSE_SPINBTN