dlgtempl.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10. #include "stdafx.h"
  11. #ifdef AFX_CORE1_SEG
  12. #pragma code_seg(AFX_CORE1_SEG)
  13. #endif
  14. #ifdef _DEBUG
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18. #define new DEBUG_NEW
  19. /////////////////////////////////////////////////////////////////////////////
  20. // _AfxConvertDialogUnitsToPixels
  21. AFX_STATIC void AFXAPI _AfxConvertDialogUnitsToPixels(LPCTSTR pszFontFace, WORD wFontSize,
  22. int cxDlg, int cyDlg, SIZE* pSizePixel)
  23. {
  24. // Attempt to create the font to be used in the dialog box
  25. UINT cxSysChar, cySysChar;
  26. LOGFONT lf;
  27. HDC hDC = ::GetDC(NULL);
  28. memset(&lf, 0, sizeof(LOGFONT));
  29. lf.lfHeight = -MulDiv(wFontSize, GetDeviceCaps(hDC, LOGPIXELSY), 72);
  30. lf.lfWeight = FW_NORMAL;
  31. lf.lfCharSet = DEFAULT_CHARSET;
  32. lstrcpy(lf.lfFaceName, pszFontFace);
  33. HFONT hNewFont = CreateFontIndirect(&lf);
  34. if (hNewFont != NULL)
  35. {
  36. HFONT hFontOld = (HFONT)SelectObject(hDC, hNewFont);
  37. TEXTMETRIC tm;
  38. GetTextMetrics(hDC, &tm);
  39. cySysChar = tm.tmHeight + tm.tmExternalLeading;
  40. SIZE size;
  41. ::GetTextExtentPoint32(hDC,
  42. _T("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"), 52,
  43. &size);
  44. cxSysChar = (size.cx + 26) / 52;
  45. SelectObject(hDC, hFontOld);
  46. DeleteObject(hNewFont);
  47. }
  48. else
  49. {
  50. // Could not create the font so just use the system's values
  51. cxSysChar = LOWORD(GetDialogBaseUnits());
  52. cySysChar = HIWORD(GetDialogBaseUnits());
  53. }
  54. ::ReleaseDC(NULL, hDC);
  55. // Translate dialog units to pixels
  56. pSizePixel->cx = MulDiv(cxDlg, cxSysChar, 4);
  57. pSizePixel->cy = MulDiv(cyDlg, cySysChar, 8);
  58. }
  59. /////////////////////////////////////////////////////////////////////////////
  60. // IsDialogEx
  61. AFX_STATIC inline BOOL IsDialogEx(const DLGTEMPLATE* pTemplate)
  62. {
  63. return ((DLGTEMPLATEEX*)pTemplate)->signature == 0xFFFF;
  64. }
  65. /////////////////////////////////////////////////////////////////////////////
  66. // HasFont
  67. AFX_STATIC inline BOOL HasFont(const DLGTEMPLATE* pTemplate)
  68. {
  69. return (DS_SETFONT &
  70. (IsDialogEx(pTemplate) ? ((DLGTEMPLATEEX*)pTemplate)->style :
  71. pTemplate->style));
  72. }
  73. /////////////////////////////////////////////////////////////////////////////
  74. // FontAttrSize
  75. AFX_STATIC inline int FontAttrSize(BOOL bDialogEx)
  76. {
  77. return sizeof(WORD) * (bDialogEx ? 3 : 1);
  78. }
  79. /////////////////////////////////////////////////////////////////////////////
  80. // CDialogTemplate - implementation class
  81. CDialogTemplate::CDialogTemplate(const DLGTEMPLATE* pTemplate)
  82. {
  83. if (pTemplate == NULL)
  84. {
  85. m_hTemplate = NULL;
  86. m_dwTemplateSize = 0;
  87. m_bSystemFont = FALSE;
  88. }
  89. else
  90. {
  91. SetTemplate(pTemplate, GetTemplateSize(pTemplate));
  92. }
  93. }
  94. CDialogTemplate::CDialogTemplate(HGLOBAL hTemplate)
  95. {
  96. if (hTemplate == NULL)
  97. {
  98. m_hTemplate = NULL;
  99. m_dwTemplateSize = 0;
  100. m_bSystemFont = FALSE;
  101. }
  102. else
  103. {
  104. DLGTEMPLATE* pTemplate = (DLGTEMPLATE*)GlobalLock(hTemplate);
  105. SetTemplate(pTemplate, GetTemplateSize(pTemplate));
  106. GlobalUnlock(hTemplate);
  107. }
  108. }
  109. BOOL CDialogTemplate::SetTemplate(const DLGTEMPLATE* pTemplate, UINT cb)
  110. {
  111. m_dwTemplateSize = cb;
  112. if ((m_hTemplate = GlobalAlloc(GPTR, m_dwTemplateSize + LF_FACESIZE * 2)) == NULL)
  113. return FALSE;
  114. DLGTEMPLATE* pNew = (DLGTEMPLATE*)GlobalLock(m_hTemplate);
  115. memcpy((BYTE*)pNew, pTemplate, (size_t)m_dwTemplateSize);
  116. m_bSystemFont = (::HasFont(pNew) == 0);
  117. GlobalUnlock(m_hTemplate);
  118. return TRUE;
  119. }
  120. CDialogTemplate::~CDialogTemplate()
  121. {
  122. if (m_hTemplate != NULL)
  123. GlobalFree(m_hTemplate);
  124. }
  125. BOOL CDialogTemplate::Load(LPCTSTR lpDialogTemplateID)
  126. {
  127. HINSTANCE hInst = AfxFindResourceHandle(lpDialogTemplateID, RT_DIALOG);
  128. if (hInst == NULL)
  129. return FALSE;
  130. HRSRC hRsrc = FindResource(hInst, lpDialogTemplateID, RT_DIALOG);
  131. if (hRsrc == NULL)
  132. return FALSE;
  133. HGLOBAL hTemplate = LoadResource(hInst, hRsrc);
  134. DLGTEMPLATE* pTemplate = (DLGTEMPLATE*)LockResource(hTemplate);
  135. SetTemplate(pTemplate, (UINT)SizeofResource(hInst, hRsrc));
  136. UnlockResource(hTemplate);
  137. FreeResource(hTemplate);
  138. return TRUE;
  139. }
  140. HGLOBAL CDialogTemplate::Detach()
  141. {
  142. HGLOBAL hTmp = m_hTemplate;
  143. m_hTemplate = NULL;
  144. return hTmp;
  145. }
  146. BOOL CDialogTemplate::HasFont() const
  147. {
  148. DLGTEMPLATE* pTemplate = (DLGTEMPLATE*)GlobalLock(m_hTemplate);
  149. BOOL bHasFont = ::HasFont(pTemplate);
  150. GlobalUnlock(m_hTemplate);
  151. return bHasFont;
  152. }
  153. inline WCHAR* _SkipString(WCHAR* p)
  154. {
  155. while (*p++);
  156. return p;
  157. }
  158. BYTE* AFX_CDECL CDialogTemplate::GetFontSizeField(const DLGTEMPLATE* pTemplate)
  159. {
  160. BOOL bDialogEx = IsDialogEx(pTemplate);
  161. WORD* pw;
  162. if (bDialogEx)
  163. pw = (WORD*)((DLGTEMPLATEEX*)pTemplate + 1);
  164. else
  165. pw = (WORD*)(pTemplate + 1);
  166. if (*pw == (WORD)-1) // Skip menu name string or ordinal
  167. pw += 2; // WORDs
  168. else
  169. while(*pw++);
  170. if (*pw == (WORD)-1) // Skip class name string or ordinal
  171. pw += 2; // WORDs
  172. else
  173. while(*pw++);
  174. while (*pw++); // Skip caption string
  175. return (BYTE*)pw;
  176. }
  177. UINT AFX_CDECL CDialogTemplate::GetTemplateSize(const DLGTEMPLATE* pTemplate)
  178. {
  179. BOOL bDialogEx = IsDialogEx(pTemplate);
  180. BYTE* pb = GetFontSizeField(pTemplate);
  181. if (::HasFont(pTemplate))
  182. {
  183. // Skip font size and name
  184. pb += FontAttrSize(bDialogEx); // Skip font size, weight, (italic, charset)
  185. pb += 2 * (wcslen((WCHAR*)pb) + 1);
  186. }
  187. WORD nCtrl = bDialogEx ? (WORD)((DLGTEMPLATEEX*)pTemplate)->cDlgItems :
  188. (WORD)pTemplate->cdit;
  189. while (nCtrl > 0)
  190. {
  191. pb = (BYTE*)(((DWORD)pb + 3) & ~3); // DWORD align
  192. pb += (bDialogEx ? sizeof(DLGITEMTEMPLATEEX) : sizeof(DLGITEMTEMPLATE));
  193. if (*(WORD*)pb == (WORD)-1) // Skip class name string or ordinal
  194. pb += 2 * sizeof(WORD);
  195. else
  196. pb = (BYTE*)_SkipString((WCHAR*)pb);
  197. if (*(WORD*)pb == (WORD)-1) // Skip text string or ordinal
  198. pb += 2 * sizeof(WORD);
  199. else
  200. pb = (BYTE*)_SkipString((WCHAR*)pb);
  201. WORD cbExtra = *(WORD*)pb; // Skip extra data
  202. pb += sizeof(WORD) + cbExtra;
  203. --nCtrl;
  204. }
  205. return pb - (BYTE*)pTemplate;
  206. }
  207. BOOL AFX_CDECL CDialogTemplate::GetFont(const DLGTEMPLATE* pTemplate,
  208. CString& strFace, WORD& nFontSize)
  209. {
  210. ASSERT(pTemplate != NULL);
  211. if (!::HasFont(pTemplate))
  212. return FALSE;
  213. BYTE* pb = GetFontSizeField(pTemplate);
  214. nFontSize = *(WORD*)pb;
  215. pb += FontAttrSize(IsDialogEx(pTemplate));
  216. #if defined(_UNICODE) || defined(OLE2ANSI)
  217. // Copy font name
  218. strFace = (LPCTSTR)pb;
  219. #else
  220. // Convert Unicode font name to MBCS
  221. WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)pb, -1,
  222. strFace.GetBufferSetLength(LF_FACESIZE), LF_FACESIZE, NULL, NULL);
  223. strFace.ReleaseBuffer();
  224. #endif
  225. return TRUE;
  226. }
  227. BOOL CDialogTemplate::GetFont(CString& strFace, WORD& nFontSize) const
  228. {
  229. ASSERT(m_hTemplate != NULL);
  230. DLGTEMPLATE* pTemplate = (DLGTEMPLATE*)GlobalLock(m_hTemplate);
  231. BOOL bResult = GetFont(pTemplate, strFace, nFontSize);
  232. GlobalUnlock(m_hTemplate);
  233. return bResult;
  234. }
  235. BOOL CDialogTemplate::SetFont(LPCTSTR lpFaceName, WORD nFontSize)
  236. {
  237. ASSERT(m_hTemplate != NULL);
  238. if (m_dwTemplateSize == 0)
  239. return FALSE;
  240. DLGTEMPLATE* pTemplate = (DLGTEMPLATE*)GlobalLock(m_hTemplate);
  241. BOOL bDialogEx = IsDialogEx(pTemplate);
  242. BOOL bHasFont = ::HasFont(pTemplate);
  243. int cbFontAttr = FontAttrSize(bDialogEx);
  244. if (bDialogEx)
  245. ((DLGTEMPLATEEX*)pTemplate)->style |= DS_SETFONT;
  246. else
  247. pTemplate->style |= DS_SETFONT;
  248. #ifdef _UNICODE
  249. int cbNew = cbFontAttr + ((lstrlen(lpFaceName) + 1) * sizeof(TCHAR));
  250. BYTE* pbNew = (BYTE*)lpFaceName;
  251. #else
  252. WCHAR wszFaceName [LF_FACESIZE];
  253. int cbNew = cbFontAttr + 2 * MultiByteToWideChar(CP_ACP, 0, lpFaceName, -1, wszFaceName, LF_FACESIZE);
  254. BYTE* pbNew = (BYTE*)wszFaceName;
  255. #endif
  256. BYTE* pb = GetFontSizeField(pTemplate);
  257. int cbOld = bHasFont ? cbFontAttr + 2 * (wcslen((WCHAR*)(pb + cbFontAttr)) + 1) : 0;
  258. BYTE* pOldControls = (BYTE*)(((DWORD)pb + cbOld + 3) & ~3);
  259. BYTE* pNewControls = (BYTE*)(((DWORD)pb + cbNew + 3) & ~3);
  260. WORD nCtrl = bDialogEx ? (WORD)((DLGTEMPLATEEX*)pTemplate)->cDlgItems :
  261. (WORD)pTemplate->cdit;
  262. if (cbNew != cbOld && nCtrl > 0)
  263. memmove(pNewControls, pOldControls, (size_t)(m_dwTemplateSize - (pOldControls - (BYTE*)pTemplate)));
  264. *(WORD*)pb = nFontSize;
  265. memmove(pb + cbFontAttr, pbNew, cbNew - cbFontAttr);
  266. m_dwTemplateSize += (pNewControls - pOldControls);
  267. GlobalUnlock(m_hTemplate);
  268. m_bSystemFont = FALSE;
  269. return TRUE;
  270. }
  271. BOOL CDialogTemplate::SetSystemFont(WORD wSize)
  272. {
  273. LPCTSTR pszFace = _T("System");
  274. WORD wDefSize = 10;
  275. HFONT hFont = (HFONT)::GetStockObject(DEFAULT_GUI_FONT);
  276. if (hFont == NULL)
  277. hFont = (HFONT)::GetStockObject(SYSTEM_FONT);
  278. if (hFont != NULL)
  279. {
  280. LOGFONT lf;
  281. if (::GetObject(hFont, sizeof(LOGFONT), &lf) != 0)
  282. {
  283. pszFace = lf.lfFaceName;
  284. HDC hDC = ::GetDC(NULL);
  285. if (lf.lfHeight < 0)
  286. lf.lfHeight = -lf.lfHeight;
  287. wDefSize = (WORD)MulDiv(lf.lfHeight, 72, GetDeviceCaps(hDC, LOGPIXELSY));
  288. ::ReleaseDC(NULL, hDC);
  289. }
  290. }
  291. if (wSize == 0)
  292. wSize = wDefSize;
  293. return SetFont(pszFace, wSize);
  294. }
  295. void CDialogTemplate::GetSizeInDialogUnits(SIZE* pSize) const
  296. {
  297. ASSERT(m_hTemplate != NULL);
  298. ASSERT_POINTER(pSize, SIZE);
  299. DLGTEMPLATE* pTemplate = (DLGTEMPLATE*)GlobalLock(m_hTemplate);
  300. if (IsDialogEx(pTemplate))
  301. {
  302. pSize->cx = ((DLGTEMPLATEEX*)pTemplate)->cx;
  303. pSize->cy = ((DLGTEMPLATEEX*)pTemplate)->cy;
  304. }
  305. else
  306. {
  307. pSize->cx = pTemplate->cx;
  308. pSize->cy = pTemplate->cy;
  309. }
  310. GlobalUnlock(m_hTemplate);
  311. }
  312. void CDialogTemplate::GetSizeInPixels(SIZE* pSize) const
  313. {
  314. ASSERT(m_hTemplate != NULL);
  315. ASSERT_POINTER(pSize, SIZE);
  316. if (m_bSystemFont)
  317. {
  318. GetSizeInDialogUnits(pSize);
  319. DWORD dwDLU = GetDialogBaseUnits();
  320. pSize->cx = (pSize->cx * LOWORD(dwDLU)) / 4;
  321. pSize->cy = (pSize->cy * HIWORD(dwDLU)) / 8;
  322. }
  323. else
  324. {
  325. CString strFace;
  326. WORD wSize;
  327. GetFont(strFace, wSize);
  328. SIZE size;
  329. GetSizeInDialogUnits(&size);
  330. _AfxConvertDialogUnitsToPixels(strFace, wSize, size.cx, size.cy, pSize);
  331. }
  332. }