home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / dlgfnt.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-16  |  6.8 KB  |  264 lines

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