source: trunk/src/comctl32/updown.c@ 8706

Last change on this file since 8706 was 8382, checked in by sandervl, 23 years ago

merge with latest Wine

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