home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_VC.50 / VC / MFC / SRC / CTLFONT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-30  |  6.0 KB  |  278 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1997 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 AFXCTL_CORE2_SEG
  14. #pragma code_seg(AFXCTL_CORE2_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. // CFontHolder
  26.  
  27. CFontHolder::CFontHolder(LPPROPERTYNOTIFYSINK pNotify) :
  28.     m_pFont(NULL),
  29.     m_dwConnectCookie(0),
  30.     m_pNotify(pNotify)
  31. {
  32.     ASSERT_NULL_OR_POINTER(pNotify, IPropertyNotifySink);
  33. }
  34.  
  35. CFontHolder::~CFontHolder()
  36. {
  37.     ReleaseFont();
  38. }
  39.  
  40. void CFontHolder::ReleaseFont()
  41. {
  42.     if ((m_pFont != NULL) && (m_pNotify != NULL))
  43.     {
  44.         AfxConnectionUnadvise(m_pFont, IID_IPropertyNotifySink, m_pNotify,
  45.             FALSE, m_dwConnectCookie);
  46.     }
  47.  
  48.     RELEASE(m_pFont);
  49. }
  50.  
  51. static const FONTDESC _fdDefault =
  52.     { sizeof(FONTDESC), OLESTR("MS Sans Serif"), FONTSIZE(12), FW_NORMAL,
  53.       DEFAULT_CHARSET, FALSE, FALSE, FALSE };
  54.  
  55. void CFontHolder::InitializeFont(const FONTDESC* pFontDesc,
  56.     LPDISPATCH pFontDispAmbient)
  57. {
  58.     ASSERT_NULL_OR_POINTER(pFontDesc, FONTDESC);
  59.     ASSERT_NULL_OR_POINTER(pFontDispAmbient, IDispatch);
  60. #ifdef _DEBUG
  61.     if (pFontDesc != NULL)
  62.         ASSERT(pFontDesc->cbSizeofstruct == sizeof(FONTDESC));
  63. #endif
  64.  
  65.     // Release any previous font, in preparation for creating a new one.
  66.     ReleaseFont();
  67.  
  68.     LPFONT pFontAmbient;
  69.     LPFONT pFontNew = NULL;
  70.  
  71.     if ((pFontDispAmbient != NULL) &&
  72.         SUCCEEDED(pFontDispAmbient->QueryInterface(IID_IFont,
  73.                 (LPVOID*)&pFontAmbient)))
  74.     {
  75.         ASSERT_POINTER(pFontAmbient, IFont);
  76.  
  77.         // Make a clone of the ambient font.
  78.         pFontAmbient->Clone(&pFontNew);
  79.         pFontAmbient->Release();
  80.     }
  81.     else
  82.     {
  83.         // Create the font.
  84.         if (pFontDesc == NULL)
  85.             pFontDesc = &_fdDefault;
  86.  
  87.         if (FAILED(_AfxOleCreateFontIndirect((LPFONTDESC)pFontDesc, IID_IFont,
  88.                 (LPVOID *)&pFontNew)))
  89.             pFontNew = NULL;
  90.     }
  91.  
  92.     // Setup advisory connection and find dispatch interface.
  93.     if (pFontNew != NULL)
  94.         SetFont(pFontNew);
  95. }
  96.  
  97. BOOL AFXAPI _AfxIsSameFont(CFontHolder& font, const FONTDESC* pFontDesc,
  98.     LPFONTDISP pFontDispAmbient)
  99. {
  100.     if (font.m_pFont == NULL)
  101.         return FALSE;
  102.  
  103.     BOOL bSame = FALSE;
  104.  
  105.     if (pFontDispAmbient != NULL)
  106.     {
  107.         LPFONT pFontAmbient;
  108.         if (SUCCEEDED(pFontDispAmbient->QueryInterface(IID_IFont,
  109.             (LPVOID*)&pFontAmbient)))
  110.         {
  111.             ASSERT_POINTER(pFontAmbient, IFont);
  112.             bSame = pFontAmbient->IsEqual(font.m_pFont) == S_OK;
  113.             pFontAmbient->Release();
  114.         }
  115.     }
  116.     else
  117.     {
  118.         if (pFontDesc == NULL)
  119.             pFontDesc = &_fdDefault;
  120.  
  121.         bSame = TRUE;
  122.         BOOL bFlag;
  123.  
  124.         font.m_pFont->get_Italic(&bFlag);
  125.         bSame = (bFlag == pFontDesc->fItalic);
  126.  
  127.         if (bSame)
  128.         {
  129.             font.m_pFont->get_Underline(&bFlag);
  130.             bSame = (bFlag == pFontDesc->fUnderline);
  131.         }
  132.  
  133.         if (bSame)
  134.         {
  135.             font.m_pFont->get_Strikethrough(&bFlag);
  136.             bSame = (bFlag == pFontDesc->fStrikethrough);
  137.         }
  138.  
  139.         if (bSame)
  140.         {
  141.             short sCharset;
  142.             font.m_pFont->get_Charset(&sCharset);
  143.             bSame = (sCharset == pFontDesc->sCharset);
  144.         }
  145.  
  146.         if (bSame)
  147.         {
  148.             short sWeight;
  149.             font.m_pFont->get_Weight(&sWeight);
  150.             bSame = (sWeight == pFontDesc->sWeight);
  151.         }
  152.  
  153.         if (bSame)
  154.         {
  155.             CURRENCY cy;
  156.             font.m_pFont->get_Size(&cy);
  157.             bSame = (memcmp(&cy, &pFontDesc->cySize, sizeof(CURRENCY)) == 0);
  158.         }
  159.  
  160.         if (bSame)
  161.         {
  162.             BSTR bstrName;
  163.             font.m_pFont->get_Name(&bstrName);
  164.             CString strName1(bstrName);
  165.             CString strName2(pFontDesc->lpstrName);
  166.             bSame = (strName1 == strName2);
  167.             SysFreeString(bstrName);
  168.         }
  169.     }
  170.  
  171.     return bSame;
  172. }
  173.  
  174. HFONT CFontHolder::GetFontHandle()
  175. {
  176.     // Assume a screen DC for logical/himetric ratio.
  177.     return GetFontHandle(afxData.cyPixelsPerInch, HIMETRIC_PER_INCH);
  178. }
  179.  
  180. HFONT CFontHolder::GetFontHandle(long cyLogical, long cyHimetric)
  181. {
  182.     HFONT hFont = NULL;
  183.  
  184.     if ((m_pFont != NULL) &&
  185.         SUCCEEDED(m_pFont->SetRatio(cyLogical, cyHimetric)) &&
  186.         SUCCEEDED(m_pFont->get_hFont(&hFont)))
  187.     {
  188.         ASSERT(hFont != NULL);
  189.     }
  190.  
  191.     return hFont;
  192. }
  193.  
  194. CFont* CFontHolder::Select(CDC* pDC, long cyLogical, long cyHimetric)
  195. {
  196.     ASSERT_POINTER(pDC, CDC);
  197.  
  198.     HFONT hFont = NULL;
  199.  
  200.     if (m_pFont != NULL)
  201.         hFont = GetFontHandle(cyLogical, cyHimetric);
  202.  
  203.     if (hFont != NULL)
  204.     {
  205.         if ((pDC->m_hAttribDC != pDC->m_hDC) &&
  206.             (pDC->m_hAttribDC != NULL))
  207.         {
  208.             ::SelectObject(pDC->m_hAttribDC, hFont);
  209.         }
  210.  
  211.         return CFont::FromHandle((HFONT)::SelectObject(pDC->m_hDC, hFont));
  212.     }
  213.  
  214.     return NULL;
  215. }
  216.  
  217. void CFontHolder::QueryTextMetrics(LPTEXTMETRIC lptm)
  218. {
  219.     ASSERT(lptm != NULL);
  220.  
  221.     if (m_pFont != NULL)
  222.     {
  223. #if defined(_UNICODE) || defined(OLE2ANSI)
  224.         // no conversion necessary
  225.         m_pFont->QueryTextMetrics(lptm);
  226. #else
  227.         TEXTMETRICW tmw;
  228.         m_pFont->QueryTextMetrics(&tmw);
  229.         AfxTextMetricW2A(lptm, &tmw);
  230. #endif
  231.     }
  232.     else
  233.     {
  234.         memset(lptm, 0, sizeof(TEXTMETRIC));
  235.     }
  236. }
  237.  
  238. LPFONTDISP CFontHolder::GetFontDispatch()
  239. {
  240.     LPFONTDISP pFontDisp = NULL;
  241.  
  242.     if ((m_pFont != NULL) &&
  243.         SUCCEEDED(m_pFont->QueryInterface(IID_IFontDisp, (LPVOID*)&pFontDisp)))
  244.     {
  245.         ASSERT_POINTER(pFontDisp, IFontDisp);
  246.     }
  247.  
  248.     return pFontDisp;
  249. }
  250.  
  251. void CFontHolder::SetFont(LPFONT pFontNew)
  252. {
  253.     ASSERT_NULL_OR_POINTER(pFontNew, IFont);
  254.  
  255.     if (m_pFont != NULL)
  256.         ReleaseFont();
  257.  
  258.     m_pFont = pFontNew;
  259.  
  260.     if (m_pNotify != NULL)
  261.     {
  262.         AfxConnectionAdvise(m_pFont, IID_IPropertyNotifySink, m_pNotify,
  263.             FALSE, &m_dwConnectCookie);
  264.     }
  265. }
  266.  
  267. BOOL CFontHolder::GetDisplayString(CString& strValue)
  268. {
  269.     return strValue.LoadString(AFX_IDS_DISPLAYSTRING_FONT);
  270. }
  271.  
  272. /////////////////////////////////////////////////////////////////////////////
  273. // Force any extra compiler-generated code into AFX_INIT_SEG
  274.  
  275. #ifdef AFX_INIT_SEG
  276. #pragma code_seg(AFX_INIT_SEG)
  277. #endif
  278.