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.

185 lines
5.8 KiB

  1. #include "ctlspriv.h"
  2. //
  3. // include HEX forms of some standard bitmaps
  4. //
  5. #include "toolbar.hex"
  6. #include "thumb.hex"
  7. // these are the default colors used to map the dib colors
  8. // to the current system colors
  9. #define RGB_BUTTONTEXT (RGB(000,000,000)) // black
  10. #define RGB_BUTTONSHADOW (RGB(128,128,128)) // dark grey
  11. #define RGB_BUTTONFACE (RGB(192,192,192)) // bright grey
  12. #define RGB_BUTTONHILIGHT (RGB(255,255,255)) // white
  13. #define RGB_BACKGROUNDSEL (RGB(000,000,255)) // blue
  14. #define RGB_BACKGROUND (RGB(255,000,255)) // magenta
  15. #define FlipColor(rgb) (RGB(GetBValue(rgb), GetGValue(rgb), GetRValue(rgb)))
  16. #define MAX_COLOR_MAPS 16
  17. HBITMAP WINAPI CreateMappedDib(LPBITMAPINFOHEADER lpBitmapInfo,
  18. WORD wFlags, LPCOLORMAP lpColorMap, int iNumMaps)
  19. {
  20. HDC hdc, hdcMem = NULL;
  21. DWORD FAR *p;
  22. DWORD FAR *lpTable;
  23. LPBYTE lpBits;
  24. HBITMAP hbm = NULL, hbmOld;
  25. int numcolors, i;
  26. int wid, hgt;
  27. DWORD rgbMaskTable[16];
  28. DWORD rgbSave[16];
  29. DWORD rgbBackground;
  30. static const COLORMAP SysColorMap[] = {
  31. {RGB_BUTTONTEXT, COLOR_BTNTEXT}, // black
  32. {RGB_BUTTONSHADOW, COLOR_BTNSHADOW}, // dark grey
  33. {RGB_BUTTONFACE, COLOR_BTNFACE}, // bright grey
  34. {RGB_BUTTONHILIGHT, COLOR_BTNHIGHLIGHT},// white
  35. {RGB_BACKGROUNDSEL, COLOR_HIGHLIGHT}, // blue
  36. {RGB_BACKGROUND, COLOR_WINDOW} // magenta
  37. };
  38. #define NUM_DEFAULT_MAPS (sizeof(SysColorMap)/sizeof(COLORMAP))
  39. COLORMAP DefaultColorMap[NUM_DEFAULT_MAPS];
  40. COLORMAP DIBColorMap[MAX_COLOR_MAPS];
  41. if (!lpBitmapInfo)
  42. return NULL;
  43. hmemcpy(rgbSave, lpBitmapInfo+1, 16 * sizeof(RGBQUAD));
  44. /* Get system colors for the default color map */
  45. if (!lpColorMap) {
  46. lpColorMap = DefaultColorMap;
  47. iNumMaps = NUM_DEFAULT_MAPS;
  48. for (i=0; i < iNumMaps; i++) {
  49. lpColorMap[i].from = SysColorMap[i].from;
  50. lpColorMap[i].to = GetSysColor((int)SysColorMap[i].to);
  51. }
  52. }
  53. /* Transform RGB color map to a BGR DIB format color map */
  54. if (iNumMaps > MAX_COLOR_MAPS)
  55. iNumMaps = MAX_COLOR_MAPS;
  56. for (i=0; i < iNumMaps; i++) {
  57. DIBColorMap[i].to = FlipColor(lpColorMap[i].to);
  58. DIBColorMap[i].from = FlipColor(lpColorMap[i].from);
  59. }
  60. lpTable = p = (DWORD FAR *)((LPBYTE)lpBitmapInfo + (int)lpBitmapInfo->biSize);
  61. /* Replace button-face and button-shadow colors with the current values
  62. */
  63. numcolors = 16;
  64. // if we are creating a mask, build a color table with white
  65. // marking the transparent section (where it used to be background)
  66. // and black marking the opaque section (everything else). this
  67. // table is used below to build the mask using the original DIB bits.
  68. if (wFlags & CMB_MASKED) {
  69. rgbBackground = FlipColor(RGB_BACKGROUND);
  70. for (i = 0; i < 16; i++) {
  71. if (p[i] == rgbBackground)
  72. rgbMaskTable[i] = 0xFFFFFF; // transparent section
  73. else
  74. rgbMaskTable[i] = 0x000000; // opaque section
  75. }
  76. }
  77. while (numcolors-- > 0) {
  78. for (i = 0; i < iNumMaps; i++) {
  79. if (*p == DIBColorMap[i].from) {
  80. *p = DIBColorMap[i].to;
  81. break;
  82. }
  83. }
  84. p++;
  85. }
  86. /* First skip over the header structure */
  87. lpBits = (LPVOID)((LPBYTE)lpBitmapInfo + (int)lpBitmapInfo->biSize);
  88. /* Skip the color table entries, if any */
  89. lpBits += (1 << (lpBitmapInfo->biBitCount)) * sizeof(RGBQUAD);
  90. /* Create a color bitmap compatible with the display device */
  91. i = wid = (int)lpBitmapInfo->biWidth;
  92. hgt = (int)lpBitmapInfo->biHeight;
  93. hdc = GetDC(NULL);
  94. hdcMem = CreateCompatibleDC(hdc);
  95. if (!hdcMem)
  96. goto cleanup;
  97. // if creating a mask, the bitmap needs to be twice as wide.
  98. if (wFlags & CMB_MASKED)
  99. i = wid*2;
  100. if (wFlags & CMB_DISCARDABLE)
  101. hbm = CreateDiscardableBitmap(hdc, i, hgt);
  102. else
  103. hbm = CreateCompatibleBitmap(hdc, i, hgt);
  104. if (hbm) {
  105. hbmOld = SelectObject(hdcMem, hbm);
  106. // set the main image
  107. StretchDIBits(hdcMem, 0, 0, wid, hgt, 0, 0, wid, hgt, lpBits,
  108. (LPBITMAPINFO)lpBitmapInfo, DIB_RGB_COLORS, SRCCOPY);
  109. // if building a mask, replace the DIB's color table with the
  110. // mask's black/white table and set the bits. in order to
  111. // complete the masked effect, the actual image needs to be
  112. // modified so that it has the color black in all sections
  113. // that are to be transparent.
  114. if (wFlags & CMB_MASKED) {
  115. hmemcpy(lpTable, (DWORD FAR *)rgbMaskTable, 16 * sizeof(RGBQUAD));
  116. StretchDIBits(hdcMem, wid, 0, wid, hgt, 0, 0, wid, hgt, lpBits,
  117. (LPBITMAPINFO)lpBitmapInfo, DIB_RGB_COLORS, SRCCOPY);
  118. BitBlt(hdcMem, 0, 0, wid, hgt, hdcMem, wid, 0, 0x00220326); // DSna
  119. }
  120. SelectObject(hdcMem, hbmOld);
  121. }
  122. cleanup:
  123. if (hdcMem)
  124. DeleteObject(hdcMem);
  125. ReleaseDC(NULL, hdc);
  126. hmemcpy(lpBitmapInfo+1, rgbSave, 16 * sizeof(RGBQUAD));
  127. return hbm;
  128. }
  129. HBITMAP WINAPI CreateMappedBitmap(HINSTANCE hInstance, int idBitmap,
  130. WORD wFlags, LPCOLORMAP lpColorMap, int iNumMaps)
  131. {
  132. HANDLE h;
  133. HANDLE hRes;
  134. HBITMAP hbm;
  135. LPBITMAPINFOHEADER lpbi;
  136. h = FindResource(hInstance, MAKEINTRESOURCE(idBitmap), RT_BITMAP);
  137. if (!h)
  138. {
  139. if (idBitmap == IDB_THUMB)
  140. lpbi = (LPVOID)Bitmap_Thumb;
  141. else
  142. lpbi = (LPVOID)Bitmap_Toolbar;
  143. return CreateMappedDib(lpbi, wFlags, lpColorMap, iNumMaps);
  144. }
  145. hRes = LoadResource(hInstance, h);
  146. /* Lock the bitmap and get a pointer to the color table. */
  147. lpbi = (LPBITMAPINFOHEADER)LockResource(hRes);
  148. if (!lpbi)
  149. return NULL;
  150. hbm = CreateMappedDib(lpbi, wFlags, lpColorMap, iNumMaps);
  151. UnlockResource(hRes);
  152. FreeResource(hRes);
  153. return hbm;
  154. }