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.

154 lines
3.9 KiB

  1. /***************************************************************************
  2. * palette.c
  3. *
  4. * Common halftone palette used by shell.
  5. *
  6. ***************************************************************************/
  7. #include "priv.h"
  8. #include "palette.h"
  9. #ifdef UNIX
  10. #include <mainwin.h>
  11. #endif /* UNIX */
  12. BYTE * g_pbWinNTCMAP = NULL;
  13. // This function behaves the same as CreateHalftone palette:
  14. // hdc == NULL always return full palette
  15. // hdc palettized return full palette
  16. // hdc not palettized return default palette (VGA colors)
  17. HPALETTE SHCreateShellPalette(HDC hdc)
  18. {
  19. HPALETTE hpalHalftone;
  20. LOGPAL256 lp;
  21. BOOL fGetSystemColors = FALSE;
  22. // We would like to use CreateHalftonePalette() always but they
  23. // differ significantly between NT and Win95. Win95's is very
  24. // close to Netscape's; NT's is significantly different (color
  25. // cube cut differently) and it is not an identity palette.
  26. //
  27. // So, we will use CreateHalftonePalette() on Win95 and on NT
  28. // we will use a custom palette containing the same colors in
  29. // Win95's halftone palette ordered so that the color flash
  30. // will be minimized when switching between NT's halftone palette
  31. // and our palette.
  32. //
  33. // On NT 5 and later the halftone palette matches Win95's so the
  34. // custom palette will only be used on machines running NT 4 or below.
  35. // However, we still need to patch in the system colors on NT 5.
  36. #ifdef UNIX
  37. if (MwIsInitLite())
  38. return NULL;
  39. #endif /* UNIX */
  40. if (!g_bRunningOnNT || g_bRunningOnNT5OrHigher)
  41. {
  42. hpalHalftone = CreateHalftonePalette(hdc);
  43. if (g_bRunningOnNT5OrHigher && hpalHalftone)
  44. {
  45. lp.wCnt = (WORD)GetPaletteEntries(hpalHalftone, 0, 256, lp.ape);
  46. lp.wVer = 0x0300;
  47. fGetSystemColors = TRUE;
  48. DeleteObject(hpalHalftone);
  49. }
  50. }
  51. else if (hdc == NULL || (GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE))
  52. {
  53. memcpy(&lp, &g_lpHalftone, sizeof(g_lpHalftone));
  54. fGetSystemColors = TRUE;
  55. }
  56. else
  57. hpalHalftone = GetStockObject(DEFAULT_PALETTE);
  58. if (fGetSystemColors)
  59. {
  60. HDC hdcScreen = hdc;
  61. if (hdcScreen == NULL)
  62. hdcScreen = CreateCompatibleDC(NULL);
  63. #ifndef UNIX
  64. if (hdcScreen)
  65. {
  66. GetSystemPaletteEntries(hdcScreen, 0, 10, lp.ape);
  67. GetSystemPaletteEntries(hdcScreen, 246, 10, lp.ape + 246);
  68. }
  69. #endif
  70. hpalHalftone = CreatePalette((LOGPALETTE *)&lp);
  71. if (hdc == NULL && hdcScreen)
  72. DeleteDC(hdcScreen);
  73. }
  74. return hpalHalftone;
  75. }
  76. static const BYTE *GetInverseCMAP()
  77. {
  78. const BYTE * abWin95ToNT;
  79. if (!g_bRunningOnNT)
  80. {
  81. return g_abWin95CMAP;
  82. }
  83. abWin95ToNT = (g_bRunningOnNT5OrHigher ? g_abWin95ToNT5 : g_abWin95ToNT4);
  84. if (g_pbWinNTCMAP == NULL)
  85. {
  86. BYTE * pbMap = LocalAlloc(LPTR, 32768);
  87. if (pbMap)
  88. {
  89. int i;
  90. BYTE * pbDst = pbMap;
  91. const BYTE * pbSrc = g_abWin95CMAP;
  92. for (i = 0; i < 32768; ++i)
  93. {
  94. *pbDst++ = abWin95ToNT[*pbSrc++];
  95. }
  96. if (SHInterlockedCompareExchange((void **)&g_pbWinNTCMAP, pbMap, NULL))
  97. {
  98. LocalFree(pbMap); // race, get rid of dupe copy
  99. }
  100. }
  101. }
  102. return g_pbWinNTCMAP;
  103. }
  104. HRESULT SHGetInverseCMAP(BYTE *pbMap, ULONG cbMap)
  105. {
  106. const BYTE *pbSrc;
  107. if (pbMap == NULL)
  108. return E_POINTER;
  109. if (cbMap != 32768 && cbMap != sizeof(BYTE *))
  110. return E_INVALIDARG;
  111. pbSrc = GetInverseCMAP();
  112. if (pbSrc == NULL)
  113. return E_OUTOFMEMORY;
  114. if (cbMap == sizeof(BYTE *))
  115. {
  116. *(const BYTE **)pbMap = pbSrc;
  117. }
  118. else
  119. {
  120. memcpy(pbMap, pbSrc, 32768);
  121. }
  122. return(S_OK);
  123. }
  124. void TermPalette()
  125. {
  126. if (g_pbWinNTCMAP)
  127. LocalFree(g_pbWinNTCMAP);
  128. }