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.

226 lines
6.3 KiB

  1. //------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000
  5. //
  6. // File: XBarGlyph.h
  7. //
  8. // Contents: image of an xBar pane
  9. //
  10. // Classes: CXBarGlyph
  11. //
  12. //------------------------------------------------------------------------
  13. #include "priv.h"
  14. #include "XBarGlyph.h"
  15. #include "resource.h"
  16. #include "tb_ids.h"
  17. //////////////////////////////////////////////////////////////////////
  18. // Construction/Destruction
  19. //////////////////////////////////////////////////////////////////////
  20. #define CX_SMALL_ICON 16
  21. #define CX_LARGE_ICON 20
  22. // These defines are zero-index offsets into the existing toolbar buttons
  23. #define IBAR_ICON_FAVORITES 6
  24. #define IBAR_ICON_SEARCH 5
  25. #define IBAR_ICON_HISTORY 12
  26. #define IBAR_ICON_EXPLORER 43
  27. #define IBAR_ICON_DEFAULT 10
  28. //------------------------------------------------------------------------
  29. CXBarGlyph::CXBarGlyph()
  30. : _hbmpColor(NULL),
  31. _hbmpMask(NULL),
  32. _fAlpha(FALSE),
  33. _lWidth(0),
  34. _lHeight(0)
  35. {
  36. }
  37. //------------------------------------------------------------------------
  38. CXBarGlyph::~CXBarGlyph()
  39. {
  40. DESTROY_OBJ_WITH_HANDLE(_hbmpColor, DeleteObject);
  41. DESTROY_OBJ_WITH_HANDLE(_hbmpMask, DeleteObject);
  42. }
  43. //------------------------------------------------------------------------
  44. HRESULT
  45. CXBarGlyph::SetIcon(HICON hIcon, BOOL fAlpha)
  46. {
  47. DESTROY_OBJ_WITH_HANDLE(_hbmpColor, DeleteObject);
  48. DESTROY_OBJ_WITH_HANDLE(_hbmpMask, DeleteObject);
  49. if (hIcon == NULL) {
  50. return E_INVALIDARG;
  51. }
  52. ICONINFO ii = {0};
  53. if (GetIconInfo(hIcon, &ii))
  54. {
  55. _hbmpColor = ii.hbmColor;
  56. _hbmpMask = ii.hbmMask;
  57. _fAlpha = fAlpha;
  58. _EnsureDimensions();
  59. }
  60. else {
  61. return E_OUTOFMEMORY;
  62. }
  63. return S_OK;
  64. }
  65. //------------------------------------------------------------------------
  66. HICON
  67. CXBarGlyph::GetIcon(void)
  68. {
  69. ICONINFO ii = {0};
  70. ii.fIcon = TRUE;
  71. ii.hbmColor = _hbmpColor;
  72. ii.hbmMask = _hbmpMask;
  73. return CreateIconIndirect(&ii);
  74. }
  75. //------------------------------------------------------------------------
  76. BOOL
  77. CXBarGlyph::HaveGlyph(void)
  78. {
  79. return (_hbmpColor != NULL);
  80. }
  81. //------------------------------------------------------------------------
  82. LONG
  83. CXBarGlyph::GetWidth(void)
  84. {
  85. _EnsureDimensions();
  86. return _lWidth;
  87. }
  88. //------------------------------------------------------------------------
  89. LONG
  90. CXBarGlyph::GetHeight(void)
  91. {
  92. _EnsureDimensions();
  93. return _lHeight;
  94. }
  95. //------------------------------------------------------------------------
  96. HRESULT
  97. CXBarGlyph::LoadGlyphFile(LPCTSTR pszPath, BOOL fSmall)
  98. {
  99. // ISSUE/010304/davidjen could be smarter and make educated guess of file format by analyzing file name
  100. // now we assume it's always an icon format
  101. USES_CONVERSION;
  102. HRESULT hr = E_FAIL;
  103. if (pszPath && *pszPath)
  104. {
  105. CString strPath = pszPath;
  106. HICON hIcon = NULL;
  107. int nBmpIndex = PathParseIconLocation((LPWSTR)T2CW(strPath));
  108. strPath.ReleaseBuffer();
  109. CString strExpPath;
  110. SHExpandEnvironmentStrings(strPath, strExpPath.GetBuffer(MAX_PATH), MAX_PATH);
  111. strExpPath.ReleaseBuffer();
  112. // If no resource id, assume it's an ico file
  113. UINT cx = fSmall ? CX_SMALL_ICON : CX_LARGE_ICON;
  114. if (nBmpIndex == 0)
  115. {
  116. hIcon = (HICON)LoadImage(0, strExpPath, IMAGE_ICON, cx, cx, LR_LOADFROMFILE);
  117. }
  118. if (hIcon == NULL)
  119. {
  120. // try loading as a embedded icon file
  121. HINSTANCE hInst = LoadLibraryEx(strExpPath, NULL, LOAD_LIBRARY_AS_DATAFILE);
  122. if (hInst)
  123. {
  124. hIcon = (HICON)LoadImage(hInst, MAKEINTRESOURCE(nBmpIndex), IMAGE_ICON, cx, cx, LR_DEFAULTCOLOR);
  125. FreeLibrary(hInst);
  126. }
  127. }
  128. if (hIcon != NULL)
  129. {
  130. // ISSUE/010304/davidjen
  131. // assume that we only have non-alpha icons, could be smarter and look at bitmap
  132. hr = SetIcon(hIcon, false);
  133. }
  134. }
  135. else {
  136. hr = E_INVALIDARG;
  137. }
  138. return hr;
  139. }
  140. //------------------------------------------------------------------------
  141. HRESULT
  142. CXBarGlyph::LoadDefaultGlyph(BOOL fSmall, BOOL fHot)
  143. {
  144. HRESULT hr = E_FAIL;
  145. UINT id = ((SHGetCurColorRes() <= 8) ? IDB_IETOOLBAR: IDB_IETOOLBARHICOLOR);
  146. id += (fSmall ? 2 : 0) + (fHot ? 1 : 0);
  147. UINT cx = fSmall ? CX_SMALL_ICON : CX_LARGE_ICON;
  148. // We should use a cached default icon, rather than repeatedly crafting the default icon ourselves
  149. HICON hIcon = NULL;
  150. HIMAGELIST himl = ImageList_LoadImage(HINST_THISDLL,
  151. MAKEINTRESOURCE(id), cx, 0,
  152. RGB(255, 0, 255),
  153. IMAGE_BITMAP, LR_CREATEDIBSECTION);
  154. if (himl)
  155. {
  156. hIcon = ImageList_GetIcon(himl, IBAR_ICON_DEFAULT, ILD_NORMAL);
  157. hr = SetIcon(hIcon, false); // know that this is always non-alpha channel bitmap
  158. ImageList_Destroy(himl);
  159. }
  160. return hr;
  161. }
  162. //------------------------------------------------------------------------
  163. HRESULT
  164. CXBarGlyph::Draw(HDC hdc, int x, int y)
  165. {
  166. if (_hbmpColor)
  167. {
  168. BITMAP bm;
  169. GetObject(_hbmpColor, sizeof(bm), &bm);
  170. if (_fAlpha && (bm.bmBitsPixel >= 32) && IsOS(OS_WHISTLERORGREATER))
  171. {
  172. DrawAlphaBitmap(hdc, x, y, GetWidth(), GetHeight(), _hbmpColor);
  173. }
  174. else
  175. {
  176. DrawTransparentBitmap(hdc, x, y, _hbmpColor, _hbmpMask);
  177. }
  178. }
  179. else
  180. {
  181. return S_FALSE; // no glyph
  182. }
  183. return S_OK;
  184. }
  185. //------------------------------------------------------------------------
  186. void
  187. CXBarGlyph::_EnsureDimensions(void)
  188. {
  189. if (_hbmpColor == NULL)
  190. {
  191. _lWidth = _lHeight = 0;
  192. return;
  193. }
  194. // update dimensions of glyph
  195. if ((_lWidth <= 0) || (_lHeight <= 0))
  196. {
  197. BITMAP bm;
  198. GetObject(_hbmpColor, sizeof(bm), &bm);
  199. _lWidth = bm.bmWidth;
  200. _lHeight = bm.bmHeight;
  201. }
  202. }