Make the standard create_file request handle serial ports too, and
[wine.git] / dlls / comctl32 / updown.c
blob2e26d77676063fd5227748778e0624bca0be620b
1 /*
2 * Updown control
4 * Copyright 1997, 2002 Dimitrie O. Paun
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.
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.
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
20 * NOTE
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.
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.
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdarg.h>
34 #include <stdio.h>
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"
46 WINE_DEFAULT_DEBUG_CHANNEL(updown);
48 typedef struct
50 HWND Self; /* Handle to this up-down control */
51 HWND Notify; /* Handle to the parent window */
52 UINT AccelCount; /* Number of elements in AccelVect */
53 UDACCEL* AccelVect; /* Vector containing AccelCount elements */
54 INT AccelIndex; /* Current accel index, -1 if not accel'ing */
55 INT Base; /* Base to display nr in the buddy window */
56 INT CurVal; /* Current up-down value */
57 INT MinVal; /* Minimum up-down value */
58 INT MaxVal; /* Maximum up-down value */
59 HWND Buddy; /* Handle to the buddy window */
60 INT BuddyType; /* Remembers the buddy type BUDDY_TYPE_* */
61 INT Flags; /* Internal Flags FLAG_* */
62 BOOL UnicodeFormat; /* Marks the use of Unicode internally */
63 } UPDOWN_INFO;
65 /* Control configuration constants */
67 #define INITIAL_DELAY 500 /* initial timer until auto-inc kicks in */
68 #define AUTOPRESS_DELAY 250 /* time to keep arrow pressed on KEY_DOWN */
69 #define REPEAT_DELAY 50 /* delay between auto-increments */
71 #define DEFAULT_WIDTH 14 /* default width of the ctrl */
72 #define DEFAULT_XSEP 0 /* default separation between buddy and ctrl */
73 #define DEFAULT_ADDTOP 0 /* amount to extend above the buddy window */
74 #define DEFAULT_ADDBOT 0 /* amount to extend below the buddy window */
75 #define DEFAULT_BUDDYBORDER 2 /* Width/height of the buddy border */
76 #define DEFAULT_BUDDYSPACER 2 /* Spacer between the buddy and the ctrl */
79 /* Work constants */
81 #define FLAG_INCR 0x01
82 #define FLAG_DECR 0x02
83 #define FLAG_MOUSEIN 0x04
84 #define FLAG_PRESSED 0x08
85 #define FLAG_ARROW (FLAG_INCR | FLAG_DECR)
87 #define BUDDY_TYPE_UNKNOWN 0
88 #define BUDDY_TYPE_LISTBOX 1
89 #define BUDDY_TYPE_EDIT 2
91 #define TIMER_AUTOREPEAT 1
92 #define TIMER_ACCEL 2
93 #define TIMER_AUTOPRESS 3
95 #define BUDDY_UPDOWN_HWND "buddyUpDownHWND"
96 #define BUDDY_SUPERCLASS_WNDPROC "buddySupperClassWndProc"
98 #define UPDOWN_GetInfoPtr(hwnd) ((UPDOWN_INFO *)GetWindowLongA (hwnd,0))
99 #define COUNT_OF(a) (sizeof(a)/sizeof(a[0]))
101 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action);
103 /***********************************************************************
104 * UPDOWN_IsBuddyEdit
105 * Tests if our buddy is an edit control.
107 static inline BOOL UPDOWN_IsBuddyEdit(UPDOWN_INFO *infoPtr)
109 return infoPtr->BuddyType == BUDDY_TYPE_EDIT;
112 /***********************************************************************
113 * UPDOWN_IsBuddyListbox
114 * Tests if our buddy is a listbox control.
116 static inline BOOL UPDOWN_IsBuddyListbox(UPDOWN_INFO *infoPtr)
118 return infoPtr->BuddyType == BUDDY_TYPE_LISTBOX;
121 /***********************************************************************
122 * UPDOWN_InBounds
123 * Tests if a given value 'val' is between the Min&Max limits
125 static BOOL UPDOWN_InBounds(UPDOWN_INFO *infoPtr, int val)
127 if(infoPtr->MaxVal > infoPtr->MinVal)
128 return (infoPtr->MinVal <= val) && (val <= infoPtr->MaxVal);
129 else
130 return (infoPtr->MaxVal <= val) && (val <= infoPtr->MinVal);
133 /***********************************************************************
134 * UPDOWN_OffsetVal
135 * Change the current value by delta.
136 * It returns TRUE is the value was changed successfuly, or FALSE
137 * if the value was not changed, as it would go out of bounds.
139 static BOOL UPDOWN_OffsetVal(UPDOWN_INFO *infoPtr, int delta)
141 /* check if we can do the modification first */
142 if(!UPDOWN_InBounds (infoPtr, infoPtr->CurVal+delta)) {
143 if (GetWindowLongW (infoPtr->Self, GWL_STYLE) & UDS_WRAP) {
144 delta += (delta < 0 ? -1 : 1) *
145 (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1) *
146 (infoPtr->MinVal - infoPtr->MaxVal) +
147 (delta < 0 ? 1 : -1);
148 } else return FALSE;
151 infoPtr->CurVal += delta;
152 return TRUE;
155 /***********************************************************************
156 * UPDOWN_HasBuddyBorder
158 * When we have a buddy set and that we are aligned on our buddy, we
159 * want to draw a sunken edge to make like we are part of that control.
161 static BOOL UPDOWN_HasBuddyBorder(UPDOWN_INFO* infoPtr)
163 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
165 return ( ((dwStyle & (UDS_ALIGNLEFT | UDS_ALIGNRIGHT)) != 0) &&
166 UPDOWN_IsBuddyEdit(infoPtr) );
169 /***********************************************************************
170 * UPDOWN_GetArrowRect
171 * wndPtr - pointer to the up-down wnd
172 * rect - will hold the rectangle
173 * arrow - FLAG_INCR to get the "increment" rect (up or right)
174 * FLAG_DECR to get the "decrement" rect (down or left)
175 * If both flags are pressent, the envelope is returned.
177 static void UPDOWN_GetArrowRect (UPDOWN_INFO* infoPtr, RECT *rect, int arrow)
179 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
181 GetClientRect (infoPtr->Self, rect);
184 * Make sure we calculate the rectangle to fit even if we draw the
185 * border.
187 if (UPDOWN_HasBuddyBorder(infoPtr)) {
188 if (dwStyle & UDS_ALIGNLEFT)
189 rect->left += DEFAULT_BUDDYBORDER;
190 else
191 rect->right -= DEFAULT_BUDDYBORDER;
193 InflateRect(rect, 0, -DEFAULT_BUDDYBORDER);
196 /* now figure out if we need a space away from the buddy */
197 if ( IsWindow(infoPtr->Buddy) ) {
198 if (dwStyle & UDS_ALIGNLEFT) rect->right -= DEFAULT_BUDDYSPACER;
199 else rect->left += DEFAULT_BUDDYSPACER;
203 * We're calculating the midpoint to figure-out where the
204 * separation between the buttons will lay. We make sure that we
205 * round the uneven numbers by adding 1.
207 if (dwStyle & UDS_HORZ) {
208 int len = rect->right - rect->left + 1; /* compute the width */
209 if (arrow & FLAG_INCR)
210 rect->left = rect->left + len/2;
211 if (arrow & FLAG_DECR)
212 rect->right = rect->left + len/2 - 1;
213 } else {
214 int len = rect->bottom - rect->top + 1; /* compute the height */
215 if (arrow & FLAG_INCR)
216 rect->bottom = rect->top + len/2 - 1;
217 if (arrow & FLAG_DECR)
218 rect->top = rect->top + len/2;
222 /***********************************************************************
223 * UPDOWN_GetArrowFromPoint
224 * Returns the rectagle (for the up or down arrow) that contains pt.
225 * If it returns the up rect, it returns TRUE.
226 * If it returns the down rect, it returns FALSE.
228 static BOOL UPDOWN_GetArrowFromPoint (UPDOWN_INFO* infoPtr, RECT *rect, POINT pt)
230 UPDOWN_GetArrowRect (infoPtr, rect, FLAG_INCR);
231 if(PtInRect(rect, pt)) return FLAG_INCR;
233 UPDOWN_GetArrowRect (infoPtr, rect, FLAG_DECR);
234 if(PtInRect(rect, pt)) return FLAG_DECR;
236 return 0;
240 /***********************************************************************
241 * UPDOWN_GetThousandSep
242 * Returns the thousand sep. If an error occurs, it returns ','.
244 static WCHAR UPDOWN_GetThousandSep()
246 WCHAR sep[2];
248 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, sep, 2) != 1)
249 sep[0] = ',';
251 return sep[0];
254 /***********************************************************************
255 * UPDOWN_GetBuddyInt
256 * Tries to read the pos from the buddy window and if it succeeds,
257 * it stores it in the control's CurVal
258 * returns:
259 * TRUE - if it read the integer from the buddy successfully
260 * FALSE - if an error occurred
262 static BOOL UPDOWN_GetBuddyInt (UPDOWN_INFO *infoPtr)
264 WCHAR txt[20], sep, *src, *dst;
265 int newVal;
267 if (!IsWindow(infoPtr->Buddy))
268 return FALSE;
270 /*if the buddy is a list window, we must set curr index */
271 if (UPDOWN_IsBuddyListbox(infoPtr)) {
272 newVal = SendMessageW(infoPtr->Buddy, LB_GETCARETINDEX, 0, 0);
273 if(newVal < 0) return FALSE;
274 } else {
275 /* we have a regular window, so will get the text */
276 if (!GetWindowTextW(infoPtr->Buddy, txt, COUNT_OF(txt))) return FALSE;
278 sep = UPDOWN_GetThousandSep();
280 /* now get rid of the separators */
281 for(src = dst = txt; *src; src++)
282 if(*src != sep) *dst++ = *src;
283 *dst = 0;
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;
290 TRACE("new value(%d) from buddy (old=%d)\n", newVal, infoPtr->CurVal);
291 infoPtr->CurVal = newVal;
292 return TRUE;
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
303 static BOOL UPDOWN_SetBuddyInt (UPDOWN_INFO *infoPtr)
305 WCHAR fmt[3] = { '%', 'd', '\0' };
306 WCHAR txt[20];
307 int len;
309 if (!IsWindow(infoPtr->Buddy)) return FALSE;
311 TRACE("set new value(%d) to buddy.\n", infoPtr->CurVal);
313 /*if the buddy is a list window, we must set curr index */
314 if (UPDOWN_IsBuddyListbox(infoPtr)) {
315 return SendMessageW(infoPtr->Buddy, LB_SETCURSEL, infoPtr->CurVal, 0) != LB_ERR;
318 /* Regular window, so set caption to the number */
319 if (infoPtr->Base == 16) fmt[1] = 'X';
320 len = wsprintfW(txt, fmt, infoPtr->CurVal);
323 /* Do thousands separation if necessary */
324 if (!(GetWindowLongW (infoPtr->Self, GWL_STYLE) & UDS_NOTHOUSANDS) && (len > 3)) {
325 WCHAR tmp[COUNT_OF(txt)], *src = tmp, *dst = txt;
326 WCHAR sep = UPDOWN_GetThousandSep();
327 int start = len % 3;
329 memcpy(tmp, txt, sizeof(txt));
330 if (start == 0) start = 3;
331 dst += start;
332 src += start;
333 for (len=0; *src; len++) {
334 if (len % 3 == 0) *dst++ = sep;
335 *dst++ = *src++;
337 *dst = 0;
340 return SetWindowTextW(infoPtr->Buddy, txt);
343 /***********************************************************************
344 * UPDOWN_Draw
346 * Draw the arrows. The background need not be erased.
348 static LRESULT UPDOWN_Draw (UPDOWN_INFO *infoPtr, HDC hdc)
350 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
351 BOOL pressed, hot;
352 RECT rect;
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 (dwStyle & UDS_ALIGNLEFT ? BF_LEFT : BF_RIGHT));
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 (dwStyle & UDS_HORZ ? DFCS_SCROLLRIGHT : DFCS_SCROLLUP) |
368 ((dwStyle & UDS_HOTTRACK) && hot ? DFCS_HOT : 0) |
369 (pressed ? DFCS_PUSHED : 0) |
370 (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
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 (dwStyle & UDS_HORZ ? DFCS_SCROLLLEFT : DFCS_SCROLLDOWN) |
378 ((dwStyle & UDS_HOTTRACK) && hot ? DFCS_HOT : 0) |
379 (pressed ? DFCS_PUSHED : 0) |
380 (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
382 return 0;
385 /***********************************************************************
386 * UPDOWN_Paint
388 * Asynchronous drawing (must ONLY be used in WM_PAINT).
389 * Calls UPDOWN_Draw.
391 static LRESULT UPDOWN_Paint (UPDOWN_INFO *infoPtr, HDC hdc)
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;
401 /***********************************************************************
402 * UPDOWN_KeyPressed
404 * Handle key presses (up & down) when we have to do so
406 static LRESULT UPDOWN_KeyPressed(UPDOWN_INFO *infoPtr, int key)
408 int arrow;
410 if (key == VK_UP) arrow = FLAG_INCR;
411 else if (key == VK_DOWN) arrow = FLAG_DECR;
412 else return 1;
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;
423 /***********************************************************************
424 * UPDOWN_Buddy_SubclassProc used to handle messages sent to the buddy
425 * control.
427 static LRESULT CALLBACK
428 UPDOWN_Buddy_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
430 WNDPROC superClassWndProc = (WNDPROC)GetPropA(hwnd, BUDDY_SUPERCLASS_WNDPROC);
431 TRACE("hwnd=%p, wndProc=%d, uMsg=%04x, wParam=%d, lParam=%d\n",
432 hwnd, (INT)superClassWndProc, uMsg, wParam, (UINT)lParam);
434 if (uMsg == WM_KEYDOWN) {
435 HWND upDownHwnd = GetPropA(hwnd, BUDDY_UPDOWN_HWND);
437 UPDOWN_KeyPressed(UPDOWN_GetInfoPtr(upDownHwnd), (int)wParam);
440 return CallWindowProcW( superClassWndProc, hwnd, uMsg, wParam, lParam);
443 /***********************************************************************
444 * UPDOWN_SetBuddy
446 * Sets bud as a new Buddy.
447 * Then, it should subclass the buddy
448 * If window has the UDS_ARROWKEYS, it subcalsses the buddy window to
449 * process the UP/DOWN arrow keys.
450 * If window has the UDS_ALIGNLEFT or UDS_ALIGNRIGHT style
451 * the size/pos of the buddy and the control are adjusted accordingly.
453 static HWND UPDOWN_SetBuddy (UPDOWN_INFO* infoPtr, HWND bud)
455 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
456 RECT budRect; /* new coord for the buddy */
457 int x, width; /* new x position and width for the up-down */
458 WNDPROC baseWndProc;
459 CHAR buddyClass[40];
460 HWND ret;
462 TRACE("(hwnd=%p, bud=%p)\n", infoPtr->Self, bud);
464 ret = infoPtr->Buddy;
466 /* there is already a body assigned */
467 if (infoPtr->Buddy) RemovePropA(infoPtr->Buddy, BUDDY_UPDOWN_HWND);
469 if(!IsWindow(bud))
470 bud = 0;
472 /* Store buddy window handle */
473 infoPtr->Buddy = bud;
475 if(bud) {
477 /* keep upDown ctrl hwnd in a buddy property */
478 SetPropA( bud, BUDDY_UPDOWN_HWND, infoPtr->Self);
480 /* Store buddy window class type */
481 infoPtr->BuddyType = BUDDY_TYPE_UNKNOWN;
482 if (GetClassNameA(bud, buddyClass, COUNT_OF(buddyClass))) {
483 if (lstrcmpiA(buddyClass, "Edit") == 0)
484 infoPtr->BuddyType = BUDDY_TYPE_EDIT;
485 else if (lstrcmpiA(buddyClass, "Listbox") == 0)
486 infoPtr->BuddyType = BUDDY_TYPE_LISTBOX;
489 if(dwStyle & UDS_ARROWKEYS){
490 /* Note that I don't clear the BUDDY_SUPERCLASS_WNDPROC property
491 when we reset the upDown ctrl buddy to another buddy because it is not
492 good to break the window proc chain. */
493 if (!GetPropA(bud, BUDDY_SUPERCLASS_WNDPROC)) {
494 baseWndProc = (WNDPROC)SetWindowLongW(bud, GWL_WNDPROC, (LPARAM)UPDOWN_Buddy_SubclassProc);
495 SetPropA(bud, BUDDY_SUPERCLASS_WNDPROC, (HANDLE)baseWndProc);
499 /* Get the rect of the buddy relative to its parent */
500 GetWindowRect(infoPtr->Buddy, &budRect);
501 MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Buddy), (POINT *)(&budRect.left), 2);
503 /* now do the positioning */
504 if (dwStyle & UDS_ALIGNLEFT) {
505 x = budRect.left;
506 budRect.left += DEFAULT_WIDTH + DEFAULT_XSEP;
507 } else if (dwStyle & UDS_ALIGNRIGHT) {
508 budRect.right -= DEFAULT_WIDTH + DEFAULT_XSEP;
509 x = budRect.right+DEFAULT_XSEP;
510 } else {
511 x = budRect.right+DEFAULT_XSEP;
514 /* first adjust the buddy to accomodate the up/down */
515 SetWindowPos(infoPtr->Buddy, 0, budRect.left, budRect.top,
516 budRect.right - budRect.left, budRect.bottom - budRect.top,
517 SWP_NOACTIVATE|SWP_NOZORDER);
519 /* now position the up/down */
520 /* Since the UDS_ALIGN* flags were used, */
521 /* we will pick the position and size of the window. */
522 width = DEFAULT_WIDTH;
525 * If the updown has a buddy border, it has to overlap with the buddy
526 * to look as if it is integrated with the buddy control.
527 * We nudge the control or change it size to overlap.
529 if (UPDOWN_HasBuddyBorder(infoPtr)) {
530 if(dwStyle & UDS_ALIGNLEFT)
531 width += DEFAULT_BUDDYBORDER;
532 else
533 x -= DEFAULT_BUDDYBORDER;
536 SetWindowPos(infoPtr->Self, infoPtr->Buddy, x,
537 budRect.top - DEFAULT_ADDTOP, width,
538 budRect.bottom - budRect.top + DEFAULT_ADDTOP + DEFAULT_ADDBOT,
539 SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOZORDER);
540 } else {
541 RECT rect;
542 GetWindowRect(infoPtr->Self, &rect);
543 MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Self), (POINT *)&rect, 2);
544 SetWindowPos(infoPtr->Self, 0, rect.left, rect.top, DEFAULT_WIDTH, rect.bottom - rect.top,
545 SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOZORDER);
547 return ret;
550 /***********************************************************************
551 * UPDOWN_DoAction
553 * This function increments/decrements the CurVal by the
554 * 'delta' amount according to the 'action' flag which can be a
555 * combination of FLAG_INCR and FLAG_DECR
556 * It notifies the parent as required.
557 * It handles wraping and non-wraping correctly.
558 * It is assumed that delta>0
560 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action)
562 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
563 NM_UPDOWN ni;
565 TRACE("%d by %d\n", action, delta);
567 /* check if we can do the modification first */
568 delta *= (action & FLAG_INCR ? 1 : -1) * (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1);
569 if ( (action & FLAG_INCR) && (action & FLAG_DECR) ) delta = 0;
571 /* We must notify parent now to obtain permission */
572 ni.iPos = infoPtr->CurVal;
573 ni.iDelta = delta;
574 ni.hdr.hwndFrom = infoPtr->Self;
575 ni.hdr.idFrom = GetWindowLongW (infoPtr->Self, GWL_ID);
576 ni.hdr.code = UDN_DELTAPOS;
577 if (!SendMessageW(infoPtr->Notify, WM_NOTIFY, (WPARAM)ni.hdr.idFrom, (LPARAM)&ni)) {
578 /* Parent said: OK to adjust */
580 /* Now adjust value with (maybe new) delta */
581 if (UPDOWN_OffsetVal (infoPtr, ni.iDelta)) {
582 /* Now take care about our buddy */
583 if (dwStyle & UDS_SETBUDDYINT) UPDOWN_SetBuddyInt (infoPtr);
587 /* Also, notify it. This message is sent in any case. */
588 SendMessageW( infoPtr->Notify, dwStyle & UDS_HORZ ? WM_HSCROLL : WM_VSCROLL,
589 MAKELONG(SB_THUMBPOSITION, infoPtr->CurVal), (LPARAM)infoPtr->Self);
592 /***********************************************************************
593 * UPDOWN_IsEnabled
595 * Returns TRUE if it is enabled as well as its buddy (if any)
596 * FALSE otherwise
598 static BOOL UPDOWN_IsEnabled (UPDOWN_INFO *infoPtr)
600 if(GetWindowLongW (infoPtr->Self, GWL_STYLE) & WS_DISABLED)
601 return FALSE;
602 if(infoPtr->Buddy)
603 return IsWindowEnabled(infoPtr->Buddy);
604 return TRUE;
607 /***********************************************************************
608 * UPDOWN_CancelMode
610 * Deletes any timers, releases the mouse and does redraw if necessary.
611 * If the control is not in "capture" mode, it does nothing.
612 * If the control was not in cancel mode, it returns FALSE.
613 * If the control was in cancel mode, it returns TRUE.
615 static BOOL UPDOWN_CancelMode (UPDOWN_INFO *infoPtr)
617 if (!(infoPtr->Flags & FLAG_PRESSED)) return FALSE;
619 KillTimer (infoPtr->Self, TIMER_AUTOREPEAT);
620 KillTimer (infoPtr->Self, TIMER_ACCEL);
621 KillTimer (infoPtr->Self, TIMER_AUTOPRESS);
623 if (GetCapture() == infoPtr->Self) {
624 NMHDR hdr;
625 hdr.hwndFrom = infoPtr->Self;
626 hdr.idFrom = GetWindowLongW (infoPtr->Self, GWL_ID);
627 hdr.code = NM_RELEASEDCAPTURE;
628 SendMessageW(infoPtr->Notify, WM_NOTIFY, hdr.idFrom, (LPARAM)&hdr);
629 ReleaseCapture();
632 infoPtr->Flags &= ~FLAG_PRESSED;
633 InvalidateRect (infoPtr->Self, NULL, FALSE);
635 return TRUE;
638 /***********************************************************************
639 * UPDOWN_HandleMouseEvent
641 * Handle a mouse event for the updown.
642 * 'pt' is the location of the mouse event in client or
643 * windows coordinates.
645 static void UPDOWN_HandleMouseEvent (UPDOWN_INFO *infoPtr, UINT msg, POINTS pts)
647 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
648 POINT pt = { pts.x, pts.y };
649 RECT rect;
650 int temp, arrow;
652 switch(msg)
654 case WM_LBUTTONDOWN: /* Initialise mouse tracking */
655 /* If we are inside an arrow, then nothing to do */
656 if(!(infoPtr->Flags & FLAG_MOUSEIN)) return;
658 /* If the buddy is an edit, will set focus to it */
659 if (UPDOWN_IsBuddyEdit(infoPtr)) SetFocus(infoPtr->Buddy);
661 /* Now see which one is the 'active' arrow */
662 if (infoPtr->Flags & FLAG_ARROW) {
664 /* Update the CurVal if necessary */
665 if (dwStyle & UDS_SETBUDDYINT) UPDOWN_GetBuddyInt (infoPtr);
667 /* Set up the correct flags */
668 infoPtr->Flags |= FLAG_PRESSED;
670 /* repaint the control */
671 InvalidateRect (infoPtr->Self, NULL, FALSE);
673 /* process the click */
674 UPDOWN_DoAction (infoPtr, 1, infoPtr->Flags & FLAG_ARROW);
676 /* now capture all mouse messages */
677 SetCapture (infoPtr->Self);
679 /* and startup the first timer */
680 SetTimer(infoPtr->Self, TIMER_AUTOREPEAT, INITIAL_DELAY, 0);
682 break;
684 case WM_MOUSEMOVE:
685 /* save the flags to see if any got modified */
686 temp = infoPtr->Flags;
688 /* Now see which one is the 'active' arrow */
689 arrow = UPDOWN_GetArrowFromPoint (infoPtr, &rect, pt);
691 /* Update the flags if we are in/out */
692 infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
693 if(arrow) {
694 infoPtr->Flags |= FLAG_MOUSEIN | arrow;
695 } else {
696 if(infoPtr->AccelIndex != -1) infoPtr->AccelIndex = 0;
699 /* If state changed, redraw the control */
700 if(temp != infoPtr->Flags)
701 InvalidateRect (infoPtr->Self, &rect, FALSE);
702 break;
704 default:
705 ERR("Impossible case (msg=%x)!\n", msg);
710 /***********************************************************************
711 * UpDownWndProc
713 static LRESULT WINAPI UpDownWindowProc(HWND hwnd, UINT message, WPARAM wParam,
714 LPARAM lParam)
716 UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr (hwnd);
717 DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
718 int temp;
720 if (!infoPtr && (message != WM_CREATE))
721 return DefWindowProcW (hwnd, message, wParam, lParam);
723 switch(message)
725 case WM_CREATE:
726 SetWindowLongW (hwnd, GWL_STYLE, dwStyle & ~WS_BORDER);
727 infoPtr = (UPDOWN_INFO*)Alloc (sizeof(UPDOWN_INFO));
728 SetWindowLongW (hwnd, 0, (DWORD)infoPtr);
730 /* initialize the info struct */
731 infoPtr->Self = hwnd;
732 infoPtr->Notify = ((LPCREATESTRUCTA)lParam)->hwndParent;
733 infoPtr->AccelCount = 0;
734 infoPtr->AccelVect = 0;
735 infoPtr->AccelIndex = -1;
736 infoPtr->CurVal = 0;
737 infoPtr->MinVal = 100;
738 infoPtr->MaxVal = 0;
739 infoPtr->Base = 10; /* Default to base 10 */
740 infoPtr->Buddy = 0; /* No buddy window yet */
741 infoPtr->Flags = 0; /* And no flags */
743 /* Do we pick the buddy win ourselves? */
744 if (dwStyle & UDS_AUTOBUDDY)
745 UPDOWN_SetBuddy (infoPtr, GetWindow (hwnd, GW_HWNDPREV));
747 TRACE("UpDown Ctrl creation, hwnd=%p\n", hwnd);
748 break;
750 case WM_DESTROY:
751 if(infoPtr->AccelVect) Free (infoPtr->AccelVect);
753 if(infoPtr->Buddy) RemovePropA(infoPtr->Buddy, BUDDY_UPDOWN_HWND);
755 Free (infoPtr);
756 SetWindowLongW (hwnd, 0, 0);
757 TRACE("UpDown Ctrl destruction, hwnd=%p\n", hwnd);
758 break;
760 case WM_ENABLE:
761 if (dwStyle & WS_DISABLED) UPDOWN_CancelMode (infoPtr);
762 InvalidateRect (infoPtr->Self, NULL, FALSE);
763 break;
765 case WM_TIMER:
766 /* is this the auto-press timer? */
767 if(wParam == TIMER_AUTOPRESS) {
768 KillTimer(hwnd, TIMER_AUTOPRESS);
769 infoPtr->Flags &= ~(FLAG_PRESSED | FLAG_ARROW);
770 InvalidateRect(infoPtr->Self, NULL, FALSE);
773 /* if initial timer, kill it and start the repeat timer */
774 if(wParam == TIMER_AUTOREPEAT) {
775 KillTimer(hwnd, TIMER_AUTOREPEAT);
776 /* if no accel info given, used default timer */
777 if(infoPtr->AccelCount==0 || infoPtr->AccelVect==0) {
778 infoPtr->AccelIndex = -1;
779 temp = REPEAT_DELAY;
780 } else {
781 infoPtr->AccelIndex = 0; /* otherwise, use it */
782 temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
784 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
787 /* now, if the mouse is above us, do the thing...*/
788 if(infoPtr->Flags & FLAG_MOUSEIN) {
789 temp = infoPtr->AccelIndex == -1 ? 1 : infoPtr->AccelVect[infoPtr->AccelIndex].nInc;
790 UPDOWN_DoAction(infoPtr, temp, infoPtr->Flags & FLAG_ARROW);
792 if(infoPtr->AccelIndex != -1 && infoPtr->AccelIndex < infoPtr->AccelCount-1) {
793 KillTimer(hwnd, TIMER_ACCEL);
794 infoPtr->AccelIndex++; /* move to the next accel info */
795 temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
796 /* make sure we have at least 1ms intervals */
797 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
800 break;
802 case WM_CANCELMODE:
803 return UPDOWN_CancelMode (infoPtr);
805 case WM_LBUTTONUP:
806 if (GetCapture() != infoPtr->Self) break;
808 if ( (infoPtr->Flags & FLAG_MOUSEIN) &&
809 (infoPtr->Flags & FLAG_ARROW) ) {
811 SendMessageW( infoPtr->Notify,
812 dwStyle & UDS_HORZ ? WM_HSCROLL : WM_VSCROLL,
813 MAKELONG(SB_ENDSCROLL, infoPtr->CurVal),
814 (LPARAM)hwnd);
815 if (UPDOWN_IsBuddyEdit(infoPtr))
816 SendMessageW(infoPtr->Buddy, EM_SETSEL, 0, MAKELONG(0, -1));
818 UPDOWN_CancelMode(infoPtr);
819 break;
821 case WM_LBUTTONDOWN:
822 case WM_MOUSEMOVE:
823 if(UPDOWN_IsEnabled(infoPtr))
824 UPDOWN_HandleMouseEvent (infoPtr, message, MAKEPOINTS(lParam));
825 break;
827 case WM_KEYDOWN:
828 if((dwStyle & UDS_ARROWKEYS) && UPDOWN_IsEnabled(infoPtr))
829 return UPDOWN_KeyPressed(infoPtr, (int)wParam);
830 break;
832 case WM_PAINT:
833 return UPDOWN_Paint (infoPtr, (HDC)wParam);
835 case UDM_GETACCEL:
836 if (wParam==0 && lParam==0) return infoPtr->AccelCount;
837 if (wParam && lParam) {
838 temp = min(infoPtr->AccelCount, wParam);
839 memcpy((void *)lParam, infoPtr->AccelVect, temp*sizeof(UDACCEL));
840 return temp;
842 return 0;
844 case UDM_SETACCEL:
845 TRACE("UpDown Ctrl new accel info, hwnd=%p\n", hwnd);
846 if(infoPtr->AccelVect) {
847 Free (infoPtr->AccelVect);
848 infoPtr->AccelCount = 0;
849 infoPtr->AccelVect = 0;
851 if(wParam==0) return TRUE;
852 infoPtr->AccelVect = Alloc (wParam*sizeof(UDACCEL));
853 if(infoPtr->AccelVect == 0) return FALSE;
854 memcpy(infoPtr->AccelVect, (void*)lParam, wParam*sizeof(UDACCEL));
855 return TRUE;
857 case UDM_GETBASE:
858 return infoPtr->Base;
860 case UDM_SETBASE:
861 TRACE("UpDown Ctrl new base(%d), hwnd=%p\n", wParam, hwnd);
862 if (wParam==10 || wParam==16) {
863 temp = infoPtr->Base;
864 infoPtr->Base = wParam;
865 return temp;
867 break;
869 case UDM_GETBUDDY:
870 return (LRESULT)infoPtr->Buddy;
872 case UDM_SETBUDDY:
873 return (LRESULT)UPDOWN_SetBuddy (infoPtr, (HWND)wParam);
875 case UDM_GETPOS:
876 temp = UPDOWN_GetBuddyInt (infoPtr);
877 return MAKELONG(infoPtr->CurVal, temp ? 0 : 1);
879 case UDM_SETPOS:
880 temp = (short)LOWORD(lParam);
881 TRACE("UpDown Ctrl new value(%d), hwnd=%p\n", temp, hwnd);
882 if(!UPDOWN_InBounds(infoPtr, temp)) {
883 if(temp < infoPtr->MinVal) temp = infoPtr->MinVal;
884 if(temp > infoPtr->MaxVal) temp = infoPtr->MaxVal;
886 wParam = infoPtr->CurVal;
887 infoPtr->CurVal = temp;
888 if(dwStyle & UDS_SETBUDDYINT) UPDOWN_SetBuddyInt (infoPtr);
889 return wParam; /* return prev value */
891 case UDM_GETRANGE:
892 return MAKELONG(infoPtr->MaxVal, infoPtr->MinVal);
894 case UDM_SETRANGE:
895 /* we must have: */
896 infoPtr->MaxVal = (short)(lParam); /* UD_MINVAL <= Max <= UD_MAXVAL */
897 infoPtr->MinVal = (short)HIWORD(lParam); /* UD_MINVAL <= Min <= UD_MAXVAL */
898 /* |Max-Min| <= UD_MAXVAL */
899 TRACE("UpDown Ctrl new range(%d to %d), hwnd=%p\n",
900 infoPtr->MinVal, infoPtr->MaxVal, hwnd);
901 break;
903 case UDM_GETRANGE32:
904 if (wParam) *(LPINT)wParam = infoPtr->MinVal;
905 if (lParam) *(LPINT)lParam = infoPtr->MaxVal;
906 break;
908 case UDM_SETRANGE32:
909 infoPtr->MinVal = (INT)wParam;
910 infoPtr->MaxVal = (INT)lParam;
911 if (infoPtr->MaxVal <= infoPtr->MinVal)
912 infoPtr->MaxVal = infoPtr->MinVal + 1;
913 TRACE("UpDown Ctrl new range(%d to %d), hwnd=%p\n",
914 infoPtr->MinVal, infoPtr->MaxVal, hwnd);
915 break;
917 case UDM_GETPOS32:
918 if ((LPBOOL)lParam != NULL) *((LPBOOL)lParam) = TRUE;
919 return infoPtr->CurVal;
921 case UDM_SETPOS32:
922 if(!UPDOWN_InBounds(infoPtr, (int)lParam)) {
923 if((int)lParam < infoPtr->MinVal) lParam = infoPtr->MinVal;
924 if((int)lParam > infoPtr->MaxVal) lParam = infoPtr->MaxVal;
926 temp = infoPtr->CurVal; /* save prev value */
927 infoPtr->CurVal = (int)lParam; /* set the new value */
928 if(dwStyle & UDS_SETBUDDYINT) UPDOWN_SetBuddyInt (infoPtr);
929 return temp; /* return prev value */
931 case UDM_GETUNICODEFORMAT:
932 /* we lie a bit here, we're always using Unicode internally */
933 return infoPtr->UnicodeFormat;
935 case UDM_SETUNICODEFORMAT:
936 /* do we really need to honour this flag? */
937 temp = infoPtr->UnicodeFormat;
938 infoPtr->UnicodeFormat = (BOOL)wParam;
939 return temp;
941 default:
942 if ((message >= WM_USER) && (message < WM_APP))
943 ERR("unknown msg %04x wp=%04x lp=%08lx\n", message, wParam, lParam);
944 return DefWindowProcW (hwnd, message, wParam, lParam);
947 return 0;
950 /***********************************************************************
951 * UPDOWN_Register [Internal]
953 * Registers the updown window class.
955 void UPDOWN_Register(void)
957 WNDCLASSW wndClass;
959 ZeroMemory( &wndClass, sizeof( WNDCLASSW ) );
960 wndClass.style = CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
961 wndClass.lpfnWndProc = (WNDPROC)UpDownWindowProc;
962 wndClass.cbClsExtra = 0;
963 wndClass.cbWndExtra = sizeof(UPDOWN_INFO*);
964 wndClass.hCursor = LoadCursorW( 0, (LPWSTR)IDC_ARROW );
965 wndClass.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
966 wndClass.lpszClassName = UPDOWN_CLASSW;
968 RegisterClassW( &wndClass );
972 /***********************************************************************
973 * UPDOWN_Unregister [Internal]
975 * Unregisters the updown window class.
977 void UPDOWN_Unregister (void)
979 UnregisterClassW (UPDOWN_CLASSW, NULL);