Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

loadres.cpp

Go to the documentation of this file.
00001 /* $Id: loadres.cpp,v 1.38 2001/07/16 19:32:55 sandervl Exp $ */
00002 
00003 /*
00004  * Win32 resource API functions for OS/2
00005  *
00006  * Copyright 1998 Sander van Leeuwen
00007  *
00008  * Parts based on Wine code (objects\bitmap.c, loader\resource.c, objects\cursoricon.c):
00009  *
00010  * Copyright 1993 Alexandre Julliard
00011  *           1993 Robert J. Amstadt
00012  *           1996 Martin Von Loewis
00013  *           1997 Alex Korobka
00014  *           1998 Turchanov Sergey
00015  *           1998 Huw D M Davies
00016  *
00017  * Project Odin Software License can be found in LICENSE.TXT
00018  *
00019  */
00020 #include <os2win.h>
00021 #include <user32.h>
00022 #include <heapstring.h>
00023 #include <oslibres.h>
00024 #include <win\virtual.h>
00025 #include "dib.h"
00026 #include "initterm.h"
00027 #include <winres.h>
00028 #include "pmwindow.h"
00029 
00030 #define DBG_LOCALLOG    DBG_loadres
00031 #include "dbglocal.h"
00032 
00033 //******************************************************************************
00034 //******************************************************************************
00035 INT WIN32API LoadStringA(HINSTANCE instance, UINT resource_id,
00036                          LPSTR buffer, INT buflen )
00037 {
00038   INT retval;
00039   LPWSTR buffer2 = NULL;
00040 
00041     if (buffer && buflen)
00042         buffer2 = (LPWSTR)HeapAlloc( GetProcessHeap(), 0, buflen * 2 );
00043 
00044     retval = LoadStringW(instance,resource_id,buffer2,buflen);
00045 
00046     if (buffer2)
00047     {
00048         if (retval) {
00049             lstrcpynWtoA( buffer, buffer2, buflen );
00050             retval = lstrlenA( buffer );
00051         }
00052         else
00053             *buffer = 0; //NT4, SP6 clears first character
00054         HeapFree( GetProcessHeap(), 0, buffer2 );
00055     }
00056     return retval;
00057 }
00058 //******************************************************************************
00059 //******************************************************************************
00060 int WIN32API LoadStringW(HINSTANCE hinst, UINT wID, LPWSTR lpBuffer, int cchBuffer)
00061 {
00062  WCHAR *p;
00063  int string_num;
00064  int i = 0;
00065  HRSRC hRes;
00066 
00067     /* Use bits 4 - 19 (incremented by 1) as resourceid, mask out
00068      * 20 - 31. */
00069     hRes = FindResourceW(hinst, (LPWSTR)(((wID>>4)&0xffff)+1), RT_STRINGW);
00070     if(hRes == NULL) {
00071         dprintf(("LoadStringW NOT FOUND from %X, id %d buffersize %d\n", hinst, wID, cchBuffer)); 
00072         *lpBuffer = 0;  //NT4, SP6 clears first character
00073         return 0;
00074     }
00075 
00076     p = (LPWSTR)LockResource(LoadResource(hinst, hRes));
00077     if(p) {
00078         string_num = wID & 0x000f;
00079         for (i = 0; i < string_num; i++)
00080                 p += *p + 1;
00081 
00082         if (lpBuffer == NULL) return *p;
00083         i = min(cchBuffer - 1, *p);
00084         if (i > 0) {
00085                 memcpy(lpBuffer, p + 1, i * sizeof (WCHAR));
00086                 lpBuffer[i] = (WCHAR) 0;
00087         }
00088         else {
00089                 if (cchBuffer > 1) {
00090                         lpBuffer[0] = (WCHAR) 0;  //NT4, SP6 clears first character
00091                         return 0;
00092                 }
00093         }
00094     }
00095 
00096     if(i) {
00097          dprintf(("LoadStringW from %X, id %d %ls buffersize %d", hinst, wID, lpBuffer, cchBuffer));
00098     }
00099     else dprintf(("LoadStringW from %X, id %d buffersize %d", hinst, wID, cchBuffer));
00100     return(i);
00101 }
00102 //******************************************************************************
00103 //******************************************************************************
00104 HICON WIN32API LoadIconA(HINSTANCE hinst, LPCSTR lpszIcon)
00105 {
00106     if(HIWORD(lpszIcon)) {
00107          dprintf(("LoadIconA %x %s", hinst, lpszIcon));
00108     }
00109     else dprintf(("LoadIconA %x %x", hinst, lpszIcon));
00110     return LoadImageA(hinst, lpszIcon, IMAGE_ICON, 0, 0, LR_SHARED | LR_DEFAULTSIZE);
00111 }
00112 //******************************************************************************
00113 //******************************************************************************
00114 HICON WIN32API LoadIconW(HINSTANCE hinst, LPCWSTR lpszIcon)
00115 {
00116     if(HIWORD(lpszIcon)) {
00117          dprintf(("LoadIconW %x %ls", hinst, lpszIcon));
00118     }
00119     else dprintf(("LoadIconW %x %x", hinst, lpszIcon));
00120     return LoadImageW(hinst, lpszIcon, IMAGE_ICON, 0, 0, LR_SHARED | LR_DEFAULTSIZE);
00121 }
00122 //******************************************************************************
00123 //******************************************************************************
00124 HCURSOR WIN32API LoadCursorA(HINSTANCE hinst, LPCSTR lpszCursor)
00125 {
00126     return LoadImageA(hinst, lpszCursor, IMAGE_CURSOR, 0, 0,
00127                       LR_SHARED | LR_DEFAULTSIZE );
00128 }
00129 //******************************************************************************
00130 //******************************************************************************
00131 HCURSOR WIN32API LoadCursorW(HINSTANCE hinst, LPCWSTR lpszCursor)
00132 {
00133     return LoadImageW(hinst, lpszCursor, IMAGE_CURSOR, 0, 0,
00134                       LR_SHARED | LR_DEFAULTSIZE );
00135 }
00136 /***********************************************************************
00137 *            LoadCursorFromFileW    (USER32.361)
00138 */
00139 HCURSOR WIN32API LoadCursorFromFileW (LPCWSTR name)
00140 {
00141     return LoadImageW(0, name, IMAGE_CURSOR, 0, 0,
00142                       LR_LOADFROMFILE | LR_DEFAULTSIZE );
00143 }
00144 /***********************************************************************
00145 *            LoadCursorFromFileA    (USER32.360)
00146 */
00147 HCURSOR WIN32API LoadCursorFromFileA (LPCSTR name)
00148 {
00149     return LoadImageA(0, name, IMAGE_CURSOR, 0, 0,
00150                       LR_LOADFROMFILE | LR_DEFAULTSIZE );
00151 }
00152 //******************************************************************************
00153 //NOTE: LR_CREATEDIBSECTION flag doesn't work (crash in GDI32)! (still??)
00154 //******************************************************************************
00155 HANDLE LoadBitmapW(HINSTANCE hinst, LPCWSTR lpszName, int cxDesired, int cyDesired,
00156                    UINT fuLoad)
00157 {
00158     HBITMAP hbitmap = 0;
00159     HDC hdc;
00160     HRSRC hRsrc;
00161     HGLOBAL handle, hMapping = 0;
00162     char *ptr = NULL;
00163     BITMAPINFO *info, *fix_info=NULL;
00164     HGLOBAL hFix;
00165     int size;
00166 
00167     if (!(fuLoad & LR_LOADFROMFILE))
00168     {
00169         handle = 0;
00170         if(!hinst)
00171         {
00172             hRsrc = FindResourceW( hInstanceUser32, lpszName, RT_BITMAPW );
00173             if(hRsrc) {
00174                 handle = LoadResource( hInstanceUser32, hRsrc );
00175             }
00176         }
00177         if(handle == 0)
00178         {
00179             if (!(hRsrc = FindResourceW( hinst, lpszName, RT_BITMAPW ))) return 0;
00180             if (!(handle = LoadResource( hinst, hRsrc ))) return 0;
00181         }
00182 
00183         if ((info = (BITMAPINFO *)LockResource( handle )) == NULL) return 0;
00184     }
00185     else
00186     {
00187         hMapping = VIRTUAL_MapFileW( lpszName, (LPVOID *)&ptr, TRUE);
00188         if (hMapping == INVALID_HANDLE_VALUE) {
00189             //TODO: last err set to ERROR_OPEN_FAILED if file not found; correct??
00190             dprintf(("LoadBitmapW: failed to load file %x (lasterr=%x)", lpszName, GetLastError()));
00191             return 0;
00192         }
00193         info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
00194     }
00195 
00196     //TODO: This has to be removed once pe2lx stores win32 resources!!!
00197     if (info->bmiHeader.biSize != sizeof(BITMAPCOREHEADER) &&
00198         info->bmiHeader.biSize != sizeof(BITMAPINFOHEADER))
00199     {//assume it contains a file header first
00200         info = (BITMAPINFO *)((char *)info + sizeof(BITMAPFILEHEADER));
00201     }
00202 
00203     if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
00204     {//determine size of converted header
00205          BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)info;
00206 
00207          int colors = 0;
00208          if (core->bcBitCount <= 8) {
00209                  colors = (1 << core->bcBitCount);
00210          }
00211          size =  sizeof(BITMAPINFOHEADER) + colors * sizeof(RGBQUAD);
00212     }
00213     else size = DIB_BitmapInfoSize(info, DIB_RGB_COLORS);
00214 
00215     if ((hFix = GlobalAlloc(0, size)) != NULL) fix_info = (BITMAPINFO *)GlobalLock(hFix);
00216     if (fix_info)
00217     {
00218       BYTE pix;
00219 
00220       if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
00221       {//convert old bitmap header to new format
00222             ULONG colors;
00223             ULONG *p, *q;
00224 
00225             memset (fix_info, 0, sizeof (BITMAPINFOHEADER));
00226             fix_info->bmiHeader.biSize     = sizeof (BITMAPINFOHEADER);
00227             fix_info->bmiHeader.biWidth    = ((BITMAPCOREHEADER *)info)->bcWidth;
00228             fix_info->bmiHeader.biHeight   = ((BITMAPCOREHEADER *)info)->bcHeight;
00229             fix_info->bmiHeader.biPlanes   = ((BITMAPCOREHEADER *)info)->bcPlanes;
00230             fix_info->bmiHeader.biBitCount = ((BITMAPCOREHEADER *)info)->bcBitCount;
00231 
00232             if(fix_info->bmiHeader.biBitCount <= 8)
00233             {
00234                 p = (PULONG)((char *)info + sizeof(BITMAPCOREHEADER));
00235                 q = (PULONG)((char *)fix_info + sizeof(BITMAPINFOHEADER));
00236                 //convert RGBTRIPLE to RGBQUAD
00237                 for (colors = 1 << fix_info->bmiHeader.biBitCount; colors > 0; colors--) {
00238                     *q = *p & 0x00FFFFFFUL;
00239                     q++;
00240                     p = (PULONG)((char *)p + sizeof (RGBTRIPLE));
00241                 }
00242             }
00243       }
00244       else {
00245             memcpy(fix_info, info, size);
00246       }
00247 
00248       size = DIB_BitmapInfoSize(info, DIB_RGB_COLORS);
00249 
00250       pix = *((LPBYTE)info + size);
00251       DIB_FixColorsToLoadflags(fix_info, fuLoad, pix);
00252       if ((hdc = CreateCompatibleDC(0)) != 0)
00253       {
00254         char *bits = (char *)info + size;
00255         if (fuLoad & LR_CREATEDIBSECTION) {
00256           DIBSECTION dib;
00257           hbitmap = CreateDIBSection(hdc, fix_info, DIB_RGB_COLORS, NULL, 0, 0);
00258           GetObjectA(hbitmap, sizeof(DIBSECTION), &dib);
00259           SetDIBits(hdc, hbitmap, 0, dib.dsBm.bmHeight, bits, info,
00260                     DIB_RGB_COLORS);
00261         }
00262         else
00263         {
00264 #if 0
00265           if(fix_info->bmiHeader.biBitCount == 1) {
00266               hbitmap = CreateBitmap(fix_info->bmiHeader.biWidth,
00267                                      fix_info->bmiHeader.biHeight,
00268                                      fix_info->bmiHeader.biPlanes,
00269                                      fix_info->bmiHeader.biBitCount,
00270                                      (PVOID)bits);
00271           }
00272           else {
00273 #endif
00274               hbitmap = CreateDIBitmap(hdc, &fix_info->bmiHeader, CBM_INIT,
00275                                        bits, fix_info, DIB_RGB_COLORS );
00276 //          }
00277           if(hbitmap == 0) {
00278               dprintf(("LoadBitmapW: CreateDIBitmap failed!!"));
00279           }
00280         }
00281         DeleteDC(hdc);
00282       }
00283       GlobalUnlock(hFix);
00284       GlobalFree(hFix);
00285     }
00286     if(fuLoad & LR_LOADFROMFILE) {
00287         UnmapViewOfFile(ptr);
00288         CloseHandle(hMapping);
00289     }
00290     return hbitmap;
00291 }
00292 //******************************************************************************
00293 //TODO: scale bitmap
00294 //******************************************************************************
00295 HANDLE CopyBitmap(HANDLE hBitmap, DWORD desiredx, DWORD desiredy)
00296 {
00297     HBITMAP res = 0;
00298     BITMAP bm;
00299 
00300     if(GetObjectA(hBitmap, sizeof(BITMAP), &bm) == FALSE) {
00301         dprintf(("CopyBitmap: GetObject failed!!"));
00302         return 0;
00303     }
00304 
00305     bm.bmBits = NULL;
00306     res = CreateBitmapIndirect(&bm);
00307 
00308     if(res)
00309     {
00310         char *buf = (char *)HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes *
00311                                        bm.bmHeight );
00312         GetBitmapBits (hBitmap, bm.bmWidthBytes * bm.bmHeight, buf);
00313         SetBitmapBits (res, bm.bmWidthBytes * bm.bmHeight, buf);
00314         HeapFree( GetProcessHeap(), 0, buf );
00315     }
00316     return res;
00317 }
00318 //******************************************************************************
00319 //TODO: No support for RT_NEWBITMAP
00320 //******************************************************************************
00321 HBITMAP WIN32API LoadBitmapA(HINSTANCE hinst, LPCSTR lpszBitmap)
00322 {
00323  HBITMAP hBitmap = 0;
00324 
00325   hBitmap = LoadImageA(hinst, lpszBitmap, IMAGE_BITMAP, 0, 0, 0);
00326 
00327   if(HIWORD(lpszBitmap)) {
00328         dprintf(("LoadBitmapA %x %s returned %08xh\n", hinst, lpszBitmap, hBitmap));
00329   }
00330   else  dprintf(("LoadBitmapA %x %x returned %08xh\n", hinst, lpszBitmap, hBitmap));
00331 
00332   return(hBitmap);
00333 }
00334 //******************************************************************************
00335 //TODO: No support for RT_NEWBITMAP
00336 //******************************************************************************
00337 HBITMAP WIN32API LoadBitmapW(HINSTANCE hinst, LPCWSTR lpszBitmap)
00338 {
00339  HBITMAP hBitmap = 0;
00340 
00341   hBitmap = LoadBitmapW((hinst == 0) ? hInstanceUser32:hinst, lpszBitmap, 0, 0, 0);
00342 
00343   if(HIWORD(lpszBitmap)) {
00344         dprintf(("LoadBitmapW %x %ls returned %08xh\n", hinst, lpszBitmap, hBitmap));
00345   }
00346   else  dprintf(("LoadBitmapW %x %x returned %08xh\n", hinst, lpszBitmap, hBitmap));
00347 
00348   return(hBitmap);
00349 }
00350 //******************************************************************************
00351 //******************************************************************************
00352 HANDLE WIN32API LoadImageA(HINSTANCE hinst, LPCSTR lpszName, UINT uType,
00353                            int cxDesired, int cyDesired, UINT fuLoad)
00354 {
00355  HANDLE res = 0;
00356  LPCWSTR u_name;
00357 
00358   if(HIWORD(lpszName)) {
00359         dprintf(("LoadImageA %x %s %d (%d,%d)\n", hinst, lpszName, uType, cxDesired, cyDesired));
00360   }
00361   else  dprintf(("LoadImageA %x %x %d (%d,%d)\n", hinst, lpszName, uType, cxDesired, cyDesired));
00362 
00363   if (HIWORD(lpszName)) {
00364         u_name = HEAP_strdupAtoW(GetProcessHeap(), 0, lpszName);
00365   }
00366   else  u_name=(LPWSTR)lpszName;
00367 
00368   res = LoadImageW(hinst, u_name, uType, cxDesired, cyDesired, fuLoad);
00369 
00370   if (HIWORD(lpszName))
00371         HeapFree(GetProcessHeap(), 0, (LPVOID)u_name);
00372 
00373   return res;
00374 }
00375 //******************************************************************************
00376 //******************************************************************************
00377 HANDLE WIN32API LoadImageW(HINSTANCE hinst, LPCWSTR lpszName, UINT uType,
00378                            int cxDesired, int cyDesired, UINT fuLoad)
00379 {
00380  HANDLE hRet = 0;
00381 
00382   if(HIWORD(lpszName)) {
00383         dprintf(("LoadImageW %x %ls %d (%d,%d)\n", hinst, lpszName, uType, cxDesired, cyDesired));
00384   }
00385   else  dprintf(("LoadImageW %x %x %d (%d,%d)\n", hinst, lpszName, uType, cxDesired, cyDesired));
00386 
00387   if (fuLoad & LR_DEFAULTSIZE) {
00388         if (uType == IMAGE_ICON) {
00389             if (!cxDesired) cxDesired = GetSystemMetrics(SM_CXICON);
00390             if (!cyDesired) cyDesired = GetSystemMetrics(SM_CYICON);
00391         }
00392         else if (uType == IMAGE_CURSOR) {
00393             if (!cxDesired) cxDesired = GetSystemMetrics(SM_CXCURSOR);
00394             if (!cyDesired) cyDesired = GetSystemMetrics(SM_CYCURSOR);
00395         }
00396   }
00397   if (fuLoad & LR_LOADFROMFILE) fuLoad &= ~LR_SHARED;
00398 
00399   switch (uType)
00400   {
00401         case IMAGE_BITMAP:
00402         {
00403             hRet = (HANDLE)LoadBitmapW(hinst, lpszName, cxDesired, cyDesired, fuLoad);
00404             break;
00405         }
00406         case IMAGE_ICON:
00407         {
00408 #ifdef __WIN32OS2__
00409             ULONG palEnts = (1 << ScreenBitsPerPel);
00410 #else
00411             HDC hdc = GetDC(0);
00412             UINT palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
00413             if (palEnts == 0)
00414                 palEnts = 256;
00415             ReleaseDC(0, hdc);
00416 #endif
00417 
00418             hRet = CURSORICON_Load(hinst, lpszName, cxDesired, cyDesired,  palEnts, FALSE, fuLoad);
00419             break;
00420         }
00421 
00422         case IMAGE_CURSOR:
00423             return CURSORICON_Load(hinst, lpszName, cxDesired, cyDesired, 1, TRUE, fuLoad);
00424 
00425         default:
00426             dprintf(("LoadImageW: unsupported type %d!!", uType));
00427             return 0;
00428   }
00429   dprintf(("LoadImageW returned %x\n", (int)hRet));
00430 
00431   return(hRet);
00432 }
00433 /******************************************************************************
00434  * CopyImage32 [USER32.61]  Creates new image and copies attributes to it
00435  *
00436  * PARAMS
00437  *    hnd      [I] Handle to image to copy
00438  *    type     [I] Type of image to copy
00439  *    desiredx [I] Desired width of new image
00440  *    desiredy [I] Desired height of new image
00441  *    flags    [I] Copy flags
00442  *
00443  * RETURNS
00444  *    Success: Handle to newly created image
00445  *    Failure: NULL
00446  *
00447  * FIXME: implementation still lacks nearly all features, see LR_*
00448  * defines in windows.h
00449  *
00450  */
00451 HICON WINAPI CopyImage(HANDLE hnd, UINT type, INT desiredx,
00452                        INT desiredy, UINT flags )
00453 {
00454     dprintf(("CopyImage %x %d (%d,%d) %x", hnd, type, desiredx, desiredy, flags));
00455     switch (type)
00456     {
00457         case IMAGE_BITMAP:
00458                 return CopyBitmap(hnd, desiredx, desiredy);
00459         case IMAGE_ICON:
00460                 return (HANDLE)CURSORICON_ExtCopy(hnd, type, desiredx, desiredy, flags);
00461         case IMAGE_CURSOR:
00462                 /* Should call CURSORICON_ExtCopy but more testing
00463                  * needs to be done before we change this
00464                  */
00465                 return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
00466         default:
00467                 dprintf(("CopyImage: Unsupported type"));
00468     }
00469     return 0;
00470 }
00471 //******************************************************************************
00472 //******************************************************************************

Generated on Wed Jan 23 23:17:34 2002 for ODIN-user32 by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001