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.

205 lines
4.7 KiB

  1. #include "stdafx.h"
  2. #include "pbrush.h"
  3. #include "pbrusdoc.h"
  4. #include "pbrusfrm.h"
  5. #include "bmobject.h"
  6. #include "imgsuprt.h"
  7. #include "imgwnd.h"
  8. #include "imgwell.h"
  9. #ifdef _DEBUG
  10. #undef THIS_FILE
  11. static CHAR BASED_CODE THIS_FILE[] = __FILE__;
  12. #endif
  13. #include "memtrace.h"
  14. #define DSx 0x00660046L
  15. #define DSna 0x00220326L
  16. CImageWell::CImageWell()
  17. : m_bitmap(), m_dc(), m_mask(), m_maskDC(), m_imageSize(0, 0)
  18. {
  19. m_nBitmapID = 0;
  20. }
  21. CImageWell::CImageWell(UINT nBitmapID, CSize imageSize)
  22. : m_bitmap(), m_dc(), m_mask(), m_maskDC(), m_imageSize(imageSize)
  23. {
  24. m_nBitmapID = nBitmapID;
  25. }
  26. CImageWell::~CImageWell()
  27. {
  28. // Destructor needed to ensure the DC is deleted before the bitmap
  29. m_dc.DeleteDC();
  30. m_bitmap.DeleteObject();
  31. m_maskDC.DeleteDC();
  32. m_mask.DeleteObject();
  33. }
  34. BOOL CImageWell::Load(UINT nBitmapID, CSize imageSize)
  35. {
  36. ASSERT(m_bitmap.m_hObject == NULL);
  37. if (!m_bitmap.LoadBitmap(nBitmapID))
  38. {
  39. TRACE1("Could not load image well %d\n", nBitmapID);
  40. return FALSE;
  41. }
  42. m_nBitmapID = nBitmapID;
  43. m_imageSize = imageSize;
  44. return TRUE;
  45. }
  46. void CImageWell::Unload()
  47. {
  48. ASSERT(m_dc.m_hDC == NULL); // can't unload if it's open!
  49. ASSERT(m_maskDC.m_hDC == NULL);
  50. ASSERT(m_bitmap.m_hObject != NULL);
  51. m_bitmap.DeleteObject();
  52. m_mask.DeleteObject();
  53. }
  54. BOOL CImageWell::CalculateMask()
  55. {
  56. ASSERT(m_maskDC.m_hDC == NULL);
  57. ASSERT(m_dc.m_hDC != NULL);
  58. ASSERT(m_bitmap.m_hObject != NULL);
  59. if (!m_maskDC.CreateCompatibleDC(NULL))
  60. {
  61. theApp.SetGdiEmergency(FALSE);
  62. return FALSE;
  63. }
  64. if (m_mask.m_hObject != NULL)
  65. {
  66. VERIFY(m_maskDC.SelectObject(&m_mask) != NULL);
  67. return TRUE;
  68. }
  69. BITMAP bmp;
  70. m_bitmap.GetObject(sizeof (BITMAP), &bmp);
  71. if (!m_mask.CreateBitmap(bmp.bmWidth, bmp.bmHeight*2, 1, 1, NULL))
  72. {
  73. m_maskDC.DeleteDC();
  74. theApp.SetMemoryEmergency(FALSE);
  75. return FALSE;
  76. }
  77. VERIFY(m_maskDC.SelectObject(&m_mask) != NULL);
  78. COLORREF oldBkColor = m_dc.SetBkColor(m_dc.GetPixel(0, 0));
  79. m_maskDC.BitBlt(0, 0, bmp.bmWidth, bmp.bmHeight,
  80. &m_dc, 0, 0, NOTSRCCOPY);
  81. // store the pixels in "button text" color in the second part of the mask
  82. m_dc.SetBkColor(RGB(0, 0, 0));
  83. m_maskDC.BitBlt(0, bmp.bmHeight, bmp.bmWidth, bmp.bmHeight,
  84. &m_dc, 0, 0, SRCCOPY);
  85. m_dc.SetBkColor(oldBkColor);
  86. return TRUE;
  87. }
  88. BOOL CImageWell::Open()
  89. {
  90. ASSERT(m_dc.m_hDC == NULL); // make sure this is not already open
  91. ASSERT(m_nBitmapID != 0);
  92. if (m_bitmap.m_hObject == NULL && !Load(m_nBitmapID, m_imageSize))
  93. return FALSE;
  94. if (!m_dc.CreateCompatibleDC(NULL))
  95. {
  96. theApp.SetGdiEmergency(FALSE);
  97. return FALSE;
  98. }
  99. VERIFY(m_dc.SelectObject(&m_bitmap));
  100. return TRUE;
  101. }
  102. void CImageWell::Close()
  103. {
  104. ASSERT(m_dc.m_hDC != NULL);
  105. m_dc.DeleteDC();
  106. m_maskDC.DeleteDC();
  107. }
  108. BOOL CImageWell::DrawImage(CDC* pDestDC, CPoint destPoint,
  109. UINT nImage, DWORD rop)
  110. {
  111. BOOL bClose = FALSE;
  112. if (m_dc.m_hDC == NULL)
  113. {
  114. ASSERT(rop != NULL); // must open first and calc mask for rop==0!
  115. if (!Open())
  116. {
  117. TRACE(TEXT("Could not open image well!\n"));
  118. return FALSE;
  119. }
  120. bClose = TRUE;
  121. }
  122. if (rop == 0)
  123. {
  124. if (m_maskDC.m_hDC == NULL && !CalculateMask())
  125. return FALSE;
  126. COLORREF oldBkColor = pDestDC->SetBkColor(RGB(255, 255, 255));
  127. COLORREF oldTextColor = pDestDC->SetTextColor(RGB(0, 0, 0));
  128. pDestDC->BitBlt(destPoint.x, destPoint.y,
  129. m_imageSize.cx, m_imageSize.cy,
  130. &m_dc, m_imageSize.cx * nImage, 0, DSx);
  131. pDestDC->BitBlt(destPoint.x, destPoint.y,
  132. m_imageSize.cx, m_imageSize.cy,
  133. &m_maskDC, m_imageSize.cx * nImage, 0, DSna);
  134. pDestDC->BitBlt(destPoint.x, destPoint.y,
  135. m_imageSize.cx, m_imageSize.cy,
  136. &m_dc, m_imageSize.cx * nImage, 0, DSx);
  137. pDestDC->SetBkColor(GetSysColor(COLOR_BTNTEXT));
  138. pDestDC->BitBlt(destPoint.x, destPoint.y,
  139. m_imageSize.cx, m_imageSize.cy,
  140. &m_maskDC, m_imageSize.cx * nImage, m_imageSize.cy, SRCPAINT);
  141. pDestDC->SetBkColor(oldBkColor);
  142. pDestDC->SetTextColor(oldTextColor);
  143. }
  144. else
  145. {
  146. pDestDC->BitBlt(destPoint.x, destPoint.y,
  147. m_imageSize.cx, m_imageSize.cy,
  148. &m_dc, m_imageSize.cx * nImage, 0, rop);
  149. }
  150. if (bClose)
  151. Close();
  152. return TRUE;
  153. }