dlgfnt.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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_AUX_SEG
  12. #pragma code_seg(AFX_AUX_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. // Choose Font dialog
  21. CFontDialog::CFontDialog(LPLOGFONT lplfInitial, DWORD dwFlags, CDC* pdcPrinter,
  22. CWnd* pParentWnd) : CCommonDialog(pParentWnd)
  23. {
  24. memset(&m_cf, 0, sizeof(m_cf));
  25. memset(&m_lf, 0, sizeof(m_lf));
  26. memset(&m_szStyleName, 0, sizeof(m_szStyleName));
  27. m_nIDHelp = AFX_IDD_FONT;
  28. m_cf.lStructSize = sizeof(m_cf);
  29. m_cf.lpszStyle = (LPTSTR)&m_szStyleName;
  30. m_cf.Flags = dwFlags | CF_ENABLEHOOK;
  31. if (!afxData.bWin4 && AfxHelpEnabled())
  32. m_cf.Flags |= CF_SHOWHELP;
  33. m_cf.lpfnHook = (COMMDLGPROC)_AfxCommDlgProc;
  34. if (lplfInitial)
  35. {
  36. m_cf.lpLogFont = lplfInitial;
  37. m_cf.Flags |= CF_INITTOLOGFONTSTRUCT;
  38. memcpy(&m_lf, m_cf.lpLogFont, sizeof(m_lf));
  39. }
  40. else
  41. {
  42. m_cf.lpLogFont = &m_lf;
  43. }
  44. if (pdcPrinter)
  45. {
  46. ASSERT(pdcPrinter->m_hDC != NULL);
  47. m_cf.hDC = pdcPrinter->m_hDC;
  48. m_cf.Flags |= CF_PRINTERFONTS;
  49. }
  50. }
  51. #ifndef _AFX_NO_RICHEDIT_SUPPORT
  52. CFontDialog::CFontDialog(const CHARFORMAT& charformat, DWORD dwFlags,
  53. CDC* pdcPrinter, CWnd* pParentWnd) : CCommonDialog(pParentWnd)
  54. {
  55. memset(&m_cf, 0, sizeof(m_cf));
  56. memset(&m_lf, 0, sizeof(m_lf));
  57. memset(&m_szStyleName, 0, sizeof(m_szStyleName));
  58. m_nIDHelp = AFX_IDD_FONT;
  59. m_cf.lStructSize = sizeof(m_cf);
  60. m_cf.lpszStyle = (LPTSTR)&m_szStyleName;
  61. m_cf.Flags = dwFlags | CF_ENABLEHOOK | CF_INITTOLOGFONTSTRUCT;
  62. m_cf.Flags |= FillInLogFont(charformat);
  63. if (!afxData.bWin4 && AfxHelpEnabled())
  64. m_cf.Flags |= CF_SHOWHELP;
  65. m_cf.lpfnHook = (COMMDLGPROC)_AfxCommDlgProc;
  66. m_cf.lpLogFont = &m_lf;
  67. if (pdcPrinter)
  68. {
  69. ASSERT(pdcPrinter->m_hDC != NULL);
  70. m_cf.hDC = pdcPrinter->m_hDC;
  71. m_cf.Flags |= CF_PRINTERFONTS;
  72. }
  73. if (charformat.dwMask & CFM_COLOR)
  74. m_cf.rgbColors = charformat.crTextColor;
  75. }
  76. #endif
  77. int CFontDialog::DoModal()
  78. {
  79. ASSERT_VALID(this);
  80. ASSERT(m_cf.Flags & CF_ENABLEHOOK);
  81. ASSERT(m_cf.lpfnHook != NULL); // can still be a user hook
  82. m_cf.hwndOwner = PreModal();
  83. int nResult = ::ChooseFont(&m_cf);
  84. PostModal();
  85. if (nResult == IDOK)
  86. {
  87. // copy logical font from user's initialization buffer (if needed)
  88. memcpy(&m_lf, m_cf.lpLogFont, sizeof(m_lf));
  89. return IDOK;
  90. }
  91. return nResult ? nResult : IDCANCEL;
  92. }
  93. void CFontDialog::GetCurrentFont(LPLOGFONT lplf)
  94. {
  95. ASSERT(lplf != NULL);
  96. if (m_hWnd != NULL)
  97. SendMessage(WM_CHOOSEFONT_GETLOGFONT, 0, (DWORD)(LPVOID)lplf);
  98. else
  99. *lplf = m_lf;
  100. }
  101. #ifndef _AFX_NO_RICHEDIT_SUPPORT
  102. ////////////////////////////////////////////////////////////////////////////
  103. // CFontDialog CHARFORMAT helpers
  104. DWORD CFontDialog::FillInLogFont(const CHARFORMAT& cf)
  105. {
  106. USES_CONVERSION;
  107. DWORD dwFlags = 0;
  108. if (cf.dwMask & CFM_SIZE)
  109. {
  110. CDC dc;
  111. dc.CreateDC(_T("DISPLAY"), NULL, NULL, NULL);
  112. LONG yPerInch = dc.GetDeviceCaps(LOGPIXELSY);
  113. m_lf.lfHeight = -(int) ((cf.yHeight * yPerInch) / 1440);
  114. }
  115. else
  116. m_lf.lfHeight = 0;
  117. m_lf.lfWidth = 0;
  118. m_lf.lfEscapement = 0;
  119. m_lf.lfOrientation = 0;
  120. if ((cf.dwMask & (CFM_ITALIC|CFM_BOLD)) == (CFM_ITALIC|CFM_BOLD))
  121. {
  122. m_lf.lfWeight = (cf.dwEffects & CFE_BOLD) ? FW_BOLD : FW_NORMAL;
  123. m_lf.lfItalic = (BYTE)((cf.dwEffects & CFE_ITALIC) ? TRUE : FALSE);
  124. }
  125. else
  126. {
  127. dwFlags |= CF_NOSTYLESEL;
  128. m_lf.lfWeight = FW_DONTCARE;
  129. m_lf.lfItalic = FALSE;
  130. }
  131. if ((cf.dwMask & (CFM_UNDERLINE|CFM_STRIKEOUT|CFM_COLOR)) ==
  132. (CFM_UNDERLINE|CFM_STRIKEOUT|CFM_COLOR))
  133. {
  134. dwFlags |= CF_EFFECTS;
  135. m_lf.lfUnderline = (BYTE)((cf.dwEffects & CFE_UNDERLINE) ? TRUE : FALSE);
  136. m_lf.lfStrikeOut = (BYTE)((cf.dwEffects & CFE_STRIKEOUT) ? TRUE : FALSE);
  137. }
  138. else
  139. {
  140. m_lf.lfUnderline = (BYTE)FALSE;
  141. m_lf.lfStrikeOut = (BYTE)FALSE;
  142. }
  143. if (cf.dwMask & CFM_CHARSET)
  144. m_lf.lfCharSet = cf.bCharSet;
  145. else
  146. dwFlags |= CF_NOSCRIPTSEL;
  147. m_lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
  148. m_lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
  149. m_lf.lfQuality = DEFAULT_QUALITY;
  150. if (cf.dwMask & CFM_FACE)
  151. {
  152. m_lf.lfPitchAndFamily = cf.bPitchAndFamily;
  153. lstrcpy(m_lf.lfFaceName, A2T((LPSTR)cf.szFaceName));
  154. }
  155. else
  156. {
  157. m_lf.lfPitchAndFamily = DEFAULT_PITCH|FF_DONTCARE;
  158. m_lf.lfFaceName[0] = (TCHAR)0;
  159. }
  160. return dwFlags;
  161. }
  162. void CFontDialog::GetCharFormat(CHARFORMAT& cf) const
  163. {
  164. USES_CONVERSION;
  165. cf.dwEffects = 0;
  166. cf.dwMask = 0;
  167. if ((m_cf.Flags & CF_NOSTYLESEL) == 0)
  168. {
  169. cf.dwMask |= CFM_BOLD | CFM_ITALIC;
  170. cf.dwEffects |= (IsBold()) ? CFE_BOLD : 0;
  171. cf.dwEffects |= (IsItalic()) ? CFE_ITALIC : 0;
  172. }
  173. if ((m_cf.Flags & CF_NOSIZESEL) == 0)
  174. {
  175. cf.dwMask |= CFM_SIZE;
  176. //GetSize() returns in tenths of points so mulitply by 2 to get twips
  177. cf.yHeight = GetSize()*2;
  178. }
  179. if ((m_cf.Flags & CF_NOFACESEL) == 0)
  180. {
  181. cf.dwMask |= CFM_FACE;
  182. cf.bPitchAndFamily = m_cf.lpLogFont->lfPitchAndFamily;
  183. lstrcpyA(cf.szFaceName, T2A((LPTSTR)(LPCTSTR)GetFaceName()));
  184. }
  185. if (m_cf.Flags & CF_EFFECTS)
  186. {
  187. cf.dwMask |= CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR;
  188. cf.dwEffects |= (IsUnderline()) ? CFE_UNDERLINE : 0;
  189. cf.dwEffects |= (IsStrikeOut()) ? CFE_STRIKEOUT : 0;
  190. cf.crTextColor = GetColor();
  191. }
  192. if ((m_cf.Flags & CF_NOSCRIPTSEL) == 0)
  193. {
  194. cf.bCharSet = m_cf.lpLogFont->lfCharSet;
  195. cf.dwMask |= CFM_CHARSET;
  196. }
  197. cf.yOffset = 0;
  198. }
  199. #endif
  200. ////////////////////////////////////////////////////////////////////////////
  201. // CFontDialog diagnostics
  202. #ifdef _DEBUG
  203. void CFontDialog::Dump(CDumpContext& dc) const
  204. {
  205. CDialog::Dump(dc);
  206. dc << "m_cf.hwndOwner = " << (UINT)m_cf.hwndOwner;
  207. dc << "\nm_cf.hDC = " << (UINT)m_cf.hDC;
  208. dc << "\nm_cf.iPointSize = " << m_cf.iPointSize;
  209. dc << "\nm_cf.Flags = " << (LPVOID)m_cf.Flags;
  210. dc << "\nm_cf.lpszStyle = " << m_cf.lpszStyle;
  211. dc << "\nm_cf.nSizeMin = " << m_cf.nSizeMin;
  212. dc << "\nm_cf.nSizeMax = " << m_cf.nSizeMax;
  213. dc << "\nm_cf.nFontType = " << m_cf.nFontType;
  214. dc << "\nm_cf.rgbColors = " << (LPVOID)m_cf.rgbColors;
  215. if (m_cf.lpfnHook == (COMMDLGPROC)_AfxCommDlgProc)
  216. dc << "\nhook function set to standard MFC hook function";
  217. else
  218. dc << "\nhook function set to non-standard hook function";
  219. dc << "\n";
  220. }
  221. #endif //_DEBUG
  222. #ifdef AFX_INIT_SEG
  223. #pragma code_seg(AFX_INIT_SEG)
  224. #endif
  225. IMPLEMENT_DYNAMIC(CFontDialog, CDialog)
  226. ////////////////////////////////////////////////////////////////////////////