Release 941017
[wine/gsoc-2012-control.git] / loader / resource.c
blob867c783cb5013404fd3bdef3b6f8d265fe2c791b
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";
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include "arch.h"
12 #include "prototypes.h"
13 #include "windows.h"
14 #include "gdi.h"
15 #include "wine.h"
16 #include "icon.h"
17 #include "menu.h"
18 #include "accel.h"
19 #include "dlls.h"
20 #include "resource.h"
21 #include "stddebug.h"
22 /* #define DEBUG_RESOURCE /* */
23 /* #undef DEBUG_RESOURCE /* */
24 /* #define DEBUG_ACCEL /* */
25 /* #undef DEBUG_ACCEL /* */
26 #include "debug.h"
28 #define MIN(a,b) ((a) < (b) ? (a) : (b))
30 RESOURCE *Top = NULL;
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); \
40 else \
41 printf(", #%d", (int) name);
43 /**********************************************************************
44 * FindResource [KERNEL.60]
46 HANDLE FindResource(HANDLE instance, LPSTR name, LPSTR type)
48 int status;
49 RESOURCE *r;
50 HANDLE rh;
52 #ifdef DEBUG_RESOURCE
53 printf("FindResource(%04X", instance);
54 PrintId(name);
55 PrintId(type);
56 printf(")\n");
57 #endif
59 if (instance == (HANDLE)NULL)
60 instance = hSysRes;
62 /* FIXME: did we already find this one ? */
64 if ((rh = GlobalAlloc(GMEM_MOVEABLE, sizeof(RESOURCE))) == 0)
65 return 0;
67 r = (RESOURCE *)GlobalLock(rh);
68 r->next = Top;
69 Top = r;
70 r->info_mem = rh;
71 r->rsc_mem = 0;
72 r->count = 0;
73 if (HIWORD((DWORD)name))
74 r->name = strdup(name);
75 else
76 r->name = name;
78 if (HIWORD((DWORD)type))
79 r->type = strdup(type);
80 else
81 r->type = type;
83 r->wpnt = GetFileInfo(instance);
84 r->fd = dup(r->wpnt->fd);
85 if (r->wpnt->ne)
86 status = NE_FindResource(instance, name, type, r);
87 else
88 status = PE_FindResource(instance, name, type, r);
90 if (!status) {
91 if (HIWORD((DWORD)r->name))
92 free(r->name);
94 if (HIWORD((DWORD)r->type))
95 free(r->type);
96 close(r->fd);
98 Top = r->next;
99 GlobalUnlock(rh);
100 return 0;
101 } else
102 return rh;
105 /**********************************************************************
106 * AllocResource [KERNEL.66]
108 HANDLE AllocResource(HANDLE instance, HANDLE hResInfo, DWORD dwSize)
110 RESOURCE *r;
111 int image_size;
113 #ifdef DEBUG_RESOURCE
114 printf("AllocResource(%04X, %04X, %08X);\n", instance, hResInfo, (int) dwSize);
115 #endif
117 if (instance == (HANDLE)NULL)
118 instance = hSysRes;
120 if ((r = (RESOURCE *)GlobalLock(hResInfo)) == NULL)
121 return 0;
123 image_size = r->size;
125 if (dwSize == 0)
126 r->rsc_mem = GlobalAlloc(GMEM_MOVEABLE, image_size);
127 else
128 r->rsc_mem = GlobalAlloc(GMEM_MOVEABLE, dwSize);
130 GlobalUnlock(hResInfo);
132 return r->rsc_mem;
135 /**********************************************************************
136 * AccessResource [KERNEL.64]
138 int AccessResource(HANDLE instance, HANDLE hResInfo)
140 int fd;
141 RESOURCE *r;
143 #ifdef DEBUG_RESOURCE
144 printf("AccessResource(%04X, %04X);\n", instance, hResInfo);
145 #endif
147 if (instance == (HANDLE)NULL)
148 instance = hSysRes;
150 if ((r = (RESOURCE *)GlobalLock(hResInfo)) == NULL)
151 return -1;
153 fd = r->fd;
154 lseek(fd, r->offset, SEEK_SET);
155 GlobalUnlock(hResInfo);
157 return fd;
160 /**********************************************************************
161 * SizeofResource [KERNEL.65]
163 WORD SizeofResource(HANDLE instance, HANDLE hResInfo)
165 RESOURCE *r;
166 int size;
168 #ifdef DEBUG_RESOURCE
169 printf("SizeofResource(%04X, %04X);\n", instance, hResInfo);
170 #endif
172 if (instance == (HANDLE)NULL)
173 instance = hSysRes;
175 if ((r = (RESOURCE *)GlobalLock(hResInfo)) == NULL)
176 return 0;
178 size = r->size;
179 GlobalUnlock(hResInfo);
181 return size;
184 /**********************************************************************
185 * LoadResource [KERNEL.61]
187 HANDLE LoadResource(HANDLE instance, HANDLE hResInfo)
189 RESOURCE *r;
190 int image_size, fd;
191 void *image;
192 HANDLE h;
194 #ifdef DEBUG_RESOURCE
195 printf("LoadResource(%04X, %04X);\n", instance, hResInfo);
196 #endif
198 if (instance == (HANDLE)NULL)
199 instance = hSysRes;
201 if ((r = (RESOURCE *)GlobalLock(hResInfo)) == NULL)
202 return 0;
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) {
210 GlobalFree(h);
211 GlobalUnlock(hResInfo);
212 return 0;
214 r->count++;
215 close(fd);
216 GlobalLinearUnlock(h);
217 GlobalUnlock(hResInfo);
218 return h;
221 /**********************************************************************
222 * LockResource [KERNEL.62]
224 LPSTR LockResource(HANDLE hResData)
226 return GlobalLock(hResData);
229 /**********************************************************************
230 * FreeResource [KERNEL.63]
232 HANDLE FreeResource(HANDLE hResData)
234 RESOURCE *r, *rp;
236 #ifdef DEBUG_RESOURCE
237 printf("FreeResource: handle %04x\n", hResData);
238 #endif
240 for (r = rp = Top; r ; r = r->next) {
241 if (r->rsc_mem == hResData) {
242 if (r->count == 0) {
243 if (rp != r)
244 rp->next = r->next;
245 else
246 Top = r->next;
248 if (HIWORD((DWORD)r->name))
249 free(r->name);
250 if (HIWORD((DWORD)r->type))
251 free(r->type);
252 GlobalFree(r->rsc_mem);
253 GlobalFree(r->info_mem);
254 return 0;
255 } else
256 r->count--;
258 rp = r;
260 return hResData;
263 /**********************************************************************
264 * ConvertCoreBitmap
266 HBITMAP ConvertCoreBitmap( HDC hdc, BITMAPCOREHEADER * image )
268 BITMAPINFO * bmpInfo;
269 HBITMAP hbitmap;
270 char * bits;
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));
280 else
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 );
314 free( bmpInfo );
315 return hbitmap;
318 /**********************************************************************
319 * ConvertInfoBitmap
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 /**********************************************************************
329 * RSC_LoadResource
331 HANDLE
332 RSC_LoadResource(int instance, LPSTR rsc_name, LPSTR type, int *image_size_ret)
334 HANDLE hResInfo;
335 RESOURCE *r;
337 if (instance == (HANDLE)NULL)
338 instance = hSysRes;
340 #ifdef DEBUG_RESOURCE
341 printf("RSC_LoadResource: instance = %04x, name = %08x, type = %08x\n",
342 instance, (int) rsc_name, (int) type);
343 #endif
345 if ((hResInfo = FindResource(instance, rsc_name, (LPSTR) type)) == (HANDLE) NULL) {
346 return (HANDLE)NULL;
348 r = (RESOURCE *)GlobalLock(hResInfo);
349 if (image_size_ret)
350 *image_size_ret = r->size;
351 r->count++;
352 GlobalUnlock(hResInfo);
353 return LoadResource(instance, hResInfo);
356 /**********************************************************************
357 * LoadIcon [USER.174]
359 HICON LoadIcon(HANDLE instance, LPSTR icon_name)
361 HICON hIcon;
362 HANDLE rsc_mem;
363 WORD *lp;
364 ICONDESCRIP *lpicodesc;
365 ICONALLOC *lpico;
366 int width, height;
367 BITMAPINFO *bmi;
368 BITMAPINFOHEADER *bih;
369 RGBQUAD *rgbq;
370 HDC hMemDC;
371 HDC hMemDC2;
372 HDC hdc;
373 int image_size;
375 #ifdef DEBUG_RESOURCE
376 printf("LoadIcon(%04X", instance);
377 PrintId(icon_name);
378 printf(")\n");
379 #endif
381 if (!(hdc = GetDC(GetDesktopWindow()))) return 0;
382 rsc_mem = RSC_LoadResource(instance, icon_name, (LPSTR) NE_RSCTYPE_GROUP_ICON,
383 &image_size);
384 if (rsc_mem == (HANDLE)NULL) {
385 printf("LoadIcon / Icon %04X not Found !\n", (int) icon_name);
386 ReleaseDC(GetDesktopWindow(), hdc);
387 return 0;
389 lp = (WORD *)GlobalLock(rsc_mem);
390 if (lp == NULL) {
391 GlobalFree(rsc_mem);
392 ReleaseDC(GetDesktopWindow(), hdc);
393 return 0;
395 lpicodesc = (ICONDESCRIP *)(lp + 3);
396 hIcon = GlobalAlloc(GMEM_MOVEABLE, sizeof(ICONALLOC) + 1024);
397 if (hIcon == (HICON)NULL) {
398 GlobalFree(rsc_mem);
399 ReleaseDC(GetDesktopWindow(), hdc);
400 return 0;
402 lpico = (ICONALLOC *)GlobalLock(hIcon);
403 lpico->descriptor = *lpicodesc;
404 width = lpicodesc->Width;
405 height = lpicodesc->Height;
406 GlobalUnlock(rsc_mem);
407 GlobalFree(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);
414 return 0;
416 lp = (WORD *)GlobalLock(rsc_mem);
417 if (lp == NULL) {
418 GlobalFree(rsc_mem);
419 ReleaseDC(GetDesktopWindow(), hdc);
420 return 0;
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);
432 else
433 lpico->hBitmap = 0;
434 bih->biBitCount = 1;
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");
447 GlobalFree(rsc_mem);
448 ReleaseDC(GetDesktopWindow(), hdc);
449 return 0;
451 bih->biSizeImage = (bih->biWidth * bih->biHeight * bih->biBitCount
452 + 7) / 8;
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);
458 GlobalFree(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);
464 DeleteDC(hMemDC);
465 DeleteDC(hMemDC2);
466 ReleaseDC(GetDesktopWindow(), hdc);
467 GlobalUnlock(hIcon);
468 dprintf_resource(stddeb,"LoadIcon Alloc hIcon=%X\n", hIcon);
469 return hIcon;
472 /**********************************************************************
473 * CreateIcon [USER.407]
475 HICON CreateIcon(HANDLE hInstance, int nWidth, int nHeight,
476 BYTE nPlanes, BYTE nBitsPixel, LPSTR lpANDbits,
477 LPSTR lpXORbits)
479 HICON hIcon;
480 ICONALLOC *lpico;
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,
487 (int)lpXORbits);
488 #endif
490 if (hInstance == (HANDLE)NULL) {
491 printf("CreateIcon / hInstance %04x not Found!\n",hInstance);
492 return 0;
494 hIcon = GlobalAlloc(GMEM_MOVEABLE, sizeof(ICONALLOC) + 1024);
495 if (hIcon == (HICON)NULL) {
496 printf("Can't allocate memory for Icon in CreateIcon\n");
497 return 0;
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,
513 lpXORbits)) ) {
514 printf("CreateIcon: couldn't create the XOR bitmap\n");
515 return(0);
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");
521 return(0);
524 GlobalUnlock(hIcon);
525 #ifdef DEBUG_RESOURCE
526 printf("CreateIcon Alloc hIcon=%X\n", hIcon);
527 #endif
528 return hIcon;
531 /**********************************************************************
532 * DestroyIcon [USER.457]
534 BOOL DestroyIcon(HICON hIcon)
536 ICONALLOC *lpico;
538 if (hIcon == (HICON)NULL)
539 return FALSE;
540 lpico = (ICONALLOC *)GlobalLock(hIcon);
541 if (lpico->hBitmap != (HBITMAP)NULL)
542 DeleteObject(lpico->hBitmap);
543 GlobalFree(hIcon);
544 return TRUE;
547 /**********************************************************************
548 * LoadAccelerators [USER.177]
550 HANDLE LoadAccelerators(HANDLE instance, LPSTR lpTableName)
552 HANDLE hAccel;
553 HANDLE rsc_mem;
554 BYTE *lp;
555 ACCELHEADER *lpAccelTbl;
556 int i, image_size, n;
558 #ifdef DEBUG_ACCEL
559 printf("LoadAccelerators(%04X", instance);
560 PrintId(lpTableName);
561 printf(")\n");
562 #endif
564 rsc_mem = RSC_LoadResource(instance, lpTableName, (LPSTR) NE_RSCTYPE_ACCELERATOR,
565 &image_size);
566 if (rsc_mem == (HANDLE)NULL) {
567 printf("LoadAccelerators(%04X", instance);
568 PrintId(lpTableName);
569 printf(") not found !\n");
570 return 0;
572 lp = (BYTE *)GlobalLock(rsc_mem);
573 if (lp == NULL) {
574 GlobalFree(rsc_mem);
575 return 0;
577 dprintf_accel(stddeb,"LoadAccelerators / image_size=%d\n", image_size);
578 n = image_size/5;
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);
586 lp += 2;
587 lpAccelTbl->tbl[i].wIDval = *((WORD *)lp);
588 lp += 2;
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);
598 GlobalFree(rsc_mem);
599 return hAccel;
602 /**********************************************************************
603 * TranslateAccelerator [USER.178]
605 int TranslateAccelerator(HWND hWnd, HANDLE hAccel, LPMSG msg)
607 ACCELHEADER *lpAccelTbl;
608 int i;
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;
615 #ifdef DEBUG_ACCEL
616 printf("TranslateAccelerators hAccel=%04X !\n", hAccel);
617 #endif
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);
627 return 0;
629 if ((lpAccelTbl->tbl[i].type & CONTROL_ACCEL) &&
630 !(GetKeyState(VK_CONTROL) & 0xf)) {
631 GlobalUnlock(hAccel);
632 return 0;
634 if ((lpAccelTbl->tbl[i].type & ALT_ACCEL) &&
635 !(GetKeyState(VK_MENU) & 0xf)) {
636 GlobalUnlock(hAccel);
637 return 0;
639 SendMessage(hWnd, WM_COMMAND, lpAccelTbl->tbl[i].wIDval, 0x00010000L);
640 GlobalUnlock(hAccel);
641 return 1;
643 if (msg->message == WM_KEYUP) return 1;
645 else {
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);
650 return 1;
654 GlobalUnlock(hAccel);
655 return 0;
658 /**********************************************************************
659 * LoadString
662 LoadString(HANDLE instance, WORD resource_id, LPSTR buffer, int buflen)
664 HANDLE hmem;
665 int rsc_size;
666 unsigned char *p;
667 int string_num;
668 int i;
670 #ifdef DEBUG_RESOURCE
671 printf("LoadString: instance = %04x, id = %d, buffer = %08x, "
672 "length = %d\n", instance, resource_id, (int) buffer, buflen);
673 #endif
675 hmem = RSC_LoadResource(instance, (char *) ((resource_id >> 4) + 1),
676 (LPSTR) NE_RSCTYPE_STRING, &rsc_size);
677 if (hmem == 0)
678 return 0;
680 p = GlobalLock(hmem);
681 string_num = resource_id & 0x000f;
682 for (i = 0; i < string_num; i++)
683 p += *p + 1;
685 i = MIN(buflen - 1, *p);
686 if (i > 0) {
687 memcpy(buffer, p + 1, i);
688 buffer[i] = '\0';
690 else {
691 if (buflen > 1) {
692 buffer[0] = '\0';
693 return 0;
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);
698 GlobalFree(hmem);
700 dprintf_resource(stddeb,"LoadString // '%s' copied !\n", buffer);
701 return i;
704 /**********************************************************************
705 * LoadMenu [USER.150]
707 HMENU LoadMenu(HINSTANCE instance, char *menu_name)
709 HMENU hMenu;
710 HANDLE hMenu_desc;
711 MENU_HEADER *menu_desc;
713 #ifdef DEBUG_MENU
714 printf("LoadMenu(%04X", instance);
715 PrintId(menu_name);
716 printf(")\n");
717 #endif
718 if (menu_name == NULL)
719 return 0;
721 if ((hMenu_desc = RSC_LoadResource(instance, menu_name, (LPSTR) NE_RSCTYPE_MENU, NULL)) == (HANDLE) NULL)
722 return 0;
724 menu_desc = (MENU_HEADER *) GlobalLock(hMenu_desc);
725 hMenu = LoadMenuIndirect((LPSTR)menu_desc);
726 return hMenu;
729 /**********************************************************************
730 * LoadBitmap
732 HBITMAP
733 LoadBitmap(HANDLE instance, LPSTR bmp_name)
735 HBITMAP hbitmap;
736 HANDLE rsc_mem;
737 HDC hdc;
738 long *lp;
739 int image_size;
740 int size;
742 #ifdef DEBUG_RESOURCE
743 printf("LoadBitmap(%04X", instance);
744 PrintId(bmp_name);
745 printf(")\n");
746 #endif
748 if (!instance) {
749 hbitmap = BITMAP_LoadOEMBitmap(((int) bmp_name) & 0xffff);
750 if (hbitmap)
751 return hbitmap;
754 rsc_mem = RSC_LoadResource(instance, bmp_name, (LPSTR) NE_RSCTYPE_BITMAP,
755 &image_size);
756 if (rsc_mem == (HANDLE)NULL) {
757 printf("LoadBitmap(%04X", instance);
758 PrintId(bmp_name);
759 printf(") NOT found!\n");
760 return 0;
762 lp = (long *) GlobalLinearLock(rsc_mem);
763 if (!(hdc = GetDC(0))) lp == NULL;
764 if (lp == NULL)
766 GlobalFree(rsc_mem);
767 return 0;
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 );
776 } else hbitmap = 0;
777 GlobalFree(rsc_mem);
778 ReleaseDC( 0, hdc );
779 return hbitmap;