1 | /*
|
---|
2 | * Updown control
|
---|
3 | *
|
---|
4 | * Copyright 1997 Dimitrie O. Paun
|
---|
5 | *
|
---|
6 | * TODO:
|
---|
7 | * - subclass the buddy window (in UPDOWN_SetBuddy) to process the
|
---|
8 | * arrow keys
|
---|
9 | * - I am not sure about the default values for the Min, Max, Pos
|
---|
10 | * (in the UPDOWN_INFO the fields: MinVal, MaxVal, CurVal)
|
---|
11 | * - I think I do not handle correctly the WS_BORDER style.
|
---|
12 | * (Should be fixed. <ekohl@abo.rhein-zeitung.de>)
|
---|
13 | *
|
---|
14 | * Testing:
|
---|
15 | * Not much. The following have not been tested at all:
|
---|
16 | * - horizontal arrows
|
---|
17 | * - listbox as buddy window
|
---|
18 | * - acceleration
|
---|
19 | * - base 16
|
---|
20 | * - UDS_ALIGNLEFT, ~UDS_WRAP
|
---|
21 | * (tested - they work)
|
---|
22 | * - integers with thousand separators.
|
---|
23 | * (fixed bugs. <noel@macadamian.com>)
|
---|
24 | *
|
---|
25 | * Even though the above list seems rather large, the control seems to
|
---|
26 | * behave very well so I am confident it does work in most (all) of the
|
---|
27 | * untested cases.
|
---|
28 | * Problems:
|
---|
29 | * I do not like the arrows yet, I'll work more on them later on.
|
---|
30 | */
|
---|
31 |
|
---|
32 | #include <stdlib.h>
|
---|
33 | #include <stdio.h>
|
---|
34 | #include <string.h>
|
---|
35 |
|
---|
36 | #include "winbase.h"
|
---|
37 | #include "winuser.h"
|
---|
38 | #include "commctrl.h"
|
---|
39 | #include "winnls.h"
|
---|
40 | #include "updown.h"
|
---|
41 |
|
---|
42 | /* Control configuration constants */
|
---|
43 |
|
---|
44 | #define INITIAL_DELAY 500 /* initial timer until auto-increment kicks in */
|
---|
45 | #define REPEAT_DELAY 50 /* delay between auto-increments */
|
---|
46 |
|
---|
47 | #define DEFAULT_WIDTH 14 /* default width of the ctrl */
|
---|
48 | #define DEFAULT_XSEP 0 /* default separation between buddy and crtl */
|
---|
49 | #define DEFAULT_ADDTOP 0 /* amount to extend above the buddy window */
|
---|
50 | #define DEFAULT_ADDBOT 0 /* amount to extend below the buddy window */
|
---|
51 |
|
---|
52 |
|
---|
53 | /* Work constants */
|
---|
54 |
|
---|
55 | #define FLAG_INCR 0x01
|
---|
56 | #define FLAG_DECR 0x02
|
---|
57 | #define FLAG_MOUSEIN 0x04
|
---|
58 | #define FLAG_CLICKED (FLAG_INCR | FLAG_DECR)
|
---|
59 |
|
---|
60 | #define TIMERID1 1
|
---|
61 | #define TIMERID2 2
|
---|
62 |
|
---|
63 | static int accelIndex = -1;
|
---|
64 |
|
---|
65 | //#define UNKNOWN_PARAM(msg, wParam, lParam) WARN(updown, \
|
---|
66 | // "UpDown Ctrl: Unknown parameter(s) for message " #msg \
|
---|
67 | // "(%04x): wp=%04x lp=%08lx\n", msg, wParam, lParam);
|
---|
68 | #define UNKNOWN_PARAM(msg, wParam, lParam)
|
---|
69 |
|
---|
70 | #define UPDOWN_GetInfoPtr(hwnd) ((UPDOWN_INFO *)GetWindowLongA (hwnd,0))
|
---|
71 |
|
---|
72 |
|
---|
73 | /***********************************************************************
|
---|
74 | * UPDOWN_InBounds
|
---|
75 | * Tests if a given value 'val' is between the Min&Max limits
|
---|
76 | */
|
---|
77 | static BOOL UPDOWN_InBounds(HWND hwnd, int val)
|
---|
78 | {
|
---|
79 | UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr (hwnd);
|
---|
80 |
|
---|
81 | if(infoPtr->MaxVal > infoPtr->MinVal)
|
---|
82 | return (infoPtr->MinVal <= val) && (val <= infoPtr->MaxVal);
|
---|
83 | else
|
---|
84 | return (infoPtr->MaxVal <= val) && (val <= infoPtr->MinVal);
|
---|
85 | }
|
---|
86 |
|
---|
87 | /***********************************************************************
|
---|
88 | * UPDOWN_OffsetVal
|
---|
89 | * Tests if we can change the current value by delta. If so, it changes
|
---|
90 | * it and returns TRUE. Else, it leaves it unchanged and returns FALSE.
|
---|
91 | */
|
---|
92 | static BOOL UPDOWN_OffsetVal(HWND hwnd, int delta)
|
---|
93 | {
|
---|
94 | UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr (hwnd);
|
---|
95 |
|
---|
96 | /* check if we can do the modification first */
|
---|
97 | if(!UPDOWN_InBounds (hwnd, infoPtr->CurVal+delta)){
|
---|
98 | if (GetWindowLongA (hwnd, GWL_STYLE) & UDS_WRAP)
|
---|
99 | {
|
---|
100 | delta += (delta < 0 ? -1 : 1) *
|
---|
101 | (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1) *
|
---|
102 | (infoPtr->MinVal - infoPtr->MaxVal) +
|
---|
103 | (delta < 0 ? 1 : -1);
|
---|
104 | }
|
---|
105 | else
|
---|
106 | return FALSE;
|
---|
107 | }
|
---|
108 |
|
---|
109 | infoPtr->CurVal += delta;
|
---|
110 | return TRUE;
|
---|
111 | }
|
---|
112 |
|
---|
113 | /***********************************************************************
|
---|
114 | * UPDOWN_GetArrowRect
|
---|
115 | * wndPtr - pointer to the up-down wnd
|
---|
116 | * rect - will hold the rectangle
|
---|
117 | * incr - TRUE get the "increment" rect (up or right)
|
---|
118 | * FALSE get the "decrement" rect (down or left)
|
---|
119 | *
|
---|
120 | */
|
---|
121 | static void UPDOWN_GetArrowRect (HWND hwnd, RECT *rect, BOOL incr)
|
---|
122 | {
|
---|
123 | int len; /* will hold the width or height */
|
---|
124 |
|
---|
125 | GetClientRect (hwnd, rect);
|
---|
126 |
|
---|
127 | if (GetWindowLongA (hwnd, GWL_STYLE) & UDS_HORZ) {
|
---|
128 | len = rect->right - rect->left; /* compute the width */
|
---|
129 | if (incr)
|
---|
130 | rect->left = len/2+1;
|
---|
131 | else
|
---|
132 | rect->right = len/2;
|
---|
133 | }
|
---|
134 | else {
|
---|
135 | len = rect->bottom - rect->top; /* compute the height */
|
---|
136 | if (incr)
|
---|
137 | rect->bottom = len/2;
|
---|
138 | else
|
---|
139 | rect->top = len/2+1;
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | /***********************************************************************
|
---|
144 | * UPDOWN_GetArrowFromPoint
|
---|
145 | * Returns the rectagle (for the up or down arrow) that contains pt.
|
---|
146 | * If it returns the up rect, it returns TRUE.
|
---|
147 | * If it returns the down rect, it returns FALSE.
|
---|
148 | */
|
---|
149 | static BOOL
|
---|
150 | UPDOWN_GetArrowFromPoint (HWND hwnd, RECT *rect, POINT pt)
|
---|
151 | {
|
---|
152 | UPDOWN_GetArrowRect (hwnd, rect, TRUE);
|
---|
153 | if(PtInRect(rect, pt))
|
---|
154 | return TRUE;
|
---|
155 |
|
---|
156 | UPDOWN_GetArrowRect (hwnd, rect, FALSE);
|
---|
157 | return FALSE;
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | /***********************************************************************
|
---|
162 | * UPDOWN_GetThousandSep
|
---|
163 | * Returns the thousand sep. If an error occurs, it returns ','.
|
---|
164 | */
|
---|
165 | static char UPDOWN_GetThousandSep()
|
---|
166 | {
|
---|
167 | char sep[2];
|
---|
168 |
|
---|
169 | if(GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND,
|
---|
170 | sep, sizeof(sep)) != 1)
|
---|
171 | return ',';
|
---|
172 |
|
---|
173 | return sep[0];
|
---|
174 | }
|
---|
175 |
|
---|
176 | /***********************************************************************
|
---|
177 | * UPDOWN_GetBuddyInt
|
---|
178 | * Tries to read the pos from the buddy window and if it succeeds,
|
---|
179 | * it stores it in the control's CurVal
|
---|
180 | * returns:
|
---|
181 | * TRUE - if it read the integer from the buddy successfully
|
---|
182 | * FALSE - if an error occured
|
---|
183 | */
|
---|
184 | static BOOL UPDOWN_GetBuddyInt (HWND hwnd)
|
---|
185 | {
|
---|
186 | UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr (hwnd);
|
---|
187 | char txt[20], sep, *src, *dst;
|
---|
188 | int newVal;
|
---|
189 |
|
---|
190 | if (!IsWindow(infoPtr->Buddy))
|
---|
191 | return FALSE;
|
---|
192 |
|
---|
193 | /*if the buddy is a list window, we must set curr index */
|
---|
194 | if (!lstrcmpA (infoPtr->szBuddyClass, "ListBox")){
|
---|
195 | newVal = SendMessageA(infoPtr->Buddy, LB_GETCARETINDEX, 0, 0);
|
---|
196 | if(newVal < 0)
|
---|
197 | return FALSE;
|
---|
198 | }
|
---|
199 | else{
|
---|
200 | /* we have a regular window, so will get the text */
|
---|
201 | if (!GetWindowTextA(infoPtr->Buddy, txt, sizeof(txt)))
|
---|
202 | return FALSE;
|
---|
203 |
|
---|
204 | sep = UPDOWN_GetThousandSep();
|
---|
205 |
|
---|
206 | /* now get rid of the separators */
|
---|
207 | for(src = dst = txt; *src; src++)
|
---|
208 | if(*src != sep)
|
---|
209 | *dst++ = *src;
|
---|
210 | *dst = 0;
|
---|
211 |
|
---|
212 | /* try to convert the number and validate it */
|
---|
213 | newVal = strtol(txt, &src, infoPtr->Base);
|
---|
214 | if(*src || !UPDOWN_InBounds (hwnd, newVal))
|
---|
215 | return FALSE;
|
---|
216 |
|
---|
217 | // TRACE(updown, "new value(%d) read from buddy (old=%d)\n",
|
---|
218 | // newVal, infoPtr->CurVal);
|
---|
219 | }
|
---|
220 |
|
---|
221 | infoPtr->CurVal = newVal;
|
---|
222 | return TRUE;
|
---|
223 | }
|
---|
224 |
|
---|
225 |
|
---|
226 | /***********************************************************************
|
---|
227 | * UPDOWN_SetBuddyInt
|
---|
228 | * Tries to set the pos to the buddy window based on current pos
|
---|
229 | * returns:
|
---|
230 | * TRUE - if it set the caption of the buddy successfully
|
---|
231 | * FALSE - if an error occured
|
---|
232 | */
|
---|
233 | static BOOL UPDOWN_SetBuddyInt (HWND hwnd)
|
---|
234 | {
|
---|
235 | UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr (hwnd);
|
---|
236 | char txt1[20], sep;
|
---|
237 | int len;
|
---|
238 |
|
---|
239 | if (!IsWindow(infoPtr->Buddy))
|
---|
240 | return FALSE;
|
---|
241 |
|
---|
242 | // TRACE(updown, "set new value(%d) to buddy.\n",
|
---|
243 | // infoPtr->CurVal);
|
---|
244 |
|
---|
245 | /*if the buddy is a list window, we must set curr index */
|
---|
246 | if(!lstrcmpA (infoPtr->szBuddyClass, "ListBox")){
|
---|
247 | SendMessageA(infoPtr->Buddy, LB_SETCURSEL, infoPtr->CurVal, 0);
|
---|
248 | }
|
---|
249 | else{ /* Regular window, so set caption to the number */
|
---|
250 | len = sprintf(txt1, (infoPtr->Base==16) ? "%X" : "%d", infoPtr->CurVal);
|
---|
251 |
|
---|
252 | sep = UPDOWN_GetThousandSep();
|
---|
253 |
|
---|
254 | /* Do thousands seperation if necessary */
|
---|
255 | if (!(GetWindowLongA (hwnd, GWL_STYLE) & UDS_NOTHOUSANDS) && (len > 3)) {
|
---|
256 | char txt2[20], *src = txt1, *dst = txt2;
|
---|
257 | if(len%3 > 0){
|
---|
258 | lstrcpynA (dst, src, len%3 + 1); /* need to include the null */
|
---|
259 | dst += len%3;
|
---|
260 | src += len%3;
|
---|
261 | }
|
---|
262 | for(len=0; *src; len++){
|
---|
263 | if(len%3==0)
|
---|
264 | *dst++ = sep;
|
---|
265 | *dst++ = *src++;
|
---|
266 | }
|
---|
267 | *dst = 0; /* null terminate it */
|
---|
268 | strcpy(txt1, txt2); /* move it to the proper place */
|
---|
269 | }
|
---|
270 | SetWindowTextA(infoPtr->Buddy, txt1);
|
---|
271 | }
|
---|
272 |
|
---|
273 | return TRUE;
|
---|
274 | }
|
---|
275 |
|
---|
276 | /***********************************************************************
|
---|
277 | * UPDOWN_Draw [Internal]
|
---|
278 | *
|
---|
279 | * Draw the arrows. The background need not be erased.
|
---|
280 | */
|
---|
281 | static void UPDOWN_Draw (HWND hwnd, HDC hdc)
|
---|
282 | {
|
---|
283 | UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr (hwnd);
|
---|
284 | DWORD dwStyle = GetWindowLongA (hwnd, GWL_STYLE);
|
---|
285 | BOOL prssed;
|
---|
286 | RECT rect;
|
---|
287 |
|
---|
288 | /* Draw the incr button */
|
---|
289 | UPDOWN_GetArrowRect (hwnd, &rect, TRUE);
|
---|
290 | prssed = (infoPtr->Flags & FLAG_INCR) && (infoPtr->Flags & FLAG_MOUSEIN);
|
---|
291 | DrawFrameControl(hdc, &rect, DFC_SCROLL,
|
---|
292 | (dwStyle & UDS_HORZ ? DFCS_SCROLLLEFT : DFCS_SCROLLUP) |
|
---|
293 | (prssed ? DFCS_PUSHED : 0) |
|
---|
294 | (dwStyle&WS_DISABLED ? DFCS_INACTIVE : 0) );
|
---|
295 |
|
---|
296 | /* Draw the space between the buttons */
|
---|
297 | rect.top = rect.bottom; rect.bottom++;
|
---|
298 | DrawEdge(hdc, &rect, 0, BF_MIDDLE);
|
---|
299 |
|
---|
300 | /* Draw the decr button */
|
---|
301 | UPDOWN_GetArrowRect(hwnd, &rect, FALSE);
|
---|
302 | prssed = (infoPtr->Flags & FLAG_DECR) && (infoPtr->Flags & FLAG_MOUSEIN);
|
---|
303 | DrawFrameControl(hdc, &rect, DFC_SCROLL,
|
---|
304 | (dwStyle & UDS_HORZ ? DFCS_SCROLLRIGHT : DFCS_SCROLLDOWN) |
|
---|
305 | (prssed ? DFCS_PUSHED : 0) |
|
---|
306 | (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
|
---|
307 | }
|
---|
308 |
|
---|
309 | /***********************************************************************
|
---|
310 | * UPDOWN_Refresh [Internal]
|
---|
311 | *
|
---|
312 | * Synchronous drawing (must NOT be used in WM_PAINT).
|
---|
313 | * Calls UPDOWN_Draw.
|
---|
314 | */
|
---|
315 | static void UPDOWN_Refresh (HWND hwnd)
|
---|
316 | {
|
---|
317 | HDC hdc;
|
---|
318 |
|
---|
319 | hdc = GetDC (hwnd);
|
---|
320 | UPDOWN_Draw (hwnd, hdc);
|
---|
321 | ReleaseDC (hwnd, hdc);
|
---|
322 | }
|
---|
323 |
|
---|
324 |
|
---|
325 | /***********************************************************************
|
---|
326 | * UPDOWN_Paint [Internal]
|
---|
327 | *
|
---|
328 | * Asynchronous drawing (must ONLY be used in WM_PAINT).
|
---|
329 | * Calls UPDOWN_Draw.
|
---|
330 | */
|
---|
331 | static void UPDOWN_Paint (HWND hwnd)
|
---|
332 | {
|
---|
333 | PAINTSTRUCT ps;
|
---|
334 | HDC hdc;
|
---|
335 |
|
---|
336 | hdc = BeginPaint (hwnd, &ps);
|
---|
337 | UPDOWN_Draw (hwnd, hdc);
|
---|
338 | EndPaint (hwnd, &ps);
|
---|
339 | }
|
---|
340 |
|
---|
341 | /***********************************************************************
|
---|
342 | * UPDOWN_SetBuddy
|
---|
343 | * Tests if 'hwndBud' is a valid window handle. If not, returns FALSE.
|
---|
344 | * Else, sets it as a new Buddy.
|
---|
345 | * Then, it should subclass the buddy
|
---|
346 | * If window has the UDS_ARROWKEYS, it subcalsses the buddy window to
|
---|
347 | * process the UP/DOWN arrow keys.
|
---|
348 | * If window has the UDS_ALIGNLEFT or UDS_ALIGNRIGHT style
|
---|
349 | * the size/pos of the buddy and the control are adjusted accordingly.
|
---|
350 | */
|
---|
351 | static BOOL UPDOWN_SetBuddy (HWND hwnd, HWND hwndBud)
|
---|
352 | {
|
---|
353 | UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr (hwnd);
|
---|
354 | DWORD dwStyle = GetWindowLongA (hwnd, GWL_STYLE);
|
---|
355 | RECT budRect; /* new coord for the buddy */
|
---|
356 | int x; /* new x position and width for the up-down */
|
---|
357 |
|
---|
358 | *infoPtr->szBuddyClass = '\0';
|
---|
359 |
|
---|
360 | /* Is is a valid bud? */
|
---|
361 | if(!IsWindow(hwndBud))
|
---|
362 | return FALSE;
|
---|
363 |
|
---|
364 | /* Store buddy window clas name */
|
---|
365 | GetClassNameA (hwndBud, infoPtr->szBuddyClass, 40);
|
---|
366 |
|
---|
367 | if(dwStyle & UDS_ARROWKEYS){
|
---|
368 | // FIXME(updown, "we need to subclass the buddy to process the arrow keys.\n");
|
---|
369 | }
|
---|
370 |
|
---|
371 | /* do we need to do any adjustments? */
|
---|
372 | if(!(dwStyle & (UDS_ALIGNLEFT | UDS_ALIGNRIGHT)))
|
---|
373 | return TRUE;
|
---|
374 |
|
---|
375 | infoPtr->Buddy = hwndBud;
|
---|
376 |
|
---|
377 | /* Get the rect of the buddy relative to its parent */
|
---|
378 | GetWindowRect(infoPtr->Buddy, &budRect);
|
---|
379 | MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Buddy),
|
---|
380 | (POINT *)(&budRect.left), 2);
|
---|
381 |
|
---|
382 | /* now do the positioning */
|
---|
383 | if(dwStyle & UDS_ALIGNRIGHT){
|
---|
384 | budRect.right -= DEFAULT_WIDTH+DEFAULT_XSEP;
|
---|
385 | x = budRect.right+DEFAULT_XSEP;
|
---|
386 | }
|
---|
387 | else{ /* UDS_ALIGNLEFT */
|
---|
388 | x = budRect.left;
|
---|
389 | budRect.left += DEFAULT_WIDTH+DEFAULT_XSEP;
|
---|
390 | }
|
---|
391 |
|
---|
392 | /* first adjust the buddy to accomodate the up/down */
|
---|
393 | SetWindowPos(infoPtr->Buddy, 0, budRect.left, budRect.top,
|
---|
394 | budRect.right - budRect.left, budRect.bottom - budRect.top,
|
---|
395 | SWP_NOACTIVATE|SWP_NOZORDER);
|
---|
396 |
|
---|
397 | /* now position the up/down */
|
---|
398 | /* Since the UDS_ALIGN* flags were used, */
|
---|
399 | /* we will pick the position and size of the window. */
|
---|
400 |
|
---|
401 | SetWindowPos (hwnd, 0, x, budRect.top-DEFAULT_ADDTOP,DEFAULT_WIDTH,
|
---|
402 | (budRect.bottom-budRect.top)+DEFAULT_ADDTOP+DEFAULT_ADDBOT,
|
---|
403 | SWP_NOACTIVATE|SWP_NOZORDER);
|
---|
404 |
|
---|
405 | return TRUE;
|
---|
406 | }
|
---|
407 |
|
---|
408 | /***********************************************************************
|
---|
409 | * UPDOWN_DoAction
|
---|
410 | *
|
---|
411 | * This function increments/decrements the CurVal by the
|
---|
412 | * 'delta' amount according to the 'incr' flag
|
---|
413 | * It notifies the parent as required.
|
---|
414 | * It handles wraping and non-wraping correctly.
|
---|
415 | * It is assumed that delta>0
|
---|
416 | */
|
---|
417 | static void UPDOWN_DoAction (HWND hwnd, int delta, BOOL incr)
|
---|
418 | {
|
---|
419 | UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr (hwnd);
|
---|
420 | DWORD dwStyle = GetWindowLongA (hwnd, GWL_STYLE);
|
---|
421 | int old_val = infoPtr->CurVal;
|
---|
422 | NM_UPDOWN ni;
|
---|
423 |
|
---|
424 | // TRACE(updown, "%s by %d\n", incr ? "inc" : "dec", delta);
|
---|
425 |
|
---|
426 | /* check if we can do the modification first */
|
---|
427 | delta *= (incr ? 1 : -1) * (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1);
|
---|
428 | if(!UPDOWN_OffsetVal (hwnd, delta))
|
---|
429 | return;
|
---|
430 |
|
---|
431 | /* so, if we can do the change, recompute delta and restore old value */
|
---|
432 | delta = infoPtr->CurVal - old_val;
|
---|
433 | infoPtr->CurVal = old_val;
|
---|
434 |
|
---|
435 | /* We must notify parent now to obtain permission */
|
---|
436 | ni.iPos = infoPtr->CurVal;
|
---|
437 | ni.iDelta = delta;
|
---|
438 | ni.hdr.hwndFrom = hwnd;
|
---|
439 | ni.hdr.idFrom = GetWindowLongA (hwnd, GWL_ID);
|
---|
440 | ni.hdr.code = UDN_DELTAPOS;
|
---|
441 | if (SendMessageA(GetParent (hwnd), WM_NOTIFY,
|
---|
442 | (WPARAM)ni.hdr.idFrom, (LPARAM)&ni))
|
---|
443 | return; /* we are not allowed to change */
|
---|
444 |
|
---|
445 | /* Now adjust value with (maybe new) delta */
|
---|
446 | if (!UPDOWN_OffsetVal (hwnd, ni.iDelta))
|
---|
447 | return;
|
---|
448 |
|
---|
449 | /* Now take care about our buddy */
|
---|
450 | if(!IsWindow(infoPtr->Buddy))
|
---|
451 | return; /* Nothing else to do */
|
---|
452 |
|
---|
453 |
|
---|
454 | if (dwStyle & UDS_SETBUDDYINT)
|
---|
455 | UPDOWN_SetBuddyInt (hwnd);
|
---|
456 |
|
---|
457 | /* Also, notify it */
|
---|
458 | /* FIXME: do we need to send the notification only if
|
---|
459 | we do not have the UDS_SETBUDDYINT style set? */
|
---|
460 |
|
---|
461 | SendMessageA (GetParent (hwnd),
|
---|
462 | dwStyle & UDS_HORZ ? WM_HSCROLL : WM_VSCROLL,
|
---|
463 | MAKELONG(incr ? SB_LINEUP : SB_LINEDOWN, infoPtr->CurVal),
|
---|
464 | hwnd);
|
---|
465 | }
|
---|
466 |
|
---|
467 | /***********************************************************************
|
---|
468 | * UPDOWN_IsEnabled
|
---|
469 | *
|
---|
470 | * Returns TRUE if it is enabled as well as its buddy (if any)
|
---|
471 | * FALSE otherwise
|
---|
472 | */
|
---|
473 | static BOOL UPDOWN_IsEnabled (HWND hwnd)
|
---|
474 | {
|
---|
475 | UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr (hwnd);
|
---|
476 |
|
---|
477 | if(GetWindowLongA (hwnd, GWL_STYLE) & WS_DISABLED)
|
---|
478 | return FALSE;
|
---|
479 | return IsWindowEnabled(infoPtr->Buddy);
|
---|
480 | }
|
---|
481 |
|
---|
482 | /***********************************************************************
|
---|
483 | * UPDOWN_CancelMode
|
---|
484 | *
|
---|
485 | * Deletes any timers, releases the mouse and does redraw if necessary.
|
---|
486 | * If the control is not in "capture" mode, it does nothing.
|
---|
487 | * If the control was not in cancel mode, it returns FALSE.
|
---|
488 | * If the control was in cancel mode, it returns TRUE.
|
---|
489 | */
|
---|
490 | static BOOL UPDOWN_CancelMode (HWND hwnd)
|
---|
491 | {
|
---|
492 | UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr (hwnd);
|
---|
493 |
|
---|
494 | /* if not in 'capture' mode, do nothing */
|
---|
495 | if(!(infoPtr->Flags & FLAG_CLICKED))
|
---|
496 | return FALSE;
|
---|
497 |
|
---|
498 | KillTimer (hwnd, TIMERID1); /* kill all possible timers */
|
---|
499 | KillTimer (hwnd, TIMERID2);
|
---|
500 |
|
---|
501 | if (GetCapture() == hwnd) /* let the mouse go */
|
---|
502 | ReleaseCapture(); /* if we still have it */
|
---|
503 |
|
---|
504 | infoPtr->Flags = 0; /* get rid of any flags */
|
---|
505 | UPDOWN_Refresh (hwnd); /* redraw the control just in case */
|
---|
506 |
|
---|
507 | return TRUE;
|
---|
508 | }
|
---|
509 |
|
---|
510 | /***********************************************************************
|
---|
511 | * UPDOWN_HandleMouseEvent
|
---|
512 | *
|
---|
513 | * Handle a mouse event for the updown.
|
---|
514 | * 'pt' is the location of the mouse event in client or
|
---|
515 | * windows coordinates.
|
---|
516 | */
|
---|
517 | static void UPDOWN_HandleMouseEvent (HWND hwnd, UINT msg, POINT pt)
|
---|
518 | {
|
---|
519 | UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr (hwnd);
|
---|
520 | DWORD dwStyle = GetWindowLongA (hwnd, GWL_STYLE);
|
---|
521 | RECT rect;
|
---|
522 | int temp;
|
---|
523 |
|
---|
524 | switch(msg)
|
---|
525 | {
|
---|
526 | case WM_LBUTTONDOWN: /* Initialise mouse tracking */
|
---|
527 | /* If we are already in the 'clicked' mode, then nothing to do */
|
---|
528 | if(infoPtr->Flags & FLAG_CLICKED)
|
---|
529 | return;
|
---|
530 |
|
---|
531 | /* If the buddy is an edit, will set focus to it */
|
---|
532 | if (!lstrcmpA (infoPtr->szBuddyClass, "Edit"))
|
---|
533 | SetFocus(infoPtr->Buddy);
|
---|
534 |
|
---|
535 | /* Now see which one is the 'active' arrow */
|
---|
536 | temp = UPDOWN_GetArrowFromPoint (hwnd, &rect, pt);
|
---|
537 |
|
---|
538 | /* Update the CurVal if necessary */
|
---|
539 | if (dwStyle & UDS_SETBUDDYINT)
|
---|
540 | UPDOWN_GetBuddyInt (hwnd);
|
---|
541 |
|
---|
542 | /* Before we proceed, see if we can spin... */
|
---|
543 | if(!(dwStyle & UDS_WRAP))
|
---|
544 | if(( temp && infoPtr->CurVal==infoPtr->MaxVal) ||
|
---|
545 | (!temp && infoPtr->CurVal==infoPtr->MinVal))
|
---|
546 | return;
|
---|
547 |
|
---|
548 | /* Set up the correct flags */
|
---|
549 | infoPtr->Flags = 0;
|
---|
550 | infoPtr->Flags |= temp ? FLAG_INCR : FLAG_DECR;
|
---|
551 | infoPtr->Flags |= FLAG_MOUSEIN;
|
---|
552 |
|
---|
553 | /* repaint the control */
|
---|
554 | UPDOWN_Refresh (hwnd);
|
---|
555 |
|
---|
556 | /* process the click */
|
---|
557 | UPDOWN_DoAction (hwnd, 1, infoPtr->Flags & FLAG_INCR);
|
---|
558 |
|
---|
559 | /* now capture all mouse messages */
|
---|
560 | SetCapture (hwnd);
|
---|
561 |
|
---|
562 | /* and startup the first timer */
|
---|
563 | SetTimer(hwnd, TIMERID1, INITIAL_DELAY, 0);
|
---|
564 | break;
|
---|
565 |
|
---|
566 | case WM_MOUSEMOVE:
|
---|
567 | /* If we are not in the 'clicked' mode, then nothing to do */
|
---|
568 | if(!(infoPtr->Flags & FLAG_CLICKED))
|
---|
569 | return;
|
---|
570 |
|
---|
571 | /* save the flags to see if any got modified */
|
---|
572 | temp = infoPtr->Flags;
|
---|
573 |
|
---|
574 | /* Now get the 'active' arrow rectangle */
|
---|
575 | if (infoPtr->Flags & FLAG_INCR)
|
---|
576 | UPDOWN_GetArrowRect (hwnd, &rect, TRUE);
|
---|
577 | else
|
---|
578 | UPDOWN_GetArrowRect (hwnd, &rect, FALSE);
|
---|
579 |
|
---|
580 | /* Update the flags if we are in/out */
|
---|
581 | if(PtInRect(&rect, pt))
|
---|
582 | infoPtr->Flags |= FLAG_MOUSEIN;
|
---|
583 | else{
|
---|
584 | infoPtr->Flags &= ~FLAG_MOUSEIN;
|
---|
585 | if(accelIndex != -1) /* if we have accel info */
|
---|
586 | accelIndex = 0; /* reset it */
|
---|
587 | }
|
---|
588 | /* If state changed, redraw the control */
|
---|
589 | if(temp != infoPtr->Flags)
|
---|
590 | UPDOWN_Refresh (hwnd);
|
---|
591 | break;
|
---|
592 |
|
---|
593 | default:
|
---|
594 | // ERR(updown, "Impossible case!\n");
|
---|
595 | break;
|
---|
596 | }
|
---|
597 |
|
---|
598 | }
|
---|
599 |
|
---|
600 | /***********************************************************************
|
---|
601 | * UpDownWndProc
|
---|
602 | */
|
---|
603 | LRESULT WINAPI UpDownWindowProc(HWND hwnd, UINT message, WPARAM wParam,
|
---|
604 | LPARAM lParam)
|
---|
605 | {
|
---|
606 | UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr (hwnd);
|
---|
607 | DWORD dwStyle = GetWindowLongA (hwnd, GWL_STYLE);
|
---|
608 | int temp;
|
---|
609 |
|
---|
610 | switch(message)
|
---|
611 | {
|
---|
612 | case WM_NCCREATE:
|
---|
613 | /* get rid of border, if any */
|
---|
614 | SetWindowLongA (hwnd, GWL_STYLE, dwStyle & ~WS_BORDER);
|
---|
615 | return TRUE;
|
---|
616 |
|
---|
617 | case WM_CREATE:
|
---|
618 | infoPtr = (UPDOWN_INFO*)COMCTL32_Alloc (sizeof(UPDOWN_INFO));
|
---|
619 | SetWindowLongA (hwnd, 0, (DWORD)infoPtr);
|
---|
620 |
|
---|
621 | /* initialize the info struct */
|
---|
622 | infoPtr->AccelCount=0; infoPtr->AccelVect=0;
|
---|
623 | infoPtr->CurVal=0; infoPtr->MinVal=0; infoPtr->MaxVal=100; /*FIXME*/
|
---|
624 | infoPtr->Base = 10; /* Default to base 10 */
|
---|
625 | infoPtr->Buddy = 0; /* No buddy window yet */
|
---|
626 | infoPtr->Flags = 0; /* And no flags */
|
---|
627 |
|
---|
628 | /* Do we pick the buddy win ourselves? */
|
---|
629 | if (dwStyle & UDS_AUTOBUDDY)
|
---|
630 | UPDOWN_SetBuddy (hwnd, GetWindow (hwnd, GW_HWNDPREV));
|
---|
631 |
|
---|
632 | // TRACE(updown, "UpDown Ctrl creation, hwnd=%04x\n", hwnd);
|
---|
633 | break;
|
---|
634 |
|
---|
635 | case WM_DESTROY:
|
---|
636 | if(infoPtr->AccelVect)
|
---|
637 | COMCTL32_Free (infoPtr->AccelVect);
|
---|
638 |
|
---|
639 | COMCTL32_Free (infoPtr);
|
---|
640 |
|
---|
641 | // TRACE(updown, "UpDown Ctrl destruction, hwnd=%04x\n", hwnd);
|
---|
642 | break;
|
---|
643 |
|
---|
644 | case WM_ENABLE:
|
---|
645 | if (dwStyle & WS_DISABLED)
|
---|
646 | UPDOWN_CancelMode (hwnd);
|
---|
647 | UPDOWN_Paint (hwnd);
|
---|
648 | break;
|
---|
649 |
|
---|
650 | case WM_TIMER:
|
---|
651 | /* if initial timer, kill it and start the repeat timer */
|
---|
652 | if(wParam == TIMERID1){
|
---|
653 | KillTimer(hwnd, TIMERID1);
|
---|
654 | /* if no accel info given, used default timer */
|
---|
655 | if(infoPtr->AccelCount==0 || infoPtr->AccelVect==0){
|
---|
656 | accelIndex = -1;
|
---|
657 | temp = REPEAT_DELAY;
|
---|
658 | }
|
---|
659 | else{
|
---|
660 | accelIndex = 0; /* otherwise, use it */
|
---|
661 | temp = infoPtr->AccelVect[accelIndex].nSec * 1000 + 1;
|
---|
662 | }
|
---|
663 | SetTimer(hwnd, TIMERID2, temp, 0);
|
---|
664 | }
|
---|
665 |
|
---|
666 | /* now, if the mouse is above us, do the thing...*/
|
---|
667 | if(infoPtr->Flags & FLAG_MOUSEIN){
|
---|
668 | temp = accelIndex==-1 ? 1 : infoPtr->AccelVect[accelIndex].nInc;
|
---|
669 | UPDOWN_DoAction(hwnd, temp, infoPtr->Flags & FLAG_INCR);
|
---|
670 |
|
---|
671 | if(accelIndex!=-1 && accelIndex < infoPtr->AccelCount-1){
|
---|
672 | KillTimer(hwnd, TIMERID2);
|
---|
673 | accelIndex++; /* move to the next accel info */
|
---|
674 | temp = infoPtr->AccelVect[accelIndex].nSec * 1000 + 1;
|
---|
675 | /* make sure we have at least 1ms intervals */
|
---|
676 | SetTimer(hwnd, TIMERID2, temp, 0);
|
---|
677 | }
|
---|
678 | }
|
---|
679 | break;
|
---|
680 |
|
---|
681 | case WM_CANCELMODE:
|
---|
682 | UPDOWN_CancelMode (hwnd);
|
---|
683 | break;
|
---|
684 |
|
---|
685 | case WM_LBUTTONUP:
|
---|
686 | if(!UPDOWN_CancelMode(hwnd))
|
---|
687 | break;
|
---|
688 | /*If we released the mouse and our buddy is an edit */
|
---|
689 | /* we must select all text in it. */
|
---|
690 | if (!lstrcmpA (infoPtr->szBuddyClass, "Edit"))
|
---|
691 | SendMessageA(infoPtr->Buddy, EM_SETSEL, 0, MAKELONG(0, -1));
|
---|
692 | break;
|
---|
693 |
|
---|
694 | case WM_LBUTTONDOWN:
|
---|
695 | case WM_MOUSEMOVE:
|
---|
696 | if(UPDOWN_IsEnabled(hwnd)){
|
---|
697 | POINT pt;
|
---|
698 | CONV_POINT16TO32( (POINT16 *)&lParam, &pt );
|
---|
699 | UPDOWN_HandleMouseEvent (hwnd, message, pt );
|
---|
700 | }
|
---|
701 | break;
|
---|
702 |
|
---|
703 | case WM_KEYDOWN:
|
---|
704 | if((dwStyle & UDS_ARROWKEYS) && UPDOWN_IsEnabled(hwnd)){
|
---|
705 | switch(wParam){
|
---|
706 | case VK_UP:
|
---|
707 | case VK_DOWN:
|
---|
708 | UPDOWN_GetBuddyInt (hwnd);
|
---|
709 | UPDOWN_DoAction (hwnd, 1, wParam==VK_UP);
|
---|
710 | break;
|
---|
711 | }
|
---|
712 | }
|
---|
713 | break;
|
---|
714 |
|
---|
715 | case WM_PAINT:
|
---|
716 | UPDOWN_Paint (hwnd);
|
---|
717 | break;
|
---|
718 |
|
---|
719 | case UDM_GETACCEL:
|
---|
720 | if (wParam==0 && lParam==0) /*if both zero, */
|
---|
721 | return infoPtr->AccelCount; /*just return the accel count*/
|
---|
722 | if (wParam || lParam){
|
---|
723 | UNKNOWN_PARAM(UDM_GETACCEL, wParam, lParam);
|
---|
724 | return 0;
|
---|
725 | }
|
---|
726 | temp = MIN(infoPtr->AccelCount, wParam);
|
---|
727 | memcpy((void *)lParam, infoPtr->AccelVect, temp*sizeof(UDACCEL));
|
---|
728 | return temp;
|
---|
729 |
|
---|
730 | case UDM_SETACCEL:
|
---|
731 | // TRACE(updown, "UpDown Ctrl new accel info, hwnd=%04x\n", hwnd);
|
---|
732 | if(infoPtr->AccelVect){
|
---|
733 | COMCTL32_Free (infoPtr->AccelVect);
|
---|
734 | infoPtr->AccelCount = 0;
|
---|
735 | infoPtr->AccelVect = 0;
|
---|
736 | }
|
---|
737 | if(wParam==0)
|
---|
738 | return TRUE;
|
---|
739 | infoPtr->AccelVect = COMCTL32_Alloc (wParam*sizeof(UDACCEL));
|
---|
740 | if(infoPtr->AccelVect==0)
|
---|
741 | return FALSE;
|
---|
742 | memcpy(infoPtr->AccelVect, (void*)lParam, wParam*sizeof(UDACCEL));
|
---|
743 | return TRUE;
|
---|
744 |
|
---|
745 | case UDM_GETBASE:
|
---|
746 | if (wParam || lParam)
|
---|
747 | UNKNOWN_PARAM(UDM_GETBASE, wParam, lParam);
|
---|
748 | return infoPtr->Base;
|
---|
749 |
|
---|
750 | case UDM_SETBASE:
|
---|
751 | // TRACE(updown, "UpDown Ctrl new base(%d), hwnd=%04x\n",
|
---|
752 | // wParam, hwnd);
|
---|
753 | if ( !(wParam==10 || wParam==16) || lParam)
|
---|
754 | UNKNOWN_PARAM(UDM_SETBASE, wParam, lParam);
|
---|
755 | if (wParam==10 || wParam==16){
|
---|
756 | temp = infoPtr->Base;
|
---|
757 | infoPtr->Base = wParam;
|
---|
758 | return temp; /* return the prev base */
|
---|
759 | }
|
---|
760 | break;
|
---|
761 |
|
---|
762 | case UDM_GETBUDDY:
|
---|
763 | if (wParam || lParam)
|
---|
764 | UNKNOWN_PARAM(UDM_GETBUDDY, wParam, lParam);
|
---|
765 | return infoPtr->Buddy;
|
---|
766 |
|
---|
767 | case UDM_SETBUDDY:
|
---|
768 | if (lParam)
|
---|
769 | UNKNOWN_PARAM(UDM_SETBUDDY, wParam, lParam);
|
---|
770 | temp = infoPtr->Buddy;
|
---|
771 | infoPtr->Buddy = wParam;
|
---|
772 | UPDOWN_SetBuddy (hwnd, wParam);
|
---|
773 | // TRACE(updown, "UpDown Ctrl new buddy(%04x), hwnd=%04x\n",
|
---|
774 | // infoPtr->Buddy, hwnd);
|
---|
775 | return temp;
|
---|
776 |
|
---|
777 | case UDM_GETPOS:
|
---|
778 | if (wParam || lParam)
|
---|
779 | UNKNOWN_PARAM(UDM_GETPOS, wParam, lParam);
|
---|
780 | temp = UPDOWN_GetBuddyInt (hwnd);
|
---|
781 | return MAKELONG(infoPtr->CurVal, temp ? 0 : 1);
|
---|
782 |
|
---|
783 | case UDM_SETPOS:
|
---|
784 | if (wParam || HIWORD(lParam))
|
---|
785 | UNKNOWN_PARAM(UDM_GETPOS, wParam, lParam);
|
---|
786 | temp = SLOWORD(lParam);
|
---|
787 | // TRACE(updown, "UpDown Ctrl new value(%d), hwnd=%04x\n",
|
---|
788 | // temp, hwnd);
|
---|
789 | if(!UPDOWN_InBounds(hwnd, temp)){
|
---|
790 | if(temp < infoPtr->MinVal)
|
---|
791 | temp = infoPtr->MinVal;
|
---|
792 | if(temp > infoPtr->MaxVal)
|
---|
793 | temp = infoPtr->MaxVal;
|
---|
794 | }
|
---|
795 | wParam = infoPtr->CurVal; /* save prev value */
|
---|
796 | infoPtr->CurVal = temp; /* set the new value */
|
---|
797 | if(dwStyle & UDS_SETBUDDYINT)
|
---|
798 | UPDOWN_SetBuddyInt (hwnd);
|
---|
799 | return wParam; /* return prev value */
|
---|
800 |
|
---|
801 | case UDM_GETRANGE:
|
---|
802 | if (wParam || lParam)
|
---|
803 | UNKNOWN_PARAM(UDM_GETRANGE, wParam, lParam);
|
---|
804 | return MAKELONG(infoPtr->MaxVal, infoPtr->MinVal);
|
---|
805 |
|
---|
806 | case UDM_SETRANGE:
|
---|
807 | if (wParam)
|
---|
808 | UNKNOWN_PARAM(UDM_SETRANGE, wParam, lParam); /* we must have: */
|
---|
809 | infoPtr->MaxVal = SLOWORD(lParam); /* UD_MINVAL <= Max <= UD_MAXVAL */
|
---|
810 | infoPtr->MinVal = SHIWORD(lParam); /* UD_MINVAL <= Min <= UD_MAXVAL */
|
---|
811 | /* |Max-Min| <= UD_MAXVAL */
|
---|
812 | // TRACE(updown, "UpDown Ctrl new range(%d to %d), hwnd=%04x\n",
|
---|
813 | // infoPtr->MinVal, infoPtr->MaxVal, hwnd);
|
---|
814 | break;
|
---|
815 |
|
---|
816 | case UDM_GETRANGE32:
|
---|
817 | if (wParam)
|
---|
818 | *(LPINT)wParam = infoPtr->MinVal;
|
---|
819 | if (lParam)
|
---|
820 | *(LPINT)lParam = infoPtr->MaxVal;
|
---|
821 | break;
|
---|
822 |
|
---|
823 | case UDM_SETRANGE32:
|
---|
824 | infoPtr->MinVal = (INT)wParam;
|
---|
825 | infoPtr->MaxVal = (INT)lParam;
|
---|
826 | if (infoPtr->MaxVal <= infoPtr->MinVal)
|
---|
827 | infoPtr->MaxVal = infoPtr->MinVal + 1;
|
---|
828 | // TRACE(updown, "UpDown Ctrl new range(%d to %d), hwnd=%04x\n",
|
---|
829 | // infoPtr->MinVal, infoPtr->MaxVal, hwnd);
|
---|
830 | break;
|
---|
831 |
|
---|
832 | default:
|
---|
833 | if (message >= WM_USER)
|
---|
834 | // ERR (updown, "unknown msg %04x wp=%04x lp=%08lx\n",
|
---|
835 | // message, wParam, lParam);
|
---|
836 | return DefWindowProcA (hwnd, message, wParam, lParam);
|
---|
837 | }
|
---|
838 |
|
---|
839 | return 0;
|
---|
840 | }
|
---|
841 |
|
---|
842 |
|
---|
843 | /***********************************************************************
|
---|
844 | * UPDOWN_Register [Internal]
|
---|
845 | *
|
---|
846 | * Registers the updown window class.
|
---|
847 | */
|
---|
848 |
|
---|
849 | VOID
|
---|
850 | UPDOWN_Register(void)
|
---|
851 | {
|
---|
852 | WNDCLASSA wndClass;
|
---|
853 |
|
---|
854 | if( GlobalFindAtomA( UPDOWN_CLASSA ) ) return;
|
---|
855 |
|
---|
856 | ZeroMemory( &wndClass, sizeof( WNDCLASSA ) );
|
---|
857 | wndClass.style = CS_GLOBALCLASS | CS_VREDRAW;
|
---|
858 | wndClass.lpfnWndProc = (WNDPROC)UpDownWindowProc;
|
---|
859 | wndClass.cbClsExtra = 0;
|
---|
860 | wndClass.cbWndExtra = sizeof(UPDOWN_INFO*);
|
---|
861 | wndClass.hCursor = LoadCursorA( 0, IDC_ARROWA );
|
---|
862 | wndClass.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
|
---|
863 | wndClass.lpszClassName = UPDOWN_CLASSA;
|
---|
864 |
|
---|
865 | RegisterClassA( &wndClass );
|
---|
866 | }
|
---|
867 |
|
---|
868 |
|
---|
869 | /***********************************************************************
|
---|
870 | * UPDOWN_Unregister [Internal]
|
---|
871 | *
|
---|
872 | * Unregisters the updown window class.
|
---|
873 | */
|
---|
874 |
|
---|
875 | VOID
|
---|
876 | UPDOWN_Unregister (VOID)
|
---|
877 | {
|
---|
878 | if (GlobalFindAtomA (UPDOWN_CLASSA))
|
---|
879 | UnregisterClassA (UPDOWN_CLASSA, (HINSTANCE)NULL);
|
---|
880 | }
|
---|
881 |
|
---|