1 static char RCSId
[] = "$Id: resource.c,v 1.4 1993/07/04 04:04:21 root Exp root $";
2 static char Copyright
[] = "Copyright Robert J. Amstadt, 1993";
12 #include "prototypes.h"
22 /* #define DEBUG_RESOURCE /* */
23 /* #undef DEBUG_RESOURCE /* */
24 /* #define DEBUG_ACCEL /* */
25 /* #undef DEBUG_ACCEL /* */
28 #define MIN(a,b) ((a) < (b) ? (a) : (b))
31 extern HINSTANCE hSysRes
;
33 extern int NE_FindResource(HANDLE
, LPSTR
, LPSTR
, RESOURCE
*);
34 extern int PE_FindResource(HANDLE
, LPSTR
, LPSTR
, RESOURCE
*);
35 extern HBITMAP
BITMAP_LoadOEMBitmap( WORD id
); /* objects/bitmap.c */
37 #define PrintId(name) \
38 if (HIWORD((DWORD)name)) \
39 printf(", %s", name); \
41 printf(", #%d", (int) name);
43 /**********************************************************************
44 * FindResource [KERNEL.60]
46 HANDLE
FindResource(HANDLE instance
, LPSTR name
, LPSTR type
)
53 printf("FindResource(%04X", instance
);
59 if (instance
== (HANDLE
)NULL
)
62 /* FIXME: did we already find this one ? */
64 if ((rh
= GlobalAlloc(GMEM_MOVEABLE
, sizeof(RESOURCE
))) == 0)
67 r
= (RESOURCE
*)GlobalLock(rh
);
73 if (HIWORD((DWORD
)name
))
74 r
->name
= strdup(name
);
78 if (HIWORD((DWORD
)type
))
79 r
->type
= strdup(type
);
83 r
->wpnt
= GetFileInfo(instance
);
84 r
->fd
= dup(r
->wpnt
->fd
);
86 status
= NE_FindResource(instance
, name
, type
, r
);
88 status
= PE_FindResource(instance
, name
, type
, r
);
91 if (HIWORD((DWORD
)r
->name
))
94 if (HIWORD((DWORD
)r
->type
))
105 /**********************************************************************
106 * AllocResource [KERNEL.66]
108 HANDLE
AllocResource(HANDLE instance
, HANDLE hResInfo
, DWORD dwSize
)
113 #ifdef DEBUG_RESOURCE
114 printf("AllocResource(%04X, %04X, %08X);\n", instance
, hResInfo
, (int) dwSize
);
117 if (instance
== (HANDLE
)NULL
)
120 if ((r
= (RESOURCE
*)GlobalLock(hResInfo
)) == NULL
)
123 image_size
= r
->size
;
126 r
->rsc_mem
= GlobalAlloc(GMEM_MOVEABLE
, image_size
);
128 r
->rsc_mem
= GlobalAlloc(GMEM_MOVEABLE
, dwSize
);
130 GlobalUnlock(hResInfo
);
135 /**********************************************************************
136 * AccessResource [KERNEL.64]
138 int AccessResource(HANDLE instance
, HANDLE hResInfo
)
143 #ifdef DEBUG_RESOURCE
144 printf("AccessResource(%04X, %04X);\n", instance
, hResInfo
);
147 if (instance
== (HANDLE
)NULL
)
150 if ((r
= (RESOURCE
*)GlobalLock(hResInfo
)) == NULL
)
154 lseek(fd
, r
->offset
, SEEK_SET
);
155 GlobalUnlock(hResInfo
);
160 /**********************************************************************
161 * SizeofResource [KERNEL.65]
163 WORD
SizeofResource(HANDLE instance
, HANDLE hResInfo
)
168 #ifdef DEBUG_RESOURCE
169 printf("SizeofResource(%04X, %04X);\n", instance
, hResInfo
);
172 if (instance
== (HANDLE
)NULL
)
175 if ((r
= (RESOURCE
*)GlobalLock(hResInfo
)) == NULL
)
179 GlobalUnlock(hResInfo
);
184 /**********************************************************************
185 * LoadResource [KERNEL.61]
187 HANDLE
LoadResource(HANDLE instance
, HANDLE hResInfo
)
194 #ifdef DEBUG_RESOURCE
195 printf("LoadResource(%04X, %04X);\n", instance
, hResInfo
);
198 if (instance
== (HANDLE
)NULL
)
201 if ((r
= (RESOURCE
*)GlobalLock(hResInfo
)) == NULL
)
204 h
= r
->rsc_mem
= AllocResource(instance
, hResInfo
, 0);
205 image
= GlobalLinearLock(h
);
206 image_size
= r
->size
;
207 fd
= AccessResource(instance
, hResInfo
);
209 if (image
== NULL
|| read(fd
, image
, image_size
) != image_size
) {
211 GlobalUnlock(hResInfo
);
216 GlobalLinearUnlock(h
);
217 GlobalUnlock(hResInfo
);
221 /**********************************************************************
222 * LockResource [KERNEL.62]
224 LPSTR
LockResource(HANDLE hResData
)
226 return GlobalLock(hResData
);
229 /**********************************************************************
230 * FreeResource [KERNEL.63]
232 HANDLE
FreeResource(HANDLE hResData
)
236 #ifdef DEBUG_RESOURCE
237 printf("FreeResource: handle %04x\n", hResData
);
240 for (r
= rp
= Top
; r
; r
= r
->next
) {
241 if (r
->rsc_mem
== hResData
) {
248 if (HIWORD((DWORD
)r
->name
))
250 if (HIWORD((DWORD
)r
->type
))
252 GlobalFree(r
->rsc_mem
);
253 GlobalFree(r
->info_mem
);
263 /**********************************************************************
266 HBITMAP
ConvertCoreBitmap( HDC hdc
, BITMAPCOREHEADER
* image
)
268 BITMAPINFO
* bmpInfo
;
271 int i
, size
, n_colors
;
273 n_colors
= 1 << image
->bcBitCount
;
275 if (image
->bcBitCount
< 24)
277 size
= sizeof(BITMAPINFOHEADER
) + n_colors
* sizeof(RGBQUAD
);
278 bits
= (char *) (image
+ 1) + (n_colors
* sizeof(RGBTRIPLE
));
282 size
= sizeof(BITMAPINFOHEADER
);
283 bits
= (char *) (image
+ 1);
285 bmpInfo
= (BITMAPINFO
*) malloc( size
);
287 bmpInfo
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
288 bmpInfo
->bmiHeader
.biWidth
= image
->bcWidth
;
289 bmpInfo
->bmiHeader
.biHeight
= image
->bcHeight
;
290 bmpInfo
->bmiHeader
.biPlanes
= image
->bcPlanes
;
291 bmpInfo
->bmiHeader
.biBitCount
= image
->bcBitCount
;
292 bmpInfo
->bmiHeader
.biCompression
= 0;
293 bmpInfo
->bmiHeader
.biSizeImage
= 0;
294 bmpInfo
->bmiHeader
.biXPelsPerMeter
= 0;
295 bmpInfo
->bmiHeader
.biYPelsPerMeter
= 0;
296 bmpInfo
->bmiHeader
.biClrUsed
= 0;
297 bmpInfo
->bmiHeader
.biClrImportant
= 0;
299 if (image
->bcBitCount
< 24)
301 RGBTRIPLE
* oldMap
= (RGBTRIPLE
*)(image
+ 1);
302 RGBQUAD
* newMap
= bmpInfo
->bmiColors
;
303 for (i
= 0; i
< n_colors
; i
++, oldMap
++, newMap
++)
305 newMap
->rgbRed
= oldMap
->rgbtRed
;
306 newMap
->rgbGreen
= oldMap
->rgbtGreen
;
307 newMap
->rgbBlue
= oldMap
->rgbtBlue
;
308 newMap
->rgbReserved
= 0;
312 hbitmap
= CreateDIBitmap( hdc
, &bmpInfo
->bmiHeader
, CBM_INIT
,
313 bits
, bmpInfo
, DIB_RGB_COLORS
);
318 /**********************************************************************
321 HBITMAP
ConvertInfoBitmap( HDC hdc
, BITMAPINFO
* image
)
323 char * bits
= ((char *)image
) + DIB_BitmapInfoSize(image
, DIB_RGB_COLORS
);
324 return CreateDIBitmap( hdc
, &image
->bmiHeader
, CBM_INIT
,
325 bits
, image
, DIB_RGB_COLORS
);
328 /**********************************************************************
332 RSC_LoadResource(int instance
, LPSTR rsc_name
, LPSTR type
, int *image_size_ret
)
337 if (instance
== (HANDLE
)NULL
)
340 #ifdef DEBUG_RESOURCE
341 printf("RSC_LoadResource: instance = %04x, name = %08x, type = %08x\n",
342 instance
, (int) rsc_name
, (int) type
);
345 if ((hResInfo
= FindResource(instance
, rsc_name
, (LPSTR
) type
)) == (HANDLE
) NULL
) {
348 r
= (RESOURCE
*)GlobalLock(hResInfo
);
350 *image_size_ret
= r
->size
;
352 GlobalUnlock(hResInfo
);
353 return LoadResource(instance
, hResInfo
);
356 /**********************************************************************
357 * LoadIcon [USER.174]
359 HICON
LoadIcon(HANDLE instance
, LPSTR icon_name
)
364 ICONDESCRIP
*lpicodesc
;
368 BITMAPINFOHEADER
*bih
;
375 #ifdef DEBUG_RESOURCE
376 printf("LoadIcon(%04X", instance
);
381 if (!(hdc
= GetDC(GetDesktopWindow()))) return 0;
382 rsc_mem
= RSC_LoadResource(instance
, icon_name
, (LPSTR
) NE_RSCTYPE_GROUP_ICON
,
384 if (rsc_mem
== (HANDLE
)NULL
) {
385 printf("LoadIcon / Icon %04X not Found !\n", (int) icon_name
);
386 ReleaseDC(GetDesktopWindow(), hdc
);
389 lp
= (WORD
*)GlobalLock(rsc_mem
);
392 ReleaseDC(GetDesktopWindow(), hdc
);
395 lpicodesc
= (ICONDESCRIP
*)(lp
+ 3);
396 hIcon
= GlobalAlloc(GMEM_MOVEABLE
, sizeof(ICONALLOC
) + 1024);
397 if (hIcon
== (HICON
)NULL
) {
399 ReleaseDC(GetDesktopWindow(), hdc
);
402 lpico
= (ICONALLOC
*)GlobalLock(hIcon
);
403 lpico
->descriptor
= *lpicodesc
;
404 width
= lpicodesc
->Width
;
405 height
= lpicodesc
->Height
;
406 GlobalUnlock(rsc_mem
);
408 rsc_mem
= RSC_LoadResource(instance
,
409 MAKEINTRESOURCE(lpicodesc
->icoDIBOffset
),
410 (LPSTR
) NE_RSCTYPE_ICON
, &image_size
);
411 if (rsc_mem
== (HANDLE
)NULL
) {
412 printf("LoadIcon / Icon %04X Bitmaps not Found !\n", (int) icon_name
);
413 ReleaseDC(GetDesktopWindow(), hdc
);
416 lp
= (WORD
*)GlobalLock(rsc_mem
);
419 ReleaseDC(GetDesktopWindow(), hdc
);
422 bmi
= (BITMAPINFO
*)lp
;
423 bih
= (BITMAPINFOHEADER
*)lp
;
424 rgbq
= &bmi
->bmiColors
[0];
425 bih
->biHeight
= bih
->biHeight
/ 2;
427 printf("LoadIcon / image_size=%d width=%d height=%d bih->biBitCount=%d bih->biSizeImage=%ld\n",
428 image_size, width, height, bih->biBitCount, bih->biSizeImage);
430 if (bih
->biSize
== sizeof(BITMAPINFOHEADER
))
431 lpico
->hBitmap
= ConvertInfoBitmap(hdc
, (BITMAPINFO
*)bih
);
435 bih
->biClrUsed
= bih
->biClrImportant
= 2;
436 rgbq
[0].rgbBlue
= 0xFF;
437 rgbq
[0].rgbGreen
= 0xFF;
438 rgbq
[0].rgbRed
= 0xFF;
439 rgbq
[0].rgbReserved
= 0x00;
440 rgbq
[1].rgbBlue
= 0x00;
441 rgbq
[1].rgbGreen
= 0x00;
442 rgbq
[1].rgbRed
= 0x00;
443 rgbq
[1].rgbReserved
= 0x00;
444 if (bih
->biSizeImage
== 0) {
445 if (bih
->biCompression
!= BI_RGB
) {
446 fprintf(stderr
,"Unknown size for compressed Icon bitmap.\n");
448 ReleaseDC(GetDesktopWindow(), hdc
);
451 bih
->biSizeImage
= (bih
->biWidth
* bih
->biHeight
* bih
->biBitCount
454 lpico
->hBitMask
= CreateDIBitmap(hdc
, bih
, CBM_INIT
,
455 (LPSTR
)lp
+ bih
->biSizeImage
- sizeof(BITMAPINFOHEADER
) / 2 - 4,
456 (BITMAPINFO
*)bih
, DIB_RGB_COLORS
);
457 GlobalUnlock(rsc_mem
);
459 hMemDC
= CreateCompatibleDC(hdc
);
460 hMemDC2
= CreateCompatibleDC(hdc
);
461 SelectObject(hMemDC
, lpico
->hBitmap
);
462 SelectObject(hMemDC2
, lpico
->hBitMask
);
463 BitBlt(hMemDC
, 0, 0, bih
->biWidth
, bih
->biHeight
, hMemDC2
, 0, 0, SRCINVERT
);
466 ReleaseDC(GetDesktopWindow(), hdc
);
468 dprintf_resource(stddeb
,"LoadIcon Alloc hIcon=%X\n", hIcon
);
472 /**********************************************************************
473 * CreateIcon [USER.407]
475 HICON
CreateIcon(HANDLE hInstance
, int nWidth
, int nHeight
,
476 BYTE nPlanes
, BYTE nBitsPixel
, LPSTR lpANDbits
,
482 #ifdef DEBUG_RESOURCE
483 printf("CreateIcon: hInstance = %04x, nWidth = %08x, nHeight = %08x \n",
484 hInstance
, nWidth
, nHeight
);
485 printf(" nPlanes = %04x, nBitsPixel = %04x,",nPlanes
, nBitsPixel
);
486 printf(" lpANDbits= %04x, lpXORbits = %04x, \n", (int)lpANDbits
,
490 if (hInstance
== (HANDLE
)NULL
) {
491 printf("CreateIcon / hInstance %04x not Found!\n",hInstance
);
494 hIcon
= GlobalAlloc(GMEM_MOVEABLE
, sizeof(ICONALLOC
) + 1024);
495 if (hIcon
== (HICON
)NULL
) {
496 printf("Can't allocate memory for Icon in CreateIcon\n");
499 lpico
= (ICONALLOC
*)GlobalLock(hIcon
);
501 lpico
->descriptor
.Width
=nWidth
;
502 lpico
->descriptor
.Height
=nHeight
;
503 lpico
->descriptor
.ColorCount
=16; /* Dummy Value */
504 lpico
->descriptor
.Reserved1
=0;
505 lpico
->descriptor
.Reserved2
=nPlanes
;
506 lpico
->descriptor
.Reserved3
=nWidth
*nHeight
;
508 /* either nPlanes and/or nBitCount is set to one */
509 lpico
->descriptor
.icoDIBSize
=nWidth
*nHeight
*nPlanes
*nBitsPixel
;
510 lpico
->descriptor
.icoDIBOffset
=0;
512 if( !(lpico
->hBitmap
=CreateBitmap(nWidth
, nHeight
, nPlanes
, nBitsPixel
,
514 printf("CreateIcon: couldn't create the XOR bitmap\n");
518 /* the AND BitMask is always monochrome */
519 if( !(lpico
->hBitMask
=CreateBitmap(nWidth
, nHeight
, 1, 1, lpANDbits
)) ) {
520 printf("CreateIcon: couldn't create the AND bitmap\n");
525 #ifdef DEBUG_RESOURCE
526 printf("CreateIcon Alloc hIcon=%X\n", hIcon
);
531 /**********************************************************************
532 * DestroyIcon [USER.457]
534 BOOL
DestroyIcon(HICON hIcon
)
538 if (hIcon
== (HICON
)NULL
)
540 lpico
= (ICONALLOC
*)GlobalLock(hIcon
);
541 if (lpico
->hBitmap
!= (HBITMAP
)NULL
)
542 DeleteObject(lpico
->hBitmap
);
547 /**********************************************************************
548 * LoadAccelerators [USER.177]
550 HANDLE
LoadAccelerators(HANDLE instance
, LPSTR lpTableName
)
555 ACCELHEADER
*lpAccelTbl
;
556 int i
, image_size
, n
;
559 printf("LoadAccelerators(%04X", instance
);
560 PrintId(lpTableName
);
564 rsc_mem
= RSC_LoadResource(instance
, lpTableName
, (LPSTR
) NE_RSCTYPE_ACCELERATOR
,
566 if (rsc_mem
== (HANDLE
)NULL
) {
567 printf("LoadAccelerators(%04X", instance
);
568 PrintId(lpTableName
);
569 printf(") not found !\n");
572 lp
= (BYTE
*)GlobalLock(rsc_mem
);
577 dprintf_accel(stddeb
,"LoadAccelerators / image_size=%d\n", image_size
);
579 hAccel
= GlobalAlloc(GMEM_MOVEABLE
,
580 sizeof(ACCELHEADER
) + (n
+ 1)*sizeof(ACCELENTRY
));
581 lpAccelTbl
= (LPACCELHEADER
)GlobalLock(hAccel
);
582 lpAccelTbl
->wCount
= 0;
583 for (i
= 0; i
< n
; i
++) {
584 lpAccelTbl
->tbl
[i
].type
= *(lp
++);
585 lpAccelTbl
->tbl
[i
].wEvent
= *((WORD
*)lp
);
587 lpAccelTbl
->tbl
[i
].wIDval
= *((WORD
*)lp
);
589 if (lpAccelTbl
->tbl
[i
].wEvent
== 0) break;
590 dprintf_accel(stddeb
,
591 "Accelerator #%u / event=%04X id=%04X type=%02X \n",
592 i
, lpAccelTbl
->tbl
[i
].wEvent
, lpAccelTbl
->tbl
[i
].wIDval
,
593 lpAccelTbl
->tbl
[i
].type
);
594 lpAccelTbl
->wCount
++;
596 GlobalUnlock(hAccel
);
597 GlobalUnlock(rsc_mem
);
602 /**********************************************************************
603 * TranslateAccelerator [USER.178]
605 int TranslateAccelerator(HWND hWnd
, HANDLE hAccel
, LPMSG msg
)
607 ACCELHEADER
*lpAccelTbl
;
610 if (hAccel
== 0 || msg
== NULL
) return 0;
611 if (msg
->message
!= WM_KEYDOWN
&&
612 msg
->message
!= WM_KEYUP
&&
613 msg
->message
!= WM_CHAR
) return 0;
616 printf("TranslateAccelerators hAccel=%04X !\n", hAccel
);
619 lpAccelTbl
= (LPACCELHEADER
)GlobalLock(hAccel
);
620 for (i
= 0; i
< lpAccelTbl
->wCount
; i
++) {
621 if (lpAccelTbl
->tbl
[i
].type
& VIRTKEY_ACCEL
) {
622 if (msg
->wParam
== lpAccelTbl
->tbl
[i
].wEvent
&&
623 msg
->message
== WM_KEYDOWN
) {
624 if ((lpAccelTbl
->tbl
[i
].type
& SHIFT_ACCEL
) &&
625 !(GetKeyState(VK_SHIFT
) & 0xf)) {
626 GlobalUnlock(hAccel
);
629 if ((lpAccelTbl
->tbl
[i
].type
& CONTROL_ACCEL
) &&
630 !(GetKeyState(VK_CONTROL
) & 0xf)) {
631 GlobalUnlock(hAccel
);
634 if ((lpAccelTbl
->tbl
[i
].type
& ALT_ACCEL
) &&
635 !(GetKeyState(VK_MENU
) & 0xf)) {
636 GlobalUnlock(hAccel
);
639 SendMessage(hWnd
, WM_COMMAND
, lpAccelTbl
->tbl
[i
].wIDval
, 0x00010000L
);
640 GlobalUnlock(hAccel
);
643 if (msg
->message
== WM_KEYUP
) return 1;
646 if (msg
->wParam
== lpAccelTbl
->tbl
[i
].wEvent
&&
647 msg
->message
== WM_CHAR
) {
648 SendMessage(hWnd
, WM_COMMAND
, lpAccelTbl
->tbl
[i
].wIDval
, 0x00010000L
);
649 GlobalUnlock(hAccel
);
654 GlobalUnlock(hAccel
);
658 /**********************************************************************
662 LoadString(HANDLE instance
, WORD resource_id
, LPSTR buffer
, int buflen
)
670 #ifdef DEBUG_RESOURCE
671 printf("LoadString: instance = %04x, id = %d, buffer = %08x, "
672 "length = %d\n", instance
, resource_id
, (int) buffer
, buflen
);
675 hmem
= RSC_LoadResource(instance
, (char *) ((resource_id
>> 4) + 1),
676 (LPSTR
) NE_RSCTYPE_STRING
, &rsc_size
);
680 p
= GlobalLock(hmem
);
681 string_num
= resource_id
& 0x000f;
682 for (i
= 0; i
< string_num
; i
++)
685 i
= MIN(buflen
- 1, *p
);
687 memcpy(buffer
, p
+ 1, i
);
695 fprintf(stderr
,"LoadString // I dont know why , but caller give buflen=%d *p=%d !\n", buflen
, *p
);
696 fprintf(stderr
,"LoadString // and try to obtain string '%s'\n", p
+ 1);
700 dprintf_resource(stddeb
,"LoadString // '%s' copied !\n", buffer
);
704 /**********************************************************************
705 * LoadMenu [USER.150]
707 HMENU
LoadMenu(HINSTANCE instance
, char *menu_name
)
711 MENU_HEADER
*menu_desc
;
714 printf("LoadMenu(%04X", instance
);
718 if (menu_name
== NULL
)
721 if ((hMenu_desc
= RSC_LoadResource(instance
, menu_name
, (LPSTR
) NE_RSCTYPE_MENU
, NULL
)) == (HANDLE
) NULL
)
724 menu_desc
= (MENU_HEADER
*) GlobalLock(hMenu_desc
);
725 hMenu
= LoadMenuIndirect((LPSTR
)menu_desc
);
729 /**********************************************************************
733 LoadBitmap(HANDLE instance
, LPSTR bmp_name
)
742 #ifdef DEBUG_RESOURCE
743 printf("LoadBitmap(%04X", instance
);
749 hbitmap
= BITMAP_LoadOEMBitmap(((int) bmp_name
) & 0xffff);
754 rsc_mem
= RSC_LoadResource(instance
, bmp_name
, (LPSTR
) NE_RSCTYPE_BITMAP
,
756 if (rsc_mem
== (HANDLE
)NULL
) {
757 printf("LoadBitmap(%04X", instance
);
759 printf(") NOT found!\n");
762 lp
= (long *) GlobalLinearLock(rsc_mem
);
763 if (!(hdc
= GetDC(0))) lp
== NULL
;
769 size
= CONV_LONG (*lp
);
770 if (size
== sizeof(BITMAPCOREHEADER
)){
771 CONV_BITMAPCOREHEADER (lp
);
772 hbitmap
= ConvertCoreBitmap( hdc
, (BITMAPCOREHEADER
*) lp
);
773 } else if (size
== sizeof(BITMAPINFOHEADER
)){
774 CONV_BITMAPINFO (lp
);
775 hbitmap
= ConvertInfoBitmap( hdc
, (BITMAPINFO
*) lp
);