progress.c 22.7 KB
Newer Older
1
/*
Alexandre Julliard's avatar
Alexandre Julliard committed
2 3
 * Progress control
 *
4
 * Copyright 1997, 2002 Dimitrie O. Paun
5
 * Copyright 1998, 1999 Eric Kohl
Alexandre Julliard's avatar
Alexandre Julliard committed
6
 *
7 8 9 10 11 12 13 14 15 16 17 18
 * 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
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20
 *
21 22 23 24 25
 * TODO:
 *
 * Styles:
 *    -- PBS_SMOOTHREVERSE
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
26 27
 */

28
#include <stdarg.h>
29
#include <string.h>
30
#include "windef.h"
31
#include "winbase.h"
32 33 34
#include "wingdi.h"
#include "winuser.h"
#include "winnls.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
35
#include "commctrl.h"
36
#include "comctl32.h"
37
#include "uxtheme.h"
38
#include "vssym32.h"
39
#include "wine/debug.h"
40
#include "wine/heap.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
41

42
WINE_DEFAULT_DEBUG_CHANNEL(progress);
43 44 45

typedef struct
{
46 47 48 49 50
    HWND      Self;         /* The window handle for this control */
    INT       CurVal;       /* Current progress value */
    INT       MinVal;       /* Minimum progress value */
    INT       MaxVal;       /* Maximum progress value */
    INT       Step;         /* Step to use on PMB_STEPIT */
51 52
    INT       MarqueePos;   /* Marquee animation position */
    BOOL      Marquee;      /* Whether the marquee animation is enabled */
53 54 55
    COLORREF  ColorBar;     /* Bar color */
    COLORREF  ColorBk;      /* Background color */
    HFONT     Font;         /* Handle to font (not unused) */
56
} PROGRESS_INFO;
Alexandre Julliard's avatar
Alexandre Julliard committed
57

Alexandre Julliard's avatar
Alexandre Julliard committed
58 59
/* Control configuration constants */

60 61 62
#define LED_GAP           2
#define MARQUEE_LEDS      5
#define ID_MARQUEE_TIMER  1
63
#define DEFAULT_MARQUEE_PERIOD 30
Alexandre Julliard's avatar
Alexandre Julliard committed
64

65
/* Helper to obtain size of a progress bar chunk ("led"). */
66
static inline int get_led_size ( const PROGRESS_INFO *infoPtr, LONG style,
67
                                 const RECT* rect )
68
{
69 70 71 72 73 74 75 76
    HTHEME theme = GetWindowTheme (infoPtr->Self);
    if (theme)
    {
        int chunkSize;
        if (SUCCEEDED( GetThemeInt( theme, 0, 0, TMT_PROGRESSCHUNKSIZE, &chunkSize )))
            return chunkSize;
    }

77 78 79 80 81 82
    if (style & PBS_VERTICAL)
        return MulDiv (rect->right - rect->left, 2, 3);
    else
        return MulDiv (rect->bottom - rect->top, 2, 3);
}

83
/* Helper to obtain gap between progress bar chunks */
84
static inline int get_led_gap ( const PROGRESS_INFO *infoPtr )
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
{
    HTHEME theme = GetWindowTheme (infoPtr->Self);
    if (theme)
    {
        int spaceSize;
        if (SUCCEEDED( GetThemeInt( theme, 0, 0, TMT_PROGRESSSPACESIZE, &spaceSize )))
            return spaceSize;
    }

    return LED_GAP;
}

/* Get client rect. Takes into account that theming needs no adjustment. */
static inline void get_client_rect (HWND hwnd, RECT* rect)
{
100
    HTHEME theme = GetWindowTheme (hwnd);
101
    GetClientRect (hwnd, rect);
102
    if (!theme)
103
        InflateRect(rect, -1, -1);
104 105 106 107 108 109
    else
    {
        DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
        int part = (dwStyle & PBS_VERTICAL) ? PP_BARVERT : PP_BAR;
        GetThemeBackgroundContentRect (theme, 0, part, 0, rect, rect);
    }
110 111
}

112 113 114 115 116 117 118 119 120 121
/* Compute the extend of the bar */
static inline int get_bar_size( LONG style, const RECT* rect )
{
    if (style & PBS_VERTICAL)
        return rect->bottom - rect->top;
    else
        return rect->right - rect->left;
}

/* Compute the pixel position of a progress value */
122
static inline int get_bar_position( const PROGRESS_INFO *infoPtr, LONG style,
123 124 125 126 127 128
                                    const RECT* rect, INT value )
{
    return MulDiv (value - infoPtr->MinVal, get_bar_size (style, rect),
                      infoPtr->MaxVal - infoPtr->MinVal);
}

129
/***********************************************************************
130 131
 * PROGRESS_Invalidate
 *
132
 * Don't be too clever about invalidating the progress bar.
133
 * InstallShield depends on this simple behaviour.
134
 */
135
static void PROGRESS_Invalidate( const PROGRESS_INFO *infoPtr, INT old, INT new )
136
{
137
    InvalidateRect( infoPtr->Self, NULL, old > new );
138 139
}

140 141 142 143 144 145 146
/* Information for a progress bar drawing helper */
typedef struct tagProgressDrawInfo
{
    HDC hdc;
    RECT rect;
    HBRUSH hbrBar;
    HBRUSH hbrBk;
147 148 149
    int ledW, ledGap;
    HTHEME theme;
    RECT bgRect;
150 151 152 153 154 155 156 157
} ProgressDrawInfo;

typedef void (*ProgressDrawProc)(const ProgressDrawInfo* di, int start, int end);

/* draw solid horizontal bar from 'start' to 'end' */
static void draw_solid_bar_H (const ProgressDrawInfo* di, int start, int end)
{
    RECT r;
158
    SetRect(&r, di->rect.left + start, di->rect.top, di->rect.left + end, di->rect.bottom);
159 160 161 162 163 164 165
    FillRect (di->hdc, &r, di->hbrBar);
}

/* draw solid horizontal background from 'start' to 'end' */
static void draw_solid_bkg_H (const ProgressDrawInfo* di, int start, int end)
{
    RECT r;
166
    SetRect(&r, di->rect.left + start, di->rect.top, di->rect.left + end, di->rect.bottom);
167 168 169 170 171 172 173
    FillRect (di->hdc, &r, di->hbrBk);
}

/* draw solid vertical bar from 'start' to 'end' */
static void draw_solid_bar_V (const ProgressDrawInfo* di, int start, int end)
{
    RECT r;
174
    SetRect(&r, di->rect.left, di->rect.bottom - end, di->rect.right, di->rect.bottom - start);
175 176 177 178 179 180 181
    FillRect (di->hdc, &r, di->hbrBar);
}

/* draw solid vertical background from 'start' to 'end' */
static void draw_solid_bkg_V (const ProgressDrawInfo* di, int start, int end)
{
    RECT r;
182
    SetRect(&r, di->rect.left, di->rect.bottom - end, di->rect.right, di->rect.bottom - start);
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
    FillRect (di->hdc, &r, di->hbrBk);
}

/* draw chunky horizontal bar from 'start' to 'end' */
static void draw_chunk_bar_H (const ProgressDrawInfo* di, int start, int end)
{
    RECT r;
    int right = di->rect.left + end;
    r.left = di->rect.left + start;
    r.top = di->rect.top;
    r.bottom = di->rect.bottom;
    while (r.left < right)
    {
        r.right = min (r.left + di->ledW, right);
        FillRect (di->hdc, &r, di->hbrBar);
        r.left = r.right;
199
        r.right = min (r.left + di->ledGap, right);
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
        FillRect (di->hdc, &r, di->hbrBk);
        r.left = r.right;
    }
}

/* draw chunky vertical bar from 'start' to 'end' */
static void draw_chunk_bar_V (const ProgressDrawInfo* di, int start, int end)
{
    RECT r;
    int top = di->rect.bottom - end;
    r.left = di->rect.left;
    r.right = di->rect.right;
    r.bottom = di->rect.bottom - start;
    while (r.bottom > top)
    {
        r.top = max (r.bottom - di->ledW, top);
        FillRect (di->hdc, &r, di->hbrBar);
        r.bottom = r.top;
218
        r.top = max (r.bottom - di->ledGap, top);
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
        FillRect (di->hdc, &r, di->hbrBk);
        r.bottom = r.top;
    }
}

/* drawing functions for "classic" style */
static const ProgressDrawProc drawProcClassic[8] = {
  /* Smooth */
    /* Horizontal */
    draw_solid_bar_H, draw_solid_bkg_H,
    /* Vertical */
    draw_solid_bar_V, draw_solid_bkg_V,
  /* Chunky */
    /* Horizontal */
    draw_chunk_bar_H, draw_solid_bkg_H,
    /* Vertical */
    draw_chunk_bar_V, draw_solid_bkg_V,
};
237

238 239 240 241 242 243 244
/* draw themed horizontal bar from 'start' to 'end' */
static void draw_theme_bar_H (const ProgressDrawInfo* di, int start, int end)
{
    RECT r;
    r.left = di->rect.left + start;
    r.top = di->rect.top;
    r.bottom = di->rect.bottom;
245 246
    r.right = di->rect.left + end;
    DrawThemeBackground (di->theme, di->hdc, PP_CHUNK, 0, &r, NULL);
247 248
}

249
/* draw themed vertical bar from 'start' to 'end' */
250 251 252 253 254 255
static void draw_theme_bar_V (const ProgressDrawInfo* di, int start, int end)
{
    RECT r;
    r.left = di->rect.left;
    r.right = di->rect.right;
    r.bottom = di->rect.bottom - start;
256 257
    r.top = di->rect.bottom - end;
    DrawThemeBackground (di->theme, di->hdc, PP_CHUNKVERT, 0, &r, NULL);
258 259 260 261 262
}

/* draw themed horizontal background from 'start' to 'end' */
static void draw_theme_bkg_H (const ProgressDrawInfo* di, int start, int end)
{
263 264
    RECT bgrect, r;

265
    SetRect(&r, di->rect.left + start, di->rect.top, di->rect.left + end, di->rect.bottom);
266 267 268 269
    bgrect = di->bgRect;
    OffsetRect(&bgrect, -bgrect.left, -bgrect.top);

    DrawThemeBackground (di->theme, di->hdc, PP_BAR, 0, &bgrect, &r);
270 271 272 273 274
}

/* draw themed vertical background from 'start' to 'end' */
static void draw_theme_bkg_V (const ProgressDrawInfo* di, int start, int end)
{
275 276
    RECT bgrect, r;

277
    SetRect(&r, di->rect.left, di->rect.bottom - end, di->rect.right, di->rect.bottom - start);
278 279 280 281
    bgrect = di->bgRect;
    OffsetRect(&bgrect, -bgrect.left, -bgrect.top);

    DrawThemeBackground (di->theme, di->hdc, PP_BARVERT, 0, &bgrect, &r);
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
}

/* drawing functions for themed style */
static const ProgressDrawProc drawProcThemed[8] = {
  /* Smooth */
    /* Horizontal */
    draw_theme_bar_H, draw_theme_bkg_H,
    /* Vertical */
    draw_theme_bar_V, draw_theme_bkg_V,
  /* Chunky */
    /* Horizontal */
    draw_theme_bar_H, draw_theme_bkg_H,
    /* Vertical */
    draw_theme_bar_V, draw_theme_bkg_V,
};

Alexandre Julliard's avatar
Alexandre Julliard committed
298
/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
299 300
 * PROGRESS_Draw
 * Draws the progress bar.
Alexandre Julliard's avatar
Alexandre Julliard committed
301
 */
302
static LRESULT PROGRESS_Draw (PROGRESS_INFO *infoPtr, HDC hdc)
Alexandre Julliard's avatar
Alexandre Julliard committed
303
{
304
    int barSize;
305
    DWORD dwStyle;
306 307 308
    BOOL barSmooth;
    const ProgressDrawProc* drawProcs;
    ProgressDrawInfo pdi;
309

310
    TRACE("(infoPtr=%p, hdc=%p)\n", infoPtr, hdc);
311

312
    pdi.hdc = hdc;
313
    pdi.theme = GetWindowTheme (infoPtr->Self);
314

315 316
    /* get the required bar brush */
    if (infoPtr->ColorBar == CLR_DEFAULT)
317
        pdi.hbrBar = GetSysColorBrush(COLOR_HIGHLIGHT);
Alexandre Julliard's avatar
Alexandre Julliard committed
318
    else
319
        pdi.hbrBar = CreateSolidBrush (infoPtr->ColorBar);
320

321
    if (infoPtr->ColorBk == CLR_DEFAULT)
322
        pdi.hbrBk = GetSysColorBrush(COLOR_3DFACE);
323
    else
324
        pdi.hbrBk = CreateSolidBrush(infoPtr->ColorBk);
325 326 327 328

    /* get the window style */
    dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);

329 330
    /* get client rectangle */
    GetClientRect (infoPtr->Self, &pdi.rect);
331 332 333 334
    if (!pdi.theme) {
        FrameRect( hdc, &pdi.rect, pdi.hbrBk );
        InflateRect(&pdi.rect, -1, -1);
    }
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
    else
    {
        RECT cntRect;
        int part = (dwStyle & PBS_VERTICAL) ? PP_BARVERT : PP_BAR;
        
        GetThemeBackgroundContentRect (pdi.theme, hdc, part, 0, &pdi.rect, 
            &cntRect);
        
        /* Exclude content rect - content background will be drawn later */
        ExcludeClipRect (hdc, cntRect.left, cntRect.top, 
            cntRect.right, cntRect.bottom);
        if (IsThemeBackgroundPartiallyTransparent (pdi.theme, part, 0))
            DrawThemeParentBackground (infoPtr->Self, hdc, NULL);
        DrawThemeBackground (pdi.theme, hdc, part, 0, &pdi.rect, NULL);
        SelectClipRgn (hdc, NULL);
350
        pdi.rect = cntRect;
351
    }
352 353

    /* compute some drawing parameters */
354 355
    barSmooth = (dwStyle & PBS_SMOOTH) && !pdi.theme;
    drawProcs = &((pdi.theme ? drawProcThemed : drawProcClassic)[(barSmooth ? 0 : 4)
356 357
        + ((dwStyle & PBS_VERTICAL) ? 2 : 0)]);
    barSize = get_bar_size( dwStyle, &pdi.rect );
358 359 360
    if (pdi.theme)
    {
        GetWindowRect( infoPtr->Self, &pdi.bgRect );
361
        MapWindowPoints( infoPtr->Self, 0, (POINT*)&pdi.bgRect, 2 );
362
    }
363 364 365

    if (!barSmooth)
        pdi.ledW = get_led_size( infoPtr, dwStyle, &pdi.rect);
366
    pdi.ledGap = get_led_gap( infoPtr );
367

368
    if (dwStyle & PBS_MARQUEE)
369
    {
370
        const int ledW = !barSmooth ? (pdi.ledW + pdi.ledGap) : 1;
371 372 373 374
        const int leds = (barSize + ledW - 1) / ledW;
        const int ledMEnd = infoPtr->MarqueePos + MARQUEE_LEDS;

        if (ledMEnd > leds)
375
        {
376 377 378 379 380 381 382 383
            /* case 1: the marquee bar extends over the end and wraps around to 
             * the start */
            const int gapStart = max((ledMEnd - leds) * ledW, 0);
            const int gapEnd = min(infoPtr->MarqueePos * ledW, barSize);

            drawProcs[0]( &pdi, 0, gapStart);
            drawProcs[1]( &pdi, gapStart, gapEnd);
            drawProcs[0]( &pdi, gapEnd, barSize);
384
        }
385
        else
386
        {
387 388 389 390 391 392 393
            /* case 2: the marquee bar is between start and end */
            const int barStart = infoPtr->MarqueePos * ledW;
            const int barEnd = min (ledMEnd * ledW, barSize);

            drawProcs[1]( &pdi, 0, barStart);
            drawProcs[0]( &pdi, barStart, barEnd);
            drawProcs[1]( &pdi, barEnd, barSize);
394
        }
395 396 397 398 399 400 401
    }
    else
    {
        int barEnd = get_bar_position( infoPtr, dwStyle, &pdi.rect,
            infoPtr->CurVal);
        if (!barSmooth)
        {
402
            const int ledW = pdi.ledW + pdi.ledGap;
403
            barEnd = min (((barEnd + ledW - 1) / ledW) * ledW, barSize);
404
        }
405 406
        drawProcs[0]( &pdi, 0, barEnd);
        drawProcs[1]( &pdi, barEnd, barSize);
407 408 409
    }

    /* delete bar brush */
410 411
    if (infoPtr->ColorBar != CLR_DEFAULT) DeleteObject (pdi.hbrBar);
    if (infoPtr->ColorBk != CLR_DEFAULT) DeleteObject (pdi.hbrBk);
412 413

    return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
414
}
Alexandre Julliard's avatar
Alexandre Julliard committed
415

Alexandre Julliard's avatar
Alexandre Julliard committed
416 417 418 419 420
/***********************************************************************
 * PROGRESS_Paint
 * Draw the progress bar. The background need not be erased.
 * If dc!=0, it draws on it
 */
421
static LRESULT PROGRESS_Paint (PROGRESS_INFO *infoPtr, HDC hdc)
Alexandre Julliard's avatar
Alexandre Julliard committed
422
{
423
    PAINTSTRUCT ps;
424 425 426 427 428
    if (hdc) return PROGRESS_Draw (infoPtr, hdc);
    hdc = BeginPaint (infoPtr->Self, &ps);
    PROGRESS_Draw (infoPtr, hdc);
    EndPaint (infoPtr->Self, &ps);
    return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
429 430 431
}


432
/***********************************************************************
433
 * Advance marquee progress by one step.
434
 */
435
static void PROGRESS_UpdateMarquee (PROGRESS_INFO *infoPtr)
436
{
437 438 439 440 441
    LONG style = GetWindowLongW (infoPtr->Self, GWL_STYLE);
    RECT rect;
    int ledWidth, leds;
    HTHEME theme = GetWindowTheme (infoPtr->Self);
    BOOL smooth = (style & PBS_SMOOTH) && !theme;
442

443
    get_client_rect (infoPtr->Self, &rect);
444

445 446 447 448
    if (smooth)
        ledWidth = 1;
    else
        ledWidth = get_led_size( infoPtr, style, &rect ) + get_led_gap( infoPtr );
449

450 451
    leds = (get_bar_size( style, &rect ) + ledWidth - 1) /
        ledWidth;
452

453 454 455
    /* increment the marquee progress */
    if (++infoPtr->MarqueePos >= leds)
        infoPtr->MarqueePos = 0;
456

457 458
    InvalidateRect(infoPtr->Self, &rect, TRUE);
    UpdateWindow(infoPtr->Self);
459 460 461
}


Alexandre Julliard's avatar
Alexandre Julliard committed
462 463
/***********************************************************************
 *           PROGRESS_CoercePos
464
 * Makes sure the current position (CurVal) is within bounds.
Alexandre Julliard's avatar
Alexandre Julliard committed
465
 */
466
static void PROGRESS_CoercePos(PROGRESS_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
467
{
468 469 470 471
    if(infoPtr->CurVal < infoPtr->MinVal)
        infoPtr->CurVal = infoPtr->MinVal;
    if(infoPtr->CurVal > infoPtr->MaxVal)
        infoPtr->CurVal = infoPtr->MaxVal;
Alexandre Julliard's avatar
Alexandre Julliard committed
472 473
}

474 475 476 477 478

/***********************************************************************
 *           PROGRESS_SetFont
 * Set new Font for progress bar
 */
479
static HFONT PROGRESS_SetFont (PROGRESS_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
480
{
481 482
    HFONT hOldFont = infoPtr->Font;
    infoPtr->Font = hFont;
483
    /* Since infoPtr->Font is not used, there is no need for repaint */
484
    return hOldFont;
485 486
}

487 488 489 490 491 492
static DWORD PROGRESS_SetRange (PROGRESS_INFO *infoPtr, int low, int high)
{
    DWORD res = MAKELONG(LOWORD(infoPtr->MinVal), LOWORD(infoPtr->MaxVal));

    /* if nothing changes, simply return */
    if(infoPtr->MinVal == low && infoPtr->MaxVal == high) return res;
493

494 495 496
    infoPtr->MinVal = low;
    infoPtr->MaxVal = high;
    PROGRESS_CoercePos(infoPtr);
497
    InvalidateRect(infoPtr->Self, NULL, TRUE);
498 499
    return res;
}
500

501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
static UINT PROGRESS_SetPos (PROGRESS_INFO *infoPtr, INT pos)
{
    DWORD style = GetWindowLongW(infoPtr->Self, GWL_STYLE);

    if (style & PBS_MARQUEE)
    {
        PROGRESS_UpdateMarquee(infoPtr);
        return 1;
    }
    else
    {
        UINT oldVal;
        oldVal = infoPtr->CurVal;
        if (oldVal != pos) {
	    infoPtr->CurVal = pos;
	    PROGRESS_CoercePos(infoPtr);
	    TRACE("PBM_SETPOS: current pos changed from %d to %d\n", oldVal, infoPtr->CurVal);
            PROGRESS_Invalidate( infoPtr, oldVal, infoPtr->CurVal );
            UpdateWindow( infoPtr->Self );
        }
        return oldVal;
    }
}

Alexandre Julliard's avatar
Alexandre Julliard committed
525 526 527
/***********************************************************************
 *           ProgressWindowProc
 */
528
static LRESULT WINAPI ProgressWindowProc(HWND hwnd, UINT message,
Dimitrie O. Paun's avatar
Dimitrie O. Paun committed
529
                                         WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
530
{
531
    PROGRESS_INFO *infoPtr;
532 533
    static const WCHAR themeClass[] = {'P','r','o','g','r','e','s','s',0};
    HTHEME theme;
534

535
    TRACE("hwnd=%p msg=%04x wparam=%lx lParam=%lx\n", hwnd, message, wParam, lParam);
536

537
    infoPtr = (PROGRESS_INFO *)GetWindowLongPtrW(hwnd, 0);
538 539

    if (!infoPtr && message != WM_CREATE)
540
        return DefWindowProcW( hwnd, message, wParam, lParam );
541

542
    switch(message) {
543
    case WM_CREATE:
544 545
    {
	DWORD dwExStyle = GetWindowLongW (hwnd, GWL_EXSTYLE);
546

547 548
        theme = OpenThemeData (hwnd, themeClass);

549
	dwExStyle &= ~(WS_EX_CLIENTEDGE | WS_EX_WINDOWEDGE);
550
	if (!theme) dwExStyle |= WS_EX_STATICEDGE;
Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
551
        SetWindowLongW (hwnd, GWL_EXSTYLE, dwExStyle);
552 553 554
	/* Force recalculation of a non-client area */
	SetWindowPos(hwnd, 0, 0, 0, 0, 0,
	    SWP_FRAMECHANGED | SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
Alexandre Julliard's avatar
Alexandre Julliard committed
555

556
        /* allocate memory for info struct */
557
        infoPtr = heap_alloc_zero (sizeof(*infoPtr));
558
        if (!infoPtr) return -1;
559
        SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
560 561 562

        /* initialize the info struct */
        infoPtr->Self = hwnd;
563
        infoPtr->MinVal = 0;
564
        infoPtr->MaxVal = 100;
565
        infoPtr->CurVal = 0;
566
        infoPtr->Step = 10;
567 568
        infoPtr->MarqueePos = 0;
        infoPtr->Marquee = FALSE;
569 570 571
        infoPtr->ColorBar = CLR_DEFAULT;
        infoPtr->ColorBk = CLR_DEFAULT;
        infoPtr->Font = 0;
572

573
        TRACE("Progress Ctrl creation, hwnd=%p\n", hwnd);
574
        return 0;
575
    }
576

Alexandre Julliard's avatar
Alexandre Julliard committed
577
    case WM_DESTROY:
578
        TRACE("Progress Ctrl destruction, hwnd=%p\n", hwnd);
579
        heap_free (infoPtr);
580
        SetWindowLongPtrW(hwnd, 0, 0);
581 582 583 584 585 586 587
        theme = GetWindowTheme (hwnd);
        CloseThemeData (theme);
        return 0;

    case WM_ERASEBKGND:
        return 1;

Alexandre Julliard's avatar
Alexandre Julliard committed
588
    case WM_GETFONT:
589
        return (LRESULT)infoPtr->Font;
Alexandre Julliard's avatar
Alexandre Julliard committed
590 591

    case WM_SETFONT:
592
        return (LRESULT)PROGRESS_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
593

594
    case WM_PRINTCLIENT:
Alexandre Julliard's avatar
Alexandre Julliard committed
595
    case WM_PAINT:
596
        return PROGRESS_Paint (infoPtr, (HDC)wParam);
597

598
    case WM_TIMER:
599 600 601
        if (wParam == ID_MARQUEE_TIMER)
            PROGRESS_UpdateMarquee (infoPtr);
        return 0;
602

603
    case WM_THEMECHANGED:
604 605 606
    {
        DWORD dwExStyle = GetWindowLongW (hwnd, GWL_EXSTYLE);
        
607 608
        theme = GetWindowTheme (hwnd);
        CloseThemeData (theme);
609 610 611 612 613 614 615 616 617
        theme = OpenThemeData (hwnd, themeClass);
        
        /* WS_EX_STATICEDGE disappears when the control is themed */
        if (theme)
            dwExStyle &= ~WS_EX_STATICEDGE;
        else
            dwExStyle |= WS_EX_STATICEDGE;
        SetWindowLongW (hwnd, GWL_EXSTYLE, dwExStyle);
        
618 619
        InvalidateRect (hwnd, NULL, FALSE);
        return 0;
620
    }
621

Alexandre Julliard's avatar
Alexandre Julliard committed
622
    case PBM_DELTAPOS:
623 624 625
    {
	INT oldVal;
        oldVal = infoPtr->CurVal;
626
        if(wParam != 0) {
627
	    infoPtr->CurVal += (INT)wParam;
628
	    PROGRESS_CoercePos (infoPtr);
629
	    TRACE("PBM_DELTAPOS: current pos changed from %d to %d\n", oldVal, infoPtr->CurVal);
630
            PROGRESS_Invalidate( infoPtr, oldVal, infoPtr->CurVal );
631
            UpdateWindow( infoPtr->Self );
632
        }
633 634
        return oldVal;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
635 636

    case PBM_SETPOS:
637
        return PROGRESS_SetPos(infoPtr, wParam);
638

639
    case PBM_SETRANGE:
640
        return PROGRESS_SetRange (infoPtr, (int)LOWORD(lParam), (int)HIWORD(lParam));
Alexandre Julliard's avatar
Alexandre Julliard committed
641 642

    case PBM_SETSTEP:
643 644 645 646 647 648
    {
	INT oldStep;
        oldStep = infoPtr->Step;
        infoPtr->Step = (INT)wParam;
        return oldStep;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
649

650 651 652
    case PBM_GETSTEP:
        return infoPtr->Step;

Alexandre Julliard's avatar
Alexandre Julliard committed
653
    case PBM_STEPIT:
654 655
    {
	INT oldVal;
656
        oldVal = infoPtr->CurVal;
657
        infoPtr->CurVal += infoPtr->Step;
658 659 660 661 662 663 664 665
        if (infoPtr->CurVal > infoPtr->MaxVal)
        {
            infoPtr->CurVal = (infoPtr->CurVal - infoPtr->MinVal) % (infoPtr->MaxVal - infoPtr->MinVal) + infoPtr->MinVal;
        }
        if (infoPtr->CurVal < infoPtr->MinVal)
        {
            infoPtr->CurVal = (infoPtr->CurVal - infoPtr->MinVal) % (infoPtr->MaxVal - infoPtr->MinVal) + infoPtr->MaxVal;
        }
666 667 668
        if(oldVal != infoPtr->CurVal)
	{
	    TRACE("PBM_STEPIT: current pos changed from %d to %d\n", oldVal, infoPtr->CurVal);
669
            PROGRESS_Invalidate( infoPtr, oldVal, infoPtr->CurVal );
670
            UpdateWindow( infoPtr->Self );
671 672 673
	}
        return oldVal;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
674

675
    case PBM_SETRANGE32:
676
        return PROGRESS_SetRange (infoPtr, (int)wParam, (int)lParam);
677

Alexandre Julliard's avatar
Alexandre Julliard committed
678
    case PBM_GETRANGE:
679 680 681 682 683
        if (lParam) {
            ((PPBRANGE)lParam)->iLow = infoPtr->MinVal;
            ((PPBRANGE)lParam)->iHigh = infoPtr->MaxVal;
        }
        return wParam ? infoPtr->MinVal : infoPtr->MaxVal;
Alexandre Julliard's avatar
Alexandre Julliard committed
684 685

    case PBM_GETPOS:
686
        return infoPtr->CurVal;
Alexandre Julliard's avatar
Alexandre Julliard committed
687 688

    case PBM_SETBARCOLOR:
689 690 691
    {
        COLORREF clr = infoPtr->ColorBar;

692 693
        infoPtr->ColorBar = (COLORREF)lParam;
	InvalidateRect(hwnd, NULL, TRUE);
694 695
        return clr;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
696

697 698 699
    case PBM_GETBARCOLOR:
	return infoPtr->ColorBar;

Alexandre Julliard's avatar
Alexandre Julliard committed
700
    case PBM_SETBKCOLOR:
701 702 703
    {
        COLORREF clr = infoPtr->ColorBk;

704
        infoPtr->ColorBk = (COLORREF)lParam;
705
	InvalidateRect(hwnd, NULL, TRUE);
706 707
        return clr;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
708

709 710 711
    case PBM_GETBKCOLOR:
	return infoPtr->ColorBk;

712 713
    case PBM_SETSTATE:
        if(wParam != PBST_NORMAL)
714
            FIXME("state %04lx not yet handled\n", wParam);
715 716 717 718 719
        return PBST_NORMAL;

    case PBM_GETSTATE:
        return PBST_NORMAL;

720 721 722
    case PBM_SETMARQUEE:
	if(wParam != 0)
        {
723
            UINT period = lParam ? (UINT)lParam : DEFAULT_MARQUEE_PERIOD;
724
            infoPtr->Marquee = TRUE;
725
            SetTimer(infoPtr->Self, ID_MARQUEE_TIMER, period, NULL);
726 727 728 729 730 731 732 733
        }
        else
        {
            infoPtr->Marquee = FALSE;
            KillTimer(infoPtr->Self, ID_MARQUEE_TIMER);
        }
	return infoPtr->Marquee;

734
    default:
735
        if ((message >= WM_USER) && (message < WM_APP) && !COMCTL32_IsReflectedMessage(message))
736
	    ERR("unknown msg %04x wp=%04lx lp=%08lx\n", message, wParam, lParam );
737 738
        return DefWindowProcW( hwnd, message, wParam, lParam );
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
739 740
}

Alexandre Julliard's avatar
Alexandre Julliard committed
741 742

/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
743
 * PROGRESS_Register [Internal]
Alexandre Julliard's avatar
Alexandre Julliard committed
744 745 746
 *
 * Registers the progress bar window class.
 */
Dimitrie O. Paun's avatar
Dimitrie O. Paun committed
747
void PROGRESS_Register (void)
Alexandre Julliard's avatar
Alexandre Julliard committed
748
{
749
    WNDCLASSW wndClass;
Alexandre Julliard's avatar
Alexandre Julliard committed
750

751
    ZeroMemory (&wndClass, sizeof(wndClass));
Alexandre Julliard's avatar
Alexandre Julliard committed
752
    wndClass.style         = CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
753
    wndClass.lpfnWndProc   = ProgressWindowProc;
Alexandre Julliard's avatar
Alexandre Julliard committed
754
    wndClass.cbClsExtra    = 0;
755
    wndClass.cbWndExtra    = sizeof (PROGRESS_INFO *);
756
    wndClass.hCursor       = LoadCursorW (0, (LPWSTR)IDC_ARROW);
757
    wndClass.lpszClassName = PROGRESS_CLASSW;
Alexandre Julliard's avatar
Alexandre Julliard committed
758

759
    RegisterClassW (&wndClass);
760 761 762 763 764 765 766 767
}


/***********************************************************************
 * PROGRESS_Unregister [Internal]
 *
 * Unregisters the progress bar window class.
 */
Dimitrie O. Paun's avatar
Dimitrie O. Paun committed
768
void PROGRESS_Unregister (void)
769
{
770
    UnregisterClassW (PROGRESS_CLASSW, NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
771
}