- BgImage improved a lot

- Added .onGUIEnd
- Some optimizations


git-svn-id: https://svn.code.sf.net/p/nsis/code/NSIS/trunk@2542 212acab6-be3b-0410-9dea-997c60f758d6
This commit is contained in:
kichik 2003-05-09 21:11:14 +00:00
parent 65806e3b4b
commit 26845a8b74
12 changed files with 724 additions and 193 deletions

View file

@ -2,128 +2,539 @@
#include <Mmsystem.h>
#include "../exdll/exdll.h"
int x, y;
char temp[MAX_PATH];
HBITMAP hBitmap;
#define NSISFunc(name) extern "C" void __declspec(dllexport) name(HWND hwndParent, int string_size, char *variables, stack_t **stacktop)
char szTemp[2048];
HWND hWndImage, hWndParent;
HINSTANCE g_hInstance;
enum {
MIL_DUMMY,
MIL_GRADIENT,
MIL_BITMAP,
MIL_TRANSPARENT_BITMAP,
MIL_TEXT
};
struct myImageList {
BYTE iType;
union {
HBITMAP hBitmap;
char *szText;
COLORREF cGradientFrom;
};
RECT rPos;
union {
COLORREF cTransparent;
COLORREF cTextColor;
COLORREF cGradientTo;
};
HFONT hFont;
BOOL bReady;
myImageList *next;
} bgBitmap;
unsigned int uWndWidth, uWndHeight;
void *oldProc;
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
int myatoi(char *s);
extern "C" void __declspec(dllexport) SetImage(HWND hwndParent, int string_size, char *variables, stack_t **stacktop);
COLORREF GetColor();
extern "C" void __declspec(dllexport) Init(HWND hwndParent, int string_size, char *variables, stack_t **stacktop) {
hWndParent = hwndParent;
BOOL bReturn;
if (!hwndParent) {
pushstring("can't find parent window");
return;
}
WNDCLASSEX wc = {
sizeof(WNDCLASSEX),
CS_VREDRAW|CS_HREDRAW,
WndProc,
0,
0,
g_hInstance,
0,//LoadIcon(GetModuleHandle(0), MAKEINTRESOURCE(103)),
0,
(HBRUSH)GetStockObject(WHITE_BRUSH),
0,
"NSISBGImage",
0
};
if (!RegisterClassEx(&wc)) {
pushstring("can't register class");
return;
}
hWndImage = CreateWindowEx(
WS_EX_TOOLWINDOW,
"NSISBGImage",
0,
WS_CLIPSIBLINGS|WS_POPUP,
0,
0,
0,
0,
0,
0,
g_hInstance,
0
);
if (!hWndImage) {
pushstring("can't create window");
return;
}
ShowWindow(hWndImage, SW_SHOW);
SetImage(hwndParent, string_size, variables, stacktop);
oldProc = (void *)SetWindowLong(hwndParent, GWL_WNDPROC, (long)WndProc);
}
extern "C" void __declspec(dllexport) SetImage(HWND hwndParent, int string_size, char *variables, stack_t **stacktop) {
NSISFunc(SetReturn) {
EXDLL_INIT();
popstring(temp);
if (!lstrcmp(temp, "/FILLSCREEN")) {
x = GetSystemMetrics(SM_CXSCREEN);
y = GetSystemMetrics(SM_CYSCREEN);
popstring(temp);
popstring(szTemp);
bReturn = !lstrcmpi(szTemp, "on");
}
static void my_pushstring(char *str)
{
stack_t *th;
if (!g_stacktop || !bReturn) return;
th=(stack_t*)GlobalAlloc(GPTR,sizeof(stack_t)+g_stringsize);
lstrcpyn(th->text,str,g_stringsize);
th->next=*g_stacktop;
*g_stacktop=th;
}
NSISFunc(SetBg) {
EXDLL_INIT();
bgBitmap.bReady = FALSE;
if (!hWndImage) {
hWndParent = hwndParent;
if (!hwndParent) {
my_pushstring("can't find parent window");
return;
}
WNDCLASSEX wc = {
sizeof(WNDCLASSEX),
CS_VREDRAW|CS_HREDRAW,
WndProc,
0,
0,
g_hInstance,
0,
LoadCursor(0, IDC_ARROW),
0,
0,
"NSISBGImage",
0
};
if (!RegisterClassEx(&wc)) {
my_pushstring("can't register class");
return;
}
hWndImage = CreateWindowEx(
WS_EX_TOOLWINDOW,
"NSISBGImage",
0,
WS_CLIPSIBLINGS|WS_POPUP,
0,
0,
0,
0,
0,
0,
g_hInstance,
0
);
if (!hWndImage) {
my_pushstring("can't create window");
return;
}
oldProc = (void *)SetWindowLong(hwndParent, GWL_WNDPROC, (long)WndProc);
}
else x = y = 0;
BITMAP bitmap;
if (bgBitmap.iType == MIL_BITMAP) DeleteObject(bgBitmap.hBitmap);
if (hBitmap) DeleteObject((HGDIOBJ)hBitmap);
hBitmap = (HBITMAP)LoadImage(0, temp, IMAGE_BITMAP, x, y, LR_LOADFROMFILE);
if (!hBitmap) {
pushstring("can't load bitmap");
unsigned int uScrWidth = GetSystemMetrics(SM_CXSCREEN);
unsigned int uScrHeight = GetSystemMetrics(SM_CYSCREEN);
bgBitmap.iType = MIL_BITMAP;
bgBitmap.rPos.right = 0;
bgBitmap.rPos.bottom = 0;
uWndWidth = uScrWidth;
uWndHeight = uScrHeight;
popstring(szTemp);
if (!lstrcmpi(szTemp, "/GRADIENT")) {
bgBitmap.cGradientFrom = GetColor();
bgBitmap.cGradientTo = GetColor();
bgBitmap.iType = MIL_GRADIENT;
goto done;
}
if (!lstrcmpi(szTemp, "/FILLSCREEN")) {
bgBitmap.rPos.right = uScrWidth;
bgBitmap.rPos.bottom = uScrHeight;
popstring(szTemp);
}
else if (!lstrcmpi(szTemp, "/TILED")) {
popstring(szTemp);
}
else {
uWndWidth = 0;
uWndHeight = 0;
}
BITMAP bBitmap;
bgBitmap.hBitmap = (HBITMAP)LoadImage(0, szTemp, IMAGE_BITMAP, bgBitmap.rPos.right, bgBitmap.rPos.bottom, LR_LOADFROMFILE);
if (!bgBitmap.hBitmap) {
my_pushstring("can't load bitmap");
return;
}
GetObject(hBitmap, sizeof(bitmap), (LPSTR)&bitmap);
x = x ? x : bitmap.bmWidth;
y = y ? y : bitmap.bmHeight;
if (!GetObject(bgBitmap.hBitmap, sizeof(bBitmap), (void *)&bBitmap)) {
my_pushstring("can't query bitmap size");
return;
}
if (!bgBitmap.rPos.right) {
bgBitmap.rPos.right = bBitmap.bmWidth;
bgBitmap.rPos.bottom = bBitmap.bmHeight;
}
if (!uWndWidth) {
uWndWidth = bBitmap.bmWidth;
uWndHeight = bBitmap.bmHeight;
}
done:
bgBitmap.bReady = TRUE;
if (hWndImage) {
SetWindowPos(
hWndImage,
hWndParent,
(GetSystemMetrics(SM_CXSCREEN)-x)/2,
(GetSystemMetrics(SM_CYSCREEN)-y)/2,
x,
y,
(uScrWidth-uWndWidth)/2,
(uScrHeight-uWndHeight)/2,
uWndWidth,
uWndHeight,
SWP_NOACTIVATE
);
RedrawWindow(hWndImage, 0, 0, RDW_INVALIDATE | RDW_UPDATENOW);
}
my_pushstring("success");
}
extern "C" void __declspec(dllexport) Destroy(HWND hwndParent, int string_size, char *variables, stack_t **stacktop) {
NSISFunc(AddImage) {
myImageList *newImg = (myImageList *)GlobalAlloc(GPTR, sizeof(myImageList));
if (!newImg) {
my_pushstring("memory allocation error");
return;
}
newImg->iType = MIL_BITMAP;
newImg->cTransparent = -1;
popstring(szTemp);
if (!lstrcmpi(szTemp, "/TRANSPARENT")) {
newImg->iType = MIL_TRANSPARENT_BITMAP;
newImg->cTransparent = GetColor();
popstring(szTemp);
}
newImg->hBitmap = (HBITMAP)LoadImage(0, szTemp, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if (!newImg->hBitmap) {
my_pushstring("can't load bitmap");
return;
}
popstring(szTemp);
int iPosTemp = myatoi(szTemp);
if (iPosTemp < 0) iPosTemp = iPosTemp + (int)uWndWidth;
newImg->rPos.left = newImg->rPos.right = (unsigned int)iPosTemp;
popstring(szTemp);
iPosTemp = myatoi(szTemp);
if (iPosTemp < 0) iPosTemp = iPosTemp + (int)uWndHeight;
newImg->rPos.top = newImg->rPos.bottom = (unsigned int)iPosTemp;
BITMAP bBitmap;
if (!GetObject(newImg->hBitmap, sizeof(bBitmap), (void *)&bBitmap)) {
my_pushstring("can't query bitmap size");
return;
}
newImg->rPos.right += bBitmap.bmWidth;
newImg->rPos.bottom += bBitmap.bmHeight;
newImg->bReady = TRUE;
myImageList *img = &bgBitmap;
while (img->next) img = img->next;
img->next = newImg;
my_pushstring("success");
}
NSISFunc(AddText) {
myImageList *newImg = (myImageList *)GlobalAlloc(GPTR, sizeof(myImageList));
if (!newImg) {
my_pushstring("memory allocation error");
return;
}
newImg->iType = MIL_TEXT;
popstring(szTemp);
newImg->szText = (char *)GlobalAlloc(GPTR, lstrlen(szTemp)+1);
if (!newImg->szText) {
my_pushstring("memory allocation error");
return;
}
lstrcpy(newImg->szText, szTemp);
popstring(szTemp);
newImg->hFont = (HFONT)myatoi(szTemp);
newImg->cTextColor = GetColor();
popstring(szTemp);
int iPosTemp = myatoi(szTemp);
if (iPosTemp < 0) iPosTemp = iPosTemp + (int)uWndWidth;
newImg->rPos.left = (unsigned int)iPosTemp;
popstring(szTemp);
iPosTemp = myatoi(szTemp);
if (iPosTemp < 0) iPosTemp = iPosTemp + (int)uWndHeight;
newImg->rPos.top = (unsigned int)iPosTemp;
popstring(szTemp);
iPosTemp = myatoi(szTemp);
if (iPosTemp < 0) iPosTemp = iPosTemp + (int)uWndWidth;
newImg->rPos.right = (unsigned int)iPosTemp;
popstring(szTemp);
iPosTemp = myatoi(szTemp);
if (iPosTemp < 0) iPosTemp = iPosTemp + (int)uWndHeight;
newImg->rPos.bottom = (unsigned int)iPosTemp;
newImg->bReady = TRUE;
myImageList *img = &bgBitmap;
while (img->next) img = img->next;
img->next = newImg;
my_pushstring("success");
}
NSISFunc(Redraw) {
RedrawWindow(hWndImage, 0, 0, RDW_INVALIDATE | RDW_UPDATENOW);
ShowWindow(hWndImage, SW_SHOWNA);
}
NSISFunc(Clear) {
bgBitmap.bReady = FALSE;
myImageList *img = &bgBitmap;
while (img) {
switch (img->iType) {
case MIL_BITMAP:
case MIL_TRANSPARENT_BITMAP:
DeleteObject(img->hBitmap);
break;
case MIL_TEXT:
GlobalFree(img->szText);
break;
}
img = img->next;
if (img) GlobalFree(img);
}
bgBitmap.next = 0;
}
NSISFunc(Destroy) {
SetWindowLong(hWndParent, GWL_WNDPROC, (long)oldProc);
SendMessage(hWndImage, WM_CLOSE, 0, 0);
hWndImage = 0;
Clear(0, 0, 0, 0);
UnregisterClass("NSISBGImage", g_hInstance);
}
extern "C" void __declspec(dllexport) Sound(HWND hwndParent, int string_size, char *variables, stack_t **stacktop) {
DWORD flags = SND_FILENAME|SND_NODEFAULT;
NSISFunc(Sound) {
DWORD flags = SND_FILENAME|SND_NODEFAULT|SND_NOWAIT;
g_stacktop=stacktop;
popstring(temp);
if (lstrcmp(temp, "/WAIT"))
popstring(szTemp);
if (lstrcmpi(szTemp, "/WAIT"))
flags |= SND_ASYNC;
else
popstring(temp);
PlaySound(temp, 0, flags);
popstring(szTemp);
PlaySound(szTemp, 0, flags);
}
BOOL WINAPI _DllMainCRTStartup(HINSTANCE hInst, ULONG ul_reason_for_call, LPVOID lpReserved) {
g_hInstance=hInst;
return TRUE;
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
if (hwnd == hWndParent) {
if (message == WM_SIZE) {
ShowWindow(hWndImage, wParam == SIZE_MINIMIZED ? SW_HIDE : SW_SHOW);
}
if (message == WM_WINDOWPOSCHANGED) {
SetWindowPos(hWndImage, hWndParent, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
}
return CallWindowProc(
(long (__stdcall *)(HWND,unsigned int,unsigned int,long))oldProc,
hwnd,
message,
wParam,
lParam
);
}
switch (message) {
case WM_PAINT:
if (bgBitmap.bReady) {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
if (bgBitmap.iType == MIL_BITMAP) {
HDC cdc = CreateCompatibleDC(hdc);
SelectObject(cdc, bgBitmap.hBitmap);
for (unsigned int x = 0; x < uWndWidth; x += bgBitmap.rPos.right) {
for (unsigned int y = 0; y < uWndHeight; y += bgBitmap.rPos.bottom) {
BitBlt(hdc, x, y, bgBitmap.rPos.right, bgBitmap.rPos.bottom, cdc, 0, 0, SRCCOPY);
}
}
DeleteDC(cdc);
}
else {
int r = GetRValue(bgBitmap.cGradientFrom) << 10;
int g = GetGValue(bgBitmap.cGradientFrom) << 10;
int b = GetBValue(bgBitmap.cGradientFrom) << 10;
int dr = ((GetRValue(bgBitmap.cGradientTo) << 10) - r) / (int)uWndHeight * 4;
int dg = ((GetGValue(bgBitmap.cGradientTo) << 10) - g) / (int)uWndHeight * 4;
int db = ((GetBValue(bgBitmap.cGradientTo) << 10) - b) / (int)uWndHeight * 4;
RECT rect;
rect.left = 0;
rect.top = 0;
rect.right = uWndWidth;
rect.bottom = 4;
while (rect.top < (int)uWndHeight)
{
HBRUSH brush = CreateSolidBrush(RGB(r>>10,g>>10,b>>10));
FillRect(hdc, &rect, brush);
DeleteObject(brush);
rect.top+=4;
rect.bottom+=4;
r+=dr;
g+=dg;
b+=db;
}
}
myImageList *img = bgBitmap.next;
while (img) {
if (!img->bReady) break;
if (img->iType == MIL_TEXT) {
SetBkMode(hdc, TRANSPARENT);
HFONT hOldFont;
SetTextColor(hdc, img->cTextColor);
hOldFont = (HFONT)SelectObject(hdc, img->hFont);
DrawText(hdc, img->szText, -1, &img->rPos, DT_TOP | DT_LEFT | DT_NOPREFIX | DT_WORDBREAK);
SelectObject(hdc, hOldFont);
}
else if (img->iType == MIL_BITMAP) {
HDC cdc = CreateCompatibleDC(hdc);
SelectObject(cdc, img->hBitmap);
BitBlt(hdc, img->rPos.left, img->rPos.top, img->rPos.right - img->rPos.left, img->rPos.bottom - img->rPos.top, cdc, 0, 0, SRCCOPY);
DeleteDC(cdc);
}
else {
COLORREF cColor;
HBITMAP bmAndBack, bmAndObject, bmAndMem, bmSave;
HBITMAP bmBackOld, bmObjectOld, bmMemOld, bmSaveOld;
HDC hdcMem, hdcBack, hdcObject, hdcTemp, hdcSave;
POINT ptSize;
HBITMAP hBitmap = img->hBitmap;
hdcTemp = CreateCompatibleDC(hdc);
SelectObject(hdcTemp, hBitmap); // Select the bitmap
ptSize.x = img->rPos.right - img->rPos.left;
ptSize.y = img->rPos.bottom - img->rPos.top;
DPtoLP(hdcTemp, &ptSize, 1); // Convert from device to logical points
// Create some DCs to hold temporary data.
hdcBack = CreateCompatibleDC(hdc);
hdcObject = CreateCompatibleDC(hdc);
hdcMem = CreateCompatibleDC(hdc);
hdcSave = CreateCompatibleDC(hdc);
// Create a bitmap for each DC. DCs are required for a number of
// GDI functions.
// Monochrome DC
bmAndBack = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
// Monochrome DC
bmAndObject = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
bmAndMem = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
bmSave = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
// Each DC must select a bitmap object to store pixel data.
bmBackOld = (HBITMAP)SelectObject(hdcBack, bmAndBack);
bmObjectOld = (HBITMAP)SelectObject(hdcObject, bmAndObject);
bmMemOld = (HBITMAP)SelectObject(hdcMem, bmAndMem);
bmSaveOld = (HBITMAP)SelectObject(hdcSave, bmSave);
// Set proper mapping mode.
SetMapMode(hdcTemp, GetMapMode(hdc));
// Save the bitmap sent here, because it will be overwritten.
BitBlt(hdcSave, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY);
// Set the background color of the source DC to the color.
// contained in the parts of the bitmap that should be transparent
cColor = SetBkColor(hdcTemp, img->cTransparent);
// Create the object mask for the bitmap by performing a BitBlt
// from the source bitmap to a monochrome bitmap.
BitBlt(hdcObject, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0,
SRCCOPY);
// Set the background color of the source DC back to the original
// color.
SetBkColor(hdcTemp, cColor);
// Create the inverse of the object mask.
BitBlt(hdcBack, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0,
NOTSRCCOPY);
// Copy the background of the main DC to the destination.
BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdc, img->rPos.left, img->rPos.top,
SRCCOPY);
// Mask out the places where the bitmap will be placed.
BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, SRCAND);
// Mask out the transparent colored pixels on the bitmap.
BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcBack, 0, 0, SRCAND);
// XOR the bitmap with the background on the destination DC.
BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCPAINT);
// Copy the destination to the screen.
BitBlt(hdc, img->rPos.left, img->rPos.top, ptSize.x, ptSize.y, hdcMem, 0, 0,
SRCCOPY);
// Place the original bitmap back into the bitmap sent here.
BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcSave, 0, 0, SRCCOPY);
// Delete the memory bitmaps.
DeleteObject(SelectObject(hdcBack, bmBackOld));
DeleteObject(SelectObject(hdcObject, bmObjectOld));
DeleteObject(SelectObject(hdcMem, bmMemOld));
DeleteObject(SelectObject(hdcSave, bmSaveOld));
// Delete the memory DCs.
DeleteDC(hdcMem);
DeleteDC(hdcBack);
DeleteDC(hdcObject);
DeleteDC(hdcSave);
DeleteDC(hdcTemp);
}
img = img->next;
}
EndPaint(hwnd, &ps);
}
break;
case WM_WINDOWPOSCHANGING:
{
LPWINDOWPOS wp = (LPWINDOWPOS) lParam;
wp->flags |= SWP_NOACTIVATE;
wp->hwndInsertAfter = hWndParent;
break;
}
case WM_CLOSE:
DestroyWindow(hWndImage);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
COLORREF GetColor() {
int iRed, iGreen, iBlue;
popstring(szTemp);
iRed = myatoi(szTemp);
popstring(szTemp);
iGreen = myatoi(szTemp);
popstring(szTemp);
iBlue = myatoi(szTemp);
return RGB(iRed, iGreen, iBlue);
}
int myatoi(char *s)
@ -171,48 +582,7 @@ int myatoi(char *s)
return (int)v;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
if (hWndImage && hwnd != hWndImage) {
if (message == WM_SIZE) {
ShowWindow(hWndImage, wParam == SIZE_MINIMIZED ? SW_HIDE : SW_SHOW);
}
if (message == WM_WINDOWPOSCHANGED) {
SetWindowPos(hWndImage, hWndParent, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
}
return CallWindowProc(
(long (__stdcall *)(HWND,unsigned int,unsigned int,long))oldProc,
hwnd,
message,
wParam,
lParam
);
}
switch (message) {
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
HDC cdc = CreateCompatibleDC(hdc);
HGDIOBJ hOldObject = SelectObject(cdc, hBitmap);
RECT cRect;
GetClientRect(hwnd, &cRect);
BitBlt(hdc, cRect.left, cRect.top, cRect.right - cRect.left, cRect.bottom - cRect.top, cdc, 0, 0, SRCCOPY);
SelectObject(cdc, hOldObject);
DeleteDC(cdc);
EndPaint(hwnd, &ps);
}
break;
case WM_WINDOWPOSCHANGING:
{
LPWINDOWPOS wp = (LPWINDOWPOS) lParam;
wp->flags |= SWP_NOACTIVATE;
wp->hwndInsertAfter = hWndParent;
break;
}
case WM_DESTROY:
SetWindowLong(hWndParent, GWL_WNDPROC, (long)oldProc);
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
BOOL WINAPI _DllMainCRTStartup(HINSTANCE hInst, ULONG ul_reason_for_call, LPVOID lpReserved) {
g_hInstance=hInst;
return TRUE;
}