📄 progress.c
字号:
/*
* Progress control
*
* Copyright 1997, 2002 Dimitrie O. Paun
* Copyright 1998, 1999 Eric Kohl
*
* 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
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
* 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.
*
*/
#include <stdarg.h>
#include <string.h>
#include "windef.h"
#include "winbase.h"
#include "wingdi.h"
#include "winuser.h"
#include "winnls.h"
#include "commctrl.h"
#include "comctl32.h"
#include "uxtheme.h"
#include "tmschema.h"
#include "wine/debug.h"
WINE_DEFAULT_DEBUG_CHANNEL(progress);
typedef struct
{
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 */
INT MarqueePos; /* Marquee animation position */
BOOL Marquee; /* Whether the marquee animation is enabled */
COLORREF ColorBar; /* Bar color */
COLORREF ColorBk; /* Background color */
HFONT Font; /* Handle to font (not unused) */
} PROGRESS_INFO;
/* Control configuration constants */
#define LED_GAP 2
#define MARQUEE_LEDS 5
#define ID_MARQUEE_TIMER 1
/* Helper to obtain size of a progress bar chunk ("led"). */
static inline int get_led_size ( PROGRESS_INFO *infoPtr, LONG style,
const RECT* rect )
{
HTHEME theme = GetWindowTheme (infoPtr->Self);
if (theme)
{
int chunkSize;
if (SUCCEEDED( GetThemeInt( theme, 0, 0, TMT_PROGRESSCHUNKSIZE, &chunkSize )))
return chunkSize;
}
if (style & PBS_VERTICAL)
return MulDiv (rect->right - rect->left, 2, 3);
else
return MulDiv (rect->bottom - rect->top, 2, 3);
}
/* Helper to obtain gap between progress bar chunks */
static inline int get_led_gap ( PROGRESS_INFO *infoPtr )
{
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)
{
HTHEME theme = GetWindowTheme (hwnd);
GetClientRect (hwnd, rect);
if (!theme)
InflateRect(rect, -1, -1);
else
{
DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
int part = (dwStyle & PBS_VERTICAL) ? PP_BARVERT : PP_BAR;
GetThemeBackgroundContentRect (theme, 0, part, 0, rect, rect);
}
}
/* 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 */
static inline int get_bar_position( PROGRESS_INFO *infoPtr, LONG style,
const RECT* rect, INT value )
{
return MulDiv (value - infoPtr->MinVal, get_bar_size (style, rect),
infoPtr->MaxVal - infoPtr->MinVal);
}
/***********************************************************************
* PROGRESS_Invalidate
*
* Don't be too clever about invalidating the progress bar.
* InstallShield depends on this simple behaviour.
*/
static void PROGRESS_Invalidate( PROGRESS_INFO *infoPtr, INT old, INT new )
{
InvalidateRect( infoPtr->Self, NULL, old > new );
}
/* Information for a progress bar drawing helper */
typedef struct tagProgressDrawInfo
{
HDC hdc;
RECT rect;
HBRUSH hbrBar;
HBRUSH hbrBk;
int ledW, ledGap;
HTHEME theme;
RECT bgRect;
} 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;
r.left = di->rect.left + start;
r.top = di->rect.top;
r.right = di->rect.left + end;
r.bottom = di->rect.bottom;
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;
r.left = di->rect.left + start;
r.top = di->rect.top;
r.right = di->rect.left + end;
r.bottom = di->rect.bottom;
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;
r.left = di->rect.left;
r.top = di->rect.bottom - end;
r.right = di->rect.right;
r.bottom = di->rect.bottom - start;
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;
r.left = di->rect.left;
r.top = di->rect.bottom - end;
r.right = di->rect.right;
r.bottom = di->rect.bottom - start;
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;
r.right = min (r.left + di->ledGap, right);
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;
r.top = max (r.bottom - di->ledGap, top);
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,
};
/* draw themed horizontal bar from 'start' to 'end' */
static void draw_theme_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);
DrawThemeBackground (di->theme, di->hdc, PP_CHUNK, 0, &r, NULL);
r.left = r.right;
r.right = min (r.left + di->ledGap, right);
DrawThemeBackground (di->theme, di->hdc, PP_BAR, 0, &di->bgRect, &r);
r.left = r.right;
}
}
/* draw themed horizontal bar from 'start' to 'end' */
static void draw_theme_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);
DrawThemeBackground (di->theme, di->hdc, PP_CHUNKVERT, 0, &r, NULL);
r.bottom = r.top;
r.top = max (r.bottom - di->ledGap, top);
DrawThemeBackground (di->theme, di->hdc, PP_BARVERT, 0, &di->bgRect, &r);
r.bottom = r.top;
}
}
/* draw themed horizontal background from 'start' to 'end' */
static void draw_theme_bkg_H (const ProgressDrawInfo* di, int start, int end)
{
RECT r;
r.left = di->rect.left + start;
r.top = di->rect.top;
r.right = di->rect.left + end;
r.bottom = di->rect.bottom;
DrawThemeBackground (di->theme, di->hdc, PP_BAR, 0, &di->bgRect, &r);
}
/* draw themed vertical background from 'start' to 'end' */
static void draw_theme_bkg_V (const ProgressDrawInfo* di, int start, int end)
{
RECT r;
r.left = di->rect.left;
r.top = di->rect.bottom - end;
r.right = di->rect.right;
r.bottom = di->rect.bottom - start;
DrawThemeBackground (di->theme, di->hdc, PP_BARVERT, 0, &di->bgRect, &r);
}
/* 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,
};
/***********************************************************************
* PROGRESS_Draw
* Draws the progress bar.
*/
static LRESULT PROGRESS_Draw (PROGRESS_INFO *infoPtr, HDC hdc)
{
int barSize;
DWORD dwStyle;
BOOL barSmooth;
const ProgressDrawProc* drawProcs;
ProgressDrawInfo pdi;
TRACE("(infoPtr=%p, hdc=%p)\n", infoPtr, hdc);
pdi.hdc = hdc;
pdi.theme = GetWindowTheme (infoPtr->Self);
/* get the required bar brush */
if (infoPtr->ColorBar == CLR_DEFAULT)
pdi.hbrBar = GetSysColorBrush(COLOR_HIGHLIGHT);
else
pdi.hbrBar = CreateSolidBrush (infoPtr->ColorBar);
if (infoPtr->ColorBk == CLR_DEFAULT)
pdi.hbrBk = GetSysColorBrush(COLOR_3DFACE);
else
pdi.hbrBk = CreateSolidBrush(infoPtr->ColorBk);
/* get the window style */
dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
/* get client rectangle */
GetClientRect (infoPtr->Self, &pdi.rect);
if (!pdi.theme) {
FrameRect( hdc, &pdi.rect, pdi.hbrBk );
InflateRect(&pdi.rect, -1, -1);
}
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);
CopyRect (&pdi.rect, &cntRect);
}
/* compute some drawing parameters */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -