/private/inet/mshtml/src/core/cdutil/color.cxx

https://github.com/pustladi/Windows-2000 · C++ · 910 lines · 709 code · 110 blank · 91 comment · 80 complexity · 6afab77da7d27831caa9b1600fa103fe MD5 · raw file

  1. //+------------------------------------------------------------------------
  2. //
  3. // File: color.cxx
  4. //
  5. // Contents: FormsTranslateColor
  6. //
  7. //-------------------------------------------------------------------------
  8. #include "headers.hxx"
  9. MtDefine(CColorInfo, Utilities, "CColorInfo")
  10. COLORREF g_acrSysColor[25];
  11. BOOL g_fSysColorInit = FALSE;
  12. COLORREF g_crPaletteRelative = 0;
  13. HPALETTE g_hpalHalftone = NULL;
  14. HDC g_hdcMem1 = NULL;
  15. HDC g_hdcMem2 = NULL;
  16. HDC g_hdcMem3 = NULL;
  17. BOOL g_fNoDisplayChange = TRUE;
  18. #if DBG==1
  19. int g_cGetMemDc = 0;
  20. DeclareTag(tagDCTrace, "Color", "Trace DCs")
  21. #endif
  22. #ifndef _MAC
  23. // Inverse color table
  24. BYTE * g_pInvCMAP = NULL;
  25. #endif
  26. RGBQUAD g_rgbHalftone[256];
  27. #if !defined(WIN16) && !defined(_MAC)
  28. LOGPAL256 g_lpHalftone;
  29. #else
  30. //Win16 only
  31. LOGPAL256 g_lpHalftone =
  32. {
  33. 0x0300, 256,
  34. {
  35. { 0x00, 0x00, 0x00, 0x00 },
  36. { 0x80, 0x00, 0x00, 0x00 },
  37. { 0x00, 0x80, 0x00, 0x00 },
  38. { 0x80, 0x80, 0x00, 0x00 },
  39. { 0x00, 0x00, 0x80, 0x00 },
  40. { 0x80, 0x00, 0x80, 0x00 },
  41. { 0x00, 0x80, 0x80, 0x00 },
  42. { 0xC0, 0xC0, 0xC0, 0x00 },
  43. { 0xC0, 0xDC, 0xC0, PC_NOCOLLAPSE },
  44. { 0xA6, 0xCA, 0xF0, PC_NOCOLLAPSE },
  45. { 0xCC, 0x33, 0x00, PC_NOCOLLAPSE },
  46. { 0xFF, 0x33, 0x00, PC_NOCOLLAPSE },
  47. { 0x00, 0x66, 0x00, PC_NOCOLLAPSE },
  48. { 0x33, 0x66, 0x00, PC_NOCOLLAPSE },
  49. { 0x66, 0x66, 0x00, PC_NOCOLLAPSE },
  50. { 0x99, 0x66, 0x00, PC_NOCOLLAPSE },
  51. { 0xCC, 0x66, 0x00, PC_NOCOLLAPSE },
  52. { 0xFF, 0x66, 0x00, PC_NOCOLLAPSE },
  53. { 0x00, 0x99, 0x00, PC_NOCOLLAPSE },
  54. { 0x33, 0x99, 0x00, PC_NOCOLLAPSE },
  55. { 0x66, 0x99, 0x00, PC_NOCOLLAPSE },
  56. { 0x99, 0x99, 0x00, PC_NOCOLLAPSE },
  57. { 0xCC, 0x99, 0x00, PC_NOCOLLAPSE },
  58. { 0xFF, 0x99, 0x00, PC_NOCOLLAPSE },
  59. { 0x00, 0xCC, 0x00, PC_NOCOLLAPSE },
  60. { 0x33, 0xCC, 0x00, PC_NOCOLLAPSE },
  61. { 0x66, 0xCC, 0x00, PC_NOCOLLAPSE },
  62. { 0x99, 0xCC, 0x00, PC_NOCOLLAPSE },
  63. { 0xCC, 0xCC, 0x00, PC_NOCOLLAPSE },
  64. { 0xFF, 0xCC, 0x00, PC_NOCOLLAPSE },
  65. { 0xEF, 0xD6, 0xC6, PC_NOCOLLAPSE },
  66. { 0xAD, 0xA9, 0x90, PC_NOCOLLAPSE },
  67. { 0x66, 0xFF, 0x00, PC_NOCOLLAPSE },
  68. { 0x99, 0xFF, 0x00, PC_NOCOLLAPSE },
  69. { 0xCC, 0xFF, 0x00, PC_NOCOLLAPSE },
  70. { 0x66, 0x33, 0x00, PC_NOCOLLAPSE },
  71. { 0x00, 0x00, 0x33, PC_NOCOLLAPSE },
  72. { 0x33, 0x00, 0x33, PC_NOCOLLAPSE },
  73. { 0x66, 0x00, 0x33, PC_NOCOLLAPSE },
  74. { 0x99, 0x00, 0x33, PC_NOCOLLAPSE },
  75. { 0xCC, 0x00, 0x33, PC_NOCOLLAPSE },
  76. { 0xFF, 0x00, 0x33, PC_NOCOLLAPSE },
  77. { 0x00, 0x33, 0x33, PC_NOCOLLAPSE },
  78. { 0x33, 0x33, 0x33, PC_NOCOLLAPSE },
  79. { 0x66, 0x33, 0x33, PC_NOCOLLAPSE },
  80. { 0x99, 0x33, 0x33, PC_NOCOLLAPSE },
  81. { 0xCC, 0x33, 0x33, PC_NOCOLLAPSE },
  82. { 0xFF, 0x33, 0x33, PC_NOCOLLAPSE },
  83. { 0x00, 0x66, 0x33, PC_NOCOLLAPSE },
  84. { 0x33, 0x66, 0x33, PC_NOCOLLAPSE },
  85. { 0x66, 0x66, 0x33, PC_NOCOLLAPSE },
  86. { 0x99, 0x66, 0x33, PC_NOCOLLAPSE },
  87. { 0xCC, 0x66, 0x33, PC_NOCOLLAPSE },
  88. { 0xFF, 0x66, 0x33, PC_NOCOLLAPSE },
  89. { 0x00, 0x99, 0x33, PC_NOCOLLAPSE },
  90. { 0x33, 0x99, 0x33, PC_NOCOLLAPSE },
  91. { 0x66, 0x99, 0x33, PC_NOCOLLAPSE },
  92. { 0x99, 0x99, 0x33, PC_NOCOLLAPSE },
  93. { 0xCC, 0x99, 0x33, PC_NOCOLLAPSE },
  94. { 0xFF, 0x99, 0x33, PC_NOCOLLAPSE },
  95. { 0x00, 0xCC, 0x33, PC_NOCOLLAPSE },
  96. { 0x33, 0xCC, 0x33, PC_NOCOLLAPSE },
  97. { 0x66, 0xCC, 0x33, PC_NOCOLLAPSE },
  98. { 0x99, 0xCC, 0x33, PC_NOCOLLAPSE },
  99. { 0xCC, 0xCC, 0x33, PC_NOCOLLAPSE },
  100. { 0xFF, 0xCC, 0x33, PC_NOCOLLAPSE },
  101. { 0x00, 0x33, 0x00, PC_NOCOLLAPSE },
  102. { 0x33, 0xFF, 0x33, PC_NOCOLLAPSE },
  103. { 0x66, 0xFF, 0x33, PC_NOCOLLAPSE },
  104. { 0x99, 0xFF, 0x33, PC_NOCOLLAPSE },
  105. { 0xCC, 0xFF, 0x33, PC_NOCOLLAPSE },
  106. { 0xFF, 0xFF, 0x33, PC_NOCOLLAPSE },
  107. { 0x00, 0x00, 0x66, PC_NOCOLLAPSE },
  108. { 0x33, 0x00, 0x66, PC_NOCOLLAPSE },
  109. { 0x66, 0x00, 0x66, PC_NOCOLLAPSE },
  110. { 0x99, 0x00, 0x66, PC_NOCOLLAPSE },
  111. { 0xCC, 0x00, 0x66, PC_NOCOLLAPSE },
  112. { 0xFF, 0x00, 0x66, PC_NOCOLLAPSE },
  113. { 0x00, 0x33, 0x66, PC_NOCOLLAPSE },
  114. { 0x33, 0x33, 0x66, PC_NOCOLLAPSE },
  115. { 0x66, 0x33, 0x66, PC_NOCOLLAPSE },
  116. { 0x99, 0x33, 0x66, PC_NOCOLLAPSE },
  117. { 0xCC, 0x33, 0x66, PC_NOCOLLAPSE },
  118. { 0xFF, 0x33, 0x66, PC_NOCOLLAPSE },
  119. { 0x00, 0x66, 0x66, PC_NOCOLLAPSE },
  120. { 0x33, 0x66, 0x66, PC_NOCOLLAPSE },
  121. { 0x66, 0x66, 0x66, PC_NOCOLLAPSE },
  122. { 0x99, 0x66, 0x66, PC_NOCOLLAPSE },
  123. { 0xCC, 0x66, 0x66, PC_NOCOLLAPSE },
  124. { 0xFF, 0x66, 0x66, PC_NOCOLLAPSE },
  125. { 0x00, 0x99, 0x66, PC_NOCOLLAPSE },
  126. { 0x33, 0x99, 0x66, PC_NOCOLLAPSE },
  127. { 0x66, 0x99, 0x66, PC_NOCOLLAPSE },
  128. { 0x99, 0x99, 0x66, PC_NOCOLLAPSE },
  129. { 0xCC, 0x99, 0x66, PC_NOCOLLAPSE },
  130. { 0xFF, 0x99, 0x66, PC_NOCOLLAPSE },
  131. { 0x00, 0xCC, 0x66, PC_NOCOLLAPSE },
  132. { 0x33, 0xCC, 0x66, PC_NOCOLLAPSE },
  133. { 0x66, 0xCC, 0x66, PC_NOCOLLAPSE },
  134. { 0x99, 0xCC, 0x66, PC_NOCOLLAPSE },
  135. { 0xCC, 0xCC, 0x66, PC_NOCOLLAPSE },
  136. { 0xFF, 0xCC, 0x66, PC_NOCOLLAPSE },
  137. { 0x00, 0xFF, 0x66, PC_NOCOLLAPSE },
  138. { 0x33, 0xFF, 0x66, PC_NOCOLLAPSE },
  139. { 0x66, 0xFF, 0x66, PC_NOCOLLAPSE },
  140. { 0x99, 0xFF, 0x66, PC_NOCOLLAPSE },
  141. { 0xCC, 0xFF, 0x66, PC_NOCOLLAPSE },
  142. { 0xFF, 0xFF, 0x66, PC_NOCOLLAPSE },
  143. { 0x00, 0x00, 0x99, PC_NOCOLLAPSE },
  144. { 0x33, 0x00, 0x99, PC_NOCOLLAPSE },
  145. { 0x66, 0x00, 0x99, PC_NOCOLLAPSE },
  146. { 0x99, 0x00, 0x99, PC_NOCOLLAPSE },
  147. { 0xCC, 0x00, 0x99, PC_NOCOLLAPSE },
  148. { 0xFF, 0x00, 0x99, PC_NOCOLLAPSE },
  149. { 0x00, 0x33, 0x99, PC_NOCOLLAPSE },
  150. { 0x33, 0x33, 0x99, PC_NOCOLLAPSE },
  151. { 0x66, 0x33, 0x99, PC_NOCOLLAPSE },
  152. { 0x99, 0x33, 0x99, PC_NOCOLLAPSE },
  153. { 0xCC, 0x33, 0x99, PC_NOCOLLAPSE },
  154. { 0xFF, 0x33, 0x99, PC_NOCOLLAPSE },
  155. { 0x00, 0x66, 0x99, PC_NOCOLLAPSE },
  156. { 0x33, 0x66, 0x99, PC_NOCOLLAPSE },
  157. { 0x66, 0x66, 0x99, PC_NOCOLLAPSE },
  158. { 0x99, 0x66, 0x99, PC_NOCOLLAPSE },
  159. { 0xCC, 0x66, 0x99, PC_NOCOLLAPSE },
  160. { 0xFF, 0x66, 0x99, PC_NOCOLLAPSE },
  161. { 0x00, 0x99, 0x99, PC_NOCOLLAPSE },
  162. { 0x33, 0x99, 0x99, PC_NOCOLLAPSE },
  163. { 0x66, 0x99, 0x99, PC_NOCOLLAPSE },
  164. { 0x99, 0x99, 0x99, PC_NOCOLLAPSE },
  165. { 0xCC, 0x99, 0x99, PC_NOCOLLAPSE },
  166. { 0xFF, 0x99, 0x99, PC_NOCOLLAPSE },
  167. { 0x00, 0xCC, 0x99, PC_NOCOLLAPSE },
  168. { 0x33, 0xCC, 0x99, PC_NOCOLLAPSE },
  169. { 0x66, 0xCC, 0x99, PC_NOCOLLAPSE },
  170. { 0x99, 0xCC, 0x99, PC_NOCOLLAPSE },
  171. { 0xCC, 0xCC, 0x99, PC_NOCOLLAPSE },
  172. { 0xFF, 0xCC, 0x99, PC_NOCOLLAPSE },
  173. { 0x00, 0xFF, 0x99, PC_NOCOLLAPSE },
  174. { 0x33, 0xFF, 0x99, PC_NOCOLLAPSE },
  175. { 0x66, 0xFF, 0x99, PC_NOCOLLAPSE },
  176. { 0x99, 0xFF, 0x99, PC_NOCOLLAPSE },
  177. { 0xCC, 0xFF, 0x99, PC_NOCOLLAPSE },
  178. { 0xFF, 0xFF, 0x99, PC_NOCOLLAPSE },
  179. { 0x00, 0x00, 0xCC, PC_NOCOLLAPSE },
  180. { 0x33, 0x00, 0xCC, PC_NOCOLLAPSE },
  181. { 0x66, 0x00, 0xCC, PC_NOCOLLAPSE },
  182. { 0x99, 0x00, 0xCC, PC_NOCOLLAPSE },
  183. { 0xCC, 0x00, 0xCC, PC_NOCOLLAPSE },
  184. { 0xFF, 0x00, 0xCC, PC_NOCOLLAPSE },
  185. { 0x00, 0x33, 0xCC, PC_NOCOLLAPSE },
  186. { 0x33, 0x33, 0xCC, PC_NOCOLLAPSE },
  187. { 0x66, 0x33, 0xCC, PC_NOCOLLAPSE },
  188. { 0x99, 0x33, 0xCC, PC_NOCOLLAPSE },
  189. { 0xCC, 0x33, 0xCC, PC_NOCOLLAPSE },
  190. { 0xFF, 0x33, 0xCC, PC_NOCOLLAPSE },
  191. { 0x00, 0x66, 0xCC, PC_NOCOLLAPSE },
  192. { 0x33, 0x66, 0xCC, PC_NOCOLLAPSE },
  193. { 0x66, 0x66, 0xCC, PC_NOCOLLAPSE },
  194. { 0x99, 0x66, 0xCC, PC_NOCOLLAPSE },
  195. { 0xCC, 0x66, 0xCC, PC_NOCOLLAPSE },
  196. { 0xFF, 0x66, 0xCC, PC_NOCOLLAPSE },
  197. { 0x00, 0x99, 0xCC, PC_NOCOLLAPSE },
  198. { 0x33, 0x99, 0xCC, PC_NOCOLLAPSE },
  199. { 0x66, 0x99, 0xCC, PC_NOCOLLAPSE },
  200. { 0x99, 0x99, 0xCC, PC_NOCOLLAPSE },
  201. { 0xCC, 0x99, 0xCC, PC_NOCOLLAPSE },
  202. { 0xFF, 0x99, 0xCC, PC_NOCOLLAPSE },
  203. { 0x00, 0xCC, 0xCC, PC_NOCOLLAPSE },
  204. { 0x33, 0xCC, 0xCC, PC_NOCOLLAPSE },
  205. { 0x66, 0xCC, 0xCC, PC_NOCOLLAPSE },
  206. { 0x99, 0xCC, 0xCC, PC_NOCOLLAPSE },
  207. { 0xCC, 0xCC, 0xCC, PC_NOCOLLAPSE },
  208. { 0xFF, 0xCC, 0xCC, PC_NOCOLLAPSE },
  209. { 0x00, 0xFF, 0xCC, PC_NOCOLLAPSE },
  210. { 0x33, 0xFF, 0xCC, PC_NOCOLLAPSE },
  211. { 0x66, 0xFF, 0xCC, PC_NOCOLLAPSE },
  212. { 0x99, 0xFF, 0xCC, PC_NOCOLLAPSE },
  213. { 0xCC, 0xFF, 0xCC, PC_NOCOLLAPSE },
  214. { 0xFF, 0xFF, 0xCC, PC_NOCOLLAPSE },
  215. { 0xA5, 0x00, 0x21, PC_NOCOLLAPSE },
  216. { 0xD6, 0x00, 0x93, PC_NOCOLLAPSE },
  217. { 0x66, 0x00, 0xFF, PC_NOCOLLAPSE },
  218. { 0x99, 0x00, 0xFF, PC_NOCOLLAPSE },
  219. { 0xCC, 0x00, 0xFF, PC_NOCOLLAPSE },
  220. { 0xFF, 0x50, 0x50, PC_NOCOLLAPSE },
  221. { 0x33, 0x33, 0x00, PC_NOCOLLAPSE },
  222. { 0x33, 0x33, 0xFF, PC_NOCOLLAPSE },
  223. { 0x66, 0x33, 0xFF, PC_NOCOLLAPSE },
  224. { 0x99, 0x33, 0xFF, PC_NOCOLLAPSE },
  225. { 0xCC, 0x33, 0xFF, PC_NOCOLLAPSE },
  226. { 0xFF, 0x33, 0xFF, PC_NOCOLLAPSE },
  227. { 0x00, 0x66, 0xFF, PC_NOCOLLAPSE },
  228. { 0x33, 0x66, 0xFF, PC_NOCOLLAPSE },
  229. { 0x66, 0x66, 0xFF, PC_NOCOLLAPSE },
  230. { 0x99, 0x66, 0xFF, PC_NOCOLLAPSE },
  231. { 0xCC, 0x66, 0xFF, PC_NOCOLLAPSE },
  232. { 0xFF, 0x66, 0xFF, PC_NOCOLLAPSE },
  233. { 0x00, 0x99, 0xFF, PC_NOCOLLAPSE },
  234. { 0x33, 0x99, 0xFF, PC_NOCOLLAPSE },
  235. { 0x66, 0x99, 0xFF, PC_NOCOLLAPSE },
  236. { 0x99, 0x99, 0xFF, PC_NOCOLLAPSE },
  237. { 0xCC, 0x99, 0xFF, PC_NOCOLLAPSE },
  238. { 0xFF, 0x99, 0xFF, PC_NOCOLLAPSE },
  239. { 0x00, 0xCC, 0xFF, PC_NOCOLLAPSE },
  240. { 0x33, 0xCC, 0xFF, PC_NOCOLLAPSE },
  241. { 0x66, 0xCC, 0xFF, PC_NOCOLLAPSE },
  242. { 0x99, 0xCC, 0xFF, PC_NOCOLLAPSE },
  243. { 0xCC, 0xCC, 0xFF, PC_NOCOLLAPSE },
  244. { 0xFF, 0xCC, 0xFF, PC_NOCOLLAPSE },
  245. { 0x99, 0x33, 0x00, PC_NOCOLLAPSE },
  246. { 0x33, 0xFF, 0xFF, PC_NOCOLLAPSE },
  247. { 0x66, 0xFF, 0xFF, PC_NOCOLLAPSE },
  248. { 0x99, 0xFF, 0xFF, PC_NOCOLLAPSE },
  249. { 0xCC, 0xFF, 0xFF, PC_NOCOLLAPSE },
  250. { 0xFF, 0x7C, 0x80, PC_NOCOLLAPSE },
  251. { 0x04, 0x04, 0x04, PC_NOCOLLAPSE },
  252. { 0x08, 0x08, 0x08, PC_NOCOLLAPSE },
  253. { 0x0C, 0x0C, 0x0C, PC_NOCOLLAPSE },
  254. { 0x11, 0x11, 0x11, PC_NOCOLLAPSE },
  255. { 0x16, 0x16, 0x16, PC_NOCOLLAPSE },
  256. { 0x1C, 0x1C, 0x1C, PC_NOCOLLAPSE },
  257. { 0x22, 0x22, 0x22, PC_NOCOLLAPSE },
  258. { 0x29, 0x29, 0x29, PC_NOCOLLAPSE },
  259. { 0x39, 0x39, 0x39, PC_NOCOLLAPSE },
  260. { 0x42, 0x42, 0x42, PC_NOCOLLAPSE },
  261. { 0x4D, 0x4D, 0x4D, PC_NOCOLLAPSE },
  262. { 0x55, 0x55, 0x55, PC_NOCOLLAPSE },
  263. { 0x5F, 0x5F, 0x5F, PC_NOCOLLAPSE },
  264. { 0x77, 0x77, 0x77, PC_NOCOLLAPSE },
  265. { 0x86, 0x86, 0x86, PC_NOCOLLAPSE },
  266. { 0x96, 0x96, 0x96, PC_NOCOLLAPSE },
  267. { 0xB2, 0xB2, 0xB2, PC_NOCOLLAPSE },
  268. { 0xCB, 0xCB, 0xCB, PC_NOCOLLAPSE },
  269. { 0xD7, 0xD7, 0xD7, PC_NOCOLLAPSE },
  270. { 0xDD, 0xDD, 0xDD, PC_NOCOLLAPSE },
  271. { 0xE3, 0xE3, 0xE3, PC_NOCOLLAPSE },
  272. { 0xEA, 0xEA, 0xEA, PC_NOCOLLAPSE },
  273. { 0xF1, 0xF1, 0xF1, PC_NOCOLLAPSE },
  274. { 0xF8, 0xF8, 0xF8, PC_NOCOLLAPSE },
  275. { 0xE7, 0xE7, 0xD6, PC_NOCOLLAPSE },
  276. { 0xCC, 0xEC, 0xFF, PC_NOCOLLAPSE },
  277. { 0x33, 0x00, 0x00, PC_NOCOLLAPSE },
  278. { 0x66, 0x00, 0x00, PC_NOCOLLAPSE },
  279. { 0x99, 0x00, 0x00, PC_NOCOLLAPSE },
  280. { 0xCC, 0x00, 0x00, PC_NOCOLLAPSE },
  281. { 0xFF, 0xFB, 0xF0, PC_NOCOLLAPSE },
  282. { 0xA0, 0xA0, 0xA4, PC_NOCOLLAPSE },
  283. { 0x80, 0x80, 0x80, 0x00 },
  284. { 0xFF, 0x00, 0x00, 0x00 },
  285. { 0x00, 0xFF, 0x00, 0x00 },
  286. { 0xFF, 0xFF, 0x00, 0x00 },
  287. { 0x00, 0x00, 0xFF, 0x00 },
  288. { 0xFF, 0x00, 0xFF, 0x00 },
  289. { 0x00, 0xFF, 0xFF, 0x00 },
  290. { 0xFF, 0xFF, 0xFF, 0x00 },
  291. }
  292. };
  293. #endif
  294. void CopyColorsFromPaletteEntries(RGBQUAD *prgb, const PALETTEENTRY *ppe,
  295. UINT uCount)
  296. {
  297. while (uCount--)
  298. {
  299. prgb->rgbRed = ppe->peRed;
  300. prgb->rgbGreen = ppe->peGreen;
  301. prgb->rgbBlue = ppe->peBlue;
  302. prgb->rgbReserved = 0;
  303. prgb++;
  304. ppe++;
  305. }
  306. }
  307. void CopyPaletteEntriesFromColors(PALETTEENTRY *ppe, const RGBQUAD *prgb,
  308. UINT uCount)
  309. {
  310. while (uCount--)
  311. {
  312. ppe->peRed = prgb->rgbRed;
  313. ppe->peGreen = prgb->rgbGreen;
  314. ppe->peBlue = prgb->rgbBlue;
  315. ppe->peFlags = 0;
  316. prgb++;
  317. ppe++;
  318. }
  319. }
  320. HRESULT
  321. InitPalette()
  322. {
  323. HDC hdc = GetDC(NULL);
  324. HRESULT hr = S_OK;
  325. #if !defined(WIN16) && !defined(_MAC)
  326. g_hpalHalftone = SHCreateShellPalette(NULL);
  327. if (g_hpalHalftone)
  328. {
  329. g_lpHalftone.wCnt = (WORD)GetPaletteEntries(g_hpalHalftone, 0, 256, g_lpHalftone.ape);
  330. g_lpHalftone.wVer = 0x0300;
  331. CopyColorsFromPaletteEntries(g_rgbHalftone, g_lpHalftone.ape, g_lpHalftone.wCnt);
  332. }
  333. else
  334. return GetLastWin32Error();
  335. #else
  336. // Win16 only
  337. g_hpalHalftone = CreatePalette((LOGPALETTE *)&g_lpHalftone);
  338. // BUGBUG This will leak hdc
  339. if (g_hpalHalftone == NULL)
  340. return GetLastWin32Error();
  341. CopyColorsFromPaletteEntries(g_rgbHalftone, g_lpHalftone.ape, g_lpHalftone.wCnt);
  342. #endif
  343. #if !defined(WIN16) && !defined(_MAC)
  344. // Get the dithering table
  345. SHGetInverseCMAP((BYTE *)&g_pInvCMAP, sizeof(BYTE *));
  346. if (g_pInvCMAP == NULL)
  347. return E_OUTOFMEMORY;
  348. #endif
  349. if (GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)
  350. {
  351. #if DBG==1
  352. PALETTEENTRY ape[20], * pe = g_lpHalftone.ape;
  353. GetSystemPaletteEntries(hdc, 0, 10, ape);
  354. GetSystemPaletteEntries(hdc, 246, 10, ape + 10);
  355. if ( memcmp(ape, pe, 10 * sizeof(PALETTEENTRY)) != 0
  356. || memcmp(ape + 10, pe + 246, 10 * sizeof(PALETTEENTRY)) != 0)
  357. {
  358. TraceTag((tagPerf, "InitPalette: Unexpected system colors detected"));
  359. }
  360. #endif
  361. #ifndef NO_PERFDBG
  362. // Make sure we are using an identity palette
  363. // Disable for IDW build
  364. // Assert(IsIdentityPalette(g_hpalHalftone));
  365. #endif
  366. // Generate the inverse mapping table
  367. g_crPaletteRelative = 0x02000000;
  368. }
  369. else
  370. g_crPaletteRelative = 0;
  371. ReleaseDC(NULL, hdc);
  372. RRETURN(hr);
  373. }
  374. void
  375. DeinitPalette()
  376. {
  377. if (g_hdcMem1)
  378. {
  379. TraceTag((tagDCTrace, "GetMemoryDC, about to delete dc, count=%d", --++g_cGetMemDc));
  380. DeleteDC(g_hdcMem1);
  381. }
  382. if (g_hdcMem2)
  383. {
  384. TraceTag((tagDCTrace, "GetMemoryDC, about to delete dc, count=%d", --++g_cGetMemDc));
  385. DeleteDC(g_hdcMem2);
  386. }
  387. if (g_hdcMem3)
  388. {
  389. TraceTag((tagDCTrace, "GetMemoryDC, about to delete dc, count=%d", --++g_cGetMemDc));
  390. DeleteDC(g_hdcMem3);
  391. }
  392. if (g_hpalHalftone)
  393. {
  394. DeleteObject(g_hpalHalftone);
  395. g_hpalHalftone = NULL;
  396. }
  397. }
  398. //+---------------------------------------------------------------------------
  399. //
  400. // Function: GetDefaultPalette
  401. //
  402. // Synopsis: Returns a generic (halftone) palette, optionally selecting it
  403. // into the DC.
  404. //
  405. // Where possible, use CDoc::GetPalette instead.
  406. //
  407. //----------------------------------------------------------------------------
  408. HPALETTE
  409. GetDefaultPalette(HDC hdc)
  410. {
  411. HPALETTE hpal = NULL;
  412. if (g_crPaletteRelative)
  413. {
  414. hpal = g_hpalHalftone;
  415. if (hdc)
  416. {
  417. SelectPalette(hdc, hpal, TRUE);
  418. RealizePalette(hdc);
  419. }
  420. }
  421. return hpal;
  422. }
  423. //+---------------------------------------------------------------------------
  424. //
  425. // Function: IsHalftonePalette
  426. //
  427. // Synopsis: Returns TRUE if the passed palette exactly matches the
  428. // halftone palette.
  429. //
  430. //----------------------------------------------------------------------------
  431. BOOL
  432. IsHalftonePalette(HPALETTE hpal)
  433. {
  434. int cColors;
  435. PALETTEENTRY ape[256];
  436. if (!hpal)
  437. return FALSE;
  438. if (hpal == g_hpalHalftone)
  439. return TRUE;
  440. cColors = GetPaletteEntries(hpal, 0, 0, NULL);
  441. // Right number of colors?
  442. if (cColors != ARRAY_SIZE(ape))
  443. return FALSE;
  444. GetPaletteEntries(hpal, 0, cColors, ape);
  445. return (!memcmp(ape, g_lpHalftone.ape, sizeof(PALETTEENTRY) * ARRAY_SIZE(ape)));
  446. }
  447. //+---------------------------------------------------------------------------
  448. //
  449. // Function: InitColorTranslation
  450. //
  451. // Synopsis: Initialize system color translation table.
  452. //
  453. //----------------------------------------------------------------------------
  454. void
  455. InitColorTranslation()
  456. {
  457. for (int i = 0; i < ARRAY_SIZE(g_acrSysColor); i++)
  458. {
  459. // WinCE redefines all the GetSysColor defines
  460. #if defined(WINCE) && !defined(WINCE_NT)
  461. g_acrSysColor[i] = GetSysColor(i | COLOR_INDEX_MASK);
  462. #else
  463. g_acrSysColor[i] = GetSysColor(i);
  464. #endif
  465. }
  466. g_fSysColorInit = TRUE;
  467. }
  468. //+---------------------------------------------------------------------------
  469. //
  470. // Function: GetSysColorQuick
  471. //
  472. // Synopsis: Looks up system colors from a cache.
  473. //
  474. //----------------------------------------------------------------------------
  475. COLORREF GetSysColorQuick(int i)
  476. {
  477. if (!g_fSysColorInit)
  478. InitColorTranslation();
  479. return g_acrSysColor[i];
  480. }
  481. //+---------------------------------------------------------------------------
  482. //
  483. // Function: FormsTranslateColor
  484. //
  485. // Synopsis: Map OLE_COLORs to COLORREFs.
  486. // This API is identical to OleTranslateColor.
  487. //
  488. //----------------------------------------------------------------------------
  489. STDAPI
  490. FormsTranslateColor(OLE_COLOR clr, HPALETTE hpal, COLORREF * pcr)
  491. {
  492. int syscolor;
  493. switch (clr & 0xff000000)
  494. {
  495. case 0x80000000:
  496. syscolor = clr & 0x00ffffff;
  497. if (syscolor >= ARRAY_SIZE(g_acrSysColor))
  498. RRETURN(E_INVALIDARG);
  499. clr = GetSysColorQuick(syscolor);
  500. break;
  501. case 0x01000000 :
  502. // check validity of index
  503. if (hpal)
  504. {
  505. PALETTEENTRY pe;
  506. // try to get palette entry, if it fails we assume index is invalid
  507. if (!GetPaletteEntries(hpal, (UINT)(clr & 0xffff), 1, &pe))
  508. RRETURN(E_INVALIDARG); // BUGBUG? : CDK uses CTL_E_OVERFLOW
  509. }
  510. break;
  511. #ifdef UNIX
  512. case 0x04000000 : // Motif System Color
  513. #endif
  514. case 0x02000000 :
  515. break;
  516. case 0:
  517. if (hpal != NULL)
  518. {
  519. clr |= 0x02000000;
  520. }
  521. break;
  522. default :
  523. RRETURN(E_INVALIDARG);
  524. }
  525. if (pcr)
  526. *pcr = clr;
  527. return S_OK;
  528. }
  529. //+---------------------------------------------------------------------------
  530. //
  531. // Function: IsOleColorValid
  532. //
  533. // Synopsis: Return true if the given OLE_COLOR is valid.
  534. //
  535. //----------------------------------------------------------------------------
  536. BOOL
  537. IsOleColorValid(OLE_COLOR clr)
  538. {
  539. //
  540. // BUGBUG -- Why aren't we calling OleTranslateColor?
  541. //
  542. return OK(FormsTranslateColor(clr, NULL, NULL));
  543. }
  544. //+---------------------------------------------------------------------------
  545. //
  546. // Function: ColorRefFromOleColor
  547. //
  548. // Synopsis: Map OLE_COLORs to COLORREFs. This function does not contain
  549. // any error checking. Callers should validate the color with
  550. // IsOleColorValid() before calling this function.
  551. //
  552. //----------------------------------------------------------------------------
  553. COLORREF
  554. ColorRefFromOleColor(OLE_COLOR clr)
  555. {
  556. // Under WinCE all the colors come in as invalid--starting with 0xC... instead of 0x8
  557. #if defined(WINCE) && !defined(WINCE_NT)
  558. clr &= 0x80ffffff;
  559. #endif // WINCE
  560. Assert(IsOleColorValid(clr));
  561. Assert((clr & 0x02000000) == 0);
  562. if ((long)clr < 0)
  563. {
  564. return GetSysColorQuick(clr & 0xff);
  565. }
  566. else
  567. {
  568. #ifdef UNIX
  569. if ( CColorValue(clr).IsMotifColor() ) // Motif System Color
  570. return clr;
  571. #endif
  572. return clr | g_crPaletteRelative;
  573. }
  574. }
  575. HDC GetMemoryDC()
  576. {
  577. HDC hdcMem;
  578. if (g_fNoDisplayChange)
  579. {
  580. hdcMem = (HDC)InterlockedExchangePointer((void **)&g_hdcMem1, NULL);
  581. if (hdcMem)
  582. {
  583. TraceTag((tagDCTrace, "GetMemoryDC, about to return dc #1, count still %d", g_cGetMemDc));
  584. return(hdcMem);
  585. }
  586. hdcMem = (HDC)InterlockedExchangePointer((void **)&g_hdcMem2, NULL);
  587. if (hdcMem)
  588. {
  589. TraceTag((tagDCTrace, "GetMemoryDC, about to return dc #2, count still %d", g_cGetMemDc));
  590. return(hdcMem);
  591. }
  592. hdcMem = (HDC)InterlockedExchangePointer((void **)&g_hdcMem3, NULL);
  593. if (hdcMem)
  594. {
  595. TraceTag((tagDCTrace, "GetMemoryDC, about to return dc #3, count still %d", g_cGetMemDc));
  596. return(hdcMem);
  597. }
  598. TraceTag((tagDCTrace, "GetMemoryDC, about to return new dc, count=%d", ++g_cGetMemDc));
  599. }
  600. hdcMem = CreateCompatibleDC(NULL);
  601. if (hdcMem)
  602. {
  603. SetStretchBltMode(hdcMem, COLORONCOLOR);
  604. GetDefaultPalette(hdcMem);
  605. }
  606. return(hdcMem);
  607. }
  608. void ReleaseMemoryDC(HDC hdcIn)
  609. {
  610. HDC hdc = hdcIn;
  611. if (g_fNoDisplayChange)
  612. {
  613. hdc = (HDC)InterlockedExchangePointer((void **)&g_hdcMem1, hdc);
  614. if (hdc == NULL)
  615. {
  616. TraceTag((tagDCTrace, "GetMemoryDC, stored a dc in #1, count still %d", g_cGetMemDc));
  617. return;
  618. }
  619. hdc = (HDC)InterlockedExchangePointer((void **)&g_hdcMem2, hdc);
  620. if (hdc == NULL)
  621. {
  622. TraceTag((tagDCTrace, "GetMemoryDC, stored a dc in #2, count still %d", g_cGetMemDc));
  623. return;
  624. }
  625. hdc = (HDC)InterlockedExchangePointer((void **)&g_hdcMem3, hdc);
  626. if (hdc == NULL)
  627. {
  628. TraceTag((tagDCTrace, "GetMemoryDC, stored a dc in #3, count still %d", g_cGetMemDc));
  629. return;
  630. }
  631. TraceTag((tagDCTrace, "GetMemoryDC, about to delete dc, count=%d", --++g_cGetMemDc));
  632. }
  633. DeleteDC(hdc);
  634. }
  635. CColorInfo::CColorInfo() : _dwDrawAspect(DVASPECT_CONTENT) , _lindex(-1), _pvAspect(NULL), _ptd(NULL), _hicTargetDev(NULL), _cColors(0) , _cColorsMax(256)
  636. {
  637. }
  638. CColorInfo::CColorInfo(DWORD dwDrawAspect, LONG lindex, void FAR *pvAspect, DVTARGETDEVICE FAR *ptd, HDC hicTargetDev, unsigned cColorsMax) :
  639. _dwDrawAspect(dwDrawAspect), _lindex(lindex) , _pvAspect(pvAspect) , _ptd(ptd) , _hicTargetDev(hicTargetDev) , _cColors(0)
  640. {
  641. _cColorsMax = max((unsigned)256, cColorsMax);
  642. }
  643. HRESULT
  644. CColorInfo::AddColors(HPALETTE hpal)
  645. {
  646. unsigned cColors = GetPaletteEntries(hpal, 0, 0, NULL);
  647. cColors = min(_cColorsMax - _cColors, cColors);
  648. if (cColors)
  649. {
  650. GetPaletteEntries(hpal, 0, cColors, &_aColors[_cColors]);
  651. _cColors += cColors;
  652. }
  653. RRETURN(S_OK);
  654. }
  655. HRESULT
  656. CColorInfo::AddColors(LOGPALETTE *pLogPal)
  657. {
  658. RRETURN(AddColors(pLogPal->palNumEntries, pLogPal->palPalEntry));
  659. }
  660. HRESULT
  661. CColorInfo::AddColors(unsigned cColors, PALETTEENTRY *pColors)
  662. {
  663. // The check for system colors assumes they occur in the same order as
  664. // the real system colors. This is the simplest possible fix for now.
  665. // Remove any system colors at the end (do this first so that cColors makes sense)
  666. Assert(g_lpHalftone.wCnt == 256);
  667. Assert(sizeof(PALETTEENTRY) == sizeof(DWORD));
  668. DWORD *pSystem = (DWORD *)&g_lpHalftone.ape[g_lpHalftone.wCnt - 1];
  669. DWORD *pInput = (DWORD *)&pColors[cColors - 1];
  670. while (cColors && (*pSystem == *pInput))
  671. {
  672. pSystem--;
  673. pInput--;
  674. cColors--;
  675. }
  676. // Remove any system colors at the beginning
  677. pSystem = (DWORD *)(g_lpHalftone.ape);
  678. pInput = (DWORD *)pColors;
  679. while (cColors && (*pSystem == *pInput))
  680. {
  681. pSystem++;
  682. pInput++;
  683. cColors--;
  684. }
  685. pColors = (PALETTEENTRY *)pInput;
  686. cColors = min(_cColorsMax - _cColors, cColors);
  687. if (cColors)
  688. {
  689. memcpy(&_aColors[_cColors], pColors, cColors * sizeof(PALETTEENTRY));
  690. _cColors += cColors;
  691. }
  692. RRETURN(S_OK);
  693. }
  694. HRESULT
  695. CColorInfo::AddColors(unsigned cColors, RGBQUAD *pColors)
  696. {
  697. cColors = min(_cColorsMax - _cColors, cColors);
  698. if (cColors)
  699. {
  700. CopyPaletteEntriesFromColors(&_aColors[_cColors], pColors, cColors);
  701. _cColors += cColors;
  702. }
  703. RRETURN(S_OK);
  704. }
  705. HRESULT
  706. CColorInfo::AddColors(unsigned cColors, COLORREF *pColors)
  707. {
  708. cColors = min(_cColorsMax - _cColors, cColors);
  709. while (cColors)
  710. {
  711. _aColors[_cColors].peRed = GetRValue(*pColors);
  712. _aColors[_cColors].peGreen = GetGValue(*pColors);
  713. _aColors[_cColors].peBlue = GetBValue(*pColors);
  714. _aColors[_cColors].peFlags = 0;
  715. pColors++;
  716. }
  717. RRETURN(S_OK);
  718. }
  719. HRESULT
  720. CColorInfo::AddColors(IViewObject *pVO)
  721. {
  722. LPLOGPALETTE pColors = NULL;
  723. HRESULT hr = pVO->GetColorSet(_dwDrawAspect, _lindex, _pvAspect, _ptd, _hicTargetDev, &pColors);
  724. if (FAILED(hr))
  725. {
  726. hr = E_NOTIMPL;
  727. }
  728. else if (hr == S_OK && pColors)
  729. {
  730. AddColors(pColors);
  731. CoTaskMemFree(pColors);
  732. }
  733. RRETURN1(hr, S_FALSE);
  734. }
  735. BOOL
  736. CColorInfo::IsFull()
  737. {
  738. Assert(_cColors <= _cColorsMax);
  739. return _cColors >= _cColorsMax;
  740. }
  741. HRESULT
  742. CColorInfo::GetColorSet(LPLOGPALETTE FAR *ppColors)
  743. {
  744. *ppColors = 0;
  745. if (_cColors == 0)
  746. RRETURN1(S_FALSE, S_FALSE);
  747. LOGPALETTE *pColors;
  748. //
  749. // It's just easier to allocate 256 colors instead of messing about.
  750. //
  751. *ppColors = pColors = (LOGPALETTE *)CoTaskMemAlloc(sizeof(LOGPALETTE) + 255 * sizeof(PALETTEENTRY));
  752. if (!pColors)
  753. RRETURN(E_OUTOFMEMORY);
  754. // This will ensure that we have a reasonable set of colors, including
  755. // the system colors to start
  756. memcpy(pColors, &g_lpHalftone, sizeof(g_lpHalftone));
  757. unsigned cColors = min((unsigned)236, _cColors);
  758. // Notice that we avoid overwriting the beginning of the _aColors array.
  759. // The assumption is that the colors are in some kind of order.
  760. memcpy(pColors->palPalEntry + 10, _aColors, cColors * sizeof(PALETTEENTRY));
  761. for (unsigned i = 10 ; i < (cColors + 10); i++)
  762. pColors->palPalEntry[i].peFlags = PC_NOCOLLAPSE;
  763. RRETURN(S_OK);
  764. }
  765. #if DBG == 1
  766. ExternTag(tagPalette);
  767. void DumpPalette(CHAR *szName, unsigned cColors, PALETTEENTRY *pColors)
  768. {
  769. if (pColors)
  770. {
  771. TraceTagEx((tagPalette, TAG_NONAME | TAG_NONEWLINE | TAG_USECONSOLE, "DumpPalette: %s\n", szName));
  772. TraceTagEx((tagPalette, TAG_NONAME | TAG_NONEWLINE | TAG_USECONSOLE, "idx r g b flags"));
  773. for (unsigned i = 0 ; i < cColors ; i++)
  774. {
  775. TraceTagEx((tagPalette, TAG_NONAME | TAG_NONEWLINE | TAG_USECONSOLE, "%02x: %02x %02x %02x %02x", i, pColors[i].peRed, pColors[i].peGreen, pColors[i].peBlue, pColors[i].peFlags));
  776. if (i % 8 == 0)
  777. TraceTagEx((tagPalette, TAG_NONAME | TAG_NONEWLINE | TAG_USECONSOLE, "\n"));
  778. }
  779. }
  780. else
  781. TraceTag((tagPalette, "%s is NULL", szName));
  782. }
  783. void DumpPalette(CHAR *szName, LOGPALETTE *lp)
  784. {
  785. if (lp)
  786. DumpPalette(szName, lp->palNumEntries, lp->palPalEntry);
  787. else
  788. TraceTag((tagPalette, "%s is NULL", szName));
  789. }
  790. void DumpPalette(CHAR *szName, HPALETTE hpal)
  791. {
  792. if (hpal)
  793. {
  794. PALETTEENTRY ape[256];
  795. DumpPalette(szName, GetPaletteEntries(hpal, 0, 256, ape), ape);
  796. }
  797. else
  798. TraceTag((tagPalette, "%s is NULL", szName));
  799. }
  800. #endif