Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

316 lines
9.2 KiB

  1. /****************************************************************************/
  2. /* */
  3. /* RMCREATE.C - */
  4. /* */
  5. /* Resource creating Routines. */
  6. /* */
  7. /****************************************************************************/
  8. #define RESOURCESTRINGS
  9. #include "user.h"
  10. #ifdef NOT_USED_ANYMORE
  11. #define NSMOVE 0x0010
  12. HGLOBAL FAR PASCAL DirectResAlloc(HGLOBAL, WORD, WORD);
  13. /******************************************************************************
  14. **
  15. ** CreateCursorIconIndirect()
  16. **
  17. ** This is the common function called by CreateCursor and
  18. ** CreateIcon()
  19. ** DirectResAlloc() is called instead of GlobalAlloc() because
  20. ** the Icons/Cursors created by one instance of the app can be used in
  21. ** a WNDCLASS structure to Register a class which will be used by other
  22. ** instances of the app and when the instance that created the icons/
  23. ** cursors terminates, the resources SHOULD NOT BE FREED; If GlobalAlloc()
  24. ** is used this is what will happen; At the same time, when the last
  25. ** instance also dies, the memory SHOULD BE FREED; To achieve this,
  26. ** DirectResAlloc() is used instead of GlobalAlloc();
  27. **
  28. ******************************************************************************/
  29. HGLOBAL CALLBACK CreateCursorIconIndirect(HINSTANCE hInstance,
  30. LPCURSORSHAPE lpHeader,
  31. CONST VOID FAR* lpANDplane,
  32. CONST VOID FAR* lpXORplane)
  33. {
  34. register WORD ANDmaskSize;
  35. register WORD XORmaskSize;
  36. WORD wTotalSize;
  37. HRSRC hResource;
  38. LPSTR lpRes;
  39. ANDmaskSize = lpHeader -> cbWidth * lpHeader -> cy;
  40. XORmaskSize = (((lpHeader -> cx * lpHeader -> BitsPixel + 0x0F) & ~0x0F)
  41. >> 3) * lpHeader -> cy * lpHeader -> Planes;
  42. /* It is assumed that Cursor/Icon size won't be more than 64K */
  43. wTotalSize = sizeof(CURSORSHAPE) + ANDmaskSize + XORmaskSize;
  44. #ifdef NEVER
  45. /* Allocate the required memory */
  46. if((hResource = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT | GMEM_SHARE,
  47. (DWORD)wTotalSize)) == NULL)
  48. return(NULL);
  49. #else
  50. /* Let us preserve the long pointers */
  51. SwapHandle(&lpANDplane);
  52. SwapHandle(&lpXORplane);
  53. hResource = DirectResAlloc(hInstance, NSMOVE, wTotalSize);
  54. /* Let us restore the long pointers */
  55. SwapHandle(&lpANDplane);
  56. SwapHandle(&lpXORplane);
  57. if(hResource == NULL)
  58. return(NULL);
  59. #endif
  60. if(!(lpRes = GlobalLock(hResource)))
  61. {
  62. GlobalFree(hResource);
  63. return(NULL);
  64. }
  65. LCopyStruct((LPSTR)lpHeader, lpRes, sizeof(CURSORSHAPE));
  66. lpRes += sizeof(CURSORSHAPE);
  67. LCopyStruct(lpANDplane, lpRes, ANDmaskSize);
  68. lpRes += ANDmaskSize;
  69. LCopyStruct(lpXORplane, lpRes, XORmaskSize);
  70. GlobalUnlock(hResource);
  71. return(hResource);
  72. }
  73. /******************************************************************************
  74. **
  75. ** CreateCursor()
  76. **
  77. ** This is the API call to create a Cursor "on-the-fly";
  78. **
  79. *******************************************************************************/
  80. HCURSOR API ICreateCursor(hInstance, iXhotspot, iYhotspot, iWidth,
  81. iHeight, lpANDplane, lpXORplane)
  82. HINSTANCE hInstance;
  83. int iXhotspot;
  84. int iYhotspot;
  85. int iWidth;
  86. int iHeight;
  87. CONST VOID FAR* lpANDplane;
  88. CONST VOID FAR* lpXORplane;
  89. {
  90. CURSORSHAPE Header;
  91. Header.xHotSpot = iXhotspot;
  92. Header.yHotSpot = iYhotspot;
  93. Header.cx = iWidth;
  94. Header.cy = iHeight;
  95. Header.Planes = 1; /* Cursors are only monochrome */
  96. Header.BitsPixel = 1;
  97. Header.cbWidth = ((iWidth + 0x0F) & ~0x0F) >> 3;
  98. return(CreateCursorIconIndirect(hInstance, &Header,
  99. lpANDplane, lpXORplane));
  100. }
  101. /******************************************************************************
  102. **
  103. ** CreateIcon()
  104. **
  105. ** This is the API call to create an Icon "on-the-fly";
  106. **
  107. *******************************************************************************/
  108. HICON API ICreateIcon(hInstance, iWidth, iHeight, bPlanes,
  109. bBitsPixel, lpANDplane, lpXORplane)
  110. HINSTANCE hInstance;
  111. int iWidth;
  112. int iHeight;
  113. BYTE bPlanes;
  114. BYTE bBitsPixel;
  115. CONST VOID FAR* lpANDplane;
  116. CONST VOID FAR* lpXORplane;
  117. {
  118. CURSORSHAPE Header;
  119. Header.xHotSpot = iWidth/2;
  120. Header.yHotSpot = iHeight/2;
  121. Header.cx = iWidth;
  122. Header.cy = iHeight;
  123. Header.Planes = bPlanes; /* Icons can be in color */
  124. Header.BitsPixel = bBitsPixel;
  125. Header.cbWidth = ((iWidth + 0x0F) & ~0x0F) >> 3;
  126. return(CreateCursorIconIndirect(hInstance, (LPCURSORSHAPE)&Header,
  127. lpANDplane, lpXORplane));
  128. }
  129. /******************************************************************************
  130. *
  131. * DestroyIcon(hIcon)
  132. * This can be called to delete only those icons created "on the fly"
  133. * using the CreateIcon() function
  134. * Returns:
  135. * TRUE if successful, FALSE otherwise.
  136. *
  137. ******************************************************************************/
  138. BOOL API IDestroyIcon(HICON hIcon)
  139. {
  140. return(!FreeResource(hIcon));
  141. }
  142. /******************************************************************************
  143. *
  144. * DestroyCursor(hIcon)
  145. * This can be called to delete only those icons created "on the fly"
  146. * using the CreateIcon() function
  147. * Returns:
  148. * TRUE if successful, FALSE otherwise.
  149. *
  150. ******************************************************************************/
  151. BOOL API IDestroyCursor(HCURSOR hCursor)
  152. {
  153. if (hCursor == hCurCursor)
  154. {
  155. /* #12068: if currently selected cursor resore arrow cursor and RIP [lalithar] */
  156. SetCursor(hCursNormal);
  157. DebugErr(DBF_ERROR, "DestroyCursor: Destroying current cursor");
  158. }
  159. return(!FreeResource(hCursor));
  160. }
  161. #endif /* NOT_USED_ANYMORE */
  162. /****************************************************************************
  163. **
  164. ** DumpIcon()
  165. **
  166. ** This function is called to get the details of a given Icon;
  167. **
  168. ** The caller must lock hIcon using LockResource() and pass the pointer
  169. ** thro lpIcon; This is the pointer to the header structure;
  170. ** Thro lpHeaderSize, the size of header is returned;
  171. ** Thro lplpANDplane and lplpXORplane pointers to actual bit info is
  172. ** returned;
  173. ** This function returns a DWORD with the size of AND plane in loword
  174. ** and size of XOR plane in hiword;
  175. **
  176. ****************************************************************************/
  177. DWORD CALLBACK DumpIcon(LPSTR lpIcon,
  178. WORD FAR * lpHeaderSize,
  179. LPSTR FAR * lplpANDplane,
  180. LPSTR FAR * lplpXORplane)
  181. {
  182. register WORD ANDmaskSize;
  183. register WORD XORmaskSize;
  184. LPCURSORSHAPE lpHeader;
  185. *lpHeaderSize = sizeof(CURSORSHAPE);
  186. if(!lpIcon)
  187. return((DWORD)0);
  188. lpHeader = (LPCURSORSHAPE)lpIcon;
  189. ANDmaskSize = lpHeader -> cbWidth * lpHeader -> cy;
  190. XORmaskSize = (((lpHeader -> cx * lpHeader -> BitsPixel + 0x0F) & ~0x0F)
  191. >> 3) * lpHeader -> cy * lpHeader -> Planes;
  192. *lplpANDplane = (lpIcon += sizeof(CURSORSHAPE));
  193. *lplpXORplane = (lpIcon + ANDmaskSize);
  194. return(MAKELONG(ANDmaskSize, XORmaskSize));
  195. }
  196. #ifdef NOT_USED_ANYMORE
  197. /****************************************************************************
  198. **
  199. ** GetInternalIconHeader(lpIcon, lpDestBuff)
  200. **
  201. ** This function has been added to fix bug #6351 with cornerstone
  202. ** XTRA_LARGE display driver. (It uses 64 X 64 Icons; Internally we
  203. ** keep the size as 32 X 32. Progman must know this internal size sothat
  204. ** it can tell that to WinOldApp.
  205. ****************************************************************************/
  206. void API IGetInternalIconHeader(LPSTR lpIcon, LPSTR lpDestBuff)
  207. {
  208. LCopyStruct(lpIcon, lpDestBuff, sizeof(CURSORSHAPE));
  209. }
  210. #endif /* NOT_USED_ANYMORE */
  211. /* APIs to make a copy of an icon or cursor */
  212. HICON API ICopyIcon(HINSTANCE hInstance, HICON hIcon)
  213. {
  214. LPSTR lpAND;
  215. LPSTR lpXOR;
  216. LPSTR lpIcon;
  217. WORD wHeaderSize;
  218. HICON hIconCopy;
  219. LPCURSORSHAPE lpHeader;
  220. lpIcon = LockResource(hIcon);
  221. if (!lpIcon)
  222. return NULL;
  223. lpHeader = (LPCURSORSHAPE)lpIcon;
  224. DumpIcon(lpIcon, &wHeaderSize, &lpAND, &lpXOR);
  225. hIconCopy = CreateIcon(hInstance,
  226. lpHeader->cx,
  227. lpHeader->cy,
  228. lpHeader->Planes,
  229. lpHeader->BitsPixel,
  230. lpAND, lpXOR);
  231. UnlockResource(hIcon);
  232. return(hIconCopy);
  233. }
  234. HCURSOR API ICopyCursor(HINSTANCE hInstance, HICON hCursor)
  235. {
  236. LPSTR lpAND;
  237. LPSTR lpXOR;
  238. LPSTR lpCursor;
  239. WORD wHeaderSize;
  240. HCURSOR hCursorCopy;
  241. LPCURSORSHAPE lpHeader;
  242. lpCursor = LockResource(hCursor);
  243. if (!lpCursor)
  244. return NULL;
  245. lpHeader = (LPCURSORSHAPE)lpCursor;
  246. DumpIcon(lpCursor, &wHeaderSize, &lpAND, &lpXOR);
  247. hCursorCopy = CreateCursor(hInstance,
  248. lpHeader->xHotSpot,
  249. lpHeader->yHotSpot,
  250. lpHeader->cx,
  251. lpHeader->cy,
  252. lpAND, lpXOR);
  253. UnlockResource(hCursor);
  254. return(hCursorCopy);
  255. }