2 static char RCSId[] = "$Id: resource.c,v 1.4 1993/07/04 04:04:21 root Exp root $";
3 static char Copyright[] = "Copyright Robert J. Amstadt, 1993";
24 #include "../rc/sysresbm.h"
26 #define MIN(a,b) ((a) < (b) ? (a) : (b))
30 extern int NE_FindResource(HANDLE
, LPSTR
, LPSTR
, RESOURCE
*);
31 extern int PE_FindResource(HANDLE
, LPSTR
, LPSTR
, RESOURCE
*);
32 extern HBITMAP
OBM_LoadOEMBitmap( WORD id
); /* objects/oembitmap.c */
34 #define PrintId(name) \
35 if (HIWORD((DWORD)name)) \
36 printf(", %s", name); \
38 printf(", #%d", (int) name);
40 /**********************************************************************
41 * FindResource [KERNEL.60]
43 HANDLE
FindResource(HANDLE instance
, LPSTR name
, LPSTR type
)
49 if(debugging_resource
){
50 printf("FindResource(%04X", instance
);
56 if (instance
== (HANDLE
)NULL
)
59 /* FIXME: did we already find this one ? */
61 if ((rh
= GlobalAlloc(GMEM_MOVEABLE
, sizeof(RESOURCE
))) == 0)
64 r
= (RESOURCE
*)GlobalLock(rh
);
70 if (HIWORD((DWORD
)name
))
71 r
->name
= strdup(name
);
75 if (HIWORD((DWORD
)type
))
76 r
->type
= strdup(type
);
80 r
->wpnt
= GetFileInfo(instance
);
81 r
->fd
= dup(r
->wpnt
->fd
);
83 status
= NE_FindResource(instance
, name
, type
, r
);
85 status
= PE_FindResource(instance
, name
, type
, r
);
88 if (HIWORD((DWORD
)r
->name
))
91 if (HIWORD((DWORD
)r
->type
))
102 /**********************************************************************
103 * AllocResource [KERNEL.66]
105 HANDLE
AllocResource(HANDLE instance
, HANDLE hResInfo
, DWORD dwSize
)
110 dprintf_resource(stddeb
, "AllocResource(%04X, %04X, %08X);\n",
111 instance
, hResInfo
, (int) dwSize
);
113 if (instance
== (HANDLE
)NULL
)
116 if ((r
= (RESOURCE
*)GlobalLock(hResInfo
)) == NULL
)
119 image_size
= r
->size
;
122 r
->rsc_mem
= GlobalAlloc(GMEM_MOVEABLE
, image_size
);
124 r
->rsc_mem
= GlobalAlloc(GMEM_MOVEABLE
, dwSize
);
126 GlobalUnlock(hResInfo
);
131 /**********************************************************************
132 * AccessResource [KERNEL.64]
134 int AccessResource(HANDLE instance
, HANDLE hResInfo
)
139 dprintf_resource(stddeb
, "AccessResource(%04X, %04X);\n",
142 if (instance
== (HANDLE
)NULL
)
145 if ((r
= (RESOURCE
*)GlobalLock(hResInfo
)) == NULL
)
149 lseek(fd
, r
->offset
, SEEK_SET
);
150 GlobalUnlock(hResInfo
);
155 /**********************************************************************
156 * SizeofResource [KERNEL.65]
158 WORD
SizeofResource(HANDLE instance
, HANDLE hResInfo
)
163 dprintf_resource(stddeb
, "SizeofResource(%04X, %04X);\n",
166 if (instance
== (HANDLE
)NULL
)
169 if ((r
= (RESOURCE
*)GlobalLock(hResInfo
)) == NULL
)
173 GlobalUnlock(hResInfo
);
178 /**********************************************************************
179 * LoadResource [KERNEL.61]
181 HANDLE
LoadResource(HANDLE instance
, HANDLE hResInfo
)
188 dprintf_resource(stddeb
, "LoadResource(%04X, %04X);\n", instance
, hResInfo
);
190 if (instance
== (HANDLE
)NULL
)
193 if ((r
= (RESOURCE
*)GlobalLock(hResInfo
)) == NULL
)
196 h
= r
->rsc_mem
= AllocResource(instance
, hResInfo
, 0);
197 image
= GlobalLinearLock(h
);
198 image_size
= r
->size
;
199 fd
= AccessResource(instance
, hResInfo
);
201 if (image
== NULL
|| read(fd
, image
, image_size
) != image_size
) {
203 GlobalUnlock(hResInfo
);
208 GlobalLinearUnlock(h
);
209 GlobalUnlock(hResInfo
);
213 /**********************************************************************
214 * LockResource [KERNEL.62]
216 LPSTR
LockResource(HANDLE hResData
)
218 return GlobalLock(hResData
);
221 /**********************************************************************
222 * FreeResource [KERNEL.63]
224 HANDLE
FreeResource(HANDLE hResData
)
228 dprintf_resource(stddeb
, "FreeResource: handle %04x\n", hResData
);
230 for (r
= rp
= Top
; r
; r
= r
->next
) {
231 if (r
->rsc_mem
== hResData
) {
238 if (HIWORD((DWORD
)r
->name
))
240 if (HIWORD((DWORD
)r
->type
))
242 GlobalFree(r
->rsc_mem
);
243 GlobalFree(r
->info_mem
);
253 /**********************************************************************
256 HBITMAP
ConvertCoreBitmap( HDC hdc
, BITMAPCOREHEADER
* image
)
258 BITMAPINFO
* bmpInfo
;
261 int i
, size
, n_colors
;
263 n_colors
= 1 << image
->bcBitCount
;
264 if (image
->bcBitCount
< 24)
266 size
= sizeof(BITMAPINFOHEADER
) + n_colors
* sizeof(RGBQUAD
);
267 bits
= (char *) (image
+ 1) + (n_colors
* sizeof(RGBTRIPLE
));
271 size
= sizeof(BITMAPINFOHEADER
);
272 bits
= (char *) (image
+ 1);
274 bmpInfo
= (BITMAPINFO
*) malloc( size
);
276 bmpInfo
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
277 bmpInfo
->bmiHeader
.biWidth
= image
->bcWidth
;
278 bmpInfo
->bmiHeader
.biHeight
= image
->bcHeight
;
279 bmpInfo
->bmiHeader
.biPlanes
= image
->bcPlanes
;
280 bmpInfo
->bmiHeader
.biBitCount
= image
->bcBitCount
;
281 bmpInfo
->bmiHeader
.biCompression
= 0;
282 bmpInfo
->bmiHeader
.biSizeImage
= 0;
283 bmpInfo
->bmiHeader
.biXPelsPerMeter
= 0;
284 bmpInfo
->bmiHeader
.biYPelsPerMeter
= 0;
285 bmpInfo
->bmiHeader
.biClrUsed
= 0;
286 bmpInfo
->bmiHeader
.biClrImportant
= 0;
288 if (image
->bcBitCount
< 24)
290 RGBTRIPLE
* oldMap
= (RGBTRIPLE
*)(image
+ 1);
291 RGBQUAD
* newMap
= bmpInfo
->bmiColors
;
292 for (i
= 0; i
< n_colors
; i
++, oldMap
++, newMap
++)
294 newMap
->rgbRed
= oldMap
->rgbtRed
;
295 newMap
->rgbGreen
= oldMap
->rgbtGreen
;
296 newMap
->rgbBlue
= oldMap
->rgbtBlue
;
297 newMap
->rgbReserved
= 0;
301 hbitmap
= CreateDIBitmap( hdc
, &bmpInfo
->bmiHeader
, CBM_INIT
,
302 bits
, bmpInfo
, DIB_RGB_COLORS
);
307 /**********************************************************************
310 HBITMAP
ConvertInfoBitmap( HDC hdc
, BITMAPINFO
* image
)
312 char * bits
= ((char *)image
) + DIB_BitmapInfoSize(image
, DIB_RGB_COLORS
);
313 return CreateDIBitmap( hdc
, &image
->bmiHeader
, CBM_INIT
,
314 bits
, image
, DIB_RGB_COLORS
);
317 /**********************************************************************
321 RSC_LoadResource(int instance
, LPSTR rsc_name
, LPSTR type
, int *image_size_ret
)
326 if (instance
== (HANDLE
)NULL
)
329 dprintf_resource(stddeb
, "RSC_LoadResource: instance = %04x, name = %08x, type = %08x\n",
330 instance
, (int) rsc_name
, (int) type
);
332 if ((hResInfo
= FindResource(instance
, rsc_name
, (LPSTR
) type
)) == (HANDLE
) NULL
) {
335 r
= (RESOURCE
*)GlobalLock(hResInfo
);
337 *image_size_ret
= r
->size
;
339 GlobalUnlock(hResInfo
);
340 return LoadResource(instance
, hResInfo
);
343 /**********************************************************************
344 * LoadIcon [USER.174]
346 HICON
LoadIcon(HANDLE instance
, LPSTR icon_name
)
351 ICONDESCRIP
*lpicodesc
;
355 BITMAPINFOHEADER
*bih
;
361 HBITMAP hbmpOld1
, hbmpOld2
;
363 if(debugging_resource
){
364 printf("LoadIcon(%04X", instance
);
369 if (!(hdc
= GetDC(GetDesktopWindow()))) return 0;
370 rsc_mem
= RSC_LoadResource(instance
, icon_name
, (LPSTR
) NE_RSCTYPE_GROUP_ICON
,
372 if (rsc_mem
== (HANDLE
)NULL
) {
373 printf("LoadIcon / Icon %04X not Found !\n", (int) icon_name
);
374 ReleaseDC(GetDesktopWindow(), hdc
);
377 lp
= (WORD
*)GlobalLock(rsc_mem
);
380 ReleaseDC(GetDesktopWindow(), hdc
);
383 lpicodesc
= (ICONDESCRIP
*)(lp
+ 3);
384 hIcon
= GlobalAlloc(GMEM_MOVEABLE
, sizeof(ICONALLOC
) + 1024);
385 if (hIcon
== (HICON
)NULL
) {
387 ReleaseDC(GetDesktopWindow(), hdc
);
390 lpico
= (ICONALLOC
*)GlobalLock(hIcon
);
391 lpico
->descriptor
= *lpicodesc
;
392 width
= lpicodesc
->Width
;
393 height
= lpicodesc
->Height
;
394 GlobalUnlock(rsc_mem
);
396 rsc_mem
= RSC_LoadResource(instance
,
397 MAKEINTRESOURCE(lpicodesc
->icoDIBOffset
),
398 (LPSTR
) NE_RSCTYPE_ICON
, &image_size
);
399 if (rsc_mem
== (HANDLE
)NULL
) {
400 printf("LoadIcon / Icon %04X Bitmaps not Found !\n", (int) icon_name
);
401 ReleaseDC(GetDesktopWindow(), hdc
);
404 lp
= (WORD
*)GlobalLock(rsc_mem
);
407 ReleaseDC(GetDesktopWindow(), hdc
);
410 bmi
= (BITMAPINFO
*)lp
;
411 bih
= (BITMAPINFOHEADER
*)lp
;
412 rgbq
= &bmi
->bmiColors
[0];
413 bih
->biHeight
= bih
->biHeight
/ 2;
415 printf("LoadIcon / image_size=%d width=%d height=%d bih->biBitCount=%d bih->biSizeImage=%ld\n",
416 image_size, width, height, bih->biBitCount, bih->biSizeImage);
418 if (bih
->biSize
== sizeof(BITMAPINFOHEADER
))
419 lpico
->hBitmap
= ConvertInfoBitmap(hdc
, (BITMAPINFO
*)bih
);
423 bih
->biClrUsed
= bih
->biClrImportant
= 2;
424 rgbq
[0].rgbBlue
= 0xFF;
425 rgbq
[0].rgbGreen
= 0xFF;
426 rgbq
[0].rgbRed
= 0xFF;
427 rgbq
[0].rgbReserved
= 0x00;
428 rgbq
[1].rgbBlue
= 0x00;
429 rgbq
[1].rgbGreen
= 0x00;
430 rgbq
[1].rgbRed
= 0x00;
431 rgbq
[1].rgbReserved
= 0x00;
432 if (bih
->biSizeImage
== 0) {
433 if (bih
->biCompression
!= BI_RGB
) {
434 fprintf(stderr
,"Unknown size for compressed Icon bitmap.\n");
436 ReleaseDC(GetDesktopWindow(), hdc
);
439 bih
->biSizeImage
= (bih
->biWidth
* bih
->biHeight
* bih
->biBitCount
442 lpico
->hBitMask
= CreateDIBitmap(hdc
, bih
, CBM_INIT
,
443 (LPSTR
)lp
+ bih
->biSizeImage
- sizeof(BITMAPINFOHEADER
) / 2 - 4,
444 (BITMAPINFO
*)bih
, DIB_RGB_COLORS
);
445 GlobalUnlock(rsc_mem
);
447 hMemDC
= CreateCompatibleDC(hdc
);
448 hMemDC2
= CreateCompatibleDC(hdc
);
449 hbmpOld1
= SelectObject(hMemDC
, lpico
->hBitmap
);
450 hbmpOld2
= SelectObject(hMemDC2
, lpico
->hBitMask
);
451 BitBlt(hMemDC
, 0, 0, bih
->biWidth
, bih
->biHeight
, hMemDC2
, 0, 0,SRCINVERT
);
452 SelectObject( hMemDC
, hbmpOld1
);
453 SelectObject( hMemDC2
, hbmpOld2
);
456 ReleaseDC(GetDesktopWindow(), hdc
);
458 dprintf_resource(stddeb
,"LoadIcon Alloc hIcon=%X\n", hIcon
);
462 /**********************************************************************
463 * CreateIcon [USER.407]
465 HICON
CreateIcon(HANDLE hInstance
, int nWidth
, int nHeight
,
466 BYTE nPlanes
, BYTE nBitsPixel
, LPSTR lpANDbits
,
472 dprintf_resource(stddeb
, "CreateIcon: hInstance = %04x, nWidth = %08x, nHeight = %08x \n",
473 hInstance
, nWidth
, nHeight
);
474 dprintf_resource(stddeb
, " nPlanes = %04x, nBitsPixel = %04x,",nPlanes
, nBitsPixel
);
475 dprintf_resource(stddeb
, " lpANDbits= %04x, lpXORbits = %04x, \n", (int)lpANDbits
,
478 if (hInstance
== (HANDLE
)NULL
) {
479 printf("CreateIcon / hInstance %04x not Found!\n",hInstance
);
482 hIcon
= GlobalAlloc(GMEM_MOVEABLE
, sizeof(ICONALLOC
) + 1024);
483 if (hIcon
== (HICON
)NULL
) {
484 printf("Can't allocate memory for Icon in CreateIcon\n");
487 lpico
= (ICONALLOC
*)GlobalLock(hIcon
);
489 lpico
->descriptor
.Width
=nWidth
;
490 lpico
->descriptor
.Height
=nHeight
;
491 lpico
->descriptor
.ColorCount
=16; /* Dummy Value */
492 lpico
->descriptor
.Reserved1
=0;
493 lpico
->descriptor
.Reserved2
=nPlanes
;
494 lpico
->descriptor
.Reserved3
=nWidth
*nHeight
;
496 /* either nPlanes and/or nBitCount is set to one */
497 lpico
->descriptor
.icoDIBSize
=nWidth
*nHeight
*nPlanes
*nBitsPixel
;
498 lpico
->descriptor
.icoDIBOffset
=0;
500 if( !(lpico
->hBitmap
=CreateBitmap(nWidth
, nHeight
, nPlanes
, nBitsPixel
,
502 printf("CreateIcon: couldn't create the XOR bitmap\n");
506 /* the AND BitMask is always monochrome */
507 if( !(lpico
->hBitMask
=CreateBitmap(nWidth
, nHeight
, 1, 1, lpANDbits
)) ) {
508 printf("CreateIcon: couldn't create the AND bitmap\n");
513 dprintf_resource(stddeb
, "CreateIcon Alloc hIcon=%X\n", hIcon
);
517 /**********************************************************************
518 * DestroyIcon [USER.457]
520 BOOL
DestroyIcon(HICON hIcon
)
524 if (hIcon
== (HICON
)NULL
)
526 lpico
= (ICONALLOC
*)GlobalLock(hIcon
);
527 if (lpico
->hBitmap
!= (HBITMAP
)NULL
)
528 DeleteObject(lpico
->hBitmap
);
533 /**********************************************************************
534 * LoadAccelerators [USER.177]
536 HANDLE
LoadAccelerators(HANDLE instance
, LPSTR lpTableName
)
541 ACCELHEADER
*lpAccelTbl
;
542 int i
, image_size
, n
;
545 printf("LoadAccelerators(%04X", instance
);
546 PrintId(lpTableName
);
550 rsc_mem
= RSC_LoadResource(instance
, lpTableName
, (LPSTR
) NE_RSCTYPE_ACCELERATOR
,
552 if (rsc_mem
== (HANDLE
)NULL
) {
553 printf("LoadAccelerators(%04X", instance
);
554 PrintId(lpTableName
);
555 printf(") not found !\n");
558 lp
= (BYTE
*)GlobalLock(rsc_mem
);
563 dprintf_accel(stddeb
,"LoadAccelerators / image_size=%d\n", image_size
);
565 hAccel
= GlobalAlloc(GMEM_MOVEABLE
,
566 sizeof(ACCELHEADER
) + (n
+ 1)*sizeof(ACCELENTRY
));
567 lpAccelTbl
= (LPACCELHEADER
)GlobalLock(hAccel
);
568 lpAccelTbl
->wCount
= 0;
569 for (i
= 0; i
< n
; i
++) {
570 lpAccelTbl
->tbl
[i
].type
= *(lp
++);
571 lpAccelTbl
->tbl
[i
].wEvent
= *((WORD
*)lp
);
573 lpAccelTbl
->tbl
[i
].wIDval
= *((WORD
*)lp
);
575 if (lpAccelTbl
->tbl
[i
].wEvent
== 0) break;
576 dprintf_accel(stddeb
,
577 "Accelerator #%u / event=%04X id=%04X type=%02X \n",
578 i
, lpAccelTbl
->tbl
[i
].wEvent
, lpAccelTbl
->tbl
[i
].wIDval
,
579 lpAccelTbl
->tbl
[i
].type
);
580 lpAccelTbl
->wCount
++;
582 GlobalUnlock(hAccel
);
583 GlobalUnlock(rsc_mem
);
588 /**********************************************************************
589 * TranslateAccelerator [USER.178]
591 int TranslateAccelerator(HWND hWnd
, HANDLE hAccel
, LPMSG msg
)
593 ACCELHEADER
*lpAccelTbl
;
596 if (hAccel
== 0 || msg
== NULL
) return 0;
597 if (msg
->message
!= WM_KEYDOWN
&&
598 msg
->message
!= WM_KEYUP
&&
599 msg
->message
!= WM_CHAR
) return 0;
601 dprintf_accel(stddeb
, "TranslateAccelerators hAccel=%04X !\n", hAccel
);
603 lpAccelTbl
= (LPACCELHEADER
)GlobalLock(hAccel
);
604 for (i
= 0; i
< lpAccelTbl
->wCount
; i
++) {
605 if (lpAccelTbl
->tbl
[i
].type
& VIRTKEY_ACCEL
) {
606 if (msg
->wParam
== lpAccelTbl
->tbl
[i
].wEvent
&&
607 msg
->message
== WM_KEYDOWN
) {
608 if ((lpAccelTbl
->tbl
[i
].type
& SHIFT_ACCEL
) &&
609 !(GetKeyState(VK_SHIFT
) & 0xf)) {
610 GlobalUnlock(hAccel
);
613 if ((lpAccelTbl
->tbl
[i
].type
& CONTROL_ACCEL
) &&
614 !(GetKeyState(VK_CONTROL
) & 0xf)) {
615 GlobalUnlock(hAccel
);
618 if ((lpAccelTbl
->tbl
[i
].type
& ALT_ACCEL
) &&
619 !(GetKeyState(VK_MENU
) & 0xf)) {
620 GlobalUnlock(hAccel
);
623 SendMessage(hWnd
, WM_COMMAND
, lpAccelTbl
->tbl
[i
].wIDval
, 0x00010000L
);
624 GlobalUnlock(hAccel
);
627 if (msg
->message
== WM_KEYUP
) return 1;
630 if (msg
->wParam
== lpAccelTbl
->tbl
[i
].wEvent
&&
631 msg
->message
== WM_CHAR
) {
632 SendMessage(hWnd
, WM_COMMAND
, lpAccelTbl
->tbl
[i
].wIDval
, 0x00010000L
);
633 GlobalUnlock(hAccel
);
638 GlobalUnlock(hAccel
);
642 /**********************************************************************
646 LoadString(HANDLE instance
, WORD resource_id
, LPSTR buffer
, int buflen
)
654 dprintf_resource(stddeb
, "LoadString: instance = %04x, id = %d, buffer = %08x, "
655 "length = %d\n", instance
, resource_id
, (int) buffer
, buflen
);
657 hmem
= RSC_LoadResource(instance
, (char *) ((resource_id
>> 4) + 1),
658 (LPSTR
) NE_RSCTYPE_STRING
, &rsc_size
);
662 p
= GlobalLock(hmem
);
663 string_num
= resource_id
& 0x000f;
664 for (i
= 0; i
< string_num
; i
++)
667 i
= MIN(buflen
- 1, *p
);
669 memcpy(buffer
, p
+ 1, i
);
677 fprintf(stderr
,"LoadString // I dont know why , but caller give buflen=%d *p=%d !\n", buflen
, *p
);
678 fprintf(stderr
,"LoadString // and try to obtain string '%s'\n", p
+ 1);
682 dprintf_resource(stddeb
,"LoadString // '%s' copied !\n", buffer
);
686 /**********************************************************************
687 * LoadMenu [USER.150]
689 HMENU
LoadMenu(HINSTANCE instance
, char *menu_name
)
693 MENU_HEADER
*menu_desc
;
696 printf("LoadMenu(%04X", instance
);
700 if (menu_name
== NULL
)
703 if ((hMenu_desc
= RSC_LoadResource(instance
, menu_name
, (LPSTR
) NE_RSCTYPE_MENU
, NULL
)) == (HANDLE
) NULL
)
706 menu_desc
= (MENU_HEADER
*) GlobalLock(hMenu_desc
);
707 hMenu
= LoadMenuIndirect((LPSTR
)menu_desc
);
711 /**********************************************************************
715 LoadBitmap(HANDLE instance
, LPSTR bmp_name
)
724 if(debugging_resource
){
725 printf("LoadBitmap(%04X", instance
);
731 struct ResourceTable
*it
;
732 hbitmap
= OBM_LoadOEMBitmap(((int) bmp_name
) & 0xffff);
735 /* Load from sysresbm */
736 dprintf_resource(stddeb
,"Searching for %d\n", (int) bmp_name
);
737 for(it
=sysresbmTable
;it
->value
;it
++){
738 if(it
->type
==NE_RSCTYPE_BITMAP
)
739 if((((int)bmp_name
& 0xFFFF0000) == 0))
740 {if(it
->id
==(int)bmp_name
)break;}
741 else if(!strcmp(it
->name
,bmp_name
))break;
743 if(!it
->value
)return 0;
744 dprintf_resource(stddeb
,"Found %s\n",it
->name
);
746 rsc_mem
=(HANDLE
)NULL
;
747 } else { /* Load from file - indent this code properly later */
749 rsc_mem
= RSC_LoadResource(instance
, bmp_name
, (LPSTR
) NE_RSCTYPE_BITMAP
,
751 if (rsc_mem
== (HANDLE
)NULL
) {
752 printf("LoadBitmap(%04X", instance
);
754 printf(") NOT found!\n");
757 lp
= (long *) GlobalLinearLock(rsc_mem
);
763 } /* Load from file */
764 if (!(hdc
= GetDC(0))) lp
= NULL
;
765 size
= CONV_LONG (*lp
);
766 if (size
== sizeof(BITMAPCOREHEADER
)){
767 CONV_BITMAPCOREHEADER (lp
);
768 hbitmap
= ConvertCoreBitmap( hdc
, (BITMAPCOREHEADER
*) lp
);
769 } else if (size
== sizeof(BITMAPINFOHEADER
)){
770 CONV_BITMAPINFO (lp
);
771 hbitmap
= ConvertInfoBitmap( hdc
, (BITMAPINFO
*) lp
);