bec0fd007a12b48b2db1fd12a220fb271ee28573
[reactos.git] / reactos / lib / comctl32 / updown.c
1 /*
2 * Updown control
3 *
4 * Copyright 1997, 2002 Dimitrie O. Paun
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * NOTE
21 *
22 * This code was audited for completeness against the documented features
23 * of Comctl32.dll version 6.0 on Sep. 9, 2002, by Dimitrie O. Paun.
24 *
25 * Unless otherwise noted, we believe this code to be complete, as per
26 * the specification mentioned above.
27 * If you discover missing features, or bugs, please note them below.
28 *
29 */
30
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35
36 #include "windef.h"
37 #include "winbase.h"
38 #include "wingdi.h"
39 #include "winuser.h"
40 #include "winnls.h"
41 #include "commctrl.h"
42 #include "comctl32.h"
43 #include "wine/unicode.h"
44 #include "wine/debug.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(updown);
47
48 typedef struct
49 {
50 HWND Self; /* Handle to this up-down control */
51 HWND Notify; /* Handle to the parent window */
52 DWORD dwStyle; /* The GWL_STYLE for this window */
53 UINT AccelCount; /* Number of elements in AccelVect */
54 UDACCEL* AccelVect; /* Vector containing AccelCount elements */
55 INT AccelIndex; /* Current accel index, -1 if not accel'ing */
56 INT Base; /* Base to display nr in the buddy window */
57 INT CurVal; /* Current up-down value */
58 INT MinVal; /* Minimum up-down value */
59 INT MaxVal; /* Maximum up-down value */
60 HWND Buddy; /* Handle to the buddy window */
61 INT BuddyType; /* Remembers the buddy type BUDDY_TYPE_* */
62 INT Flags; /* Internal Flags FLAG_* */
63 BOOL UnicodeFormat; /* Marks the use of Unicode internally */
64 } UPDOWN_INFO;
65
66 /* Control configuration constants */
67
68 #define INITIAL_DELAY 500 /* initial timer until auto-inc kicks in */
69 #define AUTOPRESS_DELAY 250 /* time to keep arrow pressed on KEY_DOWN */
70 #define REPEAT_DELAY 50 /* delay between auto-increments */
71
72 #define DEFAULT_WIDTH 14 /* default width of the ctrl */
73 #define DEFAULT_XSEP 0 /* default separation between buddy and ctrl */
74 #define DEFAULT_ADDTOP 0 /* amount to extend above the buddy window */
75 #define DEFAULT_ADDBOT 0 /* amount to extend below the buddy window */
76 #define DEFAULT_BUDDYBORDER 2 /* Width/height of the buddy border */
77 #define DEFAULT_BUDDYSPACER 2 /* Spacer between the buddy and the ctrl */
78
79
80 /* Work constants */
81
82 #define FLAG_INCR 0x01
83 #define FLAG_DECR 0x02
84 #define FLAG_MOUSEIN 0x04
85 #define FLAG_PRESSED 0x08
86 #define FLAG_ARROW (FLAG_INCR | FLAG_DECR)
87
88 #define BUDDY_TYPE_UNKNOWN 0
89 #define BUDDY_TYPE_LISTBOX 1
90 #define BUDDY_TYPE_EDIT 2
91
92 #define TIMER_AUTOREPEAT 1
93 #define TIMER_ACCEL 2
94 #define TIMER_AUTOPRESS 3
95
96 #define UPDOWN_GetInfoPtr(hwnd) ((UPDOWN_INFO *)GetWindowLongPtrW (hwnd,0))
97 #define COUNT_OF(a) (sizeof(a)/sizeof(a[0]))
98
99 static const WCHAR BUDDY_UPDOWN_HWND[] = { 'b', 'u', 'd', 'd', 'y', 'U', 'p', 'D', 'o', 'w', 'n', 'H', 'W', 'N', 'D', 0 };
100 static const WCHAR BUDDY_SUPERCLASS_WNDPROC[] = { 'b', 'u', 'd', 'd', 'y', 'S', 'u', 'p', 'p', 'e', 'r',
101 'C', 'l', 'a', 's', 's', 'W', 'n', 'd', 'P', 'r', 'o', 'c', 0 };
102 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action);
103
104 /***********************************************************************
105 * UPDOWN_IsBuddyEdit
106 * Tests if our buddy is an edit control.
107 */
108 static inline BOOL UPDOWN_IsBuddyEdit(UPDOWN_INFO *infoPtr)
109 {
110 return infoPtr->BuddyType == BUDDY_TYPE_EDIT;
111 }
112
113 /***********************************************************************
114 * UPDOWN_IsBuddyListbox
115 * Tests if our buddy is a listbox control.
116 */
117 static inline BOOL UPDOWN_IsBuddyListbox(UPDOWN_INFO *infoPtr)
118 {
119 return infoPtr->BuddyType == BUDDY_TYPE_LISTBOX;
120 }
121
122 /***********************************************************************
123 * UPDOWN_InBounds
124 * Tests if a given value 'val' is between the Min&Max limits
125 */
126 static BOOL UPDOWN_InBounds(UPDOWN_INFO *infoPtr, int val)
127 {
128 if(infoPtr->MaxVal > infoPtr->MinVal)
129 return (infoPtr->MinVal <= val) && (val <= infoPtr->MaxVal);
130 else
131 return (infoPtr->MaxVal <= val) && (val <= infoPtr->MinVal);
132 }
133
134 /***********************************************************************
135 * UPDOWN_OffsetVal
136 * Change the current value by delta.
137 * It returns TRUE is the value was changed successfuly, or FALSE
138 * if the value was not changed, as it would go out of bounds.
139 */
140 static BOOL UPDOWN_OffsetVal(UPDOWN_INFO *infoPtr, int delta)
141 {
142 /* check if we can do the modification first */
143 if(!UPDOWN_InBounds (infoPtr, infoPtr->CurVal+delta)) {
144 if (infoPtr->dwStyle & UDS_WRAP) {
145 delta += (delta < 0 ? -1 : 1) *
146 (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1) *
147 (infoPtr->MinVal - infoPtr->MaxVal) +
148 (delta < 0 ? 1 : -1);
149 } else return FALSE;
150 }
151
152 infoPtr->CurVal += delta;
153 return TRUE;
154 }
155
156 /***********************************************************************
157 * UPDOWN_HasBuddyBorder
158 *
159 * When we have a buddy set and that we are aligned on our buddy, we
160 * want to draw a sunken edge to make like we are part of that control.
161 */
162 static BOOL UPDOWN_HasBuddyBorder(UPDOWN_INFO* infoPtr)
163 {
164 return ( ((infoPtr->dwStyle & (UDS_ALIGNLEFT | UDS_ALIGNRIGHT)) != 0) &&
165 UPDOWN_IsBuddyEdit(infoPtr) );
166 }
167
168 /***********************************************************************
169 * UPDOWN_GetArrowRect
170 * wndPtr - pointer to the up-down wnd
171 * rect - will hold the rectangle
172 * arrow - FLAG_INCR to get the "increment" rect (up or right)
173 * FLAG_DECR to get the "decrement" rect (down or left)
174 * If both flags are pressent, the envelope is returned.
175 */
176 static void UPDOWN_GetArrowRect (UPDOWN_INFO* infoPtr, RECT *rect, int arrow)
177 {
178 GetClientRect (infoPtr->Self, rect);
179
180 /*
181 * Make sure we calculate the rectangle to fit even if we draw the
182 * border.
183 */
184 if (UPDOWN_HasBuddyBorder(infoPtr)) {
185 if (infoPtr->dwStyle & UDS_ALIGNLEFT)
186 rect->left += DEFAULT_BUDDYBORDER;
187 else
188 rect->right -= DEFAULT_BUDDYBORDER;
189
190 InflateRect(rect, 0, -DEFAULT_BUDDYBORDER);
191 }
192
193 /* now figure out if we need a space away from the buddy */
194 if ( IsWindow(infoPtr->Buddy) ) {
195 if (infoPtr->dwStyle & UDS_ALIGNLEFT) rect->right -= DEFAULT_BUDDYSPACER;
196 else rect->left += DEFAULT_BUDDYSPACER;
197 }
198
199 /*
200 * We're calculating the midpoint to figure-out where the
201 * separation between the buttons will lay. We make sure that we
202 * round the uneven numbers by adding 1.
203 */
204 if (infoPtr->dwStyle & UDS_HORZ) {
205 int len = rect->right - rect->left + 1; /* compute the width */
206 if (arrow & FLAG_INCR)
207 rect->left = rect->left + len/2;
208 if (arrow & FLAG_DECR)
209 rect->right = rect->left + len/2 - 1;
210 } else {
211 int len = rect->bottom - rect->top + 1; /* compute the height */
212 if (arrow & FLAG_INCR)
213 rect->bottom = rect->top + len/2 - 1;
214 if (arrow & FLAG_DECR)
215 rect->top = rect->top + len/2;
216 }
217 }
218
219 /***********************************************************************
220 * UPDOWN_GetArrowFromPoint
221 * Returns the rectagle (for the up or down arrow) that contains pt.
222 * If it returns the up rect, it returns TRUE.
223 * If it returns the down rect, it returns FALSE.
224 */
225 static BOOL UPDOWN_GetArrowFromPoint (UPDOWN_INFO* infoPtr, RECT *rect, POINT pt)
226 {
227 UPDOWN_GetArrowRect (infoPtr, rect, FLAG_INCR);
228 if(PtInRect(rect, pt)) return FLAG_INCR;
229
230 UPDOWN_GetArrowRect (infoPtr, rect, FLAG_DECR);
231 if(PtInRect(rect, pt)) return FLAG_DECR;
232
233 return 0;
234 }
235
236
237 /***********************************************************************
238 * UPDOWN_GetThousandSep
239 * Returns the thousand sep. If an error occurs, it returns ','.
240 */
241 static WCHAR UPDOWN_GetThousandSep(void)
242 {
243 WCHAR sep[2];
244
245 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, sep, 2) != 1)
246 sep[0] = ',';
247
248 return sep[0];
249 }
250
251 /***********************************************************************
252 * UPDOWN_GetBuddyInt
253 * Tries to read the pos from the buddy window and if it succeeds,
254 * it stores it in the control's CurVal
255 * returns:
256 * TRUE - if it read the integer from the buddy successfully
257 * FALSE - if an error occurred
258 */
259 static BOOL UPDOWN_GetBuddyInt (UPDOWN_INFO *infoPtr)
260 {
261 WCHAR txt[20], sep, *src, *dst;
262 int newVal;
263
264 if (!((infoPtr->dwStyle & UDS_SETBUDDYINT) && IsWindow(infoPtr->Buddy)))
265 return FALSE;
266
267 /*if the buddy is a list window, we must set curr index */
268 if (UPDOWN_IsBuddyListbox(infoPtr)) {
269 newVal = SendMessageW(infoPtr->Buddy, LB_GETCARETINDEX, 0, 0);
270 if(newVal < 0) return FALSE;
271 } else {
272 /* we have a regular window, so will get the text */
273 /* note that a zero-length string is a legitimate value for 'txt',
274 * and ought to result in a successful conversion to '0'. */
275 if (GetWindowTextW(infoPtr->Buddy, txt, COUNT_OF(txt)) < 0)
276 return FALSE;
277
278 sep = UPDOWN_GetThousandSep();
279
280 /* now get rid of the separators */
281 for(src = dst = txt; *src; src++)
282 if(*src != sep) *dst++ = *src;
283 *dst = 0;
284
285 /* try to convert the number and validate it */
286 newVal = strtolW(txt, &src, infoPtr->Base);
287 if(*src || !UPDOWN_InBounds (infoPtr, newVal)) return FALSE;
288 }
289
290 TRACE("new value(%d) from buddy (old=%d)\n", newVal, infoPtr->CurVal);
291 infoPtr->CurVal = newVal;
292 return TRUE;
293 }
294
295
296 /***********************************************************************
297 * UPDOWN_SetBuddyInt
298 * Tries to set the pos to the buddy window based on current pos
299 * returns:
300 * TRUE - if it set the caption of the buddy successfully
301 * FALSE - if an error occurred
302 */
303 static BOOL UPDOWN_SetBuddyInt (UPDOWN_INFO *infoPtr)
304 {
305 WCHAR fmt[3] = { '%', 'd', '\0' };
306 WCHAR txt[20];
307 int len;
308
309 if (!((infoPtr->dwStyle & UDS_SETBUDDYINT) && IsWindow(infoPtr->Buddy)))
310 return FALSE;
311
312 TRACE("set new value(%d) to buddy.\n", infoPtr->CurVal);
313
314 /*if the buddy is a list window, we must set curr index */
315 if (UPDOWN_IsBuddyListbox(infoPtr)) {
316 return SendMessageW(infoPtr->Buddy, LB_SETCURSEL, infoPtr->CurVal, 0) != LB_ERR;
317 }
318
319 /* Regular window, so set caption to the number */
320 if (infoPtr->Base == 16) fmt[1] = 'X';
321 len = wsprintfW(txt, fmt, infoPtr->CurVal);
322
323
324 /* Do thousands separation if necessary */
325 if (!(infoPtr->dwStyle & UDS_NOTHOUSANDS) && (len > 3)) {
326 WCHAR tmp[COUNT_OF(txt)], *src = tmp, *dst = txt;
327 WCHAR sep = UPDOWN_GetThousandSep();
328 int start = len % 3;
329
330 memcpy(tmp, txt, sizeof(txt));
331 if (start == 0) start = 3;
332 dst += start;
333 src += start;
334 for (len=0; *src; len++) {
335 if (len % 3 == 0) *dst++ = sep;
336 *dst++ = *src++;
337 }
338 *dst = 0;
339 }
340
341 return SetWindowTextW(infoPtr->Buddy, txt);
342 }
343
344 /***********************************************************************
345 * UPDOWN_Draw
346 *
347 * Draw the arrows. The background need not be erased.
348 */
349 static LRESULT UPDOWN_Draw (UPDOWN_INFO *infoPtr, HDC hdc)
350 {
351 BOOL pressed, hot;
352 RECT rect;
353
354 /* Draw the common border between ourselves and our buddy */
355 if (UPDOWN_HasBuddyBorder(infoPtr)) {
356 GetClientRect(infoPtr->Self, &rect);
357 DrawEdge(hdc, &rect, EDGE_SUNKEN,
358 BF_BOTTOM | BF_TOP |
359 (infoPtr->dwStyle & UDS_ALIGNLEFT ? BF_LEFT : BF_RIGHT));
360 }
361
362 /* Draw the incr button */
363 UPDOWN_GetArrowRect (infoPtr, &rect, FLAG_INCR);
364 pressed = (infoPtr->Flags & FLAG_PRESSED) && (infoPtr->Flags & FLAG_INCR);
365 hot = (infoPtr->Flags & FLAG_INCR) && (infoPtr->Flags & FLAG_MOUSEIN);
366 DrawFrameControl(hdc, &rect, DFC_SCROLL,
367 (infoPtr->dwStyle & UDS_HORZ ? DFCS_SCROLLRIGHT : DFCS_SCROLLUP) |
368 ((infoPtr->dwStyle & UDS_HOTTRACK) && hot ? DFCS_HOT : 0) |
369 (pressed ? DFCS_PUSHED : 0) |
370 (infoPtr->dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
371
372 /* Draw the decr button */
373 UPDOWN_GetArrowRect(infoPtr, &rect, FLAG_DECR);
374 pressed = (infoPtr->Flags & FLAG_PRESSED) && (infoPtr->Flags & FLAG_DECR);
375 hot = (infoPtr->Flags & FLAG_DECR) && (infoPtr->Flags & FLAG_MOUSEIN);
376 DrawFrameControl(hdc, &rect, DFC_SCROLL,
377 (infoPtr->dwStyle & UDS_HORZ ? DFCS_SCROLLLEFT : DFCS_SCROLLDOWN) |
378 ((infoPtr->dwStyle & UDS_HOTTRACK) && hot ? DFCS_HOT : 0) |
379 (pressed ? DFCS_PUSHED : 0) |
380 (infoPtr->dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
381
382 return 0;
383 }
384
385 /***********************************************************************
386 * UPDOWN_Paint
387 *
388 * Asynchronous drawing (must ONLY be used in WM_PAINT).
389 * Calls UPDOWN_Draw.
390 */
391 static LRESULT UPDOWN_Paint (UPDOWN_INFO *infoPtr, HDC hdc)
392 {
393 PAINTSTRUCT ps;
394 if (hdc) return UPDOWN_Draw (infoPtr, hdc);
395 hdc = BeginPaint (infoPtr->Self, &ps);
396 UPDOWN_Draw (infoPtr, hdc);
397 EndPaint (infoPtr->Self, &ps);
398 return 0;
399 }
400
401 /***********************************************************************
402 * UPDOWN_KeyPressed
403 *
404 * Handle key presses (up & down) when we have to do so
405 */
406 static LRESULT UPDOWN_KeyPressed(UPDOWN_INFO *infoPtr, int key)
407 {
408 int arrow;
409
410 if (key == VK_UP) arrow = FLAG_INCR;
411 else if (key == VK_DOWN) arrow = FLAG_DECR;
412 else return 1;
413
414 UPDOWN_GetBuddyInt (infoPtr);
415 infoPtr->Flags &= ~FLAG_ARROW;
416 infoPtr->Flags |= FLAG_PRESSED | arrow;
417 InvalidateRect (infoPtr->Self, NULL, FALSE);
418 SetTimer(infoPtr->Self, TIMER_AUTOPRESS, AUTOPRESS_DELAY, 0);
419 UPDOWN_DoAction (infoPtr, 1, arrow);
420 return 0;
421 }
422
423 /***********************************************************************
424 * UPDOWN_Buddy_SubclassProc used to handle messages sent to the buddy
425 * control.
426 */
427 static LRESULT CALLBACK
428 UPDOWN_Buddy_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
429 {
430 WNDPROC superClassWndProc = (WNDPROC)GetPropW(hwnd, BUDDY_SUPERCLASS_WNDPROC);
431
432 TRACE("hwnd=%p, wndProc=%p, uMsg=%04x, wParam=%08x, lParam=%08lx\n",
433 hwnd, superClassWndProc, uMsg, wParam, lParam);
434
435 if (uMsg == WM_KEYDOWN) {
436 HWND upDownHwnd = GetPropW(hwnd, BUDDY_UPDOWN_HWND);
437
438 UPDOWN_KeyPressed(UPDOWN_GetInfoPtr(upDownHwnd), (int)wParam);
439 }
440
441 return CallWindowProcW( superClassWndProc, hwnd, uMsg, wParam, lParam);
442 }
443
444 /***********************************************************************
445 * UPDOWN_SetBuddy
446 *
447 * Sets bud as a new Buddy.
448 * Then, it should subclass the buddy
449 * If window has the UDS_ARROWKEYS, it subcalsses the buddy window to
450 * process the UP/DOWN arrow keys.
451 * If window has the UDS_ALIGNLEFT or UDS_ALIGNRIGHT style
452 * the size/pos of the buddy and the control are adjusted accordingly.
453 */
454 static HWND UPDOWN_SetBuddy (UPDOWN_INFO* infoPtr, HWND bud)
455 {
456 static const WCHAR editW[] = { 'E', 'd', 'i', 't', 0 };
457 static const WCHAR listboxW[] = { 'L', 'i', 's', 't', 'b', 'o', 'x', 0 };
458 RECT budRect; /* new coord for the buddy */
459 int x, width; /* new x position and width for the up-down */
460 WNDPROC baseWndProc;
461 WCHAR buddyClass[40];
462 HWND ret;
463
464 TRACE("(hwnd=%p, bud=%p)\n", infoPtr->Self, bud);
465
466 ret = infoPtr->Buddy;
467
468 /* there is already a body assigned */
469 if (infoPtr->Buddy) RemovePropW(infoPtr->Buddy, BUDDY_UPDOWN_HWND);
470
471 if(!IsWindow(bud))
472 bud = 0;
473
474 /* Store buddy window handle */
475 infoPtr->Buddy = bud;
476
477 if(bud) {
478
479 /* keep upDown ctrl hwnd in a buddy property */
480 SetPropW( bud, BUDDY_UPDOWN_HWND, infoPtr->Self);
481
482 /* Store buddy window class type */
483 infoPtr->BuddyType = BUDDY_TYPE_UNKNOWN;
484 if (GetClassNameW(bud, buddyClass, COUNT_OF(buddyClass))) {
485 if (lstrcmpiW(buddyClass, editW) == 0)
486 infoPtr->BuddyType = BUDDY_TYPE_EDIT;
487 else if (lstrcmpiW(buddyClass, listboxW) == 0)
488 infoPtr->BuddyType = BUDDY_TYPE_LISTBOX;
489 }
490
491 if(infoPtr->dwStyle & UDS_ARROWKEYS){
492 /* Note that I don't clear the BUDDY_SUPERCLASS_WNDPROC property
493 when we reset the upDown ctrl buddy to another buddy because it is not
494 good to break the window proc chain. */
495 if (!GetPropW(bud, BUDDY_SUPERCLASS_WNDPROC)) {
496 baseWndProc = (WNDPROC)SetWindowLongPtrW(bud, GWLP_WNDPROC, (LPARAM)UPDOWN_Buddy_SubclassProc);
497 SetPropW(bud, BUDDY_SUPERCLASS_WNDPROC, (HANDLE)baseWndProc);
498 }
499 }
500
501 /* Get the rect of the buddy relative to its parent */
502 GetWindowRect(infoPtr->Buddy, &budRect);
503 MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Buddy), (POINT *)(&budRect.left), 2);
504
505 /* now do the positioning */
506 if (infoPtr->dwStyle & UDS_ALIGNLEFT) {
507 x = budRect.left;
508 budRect.left += DEFAULT_WIDTH + DEFAULT_XSEP;
509 } else if (infoPtr->dwStyle & UDS_ALIGNRIGHT) {
510 budRect.right -= DEFAULT_WIDTH + DEFAULT_XSEP;
511 x = budRect.right+DEFAULT_XSEP;
512 } else {
513 /* nothing to do */
514 return ret;
515 }
516
517 /* first adjust the buddy to accommodate the up/down */
518 SetWindowPos(infoPtr->Buddy, 0, budRect.left, budRect.top,
519 budRect.right - budRect.left, budRect.bottom - budRect.top,
520 SWP_NOACTIVATE|SWP_NOZORDER);
521
522 /* now position the up/down */
523 /* Since the UDS_ALIGN* flags were used, */
524 /* we will pick the position and size of the window. */
525 width = DEFAULT_WIDTH;
526
527 /*
528 * If the updown has a buddy border, it has to overlap with the buddy
529 * to look as if it is integrated with the buddy control.
530 * We nudge the control or change its size to overlap.
531 */
532 if (UPDOWN_HasBuddyBorder(infoPtr)) {
533 if(infoPtr->dwStyle & UDS_ALIGNLEFT)
534 width += DEFAULT_BUDDYBORDER;
535 else
536 x -= DEFAULT_BUDDYBORDER;
537 }
538
539 SetWindowPos(infoPtr->Self, 0, x,
540 budRect.top - DEFAULT_ADDTOP, width,
541 budRect.bottom - budRect.top + DEFAULT_ADDTOP + DEFAULT_ADDBOT,
542 SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOZORDER);
543 } else {
544 RECT rect;
545 GetWindowRect(infoPtr->Self, &rect);
546 MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Self), (POINT *)&rect, 2);
547 SetWindowPos(infoPtr->Self, 0, rect.left, rect.top, DEFAULT_WIDTH, rect.bottom - rect.top,
548 SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOZORDER);
549 }
550 return ret;
551 }
552
553 /***********************************************************************
554 * UPDOWN_DoAction
555 *
556 * This function increments/decrements the CurVal by the
557 * 'delta' amount according to the 'action' flag which can be a
558 * combination of FLAG_INCR and FLAG_DECR
559 * It notifies the parent as required.
560 * It handles wraping and non-wraping correctly.
561 * It is assumed that delta>0
562 */
563 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action)
564 {
565 NM_UPDOWN ni;
566
567 TRACE("%d by %d\n", action, delta);
568
569 /* check if we can do the modification first */
570 delta *= (action & FLAG_INCR ? 1 : -1) * (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1);
571 if ( (action & FLAG_INCR) && (action & FLAG_DECR) ) delta = 0;
572
573 TRACE("current %d, delta: %d\n", infoPtr->CurVal, delta);
574
575 /* We must notify parent now to obtain permission */
576 ni.iPos = infoPtr->CurVal;
577 ni.iDelta = delta;
578 ni.hdr.hwndFrom = infoPtr->Self;
579 ni.hdr.idFrom = GetWindowLongPtrW (infoPtr->Self, GWLP_ID);
580 ni.hdr.code = UDN_DELTAPOS;
581 if (!SendMessageW(infoPtr->Notify, WM_NOTIFY, (WPARAM)ni.hdr.idFrom, (LPARAM)&ni)) {
582 /* Parent said: OK to adjust */
583
584 /* Now adjust value with (maybe new) delta */
585 if (UPDOWN_OffsetVal (infoPtr, ni.iDelta)) {
586 TRACE("new %d, delta: %d\n", infoPtr->CurVal, ni.iDelta);
587
588 /* Now take care about our buddy */
589 UPDOWN_SetBuddyInt (infoPtr);
590 }
591 }
592
593 /* Also, notify it. This message is sent in any case. */
594 SendMessageW( infoPtr->Notify, (infoPtr->dwStyle & UDS_HORZ) ? WM_HSCROLL : WM_VSCROLL,
595 MAKELONG(SB_THUMBPOSITION, infoPtr->CurVal), (LPARAM)infoPtr->Self);
596 }
597
598 /***********************************************************************
599 * UPDOWN_IsEnabled
600 *
601 * Returns TRUE if it is enabled as well as its buddy (if any)
602 * FALSE otherwise
603 */
604 static BOOL UPDOWN_IsEnabled (UPDOWN_INFO *infoPtr)
605 {
606 if (!IsWindowEnabled(infoPtr->Self))
607 return FALSE;
608 if(infoPtr->Buddy)
609 return IsWindowEnabled(infoPtr->Buddy);
610 return TRUE;
611 }
612
613 /***********************************************************************
614 * UPDOWN_CancelMode
615 *
616 * Deletes any timers, releases the mouse and does redraw if necessary.
617 * If the control is not in "capture" mode, it does nothing.
618 * If the control was not in cancel mode, it returns FALSE.
619 * If the control was in cancel mode, it returns TRUE.
620 */
621 static BOOL UPDOWN_CancelMode (UPDOWN_INFO *infoPtr)
622 {
623 if (!(infoPtr->Flags & FLAG_PRESSED)) return FALSE;
624
625 KillTimer (infoPtr->Self, TIMER_AUTOREPEAT);
626 KillTimer (infoPtr->Self, TIMER_ACCEL);
627 KillTimer (infoPtr->Self, TIMER_AUTOPRESS);
628
629 if (GetCapture() == infoPtr->Self) {
630 NMHDR hdr;
631 hdr.hwndFrom = infoPtr->Self;
632 hdr.idFrom = GetWindowLongPtrW (infoPtr->Self, GWLP_ID);
633 hdr.code = NM_RELEASEDCAPTURE;
634 SendMessageW(infoPtr->Notify, WM_NOTIFY, hdr.idFrom, (LPARAM)&hdr);
635 ReleaseCapture();
636 }
637
638 infoPtr->Flags &= ~FLAG_PRESSED;
639 InvalidateRect (infoPtr->Self, NULL, FALSE);
640
641 return TRUE;
642 }
643
644 /***********************************************************************
645 * UPDOWN_HandleMouseEvent
646 *
647 * Handle a mouse event for the updown.
648 * 'pt' is the location of the mouse event in client or
649 * windows coordinates.
650 */
651 static void UPDOWN_HandleMouseEvent (UPDOWN_INFO *infoPtr, UINT msg, INT x, INT y)
652 {
653 POINT pt = { x, y };
654 RECT rect;
655 int temp, arrow;
656
657 TRACE("msg %04x point %s\n", msg, wine_dbgstr_point(&pt));
658
659 switch(msg)
660 {
661 case WM_LBUTTONDOWN: /* Initialise mouse tracking */
662
663 /* If the buddy is an edit, will set focus to it */
664 if (UPDOWN_IsBuddyEdit(infoPtr)) SetFocus(infoPtr->Buddy);
665
666 /* Now see which one is the 'active' arrow */
667 arrow = UPDOWN_GetArrowFromPoint (infoPtr, &rect, pt);
668
669 /* Update the flags if we are in/out */
670 infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
671 if (arrow)
672 infoPtr->Flags |= FLAG_MOUSEIN | arrow;
673 else
674 if (infoPtr->AccelIndex != -1) infoPtr->AccelIndex = 0;
675
676 if (infoPtr->Flags & FLAG_ARROW) {
677
678 /* Update the CurVal if necessary */
679 UPDOWN_GetBuddyInt (infoPtr);
680
681 /* Set up the correct flags */
682 infoPtr->Flags |= FLAG_PRESSED;
683
684 /* repaint the control */
685 InvalidateRect (infoPtr->Self, NULL, FALSE);
686
687 /* process the click */
688 temp = (infoPtr->AccelCount && infoPtr->AccelVect) ? infoPtr->AccelVect[0].nInc : 1;
689 UPDOWN_DoAction (infoPtr, temp, infoPtr->Flags & FLAG_ARROW);
690
691 /* now capture all mouse messages */
692 SetCapture (infoPtr->Self);
693
694 /* and startup the first timer */
695 SetTimer(infoPtr->Self, TIMER_AUTOREPEAT, INITIAL_DELAY, 0);
696 }
697 break;
698
699 case WM_MOUSEMOVE:
700 /* save the flags to see if any got modified */
701 temp = infoPtr->Flags;
702
703 /* Now see which one is the 'active' arrow */
704 arrow = UPDOWN_GetArrowFromPoint (infoPtr, &rect, pt);
705
706 /* Update the flags if we are in/out */
707 infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
708 if(arrow) {
709 infoPtr->Flags |= FLAG_MOUSEIN | arrow;
710 } else {
711 if(infoPtr->AccelIndex != -1) infoPtr->AccelIndex = 0;
712 }
713
714 /* If state changed, redraw the control */
715 if(temp != infoPtr->Flags)
716 InvalidateRect (infoPtr->Self, &rect, FALSE);
717 break;
718
719 default:
720 ERR("Impossible case (msg=%x)!\n", msg);
721 }
722
723 }
724
725 /***********************************************************************
726 * UpDownWndProc
727 */
728 static LRESULT WINAPI UpDownWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
729 {
730 UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr (hwnd);
731 int temp;
732
733 TRACE("hwnd=%p msg=%04x wparam=%08x lparam=%08lx\n", hwnd, message, wParam, lParam);
734
735 if (!infoPtr && (message != WM_CREATE))
736 return DefWindowProcW (hwnd, message, wParam, lParam);
737
738 switch(message)
739 {
740 case WM_CREATE:
741 infoPtr = (UPDOWN_INFO*)Alloc (sizeof(UPDOWN_INFO));
742 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
743
744 /* initialize the info struct */
745 infoPtr->Self = hwnd;
746 infoPtr->Notify = ((LPCREATESTRUCTW)lParam)->hwndParent;
747 infoPtr->dwStyle = ((LPCREATESTRUCTW)lParam)->style;
748 infoPtr->AccelCount = 0;
749 infoPtr->AccelVect = 0;
750 infoPtr->AccelIndex = -1;
751 infoPtr->CurVal = 0;
752 infoPtr->MinVal = 100;
753 infoPtr->MaxVal = 0;
754 infoPtr->Base = 10; /* Default to base 10 */
755 infoPtr->Buddy = 0; /* No buddy window yet */
756 infoPtr->Flags = 0; /* And no flags */
757
758 SetWindowLongW (hwnd, GWL_STYLE, infoPtr->dwStyle & ~WS_BORDER);
759
760 /* Do we pick the buddy win ourselves? */
761 if (infoPtr->dwStyle & UDS_AUTOBUDDY)
762 UPDOWN_SetBuddy (infoPtr, GetWindow (hwnd, GW_HWNDPREV));
763
764 TRACE("UpDown Ctrl creation, hwnd=%p\n", hwnd);
765 break;
766
767 case WM_DESTROY:
768 if(infoPtr->AccelVect) Free (infoPtr->AccelVect);
769
770 if(infoPtr->Buddy) RemovePropW(infoPtr->Buddy, BUDDY_UPDOWN_HWND);
771
772 Free (infoPtr);
773 SetWindowLongPtrW (hwnd, 0, 0);
774 TRACE("UpDown Ctrl destruction, hwnd=%p\n", hwnd);
775 break;
776
777 case WM_ENABLE:
778 if (wParam) {
779 infoPtr->dwStyle &= ~WS_DISABLED;
780 } else {
781 infoPtr->dwStyle |= WS_DISABLED;
782 UPDOWN_CancelMode (infoPtr);
783 }
784 InvalidateRect (infoPtr->Self, NULL, FALSE);
785 break;
786
787 case WM_STYLECHANGED:
788 if (wParam == GWL_STYLE) {
789 infoPtr->dwStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
790 InvalidateRect (infoPtr->Self, NULL, FALSE);
791 }
792 break;
793
794 case WM_TIMER:
795 /* is this the auto-press timer? */
796 if(wParam == TIMER_AUTOPRESS) {
797 KillTimer(hwnd, TIMER_AUTOPRESS);
798 infoPtr->Flags &= ~(FLAG_PRESSED | FLAG_ARROW);
799 InvalidateRect(infoPtr->Self, NULL, FALSE);
800 }
801
802 /* if initial timer, kill it and start the repeat timer */
803 if(wParam == TIMER_AUTOREPEAT) {
804 KillTimer(hwnd, TIMER_AUTOREPEAT);
805 /* if no accel info given, used default timer */
806 if(infoPtr->AccelCount==0 || infoPtr->AccelVect==0) {
807 infoPtr->AccelIndex = -1;
808 temp = REPEAT_DELAY;
809 } else {
810 infoPtr->AccelIndex = 0; /* otherwise, use it */
811 temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
812 }
813 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
814 }
815
816 /* now, if the mouse is above us, do the thing...*/
817 if(infoPtr->Flags & FLAG_MOUSEIN) {
818 temp = infoPtr->AccelIndex == -1 ? 1 : infoPtr->AccelVect[infoPtr->AccelIndex].nInc;
819 UPDOWN_DoAction(infoPtr, temp, infoPtr->Flags & FLAG_ARROW);
820
821 if(infoPtr->AccelIndex != -1 && infoPtr->AccelIndex < infoPtr->AccelCount-1) {
822 KillTimer(hwnd, TIMER_ACCEL);
823 infoPtr->AccelIndex++; /* move to the next accel info */
824 temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
825 /* make sure we have at least 1ms intervals */
826 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
827 }
828 }
829 break;
830
831 case WM_CANCELMODE:
832 return UPDOWN_CancelMode (infoPtr);
833
834 case WM_LBUTTONUP:
835 if (GetCapture() != infoPtr->Self) break;
836
837 if ( (infoPtr->Flags & FLAG_MOUSEIN) &&
838 (infoPtr->Flags & FLAG_ARROW) ) {
839
840 SendMessageW( infoPtr->Notify,
841 (infoPtr->dwStyle & UDS_HORZ) ? WM_HSCROLL : WM_VSCROLL,
842 MAKELONG(SB_ENDSCROLL, infoPtr->CurVal),
843 (LPARAM)hwnd);
844 if (UPDOWN_IsBuddyEdit(infoPtr))
845 SendMessageW(infoPtr->Buddy, EM_SETSEL, 0, MAKELONG(0, -1));
846 }
847 UPDOWN_CancelMode(infoPtr);
848 break;
849
850 case WM_LBUTTONDOWN:
851 case WM_MOUSEMOVE:
852 if(UPDOWN_IsEnabled(infoPtr))
853 UPDOWN_HandleMouseEvent (infoPtr, message, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
854 break;
855
856 case WM_KEYDOWN:
857 if((infoPtr->dwStyle & UDS_ARROWKEYS) && UPDOWN_IsEnabled(infoPtr))
858 return UPDOWN_KeyPressed(infoPtr, (int)wParam);
859 break;
860
861 case WM_PAINT:
862 return UPDOWN_Paint (infoPtr, (HDC)wParam);
863
864 case UDM_GETACCEL:
865 if (wParam==0 && lParam==0) return infoPtr->AccelCount;
866 if (wParam && lParam) {
867 temp = min(infoPtr->AccelCount, wParam);
868 memcpy((void *)lParam, infoPtr->AccelVect, temp*sizeof(UDACCEL));
869 return temp;
870 }
871 return 0;
872
873 case UDM_SETACCEL:
874 TRACE("UDM_SETACCEL\n");
875
876 if(infoPtr->AccelVect) {
877 Free (infoPtr->AccelVect);
878 infoPtr->AccelCount = 0;
879 infoPtr->AccelVect = 0;
880 }
881 if(wParam==0) return TRUE;
882 infoPtr->AccelVect = Alloc (wParam*sizeof(UDACCEL));
883 if(infoPtr->AccelVect == 0) return FALSE;
884 memcpy(infoPtr->AccelVect, (void*)lParam, wParam*sizeof(UDACCEL));
885 infoPtr->AccelCount = wParam;
886
887 for (temp = 0; temp < wParam; temp++)
888 TRACE("%d: nSec %u nInc %u\n", temp, infoPtr->AccelVect[temp].nSec, infoPtr->AccelVect[temp].nInc);
889
890 return TRUE;
891
892 case UDM_GETBASE:
893 return infoPtr->Base;
894
895 case UDM_SETBASE:
896 TRACE("UpDown Ctrl new base(%d), hwnd=%p\n", wParam, hwnd);
897 if (wParam==10 || wParam==16) {
898 temp = infoPtr->Base;
899 infoPtr->Base = wParam;
900 return temp;
901 }
902 break;
903
904 case UDM_GETBUDDY:
905 return (LRESULT)infoPtr->Buddy;
906
907 case UDM_SETBUDDY:
908 return (LRESULT)UPDOWN_SetBuddy (infoPtr, (HWND)wParam);
909
910 case UDM_GETPOS:
911 temp = UPDOWN_GetBuddyInt (infoPtr);
912 return MAKELONG(infoPtr->CurVal, temp ? 0 : 1);
913
914 case UDM_SETPOS:
915 temp = (short)LOWORD(lParam);
916 TRACE("UpDown Ctrl new value(%d), hwnd=%p\n", temp, hwnd);
917 if(!UPDOWN_InBounds(infoPtr, temp)) {
918 if(temp < infoPtr->MinVal) temp = infoPtr->MinVal;
919 if(temp > infoPtr->MaxVal) temp = infoPtr->MaxVal;
920 }
921 wParam = infoPtr->CurVal;
922 infoPtr->CurVal = temp;
923 UPDOWN_SetBuddyInt (infoPtr);
924 return wParam; /* return prev value */
925
926 case UDM_GETRANGE:
927 return MAKELONG(infoPtr->MaxVal, infoPtr->MinVal);
928
929 case UDM_SETRANGE:
930 /* we must have: */
931 infoPtr->MaxVal = (short)(lParam); /* UD_MINVAL <= Max <= UD_MAXVAL */
932 infoPtr->MinVal = (short)HIWORD(lParam); /* UD_MINVAL <= Min <= UD_MAXVAL */
933 /* |Max-Min| <= UD_MAXVAL */
934 TRACE("UpDown Ctrl new range(%d to %d), hwnd=%p\n",
935 infoPtr->MinVal, infoPtr->MaxVal, hwnd);
936 break;
937
938 case UDM_GETRANGE32:
939 if (wParam) *(LPINT)wParam = infoPtr->MinVal;
940 if (lParam) *(LPINT)lParam = infoPtr->MaxVal;
941 break;
942
943 case UDM_SETRANGE32:
944 infoPtr->MinVal = (INT)wParam;
945 infoPtr->MaxVal = (INT)lParam;
946 if (infoPtr->MaxVal <= infoPtr->MinVal)
947 infoPtr->MaxVal = infoPtr->MinVal + 1;
948 TRACE("UpDown Ctrl new range(%d to %d), hwnd=%p\n",
949 infoPtr->MinVal, infoPtr->MaxVal, hwnd);
950 break;
951
952 case UDM_GETPOS32:
953 if ((LPBOOL)lParam != NULL) *((LPBOOL)lParam) = TRUE;
954 return infoPtr->CurVal;
955
956 case UDM_SETPOS32:
957 if(!UPDOWN_InBounds(infoPtr, (int)lParam)) {
958 if((int)lParam < infoPtr->MinVal) lParam = infoPtr->MinVal;
959 if((int)lParam > infoPtr->MaxVal) lParam = infoPtr->MaxVal;
960 }
961 temp = infoPtr->CurVal; /* save prev value */
962 infoPtr->CurVal = (int)lParam; /* set the new value */
963 UPDOWN_SetBuddyInt (infoPtr);
964 return temp; /* return prev value */
965
966 case UDM_GETUNICODEFORMAT:
967 /* we lie a bit here, we're always using Unicode internally */
968 return infoPtr->UnicodeFormat;
969
970 case UDM_SETUNICODEFORMAT:
971 /* do we really need to honour this flag? */
972 temp = infoPtr->UnicodeFormat;
973 infoPtr->UnicodeFormat = (BOOL)wParam;
974 return temp;
975
976 default:
977 if ((message >= WM_USER) && (message < WM_APP))
978 ERR("unknown msg %04x wp=%04x lp=%08lx\n", message, wParam, lParam);
979 return DefWindowProcW (hwnd, message, wParam, lParam);
980 }
981
982 return 0;
983 }
984
985 /***********************************************************************
986 * UPDOWN_Register [Internal]
987 *
988 * Registers the updown window class.
989 */
990 void UPDOWN_Register(void)
991 {
992 WNDCLASSW wndClass;
993
994 ZeroMemory( &wndClass, sizeof( WNDCLASSW ) );
995 wndClass.style = CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
996 wndClass.lpfnWndProc = UpDownWindowProc;
997 wndClass.cbClsExtra = 0;
998 wndClass.cbWndExtra = sizeof(UPDOWN_INFO*);
999 wndClass.hCursor = LoadCursorW( 0, (LPWSTR)IDC_ARROW );
1000 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
1001 wndClass.lpszClassName = UPDOWN_CLASSW;
1002
1003 RegisterClassW( &wndClass );
1004 }
1005
1006
1007 /***********************************************************************
1008 * UPDOWN_Unregister [Internal]
1009 *
1010 * Unregisters the updown window class.
1011 */
1012 void UPDOWN_Unregister (void)
1013 {
1014 UnregisterClassW (UPDOWN_CLASSW, NULL);
1015 }