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