Debian Codegraph @

Contrib/BgImage/BgImage.cpp

raw
1 // Unicode support by Jim Park -- 08/22/2007
2
3 #include <windows.h>
4 #include <mmsystem.h>
5 #include <nsis/pluginapi.h> // nsis plugin
6
7 #undef EXDLL_INIT
8
9 #define EXDLL_INIT() { \
10 g_stringsize=string_size; \
11 g_stacktop=stacktop; }
12
13 #define NSISFunc(name) extern "C" void __declspec(dllexport) name(HWND hwndParent, int string_size, TCHAR *variables, stack_t **stacktop, extra_parameters *extra)
14
15 TCHAR szTemp[2048];
16 HWND hWndImage, hWndParent;
17
18 HINSTANCE g_hInstance;
19
20 CRITICAL_SECTION CriticalSection;
21
22 void ECS() {
23 EnterCriticalSection(&CriticalSection);
24 }
25
26 void LCS() {
27 LeaveCriticalSection(&CriticalSection);
28 }
29
30 enum {
31 MIL_DUMMY,
32 MIL_GRADIENT,
33 MIL_BITMAP,
34 MIL_TRANSPARENT_BITMAP,
35 MIL_TEXT
36 };
37
38 struct myImageList {
39 BYTE iType;
40 union {
41 HBITMAP hBitmap;
42 TCHAR *szText;
43 COLORREF cGradientFrom;
44 };
45 RECT rPos;
46 union {
47 COLORREF cTransparent;
48 COLORREF cTextColor;
49 COLORREF cGradientTo;
50 };
51 HFONT hFont;
52
53 BOOL bReady;
54
55 myImageList *next;
56 } bgBitmap;
57
58 unsigned int uWndWidth, uWndHeight;
59
60 WNDPROC oldProc;
61 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
62 HBITMAP __stdcall LoadBitmapFile(long right, long bottom, BITMAP *bBitmap);
63 COLORREF GetColor();
64 void __stdcall GetXY(LPPOINT lpPoint);
65
66 NSISFunc(Destroy);
67
68 static UINT_PTR PluginCallback(enum NSPIM msg)
69 {
70 if (msg == NSPIM_GUIUNLOAD)
71 {
72 Destroy(0, 0, 0, 0, 0);
73 }
74 return 0;
75 }
76
77 BOOL bReturn;
78
79 NSISFunc(SetReturn) {
80 EXDLL_INIT();
81
82 extra->RegisterPluginCallback(g_hInstance, PluginCallback);
83
84 popstring(szTemp);
85 bReturn = !lstrcmpi(szTemp, _T("on"));
86 }
87
88 static void __stdcall my_pushstring(const TCHAR *str)
89 {
90 stack_t *th;
91 if (!g_stacktop || !bReturn) return;
92 th=(stack_t*)GlobalAlloc(GPTR,sizeof(stack_t)+(g_stringsize*sizeof(TCHAR)));
93 lstrcpyn(th->text,str,g_stringsize);
94 th->next=*g_stacktop;
95 *g_stacktop=th;
96 }
97
98 NSISFunc(SetBg) {
99 EXDLL_INIT();
100
101 extra->RegisterPluginCallback(g_hInstance, PluginCallback);
102
103 ECS();
104
105 if (!hWndImage) {
106 hWndParent = hwndParent;
107
108 if (!hwndParent) {
109 my_pushstring(_T("can't find parent window"));
110 LCS();
111 return;
112 }
113
114 WNDCLASS wc = {
115 CS_VREDRAW|CS_HREDRAW,
116 WndProc,
117 0,
118 0,
119 g_hInstance,
120 0,
121 LoadCursor(0, IDC_ARROW),
122 0,
123 0,
124 _T("NSISBGImage"),
125 };
126 ATOM atomClass = RegisterClass(&wc);
127 if (!atomClass) {
128 my_pushstring(_T("can't create window"));
129 return;
130 }
131
132 hWndImage = CreateWindowEx(
133 WS_EX_TOOLWINDOW,
134 (LPTSTR)(UINT_PTR)atomClass,
135 0,
136 WS_CLIPSIBLINGS|WS_POPUP,
137 0,
138 0,
139 0,
140 0,
141 0,
142 0,
143 g_hInstance,
144 0
145 );
146 if (!hWndImage) {
147 my_pushstring(_T("can't create window"));
148 LCS();
149 return;
150 }
151
152 oldProc = (WNDPROC)SetWindowLongPtr(hwndParent, GWLP_WNDPROC, (LONG_PTR)WndProc);
153 }
154
155 bgBitmap.bReady = FALSE;
156
157 if (bgBitmap.iType == MIL_BITMAP)
158 DeleteObject(bgBitmap.hBitmap);
159
160 unsigned int uScrWidth = GetSystemMetrics(SM_CXSCREEN);
161 unsigned int uScrHeight = GetSystemMetrics(SM_CYSCREEN);
162
163 bgBitmap.iType = MIL_BITMAP;
164 bgBitmap.rPos.right = 0;
165 bgBitmap.rPos.bottom = 0;
166 uWndWidth = uScrWidth;
167 uWndHeight = uScrHeight;
168
169 LPCTSTR szGradient = _T("/GRADIENT");
170 LPCTSTR szFillScreen = _T("/FILLSCREEN");
171 LPCTSTR szTiled = _T("/TILED");
172
173 popstring(szTemp);
174 if (!lstrcmpi(szTemp, szGradient)) {
175 bgBitmap.cGradientFrom = GetColor();
176 bgBitmap.cGradientTo = GetColor();
177
178 bgBitmap.iType = MIL_GRADIENT;
179
180 goto done;
181 }
182 if (!lstrcmpi(szTemp, szFillScreen)) {
183 bgBitmap.rPos.right = uScrWidth;
184 bgBitmap.rPos.bottom = uScrHeight;
185 popstring(szTemp);
186 }
187 else if (!lstrcmpi(szTemp, szTiled)) {
188 popstring(szTemp);
189 }
190 else {
191 uWndWidth = 0;
192 uWndHeight = 0;
193 }
194
195 BITMAP bBitmap;
196
197 bgBitmap.hBitmap = LoadBitmapFile(bgBitmap.rPos.right, bgBitmap.rPos.bottom, &bBitmap);
198 if (!bgBitmap.hBitmap)
199 return;
200
201 if (!bgBitmap.rPos.right) {
202 bgBitmap.rPos.right = bBitmap.bmWidth;
203 bgBitmap.rPos.bottom = bBitmap.bmHeight;
204 }
205 if (!uWndWidth) {
206 uWndWidth = bBitmap.bmWidth;
207 uWndHeight = bBitmap.bmHeight;
208 }
209
210 done:
211
212 bgBitmap.bReady = TRUE;
213
214 LCS();
215
216 if (hWndImage) {
217 SetWindowPos(
218 hWndImage,
219 hWndParent,
220 (uScrWidth-uWndWidth)/2,
221 (uScrHeight-uWndHeight)/2,
222 uWndWidth,
223 uWndHeight,
224 SWP_NOACTIVATE
225 );
226 }
227
228 my_pushstring(_T("success"));
229 }
230
231 NSISFunc(AddImage) {
232 ECS();
233
234 myImageList *newImg = (myImageList *)GlobalAlloc(GPTR, sizeof(myImageList));
235 if (!newImg) {
236 my_pushstring(_T("memory allocation error"));
237 LCS();
238 return;
239 }
240
241 newImg->iType = MIL_BITMAP;
242 newImg->cTransparent = (COLORREF)-1;
243
244 popstring(szTemp);
245 if (!lstrcmpi(szTemp, _T("/TRANSPARENT"))) {
246 newImg->iType = MIL_TRANSPARENT_BITMAP;
247 newImg->cTransparent = GetColor();
248 popstring(szTemp);
249 }
250
251 BITMAP bBitmap;
252
253 newImg->hBitmap = LoadBitmapFile(0, 0, &bBitmap);
254 if (!newImg->hBitmap) {
255 GlobalFree(newImg);
256 return;
257 }
258
259 GetXY(LPPOINT(&newImg->rPos));
260
261 newImg->rPos.right = newImg->rPos.left + bBitmap.bmWidth;
262 newImg->rPos.bottom = newImg->rPos.top + bBitmap.bmHeight;
263
264 myImageList *img = &bgBitmap;
265 while (img->next) img = img->next;
266 img->next = newImg;
267
268 my_pushstring(_T("success"));
269
270 LCS();
271 }
272
273 NSISFunc(AddText) {
274 ECS();
275
276 myImageList *newImg = (myImageList *)GlobalAlloc(GPTR, sizeof(myImageList));
277 if (!newImg) {
278 my_pushstring(_T("memory allocation error"));
279 LCS();
280 return;
281 }
282
283 newImg->iType = MIL_TEXT;
284
285 popstring(szTemp);
286 newImg->szText = (TCHAR *)GlobalAlloc(GPTR, (lstrlen(szTemp)+1)*sizeof(TCHAR));
287 if (!newImg->szText) {
288 my_pushstring(_T("memory allocation error"));
289 GlobalFree(newImg);
290 LCS();
291 return;
292 }
293 lstrcpy(newImg->szText, szTemp);
294
295 popstring(szTemp);
296 newImg->hFont = (HFONT) nsishelper_str_to_ptr(szTemp);
297 newImg->cTextColor = GetColor();
298
299 GetXY(LPPOINT(&newImg->rPos));
300 GetXY(LPPOINT(&newImg->rPos) + 1);
301
302 myImageList *img = &bgBitmap;
303 while (img->next) img = img->next;
304 img->next = newImg;
305
306 my_pushstring(_T("success"));
307
308 LCS();
309 }
310
311 NSISFunc(Redraw) {
312 RedrawWindow(hWndImage, 0, 0, RDW_INVALIDATE | RDW_UPDATENOW);
313 ShowWindow(hWndImage, SW_SHOWNA);
314 }
315
316 NSISFunc(Clear) {
317 ECS();
318
319 myImageList *img = &bgBitmap;
320 while (img) {
321 switch (img->iType) {
322 case MIL_BITMAP:
323 case MIL_TRANSPARENT_BITMAP:
324 DeleteObject(img->hBitmap);
325 break;
326 case MIL_TEXT:
327 GlobalFree(img->szText);
328 break;
329 }
330
331 myImageList *thisImg = img;
332
333 img = img->next;
334 thisImg->next = NULL;
335
336 if (thisImg != &bgBitmap)
337 GlobalFree(thisImg);
338 }
339
340 bgBitmap.bReady = FALSE;
341
342 LCS();
343 }
344
345 NSISFunc(Destroy) {
346 bgBitmap.bReady = FALSE;
347 if (IsWindow(hwndParent) && oldProc)
348 SetWindowLongPtr(hwndParent, GWLP_WNDPROC, (LONG_PTR)oldProc);
349 if (IsWindow(hWndImage))
350 SendMessage(hWndImage, WM_CLOSE, 0, 0);
351 hWndImage = 0;
352 oldProc = NULL;
353 Clear(0, 0, 0, 0, 0);
354 UnregisterClass(_T("NSISBGImage"), g_hInstance);
355 }
356
357 NSISFunc(Sound) {
358 LPCTSTR szLoop = _T("/LOOP");
359 LPCTSTR szWait = _T("/WAIT");
360 LPCTSTR szStop = _T("/STOP");
361
362 DWORD flags = SND_FILENAME | SND_NODEFAULT;
363
364 g_stacktop = stacktop;
365 popstring(szTemp);
366 if (lstrcmpi(szTemp, szWait))
367 flags |= SND_ASYNC;
368 else
369 popstring(szTemp);
370 if (!lstrcmpi(szTemp, szLoop)) {
371 flags |= SND_LOOP;
372 popstring(szTemp);
373 }
374 PlaySound(lstrcmpi(szTemp, szStop) ? szTemp : 0, 0, flags);
375 }
376
377 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
378 HWND hwndParent = hWndParent;
379 HWND hwndImage = hWndImage;
380
381 if (hwnd == hwndParent) {
382 if (message == WM_SIZE) {
383 ShowWindow(hwndImage, wParam == SIZE_MINIMIZED ? SW_HIDE : SW_SHOW);
384 }
385 if (message == WM_WINDOWPOSCHANGED) {
386 SetWindowPos(hwndImage, hwndParent, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
387 }
388 return CallWindowProc(oldProc, hwnd, message, wParam, lParam);
389 }
390 switch (message) {
391 case WM_PAINT:
392 if (bgBitmap.bReady) {
393 ECS();
394
395 PAINTSTRUCT ps;
396 HDC hdc = BeginPaint(hwnd, &ps);
397
398 if (bgBitmap.iType == MIL_BITMAP) {
399 HDC cdc = CreateCompatibleDC(hdc);
400 SelectObject(cdc, bgBitmap.hBitmap);
401 for (unsigned int x = 0; x < uWndWidth; x += bgBitmap.rPos.right) {
402 for (unsigned int y = 0; y < uWndHeight; y += bgBitmap.rPos.bottom) {
403 BitBlt(hdc, x, y, bgBitmap.rPos.right, bgBitmap.rPos.bottom, cdc, 0, 0, SRCCOPY);
404 }
405 }
406 DeleteDC(cdc);
407 }
408 else {
409 int r = GetRValue(bgBitmap.cGradientFrom) << 10;
410 int g = GetGValue(bgBitmap.cGradientFrom) << 10;
411 int b = GetBValue(bgBitmap.cGradientFrom) << 10;
412 int dr = ((GetRValue(bgBitmap.cGradientTo) << 10) - r) / (int)uWndHeight * 4;
413 int dg = ((GetGValue(bgBitmap.cGradientTo) << 10) - g) / (int)uWndHeight * 4;
414 int db = ((GetBValue(bgBitmap.cGradientTo) << 10) - b) / (int)uWndHeight * 4;
415 RECT rect;
416 rect.left = 0;
417 rect.top = 0;
418 rect.right = uWndWidth;
419 rect.bottom = 4;
420 while (rect.top < (int)uWndHeight)
421 {
422 HBRUSH brush = CreateSolidBrush(RGB(r>>10,g>>10,b>>10));
423 FillRect(hdc, &rect, brush);
424 DeleteObject(brush);
425 rect.top+=4;
426 rect.bottom+=4;
427 r+=dr;
428 g+=dg;
429 b+=db;
430 }
431 }
432
433 myImageList *img = bgBitmap.next;
434 while (img) {
435 if (img->iType == MIL_TEXT) {
436 SetBkMode(hdc, TRANSPARENT);
437
438 SetTextColor(hdc, img->cTextColor);
439 SelectObject(hdc, img->hFont);
440 DrawText(hdc, img->szText, -1, &img->rPos, DT_TOP | DT_LEFT | DT_NOPREFIX | DT_WORDBREAK);
441 }
442 else if (img->iType == MIL_BITMAP) {
443 HDC cdc = CreateCompatibleDC(hdc);
444 SelectObject(cdc, img->hBitmap);
445 BitBlt(hdc, img->rPos.left, img->rPos.top, img->rPos.right - img->rPos.left, img->rPos.bottom - img->rPos.top, cdc, 0, 0, SRCCOPY);
446 DeleteDC(cdc);
447 }
448 else {
449 COLORREF cColor;
450 HBITMAP bmAndBack, bmAndObject, bmAndMem, bmSave;
451 HBITMAP bmBackOld, bmObjectOld, bmMemOld, bmSaveOld;
452 HDC hdcMem, hdcBack, hdcObject, hdcTemp, hdcSave;
453 POINT ptSize;
454
455 HBITMAP hBitmap = img->hBitmap;
456
457 hdcTemp = CreateCompatibleDC(hdc);
458 SelectObject(hdcTemp, hBitmap); // Select the bitmap
459
460 ptSize.x = img->rPos.right - img->rPos.left;
461 ptSize.y = img->rPos.bottom - img->rPos.top;
462 DPtoLP(hdcTemp, &ptSize, 1); // Convert from device to logical points
463
464 // Create some DCs to hold temporary data.
465 hdcBack = CreateCompatibleDC(hdc);
466 hdcObject = CreateCompatibleDC(hdc);
467 hdcMem = CreateCompatibleDC(hdc);
468 hdcSave = CreateCompatibleDC(hdc);
469
470 // Create a bitmap for each DC. DCs are required for a number of
471 // GDI functions.
472
473 // Monochrome DC
474 bmAndBack = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
475
476 // Monochrome DC
477 bmAndObject = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
478
479 bmAndMem = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
480 bmSave = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
481
482 // Each DC must select a bitmap object to store pixel data.
483 bmBackOld = (HBITMAP)SelectObject(hdcBack, bmAndBack);
484 bmObjectOld = (HBITMAP)SelectObject(hdcObject, bmAndObject);
485 bmMemOld = (HBITMAP)SelectObject(hdcMem, bmAndMem);
486 bmSaveOld = (HBITMAP)SelectObject(hdcSave, bmSave);
487
488 // Set proper mapping mode.
489 SetMapMode(hdcTemp, GetMapMode(hdc));
490
491 // Save the bitmap sent here, because it will be overwritten.
492 BitBlt(hdcSave, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY);
493
494 // Set the background color of the source DC to the color.
495 // contained in the parts of the bitmap that should be transparent
496 cColor = SetBkColor(hdcTemp, img->cTransparent);
497
498 // Create the object mask for the bitmap by performing a BitBlt
499 // from the source bitmap to a monochrome bitmap.
500 BitBlt(hdcObject, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0,
501 SRCCOPY);
502
503 // Set the background color of the source DC back to the original
504 // color.
505 SetBkColor(hdcTemp, cColor);
506
507 // Create the inverse of the object mask.
508 BitBlt(hdcBack, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0,
509 NOTSRCCOPY);
510
511 // Copy the background of the main DC to the destination.
512 BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdc, img->rPos.left, img->rPos.top,
513 SRCCOPY);
514
515 // Mask out the places where the bitmap will be placed.
516 BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, SRCAND);
517
518 // Mask out the transparent colored pixels on the bitmap.
519 BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcBack, 0, 0, SRCAND);
520
521 // XOR the bitmap with the background on the destination DC.
522 BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCPAINT);
523
524 // Copy the destination to the screen.
525 BitBlt(hdc, img->rPos.left, img->rPos.top, ptSize.x, ptSize.y, hdcMem, 0, 0,
526 SRCCOPY);
527
528 // Place the original bitmap back into the bitmap sent here.
529 BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcSave, 0, 0, SRCCOPY);
530
531 // Delete the memory bitmaps.
532 DeleteObject(SelectObject(hdcBack, bmBackOld));
533 DeleteObject(SelectObject(hdcObject, bmObjectOld));
534 DeleteObject(SelectObject(hdcMem, bmMemOld));
535 DeleteObject(SelectObject(hdcSave, bmSaveOld));
536
537 // Delete the memory DCs.
538 DeleteDC(hdcMem);
539 DeleteDC(hdcBack);
540 DeleteDC(hdcObject);
541 DeleteDC(hdcSave);
542 DeleteDC(hdcTemp);
543 }
544 img = img->next;
545 }
546
547 LCS();
548
549 EndPaint(hwnd, &ps);
550 }
551 break;
552 case WM_WINDOWPOSCHANGING:
553 if (IsWindow(hwndParent))
554 {
555 LPWINDOWPOS wp = (LPWINDOWPOS) lParam;
556 wp->flags |= SWP_NOACTIVATE;
557 wp->hwndInsertAfter = hwndParent;
558 }
559 break;
560 case WM_CLOSE:
561 DestroyWindow(hwnd);
562 break;
563 default:
564 return DefWindowProc(hwnd, message, wParam, lParam);
565 }
566 return 0;
567 }
568
569 HBITMAP __stdcall LoadBitmapFile(long right, long bottom, BITMAP *bBitmap)
570 {
571 HBITMAP hBitmap = (HBITMAP)LoadImage(0, szTemp, IMAGE_BITMAP, right, bottom, LR_LOADFROMFILE);
572 if (!hBitmap || !GetObject(hBitmap, sizeof(BITMAP), (void *)bBitmap)) {
573 my_pushstring(_T("can't load bitmap"));
574 if (hBitmap)
575 DeleteObject(hBitmap);
576 LCS();
577 return 0;
578 }
579 return hBitmap;
580 }
581
582 COLORREF GetColor() {
583 COLORREF cColor = 0;
584 popstring(szTemp);
585 cColor |= (BYTE) myatoi(szTemp);
586 popstring(szTemp);
587 cColor |= ((BYTE) myatoi(szTemp)) << 8;
588 popstring(szTemp);
589 cColor |= ((BYTE) myatoi(szTemp)) << 16;
590 return cColor;
591 }
592
593 void __stdcall GetXY(LPPOINT lpPoint) {
594 popstring(szTemp);
595 int iPosTemp = myatoi(szTemp);
596 if (iPosTemp < 0) iPosTemp = iPosTemp + (int)uWndWidth;
597 lpPoint->x = (unsigned int)iPosTemp;
598
599 popstring(szTemp);
600 iPosTemp = myatoi(szTemp);
601 if (iPosTemp < 0) iPosTemp = iPosTemp + (int)uWndHeight;
602 lpPoint->y = (unsigned int)iPosTemp;
603 }
604
605 extern "C" BOOL WINAPI DllMain(HINSTANCE hInst, ULONG ul_reason_for_call, LPVOID lpReserved) {
606 g_hInstance=hInst;
607 switch (ul_reason_for_call) {
608 case DLL_PROCESS_ATTACH:
609 InitializeCriticalSection(&CriticalSection);
610 break;
611 case DLL_PROCESS_DETACH:
612 DeleteCriticalSection(&CriticalSection);
613 break;
614 }
615 return TRUE;
616 }