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