updown.c 38.9 KB
Newer Older
1
/*
Alexandre Julliard's avatar
Alexandre Julliard committed
2 3
 * Updown control
 *
4
 * Copyright 1997, 2002 Dimitrie O. Paun
Alexandre Julliard's avatar
Alexandre Julliard committed
5
 *
6 7 8 9 10 11 12 13 14 15 16 17
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19
 *
20 21 22 23 24 25 26 27 28
 * NOTE
 * 
 * This code was audited for completeness against the documented features
 * of Comctl32.dll version 6.0 on Sep. 9, 2002, by Dimitrie O. Paun.
 * 
 * Unless otherwise noted, we believe this code to be complete, as per
 * the specification mentioned above.
 * If you discover missing features, or bugs, please note them below.
 * 
Alexandre Julliard's avatar
Alexandre Julliard committed
29 30 31
 */

#include <stdlib.h>
32
#include <string.h>
33
#include <stdarg.h>
34
#include <stdio.h>
35

36
#include "windef.h"
37
#include "winbase.h"
38
#include "wingdi.h"
39
#include "winuser.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
40
#include "winnls.h"
41
#include "commctrl.h"
42
#include "comctl32.h"
43
#include "uxtheme.h"
44
#include "vssym32.h"
45
#include "wine/unicode.h"
46
#include "wine/debug.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
47

48
WINE_DEFAULT_DEBUG_CHANNEL(updown);
49

50 51
typedef struct
{
52
    HWND      Self;            /* Handle to this up-down control */
53
    HWND      Notify;          /* Handle to the parent window */
54
    DWORD     dwStyle;         /* The GWL_STYLE for this window */
55 56 57 58 59 60 61 62
    UINT      AccelCount;      /* Number of elements in AccelVect */
    UDACCEL*  AccelVect;       /* Vector containing AccelCount elements */
    INT       AccelIndex;      /* Current accel index, -1 if not accel'ing */
    INT       Base;            /* Base to display nr in the buddy window */
    INT       CurVal;          /* Current up-down value */
    INT       MinVal;          /* Minimum up-down value */
    INT       MaxVal;          /* Maximum up-down value */
    HWND      Buddy;           /* Handle to the buddy window */
63
    INT       BuddyType;       /* Remembers the buddy type BUDDY_TYPE_* */
64
    INT       Flags;           /* Internal Flags FLAG_* */
65
    BOOL      UnicodeFormat;   /* Marks the use of Unicode internally */
66 67
} UPDOWN_INFO;

Alexandre Julliard's avatar
Alexandre Julliard committed
68 69
/* Control configuration constants */

70 71 72
#define INITIAL_DELAY	500    /* initial timer until auto-inc kicks in */
#define AUTOPRESS_DELAY	250    /* time to keep arrow pressed on KEY_DOWN */
#define REPEAT_DELAY	50     /* delay between auto-increments */
Alexandre Julliard's avatar
Alexandre Julliard committed
73

74
#define DEFAULT_WIDTH	    16 /* default width of the ctrl */
75 76 77 78 79
#define DEFAULT_XSEP         0 /* default separation between buddy and ctrl */
#define DEFAULT_ADDTOP       0 /* amount to extend above the buddy window */
#define DEFAULT_ADDBOT       0 /* amount to extend below the buddy window */
#define DEFAULT_BUDDYBORDER  2 /* Width/height of the buddy border */
#define DEFAULT_BUDDYSPACER  2 /* Spacer between the buddy and the ctrl */
80 81
#define DEFAULT_BUDDYBORDER_THEMED  1 /* buddy border when theming is enabled */
#define DEFAULT_BUDDYSPACER_THEMED  0 /* buddy spacer when theming is enabled */
Alexandre Julliard's avatar
Alexandre Julliard committed
82 83 84

/* Work constants */

85 86 87 88
#define FLAG_INCR	0x01
#define FLAG_DECR	0x02
#define FLAG_MOUSEIN	0x04
#define FLAG_PRESSED	0x08
89
#define FLAG_BUDDYINT	0x10 /* UDS_SETBUDDYINT was set on creation */
90
#define FLAG_ARROW	(FLAG_INCR | FLAG_DECR)
Alexandre Julliard's avatar
Alexandre Julliard committed
91

92 93 94 95
#define BUDDY_TYPE_UNKNOWN 0
#define BUDDY_TYPE_LISTBOX 1
#define BUDDY_TYPE_EDIT    2

96 97 98 99
#define TIMER_AUTOREPEAT   1
#define TIMER_ACCEL        2
#define TIMER_AUTOPRESS    3

100
#define UPDOWN_GetInfoPtr(hwnd) ((UPDOWN_INFO *)GetWindowLongPtrW (hwnd,0))
101
#define COUNT_OF(a) (sizeof(a)/sizeof(a[0]))
Alexandre Julliard's avatar
Alexandre Julliard committed
102

103 104 105
/* id used for SetWindowSubclass */
#define BUDDY_SUBCLASSID   1

106
static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action);
Alexandre Julliard's avatar
Alexandre Julliard committed
107

108 109 110 111
/***********************************************************************
 *           UPDOWN_IsBuddyEdit
 * Tests if our buddy is an edit control.
 */
112
static inline BOOL UPDOWN_IsBuddyEdit(const UPDOWN_INFO *infoPtr)
113 114 115 116 117 118 119 120
{
    return infoPtr->BuddyType == BUDDY_TYPE_EDIT;
}

/***********************************************************************
 *           UPDOWN_IsBuddyListbox
 * Tests if our buddy is a listbox control.
 */
121
static inline BOOL UPDOWN_IsBuddyListbox(const UPDOWN_INFO *infoPtr)
122 123 124 125
{
    return infoPtr->BuddyType == BUDDY_TYPE_LISTBOX;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
126 127 128 129
/***********************************************************************
 *           UPDOWN_InBounds
 * Tests if a given value 'val' is between the Min&Max limits
 */
130
static BOOL UPDOWN_InBounds(const UPDOWN_INFO *infoPtr, int val)
Alexandre Julliard's avatar
Alexandre Julliard committed
131
{
132 133 134 135
    if(infoPtr->MaxVal > infoPtr->MinVal)
        return (infoPtr->MinVal <= val) && (val <= infoPtr->MaxVal);
    else
        return (infoPtr->MaxVal <= val) && (val <= infoPtr->MinVal);
Alexandre Julliard's avatar
Alexandre Julliard committed
136 137 138 139
}

/***********************************************************************
 *           UPDOWN_OffsetVal
140
 * Change the current value by delta.
141
 * It returns TRUE is the value was changed successfully, or FALSE
142
 * if the value was not changed, as it would go out of bounds.
Alexandre Julliard's avatar
Alexandre Julliard committed
143
 */
Dimitrie O. Paun's avatar
Dimitrie O. Paun committed
144
static BOOL UPDOWN_OffsetVal(UPDOWN_INFO *infoPtr, int delta)
Alexandre Julliard's avatar
Alexandre Julliard committed
145
{
146 147
    /* check if we can do the modification first */
    if(!UPDOWN_InBounds (infoPtr, infoPtr->CurVal+delta)) {
148
        if (infoPtr->dwStyle & UDS_WRAP) {
149 150 151 152
            delta += (delta < 0 ? -1 : 1) *
		     (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1) *
		     (infoPtr->MinVal - infoPtr->MaxVal) +
		     (delta < 0 ? 1 : -1);
153
        } else if ((infoPtr->MaxVal > infoPtr->MinVal && infoPtr->CurVal+delta > infoPtr->MaxVal)
154 155 156 157 158
                || (infoPtr->MaxVal < infoPtr->MinVal && infoPtr->CurVal+delta < infoPtr->MaxVal)) {
            delta = infoPtr->MaxVal - infoPtr->CurVal;
        } else {
            delta = infoPtr->MinVal - infoPtr->CurVal;
        }
159
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
160

161
    infoPtr->CurVal += delta;
162
    return delta != 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
163 164
}

165
/***********************************************************************
166
 * UPDOWN_HasBuddyBorder
167 168 169 170
 *
 * When we have a buddy set and that we are aligned on our buddy, we
 * want to draw a sunken edge to make like we are part of that control.
 */
171
static BOOL UPDOWN_HasBuddyBorder(const UPDOWN_INFO *infoPtr)
172
{
173
    return  ( ((infoPtr->dwStyle & (UDS_ALIGNLEFT | UDS_ALIGNRIGHT)) != 0) &&
174
	      UPDOWN_IsBuddyEdit(infoPtr) );
175 176
}

Alexandre Julliard's avatar
Alexandre Julliard committed
177
/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
178
 *           UPDOWN_GetArrowRect
Alexandre Julliard's avatar
Alexandre Julliard committed
179 180
 * wndPtr   - pointer to the up-down wnd
 * rect     - will hold the rectangle
181 182
 * arrow    - FLAG_INCR to get the "increment" rect (up or right)
 *            FLAG_DECR to get the "decrement" rect (down or left)
183
 *            If both flags are present, the envelope is returned.
Alexandre Julliard's avatar
Alexandre Julliard committed
184
 */
185
static void UPDOWN_GetArrowRect (const UPDOWN_INFO* infoPtr, RECT *rect, int arrow)
Alexandre Julliard's avatar
Alexandre Julliard committed
186
{
187 188 189
    HTHEME theme = GetWindowTheme (infoPtr->Self);
    const int border = theme ? DEFAULT_BUDDYBORDER_THEMED : DEFAULT_BUDDYBORDER;
    const int spacer = theme ? DEFAULT_BUDDYSPACER_THEMED : DEFAULT_BUDDYSPACER;
190 191 192 193 194 195 196
    GetClientRect (infoPtr->Self, rect);

    /*
     * Make sure we calculate the rectangle to fit even if we draw the
     * border.
     */
    if (UPDOWN_HasBuddyBorder(infoPtr)) {
197
        if (infoPtr->dwStyle & UDS_ALIGNLEFT)
198
            rect->left += border;
199
        else
200
            rect->right -= border;
201

202
        InflateRect(rect, 0, -border);
203 204
    }

205
    /* now figure out if we need a space away from the buddy */
206 207
    if (IsWindow(infoPtr->Buddy) ) {
	if (infoPtr->dwStyle & UDS_ALIGNLEFT) rect->right -= spacer;
208
	else if (infoPtr->dwStyle & UDS_ALIGNRIGHT) rect->left += spacer;
209
    }
210

211 212 213 214 215
    /*
     * We're calculating the midpoint to figure-out where the
     * separation between the buttons will lay. We make sure that we
     * round the uneven numbers by adding 1.
     */
216
    if (infoPtr->dwStyle & UDS_HORZ) {
217 218
        int len = rect->right - rect->left + 1; /* compute the width */
        if (arrow & FLAG_INCR)
219
            rect->left = rect->left + len/2;
220
        if (arrow & FLAG_DECR)
221
            rect->right =  rect->left + len/2 - (theme ? 0 : 1);
222
    } else {
223 224
        int len = rect->bottom - rect->top + 1; /* compute the height */
        if (arrow & FLAG_INCR)
225
            rect->bottom =  rect->top + len/2 - (theme ? 0 : 1);
226
        if (arrow & FLAG_DECR)
227 228
            rect->top =  rect->top + len/2;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
229 230 231 232 233
}

/***********************************************************************
 *           UPDOWN_GetArrowFromPoint
 * Returns the rectagle (for the up or down arrow) that contains pt.
234 235
 * If it returns the up rect, it returns FLAG_INCR.
 * If it returns the down rect, it returns FLAG_DECR.
Alexandre Julliard's avatar
Alexandre Julliard committed
236
 */
237
static INT UPDOWN_GetArrowFromPoint (const UPDOWN_INFO *infoPtr, RECT *rect, POINT pt)
Alexandre Julliard's avatar
Alexandre Julliard committed
238
{
239 240 241 242 243
    UPDOWN_GetArrowRect (infoPtr, rect, FLAG_INCR);
    if(PtInRect(rect, pt)) return FLAG_INCR;

    UPDOWN_GetArrowRect (infoPtr, rect, FLAG_DECR);
    if(PtInRect(rect, pt)) return FLAG_DECR;
Alexandre Julliard's avatar
Alexandre Julliard committed
244

245
    return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
246 247 248 249 250 251 252
}


/***********************************************************************
 *           UPDOWN_GetThousandSep
 * Returns the thousand sep. If an error occurs, it returns ','.
 */
253
static WCHAR UPDOWN_GetThousandSep(void)
Alexandre Julliard's avatar
Alexandre Julliard committed
254
{
255
    WCHAR sep[2];
Alexandre Julliard's avatar
Alexandre Julliard committed
256

257 258
    if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, sep, 2) != 1)
        sep[0] = ',';
Alexandre Julliard's avatar
Alexandre Julliard committed
259

260
    return sep[0];
Alexandre Julliard's avatar
Alexandre Julliard committed
261 262 263 264 265 266 267 268
}

/***********************************************************************
 *           UPDOWN_GetBuddyInt
 * Tries to read the pos from the buddy window and if it succeeds,
 * it stores it in the control's CurVal
 * returns:
 *   TRUE  - if it read the integer from the buddy successfully
Andreas Mohr's avatar
Andreas Mohr committed
269
 *   FALSE - if an error occurred
Alexandre Julliard's avatar
Alexandre Julliard committed
270
 */
Dimitrie O. Paun's avatar
Dimitrie O. Paun committed
271
static BOOL UPDOWN_GetBuddyInt (UPDOWN_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
272
{
273 274
    WCHAR txt[20], sep, *src, *dst;
    int newVal;
Alexandre Julliard's avatar
Alexandre Julliard committed
275

276
    if (!((infoPtr->Flags & FLAG_BUDDYINT) && IsWindow(infoPtr->Buddy)))
277
        return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
278

279 280 281 282 283 284
    /*if the buddy is a list window, we must set curr index */
    if (UPDOWN_IsBuddyListbox(infoPtr)) {
        newVal = SendMessageW(infoPtr->Buddy, LB_GETCARETINDEX, 0, 0);
        if(newVal < 0) return FALSE;
    } else {
        /* we have a regular window, so will get the text */
285 286 287 288
        /* note that a zero-length string is a legitimate value for 'txt',
         * and ought to result in a successful conversion to '0'. */
        if (GetWindowTextW(infoPtr->Buddy, txt, COUNT_OF(txt)) < 0)
            return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
289

290
        sep = UPDOWN_GetThousandSep();
291 292 293 294 295 296 297

        /* now get rid of the separators */
        for(src = dst = txt; *src; src++)
            if(*src != sep) *dst++ = *src;
        *dst = 0;

        /* try to convert the number and validate it */
298
        newVal = strtolW(txt, &src, infoPtr->Base);
299 300
        if(*src || !UPDOWN_InBounds (infoPtr, newVal)) return FALSE;
    }
301

302 303 304
    TRACE("new value(%d) from buddy (old=%d)\n", newVal, infoPtr->CurVal);
    infoPtr->CurVal = newVal;
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
305 306 307 308 309 310 311 312
}


/***********************************************************************
 *           UPDOWN_SetBuddyInt
 * Tries to set the pos to the buddy window based on current pos
 * returns:
 *   TRUE  - if it set the caption of the  buddy successfully
Andreas Mohr's avatar
Andreas Mohr committed
313
 *   FALSE - if an error occurred
Alexandre Julliard's avatar
Alexandre Julliard committed
314
 */
315
static BOOL UPDOWN_SetBuddyInt (const UPDOWN_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
316
{
317 318 319
    static const WCHAR fmt_hex[] = { '0', 'x', '%', '0', '4', 'X', 0 };
    static const WCHAR fmt_dec_oct[] = { '%', 'd', '\0' };
    const WCHAR *fmt;
320
    WCHAR txt[20], txt_old[20] = { 0 };
321
    int len;
Alexandre Julliard's avatar
Alexandre Julliard committed
322

323
    if (!((infoPtr->Flags & FLAG_BUDDYINT) && IsWindow(infoPtr->Buddy)))
324
        return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
325

326
    TRACE("set new value(%d) to buddy.\n", infoPtr->CurVal);
Alexandre Julliard's avatar
Alexandre Julliard committed
327

328 329 330 331
    /*if the buddy is a list window, we must set curr index */
    if (UPDOWN_IsBuddyListbox(infoPtr)) {
        return SendMessageW(infoPtr->Buddy, LB_SETCURSEL, infoPtr->CurVal, 0) != LB_ERR;
    }
332

333
    /* Regular window, so set caption to the number */
334
    fmt = (infoPtr->Base == 16) ? fmt_hex : fmt_dec_oct;
335
    len = wsprintfW(txt, fmt, infoPtr->CurVal);
Alexandre Julliard's avatar
Alexandre Julliard committed
336 337


Francois Gouget's avatar
Francois Gouget committed
338
    /* Do thousands separation if necessary */
339
    if ((infoPtr->Base == 10) && !(infoPtr->dwStyle & UDS_NOTHOUSANDS) && (len > 3)) {
340 341 342
        WCHAR tmp[COUNT_OF(txt)], *src = tmp, *dst = txt;
        WCHAR sep = UPDOWN_GetThousandSep();
	int start = len % 3;
343

344 345 346 347 348 349 350 351 352
	memcpy(tmp, txt, sizeof(txt));
	if (start == 0) start = 3;
	dst += start;
	src += start;
        for (len=0; *src; len++) {
	    if (len % 3 == 0) *dst++ = sep;
	    *dst++ = *src++;
        }
        *dst = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
353
    }
354

355 356
    /* if nothing changed exit earlier */
    GetWindowTextW(infoPtr->Buddy, txt_old, sizeof(txt_old)/sizeof(WCHAR));
357
    if (lstrcmpiW(txt_old, txt) == 0) return FALSE;
358

359
    return SetWindowTextW(infoPtr->Buddy, txt);
360
}
Alexandre Julliard's avatar
Alexandre Julliard committed
361

362 363 364 365 366
/***********************************************************************
 * UPDOWN_DrawBuddyBackground
 *
 * Draw buddy background for visual integration.
 */
367
static BOOL UPDOWN_DrawBuddyBackground (const UPDOWN_INFO *infoPtr, HDC hdc)
368
{
369
    RECT br, r;
370 371 372
    HTHEME buddyTheme = GetWindowTheme (infoPtr->Buddy);
    if (!buddyTheme) return FALSE;

373 374 375 376 377 378 379 380
    GetWindowRect (infoPtr->Buddy, &br);
    MapWindowPoints (NULL, infoPtr->Self, (POINT*)&br, 2);
    GetClientRect (infoPtr->Self, &r);

    if (infoPtr->dwStyle & UDS_ALIGNLEFT)
        br.left = r.left;
    else if (infoPtr->dwStyle & UDS_ALIGNRIGHT)
        br.right = r.right;
381 382 383 384 385
    /* FIXME: take disabled etc. into account */
    DrawThemeBackground (buddyTheme, hdc, 0, 0, &br, NULL);
    return TRUE;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
386
/***********************************************************************
387
 * UPDOWN_Draw
Alexandre Julliard's avatar
Alexandre Julliard committed
388
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
389 390
 * Draw the arrows. The background need not be erased.
 */
391
static LRESULT UPDOWN_Draw (const UPDOWN_INFO *infoPtr, HDC hdc)
Alexandre Julliard's avatar
Alexandre Julliard committed
392
{
393
    BOOL uPressed, uHot, dPressed, dHot;
394
    RECT rect;
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
    HTHEME theme = GetWindowTheme (infoPtr->Self);
    int uPart = 0, uState = 0, dPart = 0, dState = 0;
    BOOL needBuddyBg = FALSE;

    uPressed = (infoPtr->Flags & FLAG_PRESSED) && (infoPtr->Flags & FLAG_INCR);
    uHot = (infoPtr->Flags & FLAG_INCR) && (infoPtr->Flags & FLAG_MOUSEIN);
    dPressed = (infoPtr->Flags & FLAG_PRESSED) && (infoPtr->Flags & FLAG_DECR);
    dHot = (infoPtr->Flags & FLAG_DECR) && (infoPtr->Flags & FLAG_MOUSEIN);
    if (theme) {
        uPart = (infoPtr->dwStyle & UDS_HORZ) ? SPNP_UPHORZ : SPNP_UP;
        uState = (infoPtr->dwStyle & WS_DISABLED) ? DNS_DISABLED 
            : (uPressed ? DNS_PRESSED : (uHot ? DNS_HOT : DNS_NORMAL));
        dPart = (infoPtr->dwStyle & UDS_HORZ) ? SPNP_DOWNHORZ : SPNP_DOWN;
        dState = (infoPtr->dwStyle & WS_DISABLED) ? DNS_DISABLED 
            : (dPressed ? DNS_PRESSED : (dHot ? DNS_HOT : DNS_NORMAL));
        needBuddyBg = IsWindow (infoPtr->Buddy)
            && (IsThemeBackgroundPartiallyTransparent (theme, uPart, uState)
              || IsThemeBackgroundPartiallyTransparent (theme, dPart, dState));
    }
414 415

    /* Draw the common border between ourselves and our buddy */
416 417 418 419 420 421 422
    if (UPDOWN_HasBuddyBorder(infoPtr) || needBuddyBg) {
        if (!theme || !UPDOWN_DrawBuddyBackground (infoPtr, hdc)) {
            GetClientRect(infoPtr->Self, &rect);
	    DrawEdge(hdc, &rect, EDGE_SUNKEN,
		     BF_BOTTOM | BF_TOP |
		     (infoPtr->dwStyle & UDS_ALIGNLEFT ? BF_LEFT : BF_RIGHT));
        }
423
    }
424

425
    /* Draw the incr button */
426
    UPDOWN_GetArrowRect (infoPtr, &rect, FLAG_INCR);
427 428 429 430 431 432 433 434 435
    if (theme) {
        DrawThemeBackground(theme, hdc, uPart, uState, &rect, NULL);
    } else {
        DrawFrameControl(hdc, &rect, DFC_SCROLL,
            (infoPtr->dwStyle & UDS_HORZ ? DFCS_SCROLLRIGHT : DFCS_SCROLLUP) |
            ((infoPtr->dwStyle & UDS_HOTTRACK) && uHot ? DFCS_HOT : 0) |
            (uPressed ? DFCS_PUSHED : 0) |
            (infoPtr->dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
436

437
    /* Draw the decr button */
438
    UPDOWN_GetArrowRect(infoPtr, &rect, FLAG_DECR);
439 440 441 442 443 444 445 446 447
    if (theme) {
        DrawThemeBackground(theme, hdc, dPart, dState, &rect, NULL);
    } else {
        DrawFrameControl(hdc, &rect, DFC_SCROLL,
            (infoPtr->dwStyle & UDS_HORZ ? DFCS_SCROLLLEFT : DFCS_SCROLLDOWN) |
            ((infoPtr->dwStyle & UDS_HOTTRACK) && dHot ? DFCS_HOT : 0) |
            (dPressed ? DFCS_PUSHED : 0) |
            (infoPtr->dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
    }
448 449

    return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
450 451 452
}

/***********************************************************************
453
 * UPDOWN_Paint
Alexandre Julliard's avatar
Alexandre Julliard committed
454
 *
455
 * Asynchronous drawing (must ONLY be used in WM_PAINT).
Alexandre Julliard's avatar
Alexandre Julliard committed
456 457
 * Calls UPDOWN_Draw.
 */
458
static LRESULT UPDOWN_Paint (const UPDOWN_INFO *infoPtr, HDC hdc)
Alexandre Julliard's avatar
Alexandre Julliard committed
459
{
460 461 462 463 464 465
    PAINTSTRUCT ps;
    if (hdc) return UPDOWN_Draw (infoPtr, hdc);
    hdc = BeginPaint (infoPtr->Self, &ps);
    UPDOWN_Draw (infoPtr, hdc);
    EndPaint (infoPtr->Self, &ps);
    return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
466
}
Alexandre Julliard's avatar
Alexandre Julliard committed
467

Alexandre Julliard's avatar
Alexandre Julliard committed
468
/***********************************************************************
469
 * UPDOWN_KeyPressed
Alexandre Julliard's avatar
Alexandre Julliard committed
470
 *
471
 * Handle key presses (up & down) when we have to do so
Alexandre Julliard's avatar
Alexandre Julliard committed
472
 */
473
static LRESULT UPDOWN_KeyPressed(UPDOWN_INFO *infoPtr, int key)
Alexandre Julliard's avatar
Alexandre Julliard committed
474
{
475
    int arrow, accel;
476

477 478 479
    if (key == VK_UP) arrow = FLAG_INCR;
    else if (key == VK_DOWN) arrow = FLAG_DECR;
    else return 1;
480

481 482 483 484 485
    UPDOWN_GetBuddyInt (infoPtr);
    infoPtr->Flags &= ~FLAG_ARROW;
    infoPtr->Flags |= FLAG_PRESSED | arrow;
    InvalidateRect (infoPtr->Self, NULL, FALSE);
    SetTimer(infoPtr->Self, TIMER_AUTOPRESS, AUTOPRESS_DELAY, 0);
486 487
    accel = (infoPtr->AccelCount && infoPtr->AccelVect) ? infoPtr->AccelVect[0].nInc : 1;
    UPDOWN_DoAction (infoPtr, accel, arrow);
488 489 490
    return 0;
}

491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
static int UPDOWN_GetPos(UPDOWN_INFO *infoPtr, BOOL *err)
{
    BOOL succ = UPDOWN_GetBuddyInt(infoPtr);
    int val = infoPtr->CurVal;

    if(!UPDOWN_InBounds(infoPtr, val)) {
        if((infoPtr->MinVal < infoPtr->MaxVal && val < infoPtr->MinVal)
                || (infoPtr->MinVal > infoPtr->MaxVal && val > infoPtr->MinVal))
            val = infoPtr->MinVal;
        else
            val = infoPtr->MaxVal;

        succ = FALSE;
    }

    if(err) *err = !succ;
    return val;
}

510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
static int UPDOWN_SetPos(UPDOWN_INFO *infoPtr, int pos)
{
    int ret = infoPtr->CurVal;

    if(!UPDOWN_InBounds(infoPtr, pos)) {
        if((infoPtr->MinVal < infoPtr->MaxVal && pos < infoPtr->MinVal)
                || (infoPtr->MinVal > infoPtr->MaxVal && pos > infoPtr->MinVal))
            pos = infoPtr->MinVal;
        else
            pos = infoPtr->MaxVal;
    }

    infoPtr->CurVal = pos;
    UPDOWN_SetBuddyInt(infoPtr);

    if(!UPDOWN_InBounds(infoPtr, ret)) {
        if((infoPtr->MinVal < infoPtr->MaxVal && ret < infoPtr->MinVal)
                || (infoPtr->MinVal > infoPtr->MaxVal && ret > infoPtr->MinVal))
            ret = infoPtr->MinVal;
        else
            ret = infoPtr->MaxVal;
    }
    return ret;
}


536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
/***********************************************************************
 * UPDOWN_SetRange
 *
 * Handle UDM_SETRANGE, UDM_SETRANGE32
 *
 * FIXME: handle Max == Min properly:
 *        - arrows should be disabled (without WS_DISABLED set),
 *          visually they can't be pressed and don't respond;
 *        - all input messages should still pass in.
 */
static LRESULT UPDOWN_SetRange(UPDOWN_INFO *infoPtr, INT Max, INT Min)
{
    infoPtr->MaxVal = Max;
    infoPtr->MinVal = Min;

    TRACE("UpDown Ctrl new range(%d to %d), hwnd=%p\n",
           infoPtr->MinVal, infoPtr->MaxVal, infoPtr->Self);

    return 0;
}

557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
/***********************************************************************
 * UPDOWN_MouseWheel
 *
 * Handle mouse wheel scrolling
 */
static LRESULT UPDOWN_MouseWheel(UPDOWN_INFO *infoPtr, WPARAM wParam)
{
    int iWheelDelta = GET_WHEEL_DELTA_WPARAM(wParam) / WHEEL_DELTA;

    if (wParam & (MK_SHIFT | MK_CONTROL))
        return 0;

    if (iWheelDelta != 0)
    {
        UPDOWN_GetBuddyInt(infoPtr);
        UPDOWN_DoAction(infoPtr, abs(iWheelDelta), iWheelDelta > 0 ? FLAG_INCR : FLAG_DECR);
    }

    return 1;
}


579
/***********************************************************************
580
 * UPDOWN_Buddy_SubclassProc used to handle messages sent to the buddy
581 582
 *                           control.
 */
583
static LRESULT CALLBACK
584 585
UPDOWN_Buddy_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
                          UINT_PTR uId, DWORD_PTR ref_data)
586
{
587
    UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr((HWND)ref_data);
588

589 590
    TRACE("hwnd=%p, uMsg=%04x, wParam=%08lx, lParam=%08lx\n",
          hwnd, uMsg, wParam, lParam);
Dimitrie O. Paun's avatar
Dimitrie O. Paun committed
591

592 593 594 595
    switch(uMsg)
    {
    case WM_KEYDOWN:
	UPDOWN_KeyPressed(infoPtr, (int)wParam);
596
	if ((wParam == VK_UP) || (wParam == VK_DOWN)) return 0;
597 598 599 600 601
	break;

    case WM_MOUSEWHEEL:
	UPDOWN_MouseWheel(infoPtr, (int)wParam);
	break;
602

603 604
    default:
	break;
605
    }
606

607
    return DefSubclassProc(hwnd, uMsg, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
608 609 610 611
}

/***********************************************************************
 *           UPDOWN_SetBuddy
612 613
 *
 * Sets bud as a new Buddy.
614
 * Then, it should subclass the buddy
Anders Jonsson's avatar
Anders Jonsson committed
615
 * If window has the UDS_ARROWKEYS, it subclasses the buddy window to
Alexandre Julliard's avatar
Alexandre Julliard committed
616 617 618 619
 * process the UP/DOWN arrow keys.
 * If window has the UDS_ALIGNLEFT or UDS_ALIGNRIGHT style
 * the size/pos of the buddy and the control are adjusted accordingly.
 */
620
static HWND UPDOWN_SetBuddy (UPDOWN_INFO* infoPtr, HWND bud)
Alexandre Julliard's avatar
Alexandre Julliard committed
621
{
622 623
    RECT  budRect;  /* new coord for the buddy */
    int   x, width;  /* new x position and width for the up-down */
624
    WCHAR buddyClass[40];
625
    HWND ret;
626

627
    TRACE("(hwnd=%p, bud=%p)\n", infoPtr->Self, bud);
628

629 630
    ret = infoPtr->Buddy;

631 632 633 634
    /* there is already a buddy assigned */
    if (infoPtr->Buddy) RemoveWindowSubclass(infoPtr->Buddy, UPDOWN_Buddy_SubclassProc,
                                             BUDDY_SUBCLASSID);
    if (!IsWindow(bud)) bud = NULL;
635

636
    /* Store buddy window handle */
637
    infoPtr->Buddy = bud;
638

639 640 641
    if(bud) {
        /* Store buddy window class type */
        infoPtr->BuddyType = BUDDY_TYPE_UNKNOWN;
642
        if (GetClassNameW(bud, buddyClass, COUNT_OF(buddyClass))) {
643
            if (lstrcmpiW(buddyClass, WC_EDITW) == 0)
644
                infoPtr->BuddyType = BUDDY_TYPE_EDIT;
645
            else if (lstrcmpiW(buddyClass, WC_LISTBOXW) == 0)
646 647
                infoPtr->BuddyType = BUDDY_TYPE_LISTBOX;
        }
648

649 650 651
        if (infoPtr->dwStyle & UDS_ARROWKEYS)
            SetWindowSubclass(bud, UPDOWN_Buddy_SubclassProc, BUDDY_SUBCLASSID,
                              (DWORD_PTR)infoPtr->Self);
652

653 654 655 656 657
        /* Get the rect of the buddy relative to its parent */
        GetWindowRect(infoPtr->Buddy, &budRect);
        MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Buddy), (POINT *)(&budRect.left), 2);

        /* now do the positioning */
658
        if  (infoPtr->dwStyle & UDS_ALIGNLEFT) {
659 660
            x  = budRect.left;
            budRect.left += DEFAULT_WIDTH + DEFAULT_XSEP;
661
        } else if (infoPtr->dwStyle & UDS_ALIGNRIGHT) {
662 663 664
            budRect.right -= DEFAULT_WIDTH + DEFAULT_XSEP;
            x  = budRect.right+DEFAULT_XSEP;
        } else {
665 666
            /* nothing to do */
            return ret;
667
        }
668

669
        /* first adjust the buddy to accommodate the up/down */
670 671 672 673 674 675 676 677 678 679 680 681
        SetWindowPos(infoPtr->Buddy, 0, budRect.left, budRect.top,
                     budRect.right  - budRect.left, budRect.bottom - budRect.top,
                     SWP_NOACTIVATE|SWP_NOZORDER);

        /* now position the up/down */
        /* Since the UDS_ALIGN* flags were used, */
        /* we will pick the position and size of the window. */
        width = DEFAULT_WIDTH;

        /*
         * If the updown has a buddy border, it has to overlap with the buddy
         * to look as if it is integrated with the buddy control.
682
         * We nudge the control or change its size to overlap.
683 684
         */
        if (UPDOWN_HasBuddyBorder(infoPtr)) {
685
            if(infoPtr->dwStyle & UDS_ALIGNLEFT)
686 687 688 689
                width += DEFAULT_BUDDYBORDER;
            else
                x -= DEFAULT_BUDDYBORDER;
        }
690

691
        SetWindowPos(infoPtr->Self, 0, x,
692 693 694 695 696 697 698 699 700
                     budRect.top - DEFAULT_ADDTOP, width,
                     budRect.bottom - budRect.top + DEFAULT_ADDTOP + DEFAULT_ADDBOT,
                     SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOZORDER);
    } else {
        RECT rect;
        GetWindowRect(infoPtr->Self, &rect);
        MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Self), (POINT *)&rect, 2);
        SetWindowPos(infoPtr->Self, 0, rect.left, rect.top, DEFAULT_WIDTH, rect.bottom - rect.top,
                     SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOZORDER);
701
    }
702
    return ret;
703
}
Alexandre Julliard's avatar
Alexandre Julliard committed
704 705 706 707

/***********************************************************************
 *           UPDOWN_DoAction
 *
708
 * This function increments/decrements the CurVal by the
709 710
 * 'delta' amount according to the 'action' flag which can be a
 * combination of FLAG_INCR and FLAG_DECR
Alexandre Julliard's avatar
Alexandre Julliard committed
711
 * It notifies the parent as required.
Anders Jonsson's avatar
Anders Jonsson committed
712
 * It handles wrapping and non-wrapping correctly.
Alexandre Julliard's avatar
Alexandre Julliard committed
713 714
 * It is assumed that delta>0
 */
715
static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action)
Alexandre Julliard's avatar
Alexandre Julliard committed
716
{
717 718
    NM_UPDOWN ni;

719
    TRACE("%d by %d\n", action, delta);
720 721

    /* check if we can do the modification first */
722 723
    delta *= (action & FLAG_INCR ? 1 : -1) * (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1);
    if ( (action & FLAG_INCR) && (action & FLAG_DECR) ) delta = 0;
724

725 726
    TRACE("current %d, delta: %d\n", infoPtr->CurVal, delta);

727 728 729 730
    /* We must notify parent now to obtain permission */
    ni.iPos = infoPtr->CurVal;
    ni.iDelta = delta;
    ni.hdr.hwndFrom = infoPtr->Self;
731
    ni.hdr.idFrom   = GetWindowLongPtrW (infoPtr->Self, GWLP_ID);
732
    ni.hdr.code = UDN_DELTAPOS;
733
    if (!SendMessageW(infoPtr->Notify, WM_NOTIFY, ni.hdr.idFrom, (LPARAM)&ni)) {
734 735 736 737
        /* Parent said: OK to adjust */

        /* Now adjust value with (maybe new) delta */
        if (UPDOWN_OffsetVal (infoPtr, ni.iDelta)) {
738 739
            TRACE("new %d, delta: %d\n", infoPtr->CurVal, ni.iDelta);

740
            /* Now take care about our buddy */
741
            UPDOWN_SetBuddyInt (infoPtr);
742 743
        }
    }
744

745
    /* Also, notify it. This message is sent in any case. */
746
    SendMessageW( infoPtr->Notify, (infoPtr->dwStyle & UDS_HORZ) ? WM_HSCROLL : WM_VSCROLL,
747
		  MAKELONG(SB_THUMBPOSITION, infoPtr->CurVal), (LPARAM)infoPtr->Self);
Alexandre Julliard's avatar
Alexandre Julliard committed
748 749 750 751 752 753 754 755
}

/***********************************************************************
 *           UPDOWN_IsEnabled
 *
 * Returns TRUE if it is enabled as well as its buddy (if any)
 *         FALSE otherwise
 */
756
static BOOL UPDOWN_IsEnabled (const UPDOWN_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
757
{
758
    if (!IsWindowEnabled(infoPtr->Self))
759 760 761 762
        return FALSE;
    if(infoPtr->Buddy)
        return IsWindowEnabled(infoPtr->Buddy);
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
763 764 765 766 767 768 769
}

/***********************************************************************
 *           UPDOWN_CancelMode
 *
 * Deletes any timers, releases the mouse and does  redraw if necessary.
 * If the control is not in "capture" mode, it does nothing.
770
 * If the control was not in cancel mode, it returns FALSE.
Alexandre Julliard's avatar
Alexandre Julliard committed
771 772
 * If the control was in cancel mode, it returns TRUE.
 */
Dimitrie O. Paun's avatar
Dimitrie O. Paun committed
773
static BOOL UPDOWN_CancelMode (UPDOWN_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
774
{
775
    if (!(infoPtr->Flags & FLAG_PRESSED)) return FALSE;
776

777 778 779
    KillTimer (infoPtr->Self, TIMER_AUTOREPEAT);
    KillTimer (infoPtr->Self, TIMER_ACCEL);
    KillTimer (infoPtr->Self, TIMER_AUTOPRESS);
780

781 782 783
    if (GetCapture() == infoPtr->Self) {
	NMHDR hdr;
	hdr.hwndFrom = infoPtr->Self;
784
	hdr.idFrom   = GetWindowLongPtrW (infoPtr->Self, GWLP_ID);
785
	hdr.code = NM_RELEASEDCAPTURE;
786
	SendMessageW(infoPtr->Notify, WM_NOTIFY, hdr.idFrom, (LPARAM)&hdr);
787 788
	ReleaseCapture();
    }
789

790 791
    infoPtr->Flags &= ~FLAG_PRESSED;
    InvalidateRect (infoPtr->Self, NULL, FALSE);
792

793
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
794 795 796 797 798 799 800
}

/***********************************************************************
 *           UPDOWN_HandleMouseEvent
 *
 * Handle a mouse event for the updown.
 * 'pt' is the location of the mouse event in client or
801
 * windows coordinates.
Alexandre Julliard's avatar
Alexandre Julliard committed
802
 */
803
static void UPDOWN_HandleMouseEvent (UPDOWN_INFO *infoPtr, UINT msg, INT x, INT y)
Alexandre Julliard's avatar
Alexandre Julliard committed
804
{
805
    POINT pt = { x, y };
806
    RECT rect;
807
    int temp, arrow;
808
    TRACKMOUSEEVENT tme;
Alexandre Julliard's avatar
Alexandre Julliard committed
809

810 811
    TRACE("msg %04x point %s\n", msg, wine_dbgstr_point(&pt));

812
    switch(msg)
Alexandre Julliard's avatar
Alexandre Julliard committed
813
    {
814
        case WM_LBUTTONDOWN:  /* Initialise mouse tracking */
Alexandre Julliard's avatar
Alexandre Julliard committed
815

816 817
            /* If the buddy is an edit, will set focus to it */
	    if (UPDOWN_IsBuddyEdit(infoPtr)) SetFocus(infoPtr->Buddy);
Alexandre Julliard's avatar
Alexandre Julliard committed
818

819
            /* Now see which one is the 'active' arrow */
820 821 822 823 824 825 826 827 828
            arrow = UPDOWN_GetArrowFromPoint (infoPtr, &rect, pt);

            /* Update the flags if we are in/out */
            infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
            if (arrow)
                infoPtr->Flags |= FLAG_MOUSEIN | arrow;
            else
                if (infoPtr->AccelIndex != -1) infoPtr->AccelIndex = 0;

829
	    if (infoPtr->Flags & FLAG_ARROW) {
Alexandre Julliard's avatar
Alexandre Julliard committed
830

831
            	/* Update the CurVal if necessary */
832
            	UPDOWN_GetBuddyInt (infoPtr);
833

834
            	/* Set up the correct flags */
835 836
            	infoPtr->Flags |= FLAG_PRESSED;

837 838
            	/* repaint the control */
	    	InvalidateRect (infoPtr->Self, NULL, FALSE);
839

840
            	/* process the click */
841 842
		temp = (infoPtr->AccelCount && infoPtr->AccelVect) ? infoPtr->AccelVect[0].nInc : 1;
            	UPDOWN_DoAction (infoPtr, temp, infoPtr->Flags & FLAG_ARROW);
843

844 845
            	/* now capture all mouse messages */
            	SetCapture (infoPtr->Self);
846

847 848 849
            	/* and startup the first timer */
            	SetTimer(infoPtr->Self, TIMER_AUTOREPEAT, INITIAL_DELAY, 0);
	    }
850 851 852 853 854 855
            break;

	case WM_MOUSEMOVE:
            /* save the flags to see if any got modified */
            temp = infoPtr->Flags;

856 857
            /* Now see which one is the 'active' arrow */
            arrow = UPDOWN_GetArrowFromPoint (infoPtr, &rect, pt);
858 859

            /* Update the flags if we are in/out */
860 861 862
	    infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
            if(arrow) {
	        infoPtr->Flags |=  FLAG_MOUSEIN | arrow;
863 864 865
            } else {
	        if(infoPtr->AccelIndex != -1) infoPtr->AccelIndex = 0;
            }
866

867
            /* If state changed, redraw the control */
868
            if(temp != infoPtr->Flags)
869 870 871 872 873 874 875 876 877 878 879 880 881
		 InvalidateRect (infoPtr->Self, NULL, FALSE);

            /* Set up tracking so the mousein flags can be reset when the 
             * mouse leaves the control */
            tme.cbSize = sizeof( tme );
            tme.dwFlags = TME_LEAVE;
            tme.hwndTrack = infoPtr->Self;
            TrackMouseEvent (&tme);

            break;
        case WM_MOUSELEAVE:
	    infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
            InvalidateRect (infoPtr->Self, NULL, FALSE);
882 883 884 885
            break;

	default:
	    ERR("Impossible case (msg=%x)!\n", msg);
Alexandre Julliard's avatar
Alexandre Julliard committed
886 887 888 889 890 891 892
    }

}

/***********************************************************************
 *           UpDownWndProc
 */
893
static LRESULT WINAPI UpDownWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
894
{
895
    UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr (hwnd);
896 897
    static const WCHAR themeClass[] = {'S','p','i','n',0};
    HTHEME theme;
898

899
    TRACE("hwnd=%p msg=%04x wparam=%08lx lparam=%08lx\n", hwnd, message, wParam, lParam);
900

901
    if (!infoPtr && (message != WM_CREATE))
902
        return DefWindowProcW (hwnd, message, wParam, lParam);
903 904

    switch(message)
Alexandre Julliard's avatar
Alexandre Julliard committed
905
    {
906
        case WM_CREATE:
907 908 909
	    {
	    CREATESTRUCTW *pcs = (CREATESTRUCTW*)lParam;

910
            infoPtr = Alloc (sizeof(UPDOWN_INFO));
911
	    SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
912 913 914

	    /* initialize the info struct */
	    infoPtr->Self = hwnd;
915 916
	    infoPtr->Notify  = pcs->hwndParent;
	    infoPtr->dwStyle = pcs->style;
917 918 919
	    infoPtr->AccelCount = 0;
	    infoPtr->AccelVect = 0;
	    infoPtr->AccelIndex = -1;
920
	    infoPtr->CurVal = 0;
921 922
	    infoPtr->MinVal = 100;
	    infoPtr->MaxVal = 0;
923 924
	    infoPtr->Base  = 10; /* Default to base 10  */
	    infoPtr->Buddy = 0;  /* No buddy window yet */
925
	    infoPtr->Flags = (infoPtr->dwStyle & UDS_SETBUDDYINT) ? FLAG_BUDDYINT : 0;
926

927
            SetWindowLongW (hwnd, GWL_STYLE, infoPtr->dwStyle & ~WS_BORDER);
928 929
	    if (!(infoPtr->dwStyle & UDS_HORZ))
	        SetWindowPos (hwnd, NULL, 0, 0, DEFAULT_WIDTH, pcs->cy,
930
	                      SWP_NOOWNERZORDER | SWP_NOZORDER | SWP_NOMOVE);
931

932
            /* Do we pick the buddy win ourselves? */
933
	    if (infoPtr->dwStyle & UDS_AUTOBUDDY)
934
		UPDOWN_SetBuddy (infoPtr, GetWindow (hwnd, GW_HWNDPREV));
935

936
	    OpenThemeData (hwnd, themeClass);
937

938
	    TRACE("UpDown Ctrl creation, hwnd=%p\n", hwnd);
939
	    }
940
	    break;
941

942
	case WM_DESTROY:
943
	    Free (infoPtr->AccelVect);
Alexandre Julliard's avatar
Alexandre Julliard committed
944

945 946 947
	    if (infoPtr->Buddy)
	       RemoveWindowSubclass(infoPtr->Buddy, UPDOWN_Buddy_SubclassProc,
	                            BUDDY_SUBCLASSID);
948
	    Free (infoPtr);
949
	    SetWindowLongPtrW (hwnd, 0, 0);
950 951
            theme = GetWindowTheme (hwnd);
            CloseThemeData (theme);
952
	    TRACE("UpDown Ctrl destruction, hwnd=%p\n", hwnd);
953
	    break;
954

955
	case WM_ENABLE:
956 957 958 959 960 961
	    if (wParam) {
		infoPtr->dwStyle &= ~WS_DISABLED;
	    } else {
		infoPtr->dwStyle |= WS_DISABLED;
	    	UPDOWN_CancelMode (infoPtr);
	    }
962
	    InvalidateRect (infoPtr->Self, NULL, FALSE);
963 964
	    break;

965 966 967 968 969 970 971
        case WM_STYLECHANGED:
            if (wParam == GWL_STYLE) {
                infoPtr->dwStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
	        InvalidateRect (infoPtr->Self, NULL, FALSE);
            }
            break;

972 973 974 975 976 977 978
        case WM_THEMECHANGED:
            theme = GetWindowTheme (hwnd);
            CloseThemeData (theme);
            OpenThemeData (hwnd, themeClass);
            InvalidateRect (hwnd, NULL, FALSE);
            break;

979
	case WM_TIMER:
980 981 982 983 984 985 986
	   /* is this the auto-press timer? */
	   if(wParam == TIMER_AUTOPRESS) {
		KillTimer(hwnd, TIMER_AUTOPRESS);
		infoPtr->Flags &= ~(FLAG_PRESSED | FLAG_ARROW);
		InvalidateRect(infoPtr->Self, NULL, FALSE);
	   }

987
	   /* if initial timer, kill it and start the repeat timer */
988
  	   if(wParam == TIMER_AUTOREPEAT) {
989
		INT delay;
990

991
		KillTimer(hwnd, TIMER_AUTOREPEAT);
992 993 994
		/* if no accel info given, used default timer */
		if(infoPtr->AccelCount==0 || infoPtr->AccelVect==0) {
		    infoPtr->AccelIndex = -1;
995
		    delay = REPEAT_DELAY;
996 997
		} else {
		    infoPtr->AccelIndex = 0; /* otherwise, use it */
998
		    delay = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
999
		}
1000
		SetTimer(hwnd, TIMER_ACCEL, delay, 0);
1001 1002 1003 1004
      	    }

	    /* now, if the mouse is above us, do the thing...*/
	    if(infoPtr->Flags & FLAG_MOUSEIN) {
1005 1006
		int temp;

1007
		temp = infoPtr->AccelIndex == -1 ? 1 : infoPtr->AccelVect[infoPtr->AccelIndex].nInc;
1008
		UPDOWN_DoAction(infoPtr, temp, infoPtr->Flags & FLAG_ARROW);
1009

1010
		if(infoPtr->AccelIndex != -1 && infoPtr->AccelIndex < infoPtr->AccelCount-1) {
1011
		    KillTimer(hwnd, TIMER_ACCEL);
1012 1013 1014
		    infoPtr->AccelIndex++; /* move to the next accel info */
		    temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
	  	    /* make sure we have at least 1ms intervals */
1015
		    SetTimer(hwnd, TIMER_ACCEL, temp, 0);
1016 1017 1018 1019 1020 1021 1022 1023
		}
	    }
	    break;

	case WM_CANCELMODE:
	  return UPDOWN_CancelMode (infoPtr);

	case WM_LBUTTONUP:
1024
	    if (GetCapture() != infoPtr->Self) break;
1025

1026 1027
	    if ( (infoPtr->Flags & FLAG_MOUSEIN) &&
		 (infoPtr->Flags & FLAG_ARROW) ) {
1028

1029
	    	SendMessageW( infoPtr->Notify,
1030
			      (infoPtr->dwStyle & UDS_HORZ) ? WM_HSCROLL : WM_VSCROLL,
1031 1032
                  	      MAKELONG(SB_ENDSCROLL, infoPtr->CurVal),
			      (LPARAM)hwnd);
1033 1034 1035 1036
		if (UPDOWN_IsBuddyEdit(infoPtr))
		    SendMessageW(infoPtr->Buddy, EM_SETSEL, 0, MAKELONG(0, -1));
	    }
	    UPDOWN_CancelMode(infoPtr);
1037
	    break;
1038

1039 1040
	case WM_LBUTTONDOWN:
	case WM_MOUSEMOVE:
1041
        case WM_MOUSELEAVE:
1042
	    if(UPDOWN_IsEnabled(infoPtr))
1043
		UPDOWN_HandleMouseEvent (infoPtr, message, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1044 1045
	    break;

1046 1047 1048 1049
        case WM_MOUSEWHEEL:
            UPDOWN_MouseWheel(infoPtr, wParam);
            break;

1050
	case WM_KEYDOWN:
1051
	    if((infoPtr->dwStyle & UDS_ARROWKEYS) && UPDOWN_IsEnabled(infoPtr))
1052
		return UPDOWN_KeyPressed(infoPtr, (int)wParam);
1053
	    break;
1054

1055
	case WM_PRINTCLIENT:
1056
	case WM_PAINT:
1057
	    return UPDOWN_Paint (infoPtr, (HDC)wParam);
1058

1059 1060 1061
	case UDM_GETACCEL:
	    if (wParam==0 && lParam==0) return infoPtr->AccelCount;
	    if (wParam && lParam) {
1062
		int temp = min(infoPtr->AccelCount, wParam);
1063 1064 1065 1066 1067 1068
	        memcpy((void *)lParam, infoPtr->AccelVect, temp*sizeof(UDACCEL));
	        return temp;
      	    }
	    return 0;

	case UDM_SETACCEL:
1069
	{
1070 1071
	    TRACE("UDM_SETACCEL\n");

1072
	    if(infoPtr->AccelVect) {
1073
		Free (infoPtr->AccelVect);
1074 1075 1076 1077
		infoPtr->AccelCount = 0;
		infoPtr->AccelVect  = 0;
      	    }
	    if(wParam==0) return TRUE;
1078
	    infoPtr->AccelVect = Alloc (wParam*sizeof(UDACCEL));
1079 1080
	    if(infoPtr->AccelVect == 0) return FALSE;
	    memcpy(infoPtr->AccelVect, (void*)lParam, wParam*sizeof(UDACCEL));
1081 1082
            infoPtr->AccelCount = wParam;

1083 1084
            if (TRACE_ON(updown))
            {
1085
                UINT i;
1086 1087

                for (i = 0; i < wParam; i++)
1088
                    TRACE("%u: nSec %u nInc %u\n", i,
1089 1090
                        infoPtr->AccelVect[i].nSec, infoPtr->AccelVect[i].nInc);
            }
1091

1092
    	    return TRUE;
1093
	}
1094 1095 1096 1097
	case UDM_GETBASE:
	    return infoPtr->Base;

	case UDM_SETBASE:
1098
	    TRACE("UpDown Ctrl new base(%ld), hwnd=%p\n", wParam, hwnd);
1099
	    if (wParam==10 || wParam==16) {
1100
		WPARAM old_base = infoPtr->Base;
1101
		infoPtr->Base = wParam;
1102 1103 1104 1105 1106

		if (old_base != infoPtr->Base)
		    UPDOWN_SetBuddyInt(infoPtr);

		return old_base;
1107 1108 1109 1110
	    }
	    break;

	case UDM_GETBUDDY:
1111
	    return (LRESULT)infoPtr->Buddy;
1112 1113

	case UDM_SETBUDDY:
1114
	    return (LRESULT)UPDOWN_SetBuddy (infoPtr, (HWND)wParam);
1115 1116

	case UDM_GETPOS:
1117
	{
1118 1119 1120 1121 1122
            BOOL err;
            int pos;

            pos = UPDOWN_GetPos(infoPtr, &err);
            return MAKELONG(pos, err);
1123
	}
1124
	case UDM_SETPOS:
1125
	{
1126
            return UPDOWN_SetPos(infoPtr, (short)LOWORD(lParam));
1127
	}
1128 1129 1130 1131
	case UDM_GETRANGE:
	    return MAKELONG(infoPtr->MaxVal, infoPtr->MinVal);

	case UDM_SETRANGE:
1132 1133 1134 1135 1136
	    /* we must have:
	    UD_MINVAL <= Max <= UD_MAXVAL
	    UD_MINVAL <= Min <= UD_MAXVAL
	    |Max-Min| <= UD_MAXVAL */
	    UPDOWN_SetRange(infoPtr, (short)lParam, (short)HIWORD(lParam));
1137
	    break;
1138 1139 1140 1141 1142 1143 1144

	case UDM_GETRANGE32:
	    if (wParam) *(LPINT)wParam = infoPtr->MinVal;
	    if (lParam) *(LPINT)lParam = infoPtr->MaxVal;
	    break;

	case UDM_SETRANGE32:
1145
	    UPDOWN_SetRange(infoPtr, (INT)lParam, (INT)wParam);
1146 1147 1148
	    break;

	case UDM_GETPOS32:
1149
	{
1150
            return UPDOWN_GetPos(infoPtr, (BOOL*)lParam);
1151
	}
1152
	case UDM_SETPOS32:
1153
	{
1154
            return UPDOWN_SetPos(infoPtr, (int)lParam);
1155
	}
1156 1157 1158 1159 1160
	case UDM_GETUNICODEFORMAT:
	    /* we lie a bit here, we're always using Unicode internally */
	    return infoPtr->UnicodeFormat;

	case UDM_SETUNICODEFORMAT:
1161
	{
1162
	    /* do we really need to honour this flag? */
1163
	    int temp = infoPtr->UnicodeFormat;
1164 1165
	    infoPtr->UnicodeFormat = (BOOL)wParam;
	    return temp;
1166
	}
1167
	default:
1168
	    if ((message >= WM_USER) && (message < WM_APP) && !COMCTL32_IsReflectedMessage(message))
1169
		ERR("unknown msg %04x wp=%04lx lp=%08lx\n", message, wParam, lParam);
1170 1171
	    return DefWindowProcW (hwnd, message, wParam, lParam);
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1172 1173 1174 1175

    return 0;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1176
/***********************************************************************
1177
 *		UPDOWN_Register	[Internal]
Alexandre Julliard's avatar
Alexandre Julliard committed
1178 1179 1180
 *
 * Registers the updown window class.
 */
Dimitrie O. Paun's avatar
Dimitrie O. Paun committed
1181
void UPDOWN_Register(void)
Alexandre Julliard's avatar
Alexandre Julliard committed
1182
{
1183
    WNDCLASSW wndClass;
Alexandre Julliard's avatar
Alexandre Julliard committed
1184

1185
    ZeroMemory( &wndClass, sizeof( WNDCLASSW ) );
Huw Davies's avatar
Huw Davies committed
1186
    wndClass.style         = CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
1187
    wndClass.lpfnWndProc   = UpDownWindowProc;
Alexandre Julliard's avatar
Alexandre Julliard committed
1188
    wndClass.cbClsExtra    = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1189
    wndClass.cbWndExtra    = sizeof(UPDOWN_INFO*);
1190
    wndClass.hCursor       = LoadCursorW( 0, (LPWSTR)IDC_ARROW );
1191
    wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
1192
    wndClass.lpszClassName = UPDOWN_CLASSW;
1193

1194
    RegisterClassW( &wndClass );
Alexandre Julliard's avatar
Alexandre Julliard committed
1195 1196
}

1197 1198 1199 1200 1201 1202

/***********************************************************************
 *		UPDOWN_Unregister	[Internal]
 *
 * Unregisters the updown window class.
 */
Dimitrie O. Paun's avatar
Dimitrie O. Paun committed
1203
void UPDOWN_Unregister (void)
1204
{
1205
    UnregisterClassW (UPDOWN_CLASSW, NULL);
1206
}