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.

225 lines
6.3 KiB

  1. //
  2. // image.cpp
  3. //
  4. // utilities for images and imagelists
  5. //
  6. //#include "pch.hxx"
  7. //#include "resource.h"
  8. //#include "globals.h"
  9. //#include "util.h"
  10. #include "_apipch.h"
  11. //extern HINSTANCE hinstMapiX;
  12. //
  13. // TileImage
  14. //
  15. // This function tiles a bitmap in lprcDest, using lpptOrigin as the origin
  16. //
  17. #define ILT_PARTIALBLT_LEFT 0
  18. #define ILT_PARTIALBLT_TOP 1
  19. #define ILT_PARTIALBLT_RIGHT 2
  20. #define ILT_PARTIALBLT_BOTTOM 3
  21. void TileImage(HBITMAP hbmp, HDC hdc, LPPOINT lpptOrigin, LPRECT lprcDest)
  22. {
  23. BOOL fFirstRow, fFirstCol;
  24. int nRows, nCols, nSaveCols, offset;
  25. int rgOffsetPartialBlt[4];
  26. POINT ptTileOrigin, ptDraw, ptDest, ptSrc;
  27. SIZE sizeImage, sizeRect, sizeBlt;
  28. HDC hdcBmp=0;
  29. HBITMAP hbmpOrig=0;
  30. BITMAP bm;
  31. if(!hdc)
  32. return;
  33. if(!(hdcBmp = CreateCompatibleDC(hdc)))
  34. goto fail;
  35. if(!(hbmpOrig = SelectObject(hdcBmp, hbmp)))
  36. goto fail;
  37. GetObject(hbmp, sizeof(BITMAP), &bm);
  38. sizeImage.cx = bm.bmWidth;
  39. sizeImage.cy = bm.bmHeight;
  40. // Generate the true origin with the tile
  41. lpptOrigin->x %= sizeImage.cx;
  42. lpptOrigin->y %= sizeImage.cy;
  43. // We will start the tile so that the origin lines up
  44. ptTileOrigin.x = lprcDest->left - lpptOrigin->x;
  45. ptTileOrigin.y = lprcDest->top - lpptOrigin->y;
  46. // Figure out how many rows and columns wo need
  47. sizeRect.cx = lprcDest->right - ptTileOrigin.x;
  48. sizeRect.cy = lprcDest->bottom - ptTileOrigin.y;
  49. nRows = sizeRect.cy / sizeImage.cy;
  50. nRows += (sizeRect.cy % sizeImage.cy) ? 1 : 0;
  51. nSaveCols = sizeRect.cx / sizeImage.cx;
  52. nSaveCols += (sizeRect.cx % sizeImage.cx) ? 1 : 0;
  53. // Generate the partial blt offsets
  54. rgOffsetPartialBlt[ILT_PARTIALBLT_TOP] = lprcDest->top - ptTileOrigin.y;
  55. rgOffsetPartialBlt[ILT_PARTIALBLT_LEFT] = lprcDest->left - ptTileOrigin.x;
  56. if (sizeImage.cy >= sizeRect.cy)
  57. rgOffsetPartialBlt[ILT_PARTIALBLT_BOTTOM] = sizeImage.cy - sizeRect.cy;
  58. else if(sizeRect.cy % sizeImage.cy)
  59. rgOffsetPartialBlt[ILT_PARTIALBLT_BOTTOM] = sizeImage.cy -
  60. (sizeRect.cy % sizeImage.cy);
  61. else
  62. rgOffsetPartialBlt[ILT_PARTIALBLT_BOTTOM] = 0;
  63. if (sizeImage.cx >= sizeRect.cx)
  64. rgOffsetPartialBlt[ILT_PARTIALBLT_RIGHT] = sizeImage.cx - sizeRect.cx;
  65. else if(sizeRect.cx % sizeImage.cx)
  66. rgOffsetPartialBlt[ILT_PARTIALBLT_RIGHT] = sizeImage.cx -
  67. (sizeRect.cx % sizeImage.cx);
  68. else
  69. rgOffsetPartialBlt[ILT_PARTIALBLT_RIGHT] = 0;
  70. // Draw the tiles
  71. ptDraw.y = ptTileOrigin.y;
  72. fFirstRow = TRUE;
  73. while (nRows--)
  74. {
  75. ptDraw.x = ptTileOrigin.x;
  76. nCols = nSaveCols;
  77. fFirstCol = TRUE;
  78. while (nCols--)
  79. {
  80. ptDest = ptDraw;
  81. sizeBlt = sizeImage;
  82. ptSrc.x = ptSrc.y = 0;
  83. // Handle partial boundary Blts
  84. if (fFirstRow && rgOffsetPartialBlt[ILT_PARTIALBLT_TOP] != 0)
  85. {
  86. offset = rgOffsetPartialBlt[ILT_PARTIALBLT_TOP];
  87. ptDest.y += offset;
  88. sizeBlt.cy -= offset;
  89. ptSrc.y += offset;
  90. }
  91. if (nRows == 0 && rgOffsetPartialBlt[ILT_PARTIALBLT_BOTTOM] != 0)
  92. sizeBlt.cy -= rgOffsetPartialBlt[ILT_PARTIALBLT_BOTTOM];
  93. if (fFirstCol && rgOffsetPartialBlt[ILT_PARTIALBLT_LEFT] != 0)
  94. {
  95. offset = rgOffsetPartialBlt[ILT_PARTIALBLT_LEFT];
  96. ptDest.x += offset;
  97. sizeBlt.cx -= offset;
  98. ptSrc.x += offset;
  99. }
  100. if (nCols == 0 && rgOffsetPartialBlt[ILT_PARTIALBLT_RIGHT] != 0)
  101. sizeBlt.cx -= rgOffsetPartialBlt[ILT_PARTIALBLT_RIGHT];
  102. // Just do it
  103. BitBlt(hdc, ptDest.x, ptDest.y, sizeBlt.cx, sizeBlt.cy,
  104. hdcBmp, ptSrc.x, ptSrc.y, SRCCOPY);
  105. ptDraw.x += sizeImage.cx;
  106. fFirstCol = FALSE;
  107. }
  108. ptDraw.y += sizeImage.cy;
  109. fFirstRow = FALSE;
  110. }
  111. fail:
  112. if(hdcBmp)
  113. {
  114. if(hbmpOrig)
  115. SelectObject(hdcBmp, hbmpOrig);
  116. DeleteDC(hdcBmp);
  117. }
  118. }
  119. //
  120. // shared image list..
  121. //
  122. static HIMAGELIST g_himlAthSm = NULL;
  123. static HIMAGELIST g_himlAthLg = NULL;
  124. static HIMAGELIST g_himlAthSt = NULL;
  125. HIMAGELIST InitImageList(int cx, int cy, LPSTR szbm, int cicon)
  126. {
  127. HBITMAP hbm;
  128. HIMAGELIST himl;
  129. himl = ImageList_Create(cx, cy, ILC_MASK, cicon, 0);
  130. if (himl != NULL)
  131. {
  132. hbm = LoadBitmap(hinstMapiX, szbm);
  133. ImageList_AddMasked(himl, hbm, RGB_TRANSPARENT);
  134. DeleteObject((HGDIOBJ)hbm);
  135. }
  136. return(himl);
  137. }
  138. void FreeImageLists(void)
  139. {
  140. if (g_himlAthSm != NULL)
  141. {
  142. ImageList_Destroy(g_himlAthSm);
  143. g_himlAthSm = NULL;
  144. }
  145. if (g_himlAthLg != NULL)
  146. {
  147. ImageList_Destroy(g_himlAthLg);
  148. g_himlAthLg = NULL;
  149. }
  150. if (g_himlAthSt != NULL)
  151. {
  152. ImageList_Destroy(g_himlAthSt);
  153. g_himlAthSt = NULL;
  154. }
  155. }
  156. BOOL LoadBitmapAndPalette(int idbmp, HBITMAP *phbmp, HPALETTE *phpal)
  157. {
  158. int i, n;
  159. HBITMAP hbmp;
  160. HPALETTE hpal;
  161. HDC hdcBitmap;
  162. DWORD adw[257];
  163. BOOL fret = FALSE;
  164. hdcBitmap = NULL;
  165. *phbmp = NULL;
  166. *phpal = NULL;
  167. hbmp = (HBITMAP)LoadImage(hinstMapiX, MAKEINTRESOURCE(idbmp),
  168. IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
  169. if (hbmp == NULL)
  170. goto DoneLoadBitmap;
  171. hdcBitmap = CreateCompatibleDC(NULL);
  172. if (hdcBitmap == NULL)
  173. goto DoneLoadBitmap;
  174. SelectObject(hdcBitmap, (HGDIOBJ)hbmp);
  175. n = GetDIBColorTable(hdcBitmap, 0, 256, (LPRGBQUAD)&adw[1]);
  176. for (i = 1; i <= n; i++)
  177. adw[i] = RGB(GetBValue(adw[i]), GetGValue(adw[i]), GetRValue(adw[i]));
  178. adw[0] = MAKELONG(0x300, n);
  179. hpal = CreatePalette((LPLOGPALETTE)&adw[0]);
  180. if (hpal == NULL)
  181. goto DoneLoadBitmap;
  182. *phbmp = hbmp;
  183. *phpal = hpal;
  184. fret = TRUE;
  185. DoneLoadBitmap:
  186. if (hdcBitmap != NULL)
  187. DeleteDC(hdcBitmap);
  188. return(fret);
  189. }