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";
26 #define MIN(a,b) ((a) < (b) ? (a) : (b))
30 extern int NE_FindResource(HANDLE, SEGPTR, SEGPTR, RESOURCE *);
31 extern int PE_FindResource(HANDLE, SEGPTR, SEGPTR, RESOURCE *);
33 #define PrintId(name) \
34 if (HIWORD((DWORD)name)) \
35 printf(", '%s'", (char *)PTR_SEG_TO_LIN(name)); \
37 printf(", #%d", LOWORD(name));
39 /**********************************************************************
40 * FindResource [KERNEL.60]
42 HANDLE FindResource(HANDLE instance, SEGPTR name, SEGPTR type)
48 if(debugging_resource){
49 printf("FindResource(%04X", instance);
55 /* FIXME: did we already find this one ? */
57 if ((rh = GlobalAlloc(GMEM_MOVEABLE, sizeof(RESOURCE))) == 0)
60 r = (RESOURCE *)GlobalLock(rh);
66 if (HIWORD((DWORD)name))
67 r->name = strdup(PTR_SEG_TO_LIN(name));
69 r->name = (LPSTR)name;
71 if (HIWORD((DWORD)type))
72 r->type = strdup(PTR_SEG_TO_LIN(type));
74 r->type = (LPSTR)type;
76 r->wpnt = GetFileInfo(instance);
77 r->fd = dup(r->wpnt->fd);
79 status = NE_FindResource(instance, name, type, r);
81 status = PE_FindResource(instance, name, type, r);
84 if (HIWORD((DWORD)r->name))
87 if (HIWORD((DWORD)r->type))
98 /**********************************************************************
99 * AllocResource [KERNEL.66]
101 HANDLE AllocResource(HANDLE instance, HANDLE hResInfo, DWORD dwSize)
106 dprintf_resource(stddeb, "AllocResource(%04X, %04X, %08X);\n",
107 instance, hResInfo, (int) dwSize);
109 if ((r = (RESOURCE *)GlobalLock(hResInfo)) == NULL)
112 image_size = r->size;
115 r->rsc_mem = GlobalAlloc(GMEM_MOVEABLE, image_size);
117 r->rsc_mem = GlobalAlloc(GMEM_MOVEABLE, dwSize);
119 GlobalUnlock(hResInfo);
124 /**********************************************************************
125 * AccessResource [KERNEL.64]
127 int AccessResource(HANDLE instance, HANDLE hResInfo)
132 dprintf_resource(stddeb, "AccessResource(%04X, %04X);\n",
135 if ((r = (RESOURCE *)GlobalLock(hResInfo)) == NULL)
139 lseek(fd, r->offset, SEEK_SET);
140 GlobalUnlock(hResInfo);
145 /**********************************************************************
146 * SizeofResource [KERNEL.65]
148 WORD SizeofResource(HANDLE instance, HANDLE hResInfo)
153 dprintf_resource(stddeb, "SizeofResource(%04X, %04X);\n",
156 if ((r = (RESOURCE *)GlobalLock(hResInfo)) == NULL)
160 GlobalUnlock(hResInfo);
165 /**********************************************************************
166 * LoadResource [KERNEL.61]
168 HANDLE LoadResource(HANDLE instance, HANDLE hResInfo)
175 dprintf_resource(stddeb, "LoadResource(%04X, %04X);\n", instance, hResInfo);
177 if ((r = (RESOURCE *)GlobalLock(hResInfo)) == NULL)
180 h = r->rsc_mem = AllocResource(instance, hResInfo, 0);
181 image = GlobalLock(h);
182 image_size = r->size;
183 fd = AccessResource(instance, hResInfo);
185 if (image == NULL || read(fd, image, image_size) != image_size) {
187 GlobalUnlock(hResInfo);
193 GlobalUnlock(hResInfo);
197 /**********************************************************************
198 * LockResource [KERNEL.62]
202 SEGPTR WIN16_LockResource(HANDLE hResData)
204 return WIN16_GlobalLock(hResData);
208 LPSTR LockResource(HANDLE hResData)
210 return GlobalLock(hResData);
213 /**********************************************************************
214 * FreeResource [KERNEL.63]
216 HANDLE FreeResource(HANDLE hResData)
220 dprintf_resource(stddeb, "FreeResource: handle %04x\n", hResData);
222 for (r = rp = Top; r ; r = r->next) {
223 if (r->rsc_mem == hResData) {
230 if (HIWORD((DWORD)r->name))
232 if (HIWORD((DWORD)r->type))
234 GlobalFree(r->rsc_mem);
235 GlobalFree(r->info_mem);
245 /**********************************************************************
248 HBITMAP ConvertCoreBitmap( HDC hdc, BITMAPCOREHEADER * image )
250 BITMAPINFO * bmpInfo;
253 int i, size, n_colors;
255 n_colors = 1 << image->bcBitCount;
256 if (image->bcBitCount < 24)
258 size = sizeof(BITMAPINFOHEADER) + n_colors * sizeof(RGBQUAD);
259 bits = (char *) (image + 1) + (n_colors * sizeof(RGBTRIPLE));
263 size = sizeof(BITMAPINFOHEADER);
264 bits = (char *) (image + 1);
266 bmpInfo = (BITMAPINFO *) malloc( size );
268 bmpInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
269 bmpInfo->bmiHeader.biWidth = image->bcWidth;
270 bmpInfo->bmiHeader.biHeight = image->bcHeight;
271 bmpInfo->bmiHeader.biPlanes = image->bcPlanes;
272 bmpInfo->bmiHeader.biBitCount = image->bcBitCount;
273 bmpInfo->bmiHeader.biCompression = 0;
274 bmpInfo->bmiHeader.biSizeImage = 0;
275 bmpInfo->bmiHeader.biXPelsPerMeter = 0;
276 bmpInfo->bmiHeader.biYPelsPerMeter = 0;
277 bmpInfo->bmiHeader.biClrUsed = 0;
278 bmpInfo->bmiHeader.biClrImportant = 0;
280 if (image->bcBitCount < 24)
282 RGBTRIPLE * oldMap = (RGBTRIPLE *)(image + 1);
283 RGBQUAD * newMap = bmpInfo->bmiColors;
284 for (i = 0; i < n_colors; i++, oldMap++, newMap++)
286 newMap->rgbRed = oldMap->rgbtRed;
287 newMap->rgbGreen = oldMap->rgbtGreen;
288 newMap->rgbBlue = oldMap->rgbtBlue;
289 newMap->rgbReserved = 0;
293 hbitmap = CreateDIBitmap( hdc, &bmpInfo->bmiHeader, CBM_INIT,
294 bits, bmpInfo, DIB_RGB_COLORS );
299 /**********************************************************************
302 HBITMAP ConvertInfoBitmap( HDC hdc, BITMAPINFO * image )
304 char * bits = ((char *)image) + DIB_BitmapInfoSize(image, DIB_RGB_COLORS);
305 return CreateDIBitmap( hdc, &image->bmiHeader, CBM_INIT,
306 bits, image, DIB_RGB_COLORS );
309 /**********************************************************************
313 RSC_LoadResource(int instance, SEGPTR rsc_name, SEGPTR type, int *image_size_ret)
318 dprintf_resource(stddeb, "RSC_LoadResource: instance = %04x, name = %08lx, type = %08lx\n",
319 instance, rsc_name, type);
321 if ((hResInfo = FindResource(instance, rsc_name, type)) == (HANDLE) NULL) {
324 r = (RESOURCE *)GlobalLock(hResInfo);
326 *image_size_ret = r->size;
328 GlobalUnlock(hResInfo);
329 return LoadResource(instance, hResInfo);
332 /**********************************************************************
333 * LoadIcon [USER.174]
335 HICON LoadIcon( HANDLE instance, SEGPTR icon_name )
340 ICONDESCRIP *lpicodesc;
344 BITMAPINFOHEADER *bih;
349 if (HIWORD(icon_name))
350 dprintf_resource( stddeb, "LoadIcon: %04x '%s'\n",
351 instance, (char *)PTR_SEG_TO_LIN( icon_name ) );
353 dprintf_resource( stddeb, "LoadIcon: %04x %04x\n",
354 instance, LOWORD(icon_name) );
358 if (HIWORD((int)icon_name)) return 0; /* FIXME: should handle '#xxx' */
359 return OBM_LoadIcon( LOWORD((int)icon_name) );
362 if (!(hdc = GetDC(GetDesktopWindow()))) return 0;
363 rsc_mem = RSC_LoadResource(instance, icon_name,
364 (SEGPTR) NE_RSCTYPE_GROUP_ICON, &image_size);
365 if (rsc_mem == (HANDLE)NULL) {
366 printf("LoadIcon / Icon %08x not Found !\n", (int) icon_name);
367 ReleaseDC(GetDesktopWindow(), hdc);
370 lp = (WORD *)GlobalLock(rsc_mem);
373 ReleaseDC(GetDesktopWindow(), hdc);
376 lpicodesc = (ICONDESCRIP *)(lp + 3);
377 hIcon = GlobalAlloc(GMEM_MOVEABLE, sizeof(ICONALLOC) + 1024);
378 if (hIcon == (HICON)NULL) {
380 ReleaseDC(GetDesktopWindow(), hdc);
383 lpico = (ICONALLOC *)GlobalLock(hIcon);
384 lpico->descriptor = *lpicodesc;
385 width = lpicodesc->Width;
386 height = lpicodesc->Height;
387 GlobalUnlock(rsc_mem);
389 rsc_mem = RSC_LoadResource( instance,
390 MAKEINTRESOURCE(lpicodesc->icoDIBOffset),
391 (SEGPTR) NE_RSCTYPE_ICON, &image_size );
392 if (rsc_mem == (HANDLE)NULL) {
393 printf("LoadIcon / Icon %08lx Bitmaps not Found !\n", icon_name );
394 ReleaseDC(GetDesktopWindow(), hdc);
397 lp = (WORD *)GlobalLock(rsc_mem);
400 ReleaseDC(GetDesktopWindow(), hdc);
403 bmi = (BITMAPINFO *)lp;
404 bih = (BITMAPINFOHEADER *)lp;
405 rgbq = &bmi->bmiColors[0];
406 bih->biHeight = bih->biHeight / 2;
408 printf("LoadIcon / image_size=%d width=%d height=%d bih->biBitCount=%d bih->biSizeImage=%ld\n",
409 image_size, width, height, bih->biBitCount, bih->biSizeImage);
411 if (bih->biSize == sizeof(BITMAPINFOHEADER))
412 lpico->hBitmap = ConvertInfoBitmap(hdc, (BITMAPINFO *)bih);
416 bih->biClrUsed = bih->biClrImportant = 2;
417 rgbq[0].rgbBlue = rgbq[0].rgbGreen = rgbq[0].rgbRed = 0x00;
418 rgbq[1].rgbBlue = rgbq[1].rgbGreen = rgbq[1].rgbRed = 0xff;
419 rgbq[0].rgbReserved = rgbq[1].rgbReserved = 0;
420 if (bih->biSizeImage == 0) {
421 if (bih->biCompression != BI_RGB) {
422 fprintf(stderr,"Unknown size for compressed Icon bitmap.\n");
424 ReleaseDC(GetDesktopWindow(), hdc);
427 bih->biSizeImage = (bih->biWidth * bih->biHeight * bih->biBitCount
430 lpico->hBitMask = CreateDIBitmap(hdc, bih, CBM_INIT,
431 (LPSTR)lp + bih->biSizeImage - sizeof(BITMAPINFOHEADER) / 2 - 4,
432 (BITMAPINFO *)bih, DIB_RGB_COLORS );
433 GlobalUnlock(rsc_mem);
435 ReleaseDC(GetDesktopWindow(), hdc);
437 dprintf_resource(stddeb,"LoadIcon Alloc hIcon=%X\n", hIcon);
441 /**********************************************************************
442 * CreateIcon [USER.407]
444 HICON CreateIcon(HANDLE hInstance, int nWidth, int nHeight,
445 BYTE nPlanes, BYTE nBitsPixel, LPSTR lpANDbits,
451 dprintf_resource(stddeb, "CreateIcon: hInstance = %04x, nWidth = %08x, nHeight = %08x \n",
452 hInstance, nWidth, nHeight);
453 dprintf_resource(stddeb, " nPlanes = %04x, nBitsPixel = %04x,",nPlanes, nBitsPixel);
454 dprintf_resource(stddeb, " lpANDbits= %04x, lpXORbits = %04x, \n", (int)lpANDbits,
457 if (hInstance == (HANDLE)NULL) {
458 printf("CreateIcon / hInstance %04x not Found!\n",hInstance);
461 hIcon = GlobalAlloc(GMEM_MOVEABLE, sizeof(ICONALLOC) + 1024);
462 if (hIcon == (HICON)NULL) {
463 printf("Can't allocate memory for Icon in CreateIcon\n");
466 lpico= (ICONALLOC *)GlobalLock(hIcon);
468 lpico->descriptor.Width=nWidth;
469 lpico->descriptor.Height=nHeight;
470 lpico->descriptor.ColorCount=16; /* Dummy Value */
471 lpico->descriptor.Reserved1=0;
472 lpico->descriptor.Reserved2=nPlanes;
473 lpico->descriptor.Reserved3=nWidth*nHeight;
475 /* either nPlanes and/or nBitCount is set to one */
476 lpico->descriptor.icoDIBSize=nWidth*nHeight*nPlanes*nBitsPixel;
477 lpico->descriptor.icoDIBOffset=0;
479 if( !(lpico->hBitmap=CreateBitmap(nWidth, nHeight, nPlanes, nBitsPixel,
481 printf("CreateIcon: couldn't create the XOR bitmap\n");
485 /* the AND BitMask is always monochrome */
486 if( !(lpico->hBitMask=CreateBitmap(nWidth, nHeight, 1, 1, lpANDbits)) ) {
487 printf("CreateIcon: couldn't create the AND bitmap\n");
492 dprintf_resource(stddeb, "CreateIcon Alloc hIcon=%X\n", hIcon);
496 /**********************************************************************
497 * DestroyIcon [USER.457]
499 BOOL DestroyIcon(HICON hIcon)
503 if (hIcon == (HICON)NULL)
505 lpico = (ICONALLOC *)GlobalLock(hIcon);
506 if (lpico->hBitmap != (HBITMAP)NULL)
507 DeleteObject(lpico->hBitmap);
512 /**********************************************************************
513 * LoadAccelerators [USER.177]
515 HANDLE LoadAccelerators(HANDLE instance, SEGPTR lpTableName)
520 ACCELHEADER *lpAccelTbl;
521 int i, image_size, n;
523 if (HIWORD(lpTableName))
524 dprintf_accel( stddeb, "LoadAccelerators: %04x '%s'\n",
525 instance, (char *)PTR_SEG_TO_LIN( lpTableName ) );
527 dprintf_accel( stddeb, "LoadAccelerators: %04x %04x\n",
528 instance, LOWORD(lpTableName) );
530 rsc_mem = RSC_LoadResource( instance, lpTableName,
531 (SEGPTR) NE_RSCTYPE_ACCELERATOR, &image_size );
532 if (rsc_mem == (HANDLE)NULL) {
533 printf("LoadAccelerators(%08lx) not found!\n", lpTableName );
536 lp = (BYTE *)GlobalLock(rsc_mem);
541 dprintf_accel(stddeb,"LoadAccelerators / image_size=%d\n", image_size);
543 hAccel = GlobalAlloc(GMEM_MOVEABLE,
544 sizeof(ACCELHEADER) + (n + 1)*sizeof(ACCELENTRY));
545 lpAccelTbl = (LPACCELHEADER)GlobalLock(hAccel);
546 lpAccelTbl->wCount = 0;
547 for (i = 0; i < n; i++) {
548 lpAccelTbl->tbl[i].type = *(lp++);
549 lpAccelTbl->tbl[i].wEvent = *((WORD *)lp);
551 lpAccelTbl->tbl[i].wIDval = *((WORD *)lp);
553 if (lpAccelTbl->tbl[i].wEvent == 0) break;
554 dprintf_accel(stddeb,
555 "Accelerator #%u / event=%04X id=%04X type=%02X \n",
556 i, lpAccelTbl->tbl[i].wEvent, lpAccelTbl->tbl[i].wIDval,
557 lpAccelTbl->tbl[i].type);
558 lpAccelTbl->wCount++;
560 GlobalUnlock(hAccel);
561 GlobalUnlock(rsc_mem);
566 /**********************************************************************
567 * TranslateAccelerator [USER.178]
569 int TranslateAccelerator(HWND hWnd, HANDLE hAccel, LPMSG msg)
571 ACCELHEADER *lpAccelTbl;
574 if (hAccel == 0 || msg == NULL) return 0;
575 if (msg->message != WM_KEYDOWN &&
576 msg->message != WM_KEYUP &&
577 msg->message != WM_CHAR) return 0;
579 dprintf_accel(stddeb, "TranslateAccelerators hAccel=%04X !\n", hAccel);
581 lpAccelTbl = (LPACCELHEADER)GlobalLock(hAccel);
582 for (i = 0; i < lpAccelTbl->wCount; i++) {
583 if (lpAccelTbl->tbl[i].type & VIRTKEY_ACCEL) {
584 if (msg->wParam == lpAccelTbl->tbl[i].wEvent &&
585 msg->message == WM_KEYDOWN) {
586 if ((lpAccelTbl->tbl[i].type & SHIFT_ACCEL) &&
587 !(GetKeyState(VK_SHIFT) & 0xf)) {
588 GlobalUnlock(hAccel);
591 if ((lpAccelTbl->tbl[i].type & CONTROL_ACCEL) &&
592 !(GetKeyState(VK_CONTROL) & 0xf)) {
593 GlobalUnlock(hAccel);
596 if ((lpAccelTbl->tbl[i].type & ALT_ACCEL) &&
597 !(GetKeyState(VK_MENU) & 0xf)) {
598 GlobalUnlock(hAccel);
601 SendMessage(hWnd, WM_COMMAND, lpAccelTbl->tbl[i].wIDval, 0x00010000L);
602 GlobalUnlock(hAccel);
605 if (msg->message == WM_KEYUP) return 1;
608 if (msg->wParam == lpAccelTbl->tbl[i].wEvent &&
609 msg->message == WM_CHAR) {
610 SendMessage(hWnd, WM_COMMAND, lpAccelTbl->tbl[i].wIDval, 0x00010000L);
611 GlobalUnlock(hAccel);
616 GlobalUnlock(hAccel);
620 /**********************************************************************
624 LoadString(HANDLE instance, WORD resource_id, LPSTR buffer, int buflen)
631 dprintf_resource(stddeb, "LoadString: instance = %04x, id = %d, buffer = %08x, "
632 "length = %d\n", instance, resource_id, (int) buffer, buflen);
634 hrsrc = FindResource( instance, (SEGPTR)((resource_id >> 4) + 1),
635 (SEGPTR) NE_RSCTYPE_STRING );
636 if (!hrsrc) return 0;
637 hmem = LoadResource( instance, hrsrc );
640 p = GlobalLock(hmem);
641 string_num = resource_id & 0x000f;
642 for (i = 0; i < string_num; i++)
645 i = MIN(buflen - 1, *p);
647 memcpy(buffer, p + 1, i);
655 fprintf(stderr,"LoadString // I dont know why , but caller give buflen=%d *p=%d !\n", buflen, *p);
656 fprintf(stderr,"LoadString // and try to obtain string '%s'\n", p + 1);
658 FreeResource( hrsrc );
660 dprintf_resource(stddeb,"LoadString // '%s' copied !\n", buffer);
664 /**********************************************************************
665 * LoadMenu [USER.150]
667 HMENU LoadMenu( HINSTANCE instance, SEGPTR menu_name )
671 MENU_HEADER *menu_desc;
673 if (HIWORD(menu_name))
674 dprintf_resource( stddeb, "LoadMenu(%04x,'%s')\n",
675 instance, (char *)PTR_SEG_TO_LIN( menu_name ) );
677 dprintf_resource( stddeb, "LoadMenu(%04x,%04x)\n",
678 instance, LOWORD(menu_name) );
680 if (!menu_name) return 0;
682 if (!(hMenu_desc = RSC_LoadResource( instance, menu_name,
683 (SEGPTR) NE_RSCTYPE_MENU, NULL )))
686 menu_desc = (MENU_HEADER *) GlobalLock(hMenu_desc);
687 hMenu = LoadMenuIndirect((LPSTR)menu_desc);
691 /**********************************************************************
694 HBITMAP LoadBitmap( HANDLE instance, SEGPTR bmp_name )
703 if (HIWORD(bmp_name))
704 dprintf_resource( stddeb, "LoadBitmap(%04x,'%s')\n",
705 instance, (char *)PTR_SEG_TO_LIN( bmp_name ) );
707 dprintf_resource( stddeb, "LoadBitmap(%04x,%04x)\n",
708 instance, LOWORD( bmp_name ) );
712 if (HIWORD((int)bmp_name)) return 0; /* FIXME: should handle '#xxx' */
713 return OBM_LoadBitmap( LOWORD((int)bmp_name) );
716 rsc_mem = RSC_LoadResource(instance, bmp_name, (SEGPTR) NE_RSCTYPE_BITMAP,
718 if (rsc_mem == (HANDLE)NULL) {
719 printf("LoadBitmap(%04x,%08lx)\n", instance, bmp_name);
722 lp = (long *) GlobalLock(rsc_mem);
729 if (!(hdc = GetDC(0))) lp = NULL;
730 size = CONV_LONG (*lp);
731 if (size == sizeof(BITMAPCOREHEADER)){
732 CONV_BITMAPCOREHEADER (lp);
733 hbitmap = ConvertCoreBitmap( hdc, (BITMAPCOREHEADER *) lp );
734 } else if (size == sizeof(BITMAPINFOHEADER)){
735 CONV_BITMAPINFO (lp);
736 hbitmap = ConvertInfoBitmap( hdc, (BITMAPINFO *) lp );