Leaked source code of windows server 2003
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.

339 lines
11 KiB

  1. /******************************Module*Header*******************************\
  2. * Module Name: pal.c *
  3. * *
  4. * C/S support for palette routines. *
  5. * *
  6. * Created: 29-May-1991 14:24:06 *
  7. * Author: Eric Kutter [erick] *
  8. * *
  9. * Copyright (c) 1991-1999 Microsoft Corporation *
  10. \**************************************************************************/
  11. #include "precomp.h"
  12. #pragma hdrstop
  13. /**************************************************************************\
  14. * gajFakeHalftone
  15. *
  16. * Copy of the pal666 hardcoded halftone palette from Win9x source code
  17. * (win\core\gdi\palette.asm). Actually, we're hacking a little so
  18. * we'll only use the top and bottom 10 entries.
  19. *
  20. \**************************************************************************/
  21. static const ULONG gaulFakeHalftone[] = {
  22. 0x00000000, // 0 Sys Black gray 0
  23. 0x00000080, // 1 Sys Dk Red
  24. 0x00008000, // 2 Sys Dk Green
  25. 0x00008080, // 3 Sys Dk Yellow
  26. 0x00800000, // 4 Sys Dk Blue
  27. 0x00800080, // 5 Sys Dk Violet
  28. 0x00808000, // 6 Sys Dk Cyan
  29. 0x00c0c0c0, // 7 Sys Lt Grey gray 192
  30. 0x00c0dcc0, // 8 Sys 8
  31. 0x00f0caa6, // 9 Sys 9 (the first 10 are fixed by Windows)
  32. 0x00f0fbff, // 246 Sys Reserved
  33. 0x00a4a0a0, // 247 Sys Reserved
  34. 0x00808080, // 248 Sys Lt Gray gray 128
  35. 0x000000ff, // 249 Sys Red
  36. 0x0000ff00, // 250 Sys Green
  37. 0x0000ffff, // 251 Sys Yellow
  38. 0x00ff0000, // 252 Sys Blue
  39. 0x00ff00ff, // 253 Sys Violet
  40. 0x00ffff00, // 254 Sys Cyan
  41. 0x00ffffff // 255 Sys White gray 255
  42. };
  43. /******************************Public*Routine******************************\
  44. * AnimatePalette *
  45. * SetPaletteEntries *
  46. * GetPaletteEntries *
  47. * GetSystemPaletteEntries *
  48. * SetDIBColorTable *
  49. * GetDIBColorTable *
  50. * *
  51. * These entry points just pass the call on to DoPalette. *
  52. * *
  53. * Warning: *
  54. * The pv field of a palette's LHE is used to determine if a palette *
  55. * has been modified since it was last realized. SetPaletteEntries *
  56. * and ResizePalette will increment this field after they have *
  57. * modified the palette. It is only updated for metafiled palettes *
  58. * *
  59. * *
  60. * History: *
  61. * Thu 20-Jun-1991 00:46:15 -by- Charles Whitmer [chuckwh] *
  62. * Added handle translation. (And filled in the comment block.) *
  63. * *
  64. * 29-May-1991 -by- Eric Kutter [erick] *
  65. * Wrote it. *
  66. \**************************************************************************/
  67. BOOL WINAPI AnimatePalette
  68. (
  69. HPALETTE hpal,
  70. UINT iStart,
  71. UINT cEntries,
  72. CONST PALETTEENTRY *pPalEntries
  73. )
  74. {
  75. FIXUP_HANDLE(hpal);
  76. // Inform the 16-bit metafile if it knows this object.
  77. // This is not recorded by the 32-bit metafiles.
  78. if (pmetalink16Get(hpal))
  79. if (!MF16_AnimatePalette(hpal, iStart, cEntries, pPalEntries))
  80. return(FALSE);
  81. return
  82. !!NtGdiDoPalette
  83. (
  84. hpal,
  85. (WORD)iStart,
  86. (WORD)cEntries,
  87. (PALETTEENTRY*)pPalEntries,
  88. I_ANIMATEPALETTE,
  89. TRUE
  90. );
  91. }
  92. UINT WINAPI SetPaletteEntries
  93. (
  94. HPALETTE hpal,
  95. UINT iStart,
  96. UINT cEntries,
  97. CONST PALETTEENTRY *pPalEntries
  98. )
  99. {
  100. PMETALINK16 pml16;
  101. FIXUP_HANDLE(hpal);
  102. // Inform the metafile if it knows this object.
  103. if (pml16 = pmetalink16Get(hpal))
  104. {
  105. if (!MF_SetPaletteEntries(hpal, iStart, cEntries, pPalEntries))
  106. return(0);
  107. // Mark the palette as changed (for 16-bit metafile tracking)
  108. pml16->pv = (PVOID)(((ULONG_PTR)pml16->pv)++);
  109. }
  110. return
  111. NtGdiDoPalette
  112. (
  113. hpal,
  114. (WORD)iStart,
  115. (WORD)cEntries,
  116. (PALETTEENTRY*)pPalEntries,
  117. I_SETPALETTEENTRIES,
  118. TRUE
  119. );
  120. }
  121. UINT WINAPI GetPaletteEntries
  122. (
  123. HPALETTE hpal,
  124. UINT iStart,
  125. UINT cEntries,
  126. LPPALETTEENTRY pPalEntries
  127. )
  128. {
  129. FIXUP_HANDLE(hpal);
  130. return
  131. NtGdiDoPalette
  132. (
  133. hpal,
  134. (WORD)iStart,
  135. (WORD)cEntries,
  136. pPalEntries,
  137. I_GETPALETTEENTRIES,
  138. FALSE
  139. );
  140. }
  141. UINT WINAPI GetSystemPaletteEntries
  142. (
  143. HDC hdc,
  144. UINT iStart,
  145. UINT cEntries,
  146. LPPALETTEENTRY pPalEntries
  147. )
  148. {
  149. LONG lRet = 0;
  150. FIXUP_HANDLE(hdc);
  151. //
  152. // There's an app out there that sometimes calls us with a -1
  153. // and then whines that we overwrote some of its memory. Win9x clamps
  154. // this value, so we can too.
  155. //
  156. if ((LONG)cEntries < 0)
  157. return (UINT) lRet;
  158. //
  159. // GreGetSystemPaletteEntries will only succeed on palettized devices.
  160. //
  161. if (GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)
  162. {
  163. lRet =
  164. NtGdiDoPalette
  165. (
  166. (HPALETTE) hdc,
  167. (WORD)iStart,
  168. (WORD)cEntries,
  169. pPalEntries,
  170. I_GETSYSTEMPALETTEENTRIES,
  171. FALSE
  172. );
  173. }
  174. else
  175. {
  176. //
  177. // Win9x compatibility: Unlike NT, GetSystemPaletteEntries does
  178. // not fail on non-palettized devices, it returns the halftone
  179. // palette (hardcoded in win\core\gdi\palette.asm in the
  180. // Win9x source code).
  181. //
  182. // However, Macromedia Directory (which is used by Encarta 99)
  183. // relies on GetSystemPaletteEntries failing on NT. Luckily, the
  184. // only apps found so far that rely on GetSystemPaletteEntries
  185. // returning the halftone palette on non-palettized devices
  186. // also ignore the return value. This makes sense in that any
  187. // app that *did* check the return value also would likely have
  188. // code to handle the failure in the first place.
  189. //
  190. // So, attemp to satisfy both camps by filling in the return
  191. // buffer *and* returning failure for this case.
  192. //
  193. if (pPalEntries != NULL)
  194. {
  195. ULONG aulFake[256];
  196. UINT uiNumCopy;
  197. //
  198. // More cheating: to avoid having to have the whole fake
  199. // halftone palette taking up space in our binary (even if
  200. // it is const data), we can get away with just returning
  201. // the first and last 10 since the apps that use this on
  202. // non-palettized displays really just want the 20 system
  203. // colors and will fill in the middle 236 with their own data.
  204. //
  205. // Also, it's less code to waste 40 bytes in const data than
  206. // to fetch the default palette and split it into into the top
  207. // and bottom halves (not to mention that we don't want the
  208. // real magic colors in 8, 9, 246, and 247). This is also
  209. // the same motivation for creating the aulFake array then
  210. // copying it into the return buffer. Not worth the extra code
  211. // to handle copying directly into return buffer.
  212. //
  213. RtlCopyMemory(&aulFake[0], &gaulFakeHalftone[0], 10*sizeof(ULONG));
  214. RtlCopyMemory(&aulFake[246], &gaulFakeHalftone[10], 10*sizeof(ULONG));
  215. RtlZeroMemory(&aulFake[10], 236*sizeof(ULONG));
  216. //
  217. // Copy requested portion of palette.
  218. //
  219. if (iStart < 256)
  220. {
  221. uiNumCopy = min((256 - iStart), cEntries);
  222. RtlCopyMemory(pPalEntries, &aulFake[iStart],
  223. uiNumCopy * sizeof(ULONG));
  224. }
  225. //
  226. // Want to return failure, so *do not* set lRet to non-zero.
  227. //
  228. }
  229. }
  230. return (UINT) lRet;
  231. }
  232. /******************************Public*Routine******************************\
  233. * GetDIBColorTable
  234. *
  235. * Get the color table of the DIB section currently selected into the
  236. * given hdc. If the surface is not a DIB section, this function
  237. * will fail.
  238. *
  239. * History:
  240. *
  241. * 03-Sep-1993 -by- Wendy Wu [wendywu]
  242. * Wrote it.
  243. \**************************************************************************/
  244. UINT WINAPI GetDIBColorTable
  245. (
  246. HDC hdc,
  247. UINT iStart,
  248. UINT cEntries,
  249. RGBQUAD *prgbq
  250. )
  251. {
  252. FIXUP_HANDLE(hdc);
  253. if (cEntries == 0)
  254. return(0);
  255. return
  256. NtGdiDoPalette
  257. (
  258. (HPALETTE) hdc,
  259. (WORD)iStart,
  260. (WORD)cEntries,
  261. (PALETTEENTRY *)prgbq,
  262. I_GETDIBCOLORTABLE,
  263. FALSE
  264. );
  265. }
  266. /******************************Public*Routine******************************\
  267. * SetDIBColorTable
  268. *
  269. * Set the color table of the DIB section currently selected into the
  270. * given hdc. If the surface is not a DIB section, this function
  271. * will fail.
  272. *
  273. * History:
  274. *
  275. * 03-Sep-1993 -by- Wendy Wu [wendywu]
  276. * Wrote it.
  277. \**************************************************************************/
  278. UINT WINAPI SetDIBColorTable
  279. (
  280. HDC hdc,
  281. UINT iStart,
  282. UINT cEntries,
  283. CONST RGBQUAD *prgbq
  284. )
  285. {
  286. FIXUP_HANDLE(hdc);
  287. if (cEntries == 0)
  288. return(0);
  289. return( NtGdiDoPalette(
  290. (HPALETTE) hdc,
  291. (WORD)iStart,
  292. (WORD)cEntries,
  293. (PALETTEENTRY *)prgbq,
  294. I_SETDIBCOLORTABLE,
  295. TRUE));
  296. }