home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / ctlprop.cpp < prev    next >
C/C++ Source or Header  |  1998-06-16  |  37KB  |  1,564 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 AFXCTL_PROP_SEG
  14. #pragma code_seg(AFXCTL_PROP_SEG)
  15. #endif
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. #ifndef _DEBUG
  23. #pragma intrinsic(memset)
  24. #endif
  25.  
  26. #define new DEBUG_NEW
  27.  
  28. /////////////////////////////////////////////////////////////////////////////
  29. // Stock property mask
  30.  
  31. #define STOCKPROP_BACKCOLOR     0x00000001
  32. #define STOCKPROP_CAPTION       0x00000002
  33. #define STOCKPROP_FONT          0x00000004
  34. #define STOCKPROP_FORECOLOR     0x00000008
  35. #define STOCKPROP_TEXT          0x00000010
  36. #define STOCKPROP_BORDERSTYLE   0x00000020
  37. #define STOCKPROP_ENABLED       0x00000040
  38. #define STOCKPROP_APPEARANCE    0x00000080
  39.  
  40. AFX_STATIC_DATA const DWORD _afxStockProps[] =
  41. {
  42.     STOCKPROP_BACKCOLOR,    // -501
  43.     0,                      // -502
  44.     0,                      // -503
  45.     STOCKPROP_BORDERSTYLE,  // -504
  46.     0,                      // -505
  47.     0,                      // -506
  48.     0,                      // -507
  49.     0,                      // -508
  50.     0,                      // -509
  51.     0,                      // -510
  52.     0,                      // -511
  53.     STOCKPROP_FONT,         // -512
  54.     STOCKPROP_FORECOLOR,    // -513
  55.     STOCKPROP_ENABLED,      // -514
  56.     0,                      // -515
  57.     0,                      // -516
  58.     STOCKPROP_TEXT,         // -517
  59.     STOCKPROP_CAPTION,      // -518
  60.     0,                      // -519
  61.     STOCKPROP_APPEARANCE,   // -520
  62. };
  63.  
  64. void COleControl::InitStockPropMask()
  65. {
  66.     const AFX_DISPMAP* pDispMap = GetDispatchMap();
  67.     const AFX_DISPMAP_ENTRY* pEntry;
  68.     ASSERT(pDispMap != NULL);
  69.  
  70.     // If stock property mask is already initialized, we're outta here.
  71.     if (*pDispMap->lpStockPropMask != (DWORD)-1)
  72.         return;
  73.  
  74.     AfxLockGlobals(CRIT_STOCKMASK);
  75.  
  76.     if (*pDispMap->lpStockPropMask == (DWORD)-1)
  77.     {
  78.         const AFX_DISPMAP* pDispMapTop = pDispMap;
  79.         DWORD dwStockPropMask = 0;
  80.  
  81.         while (pDispMap != NULL)
  82.         {
  83.             pEntry = pDispMap->lpEntries;
  84.             while (pEntry->nPropOffset != -1)
  85.             {
  86.                 int nIndex = DISPID_BACKCOLOR - pEntry->lDispID;
  87.                 DWORD dwFlag;
  88.                 if (nIndex >= 0 && nIndex < _countof(_afxStockProps) &&
  89.                     (dwFlag = _afxStockProps[nIndex]) != 0)
  90.                 {
  91.                     dwStockPropMask |= dwFlag;
  92.                 }
  93.  
  94.                 ++pEntry;
  95.             }
  96.             // check base class
  97. #ifdef _AFXDLL
  98.             pDispMap = (*pDispMap->pfnGetBaseMap)();
  99. #else
  100.             pDispMap = pDispMap->pBaseMap;
  101. #endif
  102.         }
  103.  
  104.         *pDispMapTop->lpStockPropMask = dwStockPropMask;
  105.     }
  106.  
  107.     AfxUnlockGlobals(CRIT_STOCKMASK);
  108. }
  109.  
  110. AFX_STATIC void AFXAPI _AfxToggleBorderStyle(CWnd* pWnd)
  111. {
  112.     if (pWnd->m_hWnd != NULL)
  113.     {
  114.         // toggle border style and force redraw of border
  115.         ::SetWindowLong(pWnd->m_hWnd, GWL_STYLE, pWnd->GetStyle() ^ WS_BORDER);
  116.         ::SetWindowPos(pWnd->m_hWnd, NULL, 0, 0, 0, 0,
  117.             SWP_DRAWFRAME | SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
  118.     }
  119. }
  120.  
  121. AFX_STATIC void AFXAPI _AfxToggleAppearance(CWnd* pWnd)
  122. {
  123.     if (pWnd->m_hWnd != NULL)
  124.     {
  125.         // toggle border style and force redraw of border
  126.         ::SetWindowLong(pWnd->m_hWnd, GWL_EXSTYLE, pWnd->GetExStyle() ^
  127.             WS_EX_CLIENTEDGE);
  128.         ::SetWindowPos(pWnd->m_hWnd, NULL, 0, 0, 0, 0,
  129.             SWP_DRAWFRAME | SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
  130.     }
  131. }
  132.  
  133. /////////////////////////////////////////////////////////////////////////////
  134. // Property exchange for stock properties
  135.  
  136. void COleControl::ExchangeStockProps(CPropExchange* pPX)
  137. {
  138.     BOOL bLoading = pPX->IsLoading();
  139.     DWORD dwStockPropMask = GetStockPropMask();
  140.     DWORD dwPersistMask = dwStockPropMask;
  141.  
  142.     PX_ULong(pPX, _T("_StockProps"), dwPersistMask);
  143.  
  144.     if (dwStockPropMask & (STOCKPROP_CAPTION | STOCKPROP_TEXT))
  145.     {
  146.         CString strText;
  147.  
  148.         if (dwPersistMask & (STOCKPROP_CAPTION | STOCKPROP_TEXT))
  149.         {
  150.             if (!bLoading)
  151.                 strText = InternalGetText();
  152.             if (dwStockPropMask & STOCKPROP_CAPTION)
  153.                 PX_String(pPX, _T("Caption"), strText, _T(""));
  154.             if (dwStockPropMask & STOCKPROP_TEXT)
  155.                 PX_String(pPX, _T("Text"), strText, _T(""));
  156.         }
  157.         if (bLoading)
  158.         {
  159.             TRY
  160.                 SetText(strText);
  161.             END_TRY
  162.         }
  163.     }
  164.  
  165.     if (dwStockPropMask & STOCKPROP_FORECOLOR)
  166.     {
  167.         if (dwPersistMask & STOCKPROP_FORECOLOR)
  168.             PX_Color(pPX, _T("ForeColor"), m_clrForeColor, AmbientForeColor());
  169.         else if (bLoading)
  170.             m_clrForeColor = AmbientForeColor();
  171.     }
  172.  
  173.     if (dwStockPropMask & STOCKPROP_BACKCOLOR)
  174.     {
  175.         if (dwPersistMask & STOCKPROP_BACKCOLOR)
  176.             PX_Color(pPX, _T("BackColor"), m_clrBackColor, AmbientBackColor());
  177.         else if (bLoading)
  178.             m_clrBackColor = AmbientBackColor();
  179.     }
  180.  
  181.     if (dwStockPropMask & STOCKPROP_FONT)
  182.     {
  183.         LPFONTDISP pFontDispAmbient = AmbientFont();
  184.         BOOL bChanged = TRUE;
  185.  
  186.         if (dwPersistMask & STOCKPROP_FONT)
  187.             bChanged = PX_Font(pPX, _T("Font"), m_font, NULL, pFontDispAmbient);
  188.         else if (bLoading)
  189.             m_font.InitializeFont(NULL, pFontDispAmbient);
  190.  
  191.         if (bLoading && bChanged)
  192.             OnFontChanged();
  193.  
  194.         RELEASE(pFontDispAmbient);
  195.     }
  196.  
  197.     if (dwStockPropMask & STOCKPROP_BORDERSTYLE)
  198.     {
  199.         short sBorderStyle = m_sBorderStyle;
  200.  
  201.         if (dwPersistMask & STOCKPROP_BORDERSTYLE)
  202.             PX_Short(pPX, _T("BorderStyle"), m_sBorderStyle, 0);
  203.         else if (bLoading)
  204.             m_sBorderStyle = 0;
  205.  
  206.         if (sBorderStyle != m_sBorderStyle)
  207.             _AfxToggleBorderStyle(this);
  208.     }
  209.  
  210.     if (dwStockPropMask & STOCKPROP_ENABLED)
  211.     {
  212.         BOOL bEnabled = m_bEnabled;
  213.  
  214.         if (dwPersistMask & STOCKPROP_ENABLED)
  215.             PX_Bool(pPX, _T("Enabled"), m_bEnabled, TRUE);
  216.         else if (bLoading)
  217.             m_bEnabled = TRUE;
  218.  
  219.         if ((bEnabled != m_bEnabled) && (m_hWnd != NULL))
  220.             ::EnableWindow(m_hWnd, m_bEnabled);
  221.     }
  222.  
  223.     if (dwStockPropMask & STOCKPROP_APPEARANCE)
  224.     {
  225.         short sAppearance = m_sAppearance;
  226.  
  227.         if (dwPersistMask & STOCKPROP_APPEARANCE)
  228.             PX_Short(pPX, _T("Appearance"), m_sAppearance, 0);
  229.         else if (bLoading)
  230.             m_sAppearance = AmbientAppearance();
  231.  
  232.         if (sAppearance != m_sAppearance)
  233.             _AfxToggleAppearance(this);
  234.     }
  235. }
  236.  
  237. /////////////////////////////////////////////////////////////////////////////
  238. // Serialization for stock properties
  239.  
  240. void COleControl::SerializeStockProps(CArchive& ar)
  241. {
  242.     BOOL bLoading = ar.IsLoading();
  243.     DWORD dwStockPropMask = GetStockPropMask();
  244.     DWORD dwPersistMask = dwStockPropMask;
  245.  
  246.     if (bLoading)
  247.     {
  248.         ar >> dwPersistMask;
  249.  
  250.         if (dwStockPropMask & (STOCKPROP_CAPTION | STOCKPROP_TEXT))
  251.         {
  252.             if (dwPersistMask & (STOCKPROP_CAPTION | STOCKPROP_TEXT))
  253.             {
  254.                 CString strText;
  255.                 ar >> strText;
  256.                 TRY
  257.                     SetText(strText);
  258.                 END_TRY
  259.             }
  260.             else
  261.             {
  262.                 TRY
  263.                     SetText(_T(""));
  264.                 END_TRY
  265.             }
  266.         }
  267.  
  268.         if (dwStockPropMask & STOCKPROP_FORECOLOR)
  269.         {
  270.             if (dwPersistMask & STOCKPROP_FORECOLOR)
  271.                 ar >> m_clrForeColor;
  272.             else
  273.                 m_clrForeColor = AmbientForeColor();
  274.         }
  275.  
  276.         if (dwStockPropMask & STOCKPROP_BACKCOLOR)
  277.         {
  278.             if (dwPersistMask & STOCKPROP_BACKCOLOR)
  279.                 ar >> m_clrBackColor;
  280.             else
  281.                 m_clrBackColor = AmbientBackColor();
  282.         }
  283.  
  284.         if (dwStockPropMask & STOCKPROP_FONT)
  285.         {
  286.             BOOL bRead = FALSE;
  287.  
  288.             if (dwPersistMask & STOCKPROP_FONT)
  289.             {
  290.                 BYTE bFlag;
  291.                 ar >> bFlag;
  292.                 if (bFlag != 0xFF)
  293.                 {
  294.                     CArchiveStream stm(&ar);
  295.                     LPSTREAM pstm = _AfxGetArchiveStream(ar, stm);
  296.                     LPFONT pFont = _AfxCreateFontFromStream(pstm);
  297.                     if (pFont != NULL)
  298.                     {
  299.                         m_font.SetFont(pFont);
  300.                         bRead = TRUE;
  301.                     }
  302.                 }
  303.             }
  304.  
  305.             if (! bRead)
  306.             {
  307.                 LPFONTDISP pFontDispAmbient = AmbientFont();
  308.                 m_font.InitializeFont(NULL, pFontDispAmbient);
  309.                 RELEASE(pFontDispAmbient);
  310.             }
  311.  
  312.             OnFontChanged();
  313.         }
  314.  
  315.         if (dwStockPropMask & STOCKPROP_BORDERSTYLE)
  316.         {
  317.             if (dwPersistMask & STOCKPROP_BORDERSTYLE)
  318.             {
  319.                 short sBorderStyle = m_sBorderStyle;
  320.                 ar >> m_sBorderStyle;
  321.                 if (sBorderStyle != m_sBorderStyle)
  322.                     _AfxToggleBorderStyle(this);
  323.             }
  324.             else
  325.             {
  326.                 m_sBorderStyle = 0;
  327.             }
  328.         }
  329.  
  330.         if (dwStockPropMask & STOCKPROP_ENABLED)
  331.         {
  332.             if (dwPersistMask & STOCKPROP_ENABLED)
  333.             {
  334.                 BOOL bEnabled = m_bEnabled;
  335.                 ar >> m_bEnabled;
  336.                 if ((bEnabled != m_bEnabled) && (m_hWnd != NULL))
  337.                     ::EnableWindow(m_hWnd, m_bEnabled);
  338.             }
  339.             else
  340.             {
  341.                 m_bEnabled = TRUE;
  342.             }
  343.         }
  344.  
  345.         if (dwStockPropMask & STOCKPROP_APPEARANCE)
  346.         {
  347.             if (dwPersistMask & STOCKPROP_APPEARANCE)
  348.             {
  349.                 short sAppearance = m_sAppearance;
  350.                 ar >> m_sAppearance;
  351.  
  352.                 if (sAppearance != m_sAppearance)
  353.                     _AfxToggleAppearance(this);
  354.             }
  355.             else
  356.             {
  357.                 m_sAppearance = AmbientAppearance();
  358.             }
  359.         }
  360.     }
  361.     else
  362.     {
  363.         ar << dwPersistMask;
  364.  
  365.         if (dwStockPropMask & (STOCKPROP_CAPTION | STOCKPROP_TEXT))
  366.             ar << m_strText;
  367.  
  368.         if (dwStockPropMask & STOCKPROP_FORECOLOR)
  369.             ar << m_clrForeColor;
  370.  
  371.         if (dwStockPropMask & STOCKPROP_BACKCOLOR)
  372.             ar << m_clrBackColor;
  373.  
  374.         if (dwStockPropMask & STOCKPROP_FONT)
  375.         {
  376.             BOOL bWrite = FALSE;
  377.             LPFONT pFont = m_font.m_pFont;
  378.             if (pFont != NULL)
  379.             {
  380.                 // If same as ambient font (or error), write 0xFF for the flag
  381.                 LPFONTDISP pFontDispAmbient = AmbientFont();
  382.                 if (!_AfxIsSameFont(m_font, NULL, pFontDispAmbient))
  383.                 {
  384.                     LPPERSISTSTREAM pps = NULL;
  385.                     if (SUCCEEDED(pFont->QueryInterface(IID_IPersistStream,
  386.                         (LPVOID*)&pps)))
  387.                     {
  388.                         ASSERT_POINTER(pps, IPersistStream);
  389.                         ar << (BYTE)0x00;
  390.                         CArchiveStream stm(&ar);
  391.                         LPSTREAM pstm = _AfxGetArchiveStream(ar, stm);
  392.                         bWrite = SUCCEEDED(::OleSaveToStream(pps, pstm));
  393.                         pps->Release();
  394.                         if (!bWrite)
  395.                             AfxThrowArchiveException(CArchiveException::generic);
  396.                     }
  397.                 }
  398.                 RELEASE(pFontDispAmbient);
  399.             }
  400.             if (! bWrite)
  401.             {
  402.                 ar << (BYTE)0xFF;
  403.             }
  404.         }
  405.  
  406.         if (dwStockPropMask & STOCKPROP_BORDERSTYLE)
  407.             ar << m_sBorderStyle;
  408.  
  409.         if (dwStockPropMask & STOCKPROP_ENABLED)
  410.             ar << m_bEnabled;
  411.  
  412.         if (dwStockPropMask & STOCKPROP_APPEARANCE)
  413.             ar << m_sAppearance;
  414.     }
  415. }
  416.  
  417. /////////////////////////////////////////////////////////////////////////////
  418. // Initialization for stock properties
  419.  
  420. void COleControl::ResetStockProps()
  421. {
  422.     DWORD dwStockPropMask = GetStockPropMask();
  423.  
  424.     if (dwStockPropMask & (STOCKPROP_CAPTION | STOCKPROP_TEXT))
  425.     {
  426.         TRY
  427.             SetText(_T(""));
  428.         END_TRY
  429.     }
  430.  
  431.     if (dwStockPropMask & STOCKPROP_FORECOLOR)
  432.         m_clrForeColor = AmbientForeColor();
  433.  
  434.     if (dwStockPropMask & STOCKPROP_BACKCOLOR)
  435.         m_clrBackColor = AmbientBackColor();
  436.  
  437.     if (dwStockPropMask & STOCKPROP_FONT)
  438.     {
  439.         LPFONTDISP pFontDispAmbient = AmbientFont();
  440.         m_font.InitializeFont(NULL, pFontDispAmbient);
  441.         RELEASE(pFontDispAmbient);
  442.         OnFontChanged();
  443.     }
  444.  
  445.     if (dwStockPropMask & STOCKPROP_BORDERSTYLE)
  446.         m_sBorderStyle = 0;
  447.  
  448.     if (dwStockPropMask & STOCKPROP_ENABLED)
  449.         m_bEnabled = TRUE;
  450.  
  451.     if (dwStockPropMask & STOCKPROP_APPEARANCE)
  452.         m_sAppearance = AmbientAppearance();
  453. }
  454.  
  455. /////////////////////////////////////////////////////////////////////////////
  456. // Appearance property
  457.  
  458. short COleControl::GetAppearance()
  459. {
  460.     return m_sAppearance;
  461. }
  462.  
  463. void COleControl::SetAppearance(short sAppearance)
  464. {
  465.     if (sAppearance != 0 && sAppearance != 1)
  466.         ThrowError(CTL_E_INVALIDPROPERTYVALUE, AFX_IDP_E_INVALIDPROPERTYVALUE);
  467.  
  468.     // Is the property changing?
  469.     if (m_sAppearance == sAppearance)
  470.         return;
  471.  
  472.     if (!BoundPropertyRequestEdit(DISPID_APPEARANCE))
  473.         SetNotPermitted();
  474.  
  475.     ASSERT((m_hWnd == NULL) ||
  476.         ((GetExStyle() & WS_EX_CLIENTEDGE) == (DWORD)(m_sAppearance ?
  477.             WS_EX_CLIENTEDGE : 0)));
  478.  
  479.     m_sAppearance = sAppearance;
  480.     m_bModified = TRUE;
  481.  
  482.     _AfxToggleAppearance(this);
  483.     OnAppearanceChanged();
  484.  
  485.     BoundPropertyChanged(DISPID_APPEARANCE);
  486. }
  487.  
  488. void COleControl::OnAppearanceChanged()
  489. {
  490.     // Can be overridden by subclass
  491.  
  492.     InvalidateControl();
  493. }
  494.  
  495. /////////////////////////////////////////////////////////////////////////////
  496. // BackColor property
  497.  
  498. OLE_COLOR COleControl::GetBackColor()
  499. {
  500.     return m_clrBackColor;
  501. }
  502.  
  503. void COleControl::SetBackColor(OLE_COLOR clrBackColor)
  504. {
  505.     // Is the property changing?
  506.     if (m_clrBackColor == clrBackColor)
  507.         return;
  508.  
  509.     if (FAILED(::OleTranslateColor(clrBackColor, NULL, NULL)))
  510.         ThrowError(CTL_E_INVALIDPROPERTYVALUE, AFX_IDP_E_INVALIDPROPERTYVALUE);
  511.  
  512.     if (!BoundPropertyRequestEdit(DISPID_BACKCOLOR))
  513.         SetNotPermitted();
  514.  
  515.     m_clrBackColor = clrBackColor;
  516.     m_bModified = TRUE;
  517.     OnBackColorChanged();
  518.  
  519.     BoundPropertyChanged(DISPID_BACKCOLOR);
  520. }
  521.  
  522. void COleControl::OnBackColorChanged()
  523. {
  524.     // Can be overridden by subclass
  525.     InvalidateControl();
  526. }
  527.  
  528. /////////////////////////////////////////////////////////////////////////////
  529. // BorderStyle property
  530.  
  531. short COleControl::GetBorderStyle()
  532. {
  533.     return m_sBorderStyle;
  534. }
  535.  
  536. void COleControl::SetBorderStyle(short sBorderStyle)
  537. {
  538.     if (sBorderStyle != 0 && sBorderStyle != 1)
  539.         ThrowError(CTL_E_INVALIDPROPERTYVALUE, AFX_IDP_E_INVALIDPROPERTYVALUE);
  540.  
  541.     // Is the property changing?
  542.     if (m_sBorderStyle == sBorderStyle)
  543.         return;
  544.  
  545.     if (!BoundPropertyRequestEdit(DISPID_BORDERSTYLE))
  546.         SetNotPermitted();
  547.  
  548.     ASSERT((m_hWnd == NULL) ||
  549.         ((GetStyle() & WS_BORDER) == (DWORD)(m_sBorderStyle ? WS_BORDER : 0)));
  550.  
  551.     m_sBorderStyle = sBorderStyle;
  552.     m_bModified = TRUE;
  553.  
  554.     _AfxToggleBorderStyle(this);
  555.     OnBorderStyleChanged();
  556.  
  557.     BoundPropertyChanged(DISPID_BORDERSTYLE);
  558. }
  559.  
  560. void COleControl::OnBorderStyleChanged()
  561. {
  562.     // Can be overridden by subclass
  563.  
  564.     InvalidateControl();
  565. }
  566.  
  567. /////////////////////////////////////////////////////////////////////////////
  568. // Text and Caption properties
  569.  
  570. const CString& COleControl::InternalGetText()
  571. {
  572.     // Some subclassed controls (such as edit controls and comboboxes) change
  573.     // the window text without sending WM_SETTEXT. Therefore, we need to
  574.     // ensure that m_strText is up-to-date.
  575.  
  576.     if (m_hWnd != NULL && IsSubclassedControl())
  577.     {
  578.         // Usually, the window text will be shorter than 32 characters.
  579.         // When it is, we can be more efficient.
  580.         const int _cchUsual = 32;
  581.  
  582.         if (DefWindowProc(WM_GETTEXT, (WPARAM)_cchUsual,
  583.             (LPARAM)m_strText.GetBufferSetLength(_cchUsual)) >= _cchUsual - 1)
  584.         {
  585.             // Text was too long: allocate a bigger buffer.
  586.  
  587.             int nLen = DefWindowProc(WM_GETTEXTLENGTH, (WPARAM)0, (LPARAM)0) + 1;
  588.             DefWindowProc(WM_GETTEXT, (WPARAM)nLen,
  589.                 (LPARAM)m_strText.GetBufferSetLength(nLen));
  590.         }
  591.         m_strText.ReleaseBuffer();
  592.     }
  593.  
  594.     return m_strText;
  595. }
  596.  
  597. BSTR COleControl::GetText()
  598. {
  599.     return ((CString&)InternalGetText()).AllocSysString();
  600. }
  601.  
  602. void COleControl::SetText(LPCTSTR pszText)
  603. {
  604.     LRESULT lResult;
  605.     if (m_hWnd != NULL)
  606.         lResult = SendMessage(WM_SETTEXT, 0, (LPARAM)pszText);
  607.     else
  608.         lResult = OnSetText(0, (LPARAM)pszText);
  609.  
  610.     if (lResult == -1)
  611.         SetNotPermitted();
  612. }
  613.  
  614. LRESULT COleControl::OnSetText(WPARAM wParam, LPARAM lParam)
  615. {
  616.     ASSERT(lParam == 0 || AfxIsValidString((LPCTSTR)lParam));
  617.  
  618.     CString str = InternalGetText();
  619.  
  620.     // Is the property changing?
  621.     if ((lParam == 0 && str.IsEmpty()) ||
  622.         (lParam != 0 && str == (LPCTSTR)lParam))
  623.         return 0;
  624.  
  625.     DWORD dwStockPropMask = GetStockPropMask();
  626.  
  627.     if (dwStockPropMask & STOCKPROP_CAPTION)
  628.         if (!BoundPropertyRequestEdit(DISPID_CAPTION))
  629.             return -1;
  630.  
  631.     if (dwStockPropMask & STOCKPROP_TEXT)
  632.         if (!BoundPropertyRequestEdit(DISPID_TEXT))
  633.             return -1;
  634.  
  635.     LRESULT lResult = 0;
  636.     m_strText = (LPCTSTR)lParam;
  637.     m_bModified = TRUE;
  638.  
  639.     if (m_hWnd != NULL)
  640.         lResult = DefWindowProc(WM_SETTEXT, wParam, lParam);
  641.  
  642.     OnTextChanged();
  643.  
  644.     if (dwStockPropMask & STOCKPROP_CAPTION)
  645.         BoundPropertyChanged(DISPID_CAPTION);
  646.  
  647.     if (dwStockPropMask & STOCKPROP_TEXT)
  648.         BoundPropertyChanged(DISPID_TEXT);
  649.  
  650.     return lResult;
  651. }
  652.  
  653. void COleControl::OnTextChanged()
  654. {
  655.     // Can be overridden by subclass
  656.     InvalidateControl();
  657. }
  658.  
  659. /////////////////////////////////////////////////////////////////////////////
  660. // Enabled property
  661.  
  662. BOOL COleControl::GetEnabled()
  663. {
  664.     return m_bEnabled;
  665. }
  666.  
  667. void COleControl::SetEnabled(BOOL bEnabled)
  668. {
  669.     // Is the property changing?
  670.     if (m_bEnabled == bEnabled)
  671.         return;
  672.  
  673.     if (!BoundPropertyRequestEdit(DISPID_ENABLED))
  674.         SetNotPermitted();
  675.  
  676.     m_bEnabled = bEnabled;
  677.     m_bModified = TRUE;
  678.     if (m_hWnd != NULL)
  679.         ::EnableWindow(m_hWnd, m_bEnabled);
  680.  
  681.     // If the control is UI Active and the Enabled property changed to FALSE,
  682.     // then UI Deactivate the control.
  683.  
  684.     if (m_bUIActive && !bEnabled)
  685.         m_xOleInPlaceObject.UIDeactivate();
  686.  
  687.     OnEnabledChanged();
  688.  
  689.     BoundPropertyChanged(DISPID_ENABLED);
  690. }
  691.  
  692. void COleControl::OnEnabledChanged()
  693. {
  694.     // Can be overridden by subclass
  695.     InvalidateControl();
  696. }
  697.  
  698. /////////////////////////////////////////////////////////////////////////////
  699. // Font property
  700.  
  701. CFontHolder& COleControl::InternalGetFont()
  702. {
  703.     return m_font;
  704. }
  705.  
  706. LPFONTDISP COleControl::GetFont()
  707. {
  708.     return m_font.GetFontDispatch();
  709. }
  710.  
  711. void COleControl::SetFont(LPFONTDISP pFontDisp)
  712. {
  713.     ASSERT((pFontDisp == NULL) ||
  714.            AfxIsValidAddress(pFontDisp, sizeof(IDispatch), FALSE));
  715.  
  716.     m_font.InitializeFont(NULL, pFontDisp);
  717.     m_bModified = TRUE;
  718.     OnFontChanged();
  719.  
  720.     BoundPropertyChanged(DISPID_FONT);
  721. }
  722.  
  723. void COleControl::OnFontChanged()
  724. {
  725.     // Can be overridden by subclass
  726.  
  727.     // Send WM_SETFONT to control's window
  728.     if ((m_hWnd != NULL) &&
  729.         (GetStockPropMask() & STOCKPROP_FONT) &&
  730.         IsSubclassedControl())
  731.     {
  732.         HFONT hFontPrev = (HFONT)SendMessage(WM_GETFONT, 0, 0);
  733.  
  734.         CFontHolder& font = InternalGetFont();
  735.  
  736.         if (font.m_pFont != NULL)
  737.         {
  738.             HFONT hFont = font.GetFontHandle();
  739.             font.m_pFont->AddRefHfont(hFont);
  740.             SendMessage(WM_SETFONT, (WPARAM)hFont, 0);
  741.  
  742.             if (m_hFontPrev != NULL)
  743.             {
  744.                 ASSERT(hFontPrev == m_hFontPrev);
  745.                 font.m_pFont->ReleaseHfont(hFontPrev);
  746.             }
  747.             m_hFontPrev = hFont;
  748.         }
  749.         else
  750.         {
  751.             SendMessage(WM_SETFONT, NULL, 0);
  752.             m_hFontPrev = NULL;
  753.         }
  754.     }
  755.  
  756.     // Invalidate the control
  757.     InvalidateControl();
  758. }
  759.  
  760. /////////////////////////////////////////////////////////////////////////////
  761. // ForeColor property
  762.  
  763. OLE_COLOR COleControl::GetForeColor()
  764. {
  765.     return m_clrForeColor;
  766. }
  767.  
  768. void COleControl::SetForeColor(OLE_COLOR clrForeColor)
  769. {
  770.     // Is the property changing?
  771.     if (m_clrForeColor == clrForeColor)
  772.         return;
  773.  
  774.     if (FAILED(::OleTranslateColor(clrForeColor, NULL, NULL)))
  775.         ThrowError(CTL_E_INVALIDPROPERTYVALUE, AFX_IDP_E_INVALIDPROPERTYVALUE);
  776.  
  777.     if (!BoundPropertyRequestEdit(DISPID_FORECOLOR))
  778.         SetNotPermitted();
  779.  
  780.     m_clrForeColor = clrForeColor;
  781.     m_bModified = TRUE;
  782.     OnForeColorChanged();
  783.  
  784.     BoundPropertyChanged(DISPID_FORECOLOR);
  785. }
  786.  
  787. void COleControl::OnForeColorChanged()
  788. {
  789.     // Can be overridden by subclass
  790.     InvalidateControl();
  791. }
  792.  
  793. /////////////////////////////////////////////////////////////////////////////
  794. // hWnd property
  795.  
  796. OLE_HANDLE COleControl::GetHwnd()
  797. {
  798. #ifdef _AFXDLL
  799.     return (OLE_HANDLE)((m_bInPlaceActive || m_bOpen) ? m_hWnd : NULL);
  800. #else
  801.     return (OLE_HANDLE)(m_bInPlaceActive ? m_hWnd : NULL);
  802. #endif
  803. }
  804.  
  805. /////////////////////////////////////////////////////////////////////////////
  806. // ReadyState property
  807.  
  808. long COleControl::GetReadyState()
  809. {
  810.     return m_lReadyState;
  811. }
  812.  
  813. ////////////////////////////////////////////////////////////////////////////
  814. // COleControl::XFontNotification
  815.  
  816. STDMETHODIMP_(ULONG) COleControl::XFontNotification::AddRef()
  817. {
  818.     return 1;
  819. }
  820.  
  821. STDMETHODIMP_(ULONG) COleControl::XFontNotification::Release()
  822. {
  823.     return 0;
  824. }
  825.  
  826. STDMETHODIMP COleControl::XFontNotification::QueryInterface(
  827.     REFIID iid, LPVOID* ppvObj)
  828. {
  829.     ASSERT(ppvObj != NULL);
  830.  
  831.     if (IsEqualIID(iid, IID_IUnknown) ||
  832.         IsEqualIID(iid, IID_IPropertyNotifySink))
  833.     {
  834.         *ppvObj = this;
  835.         return S_OK;
  836.     }
  837.  
  838.     return E_NOINTERFACE;
  839. }
  840.  
  841. STDMETHODIMP COleControl::XFontNotification::OnChanged(DISPID)
  842. {
  843.     METHOD_PROLOGUE_EX(COleControl, FontNotification)
  844.     pThis->OnFontChanged();
  845.     pThis->m_bModified = TRUE;
  846.     pThis->BoundPropertyChanged(DISPID_FONT);
  847.     return S_OK;
  848. }
  849.  
  850. STDMETHODIMP COleControl::XFontNotification::OnRequestEdit(DISPID)
  851. {
  852.     return S_OK;
  853. }
  854.  
  855. CFont* COleControl::SelectStockFont(CDC* pDC)
  856. {
  857.     return SelectFontObject(pDC, m_font);
  858. }
  859.  
  860. CFont* COleControl::SelectFontObject(CDC* pDC, CFontHolder& fontHolder)
  861. {
  862.     return fontHolder.Select(pDC, m_rcBounds.Height(), m_cyExtent);
  863. }
  864.  
  865. void COleControl::GetStockTextMetrics(LPTEXTMETRIC lptm)
  866. {
  867.     m_font.QueryTextMetrics(lptm);
  868. }
  869.  
  870. void COleControl::GetFontTextMetrics(LPTEXTMETRIC lptm, CFontHolder& fontHolder)
  871. {
  872.     fontHolder.QueryTextMetrics(lptm);
  873. }
  874.  
  875. /////////////////////////////////////////////////////////////////////////////
  876. // Ambient property access
  877.  
  878. BOOL AFXAPI _GetI4Property(LPDISPATCH pDispatch, DISPID dwDispID, DWORD* pdwResult)
  879. {
  880.     if (pDispatch == NULL)
  881.         return FALSE;
  882.  
  883.     DISPPARAMS dispparams;
  884.     memset(&dispparams, 0, sizeof dispparams);
  885.  
  886.     VARIANT vaResult;
  887.     AfxVariantInit(&vaResult);
  888.  
  889.     EXCEPINFO excepInfo;
  890.     memset(&excepInfo, 0, sizeof excepInfo);
  891.  
  892.     UINT nArgErr = (UINT)-1;  // initialize to invalid arg
  893.  
  894.     HRESULT hr = pDispatch->Invoke(dwDispID, IID_NULL, 0, DISPATCH_PROPERTYGET,
  895.         &dispparams, &vaResult, &excepInfo, &nArgErr);
  896.  
  897.     if (SUCCEEDED(hr))
  898.     {
  899.         if ((V_VT(&vaResult) == VT_I4) ||
  900.             SUCCEEDED(VariantChangeType(&vaResult, &vaResult, 0, VT_I4)))
  901.         {
  902.             *pdwResult = V_I4(&vaResult);
  903.             return TRUE;
  904.         }
  905.     }
  906.  
  907.     VariantClear(&vaResult);
  908.  
  909.     if (excepInfo.bstrSource != NULL)
  910.         SysFreeString(excepInfo.bstrSource);
  911.     if (excepInfo.bstrDescription != NULL)
  912.         SysFreeString(excepInfo.bstrDescription);
  913.     if (excepInfo.bstrHelpFile != NULL)
  914.         SysFreeString(excepInfo.bstrHelpFile);
  915.  
  916.     return FALSE;
  917. }
  918.  
  919. COleDispatchDriver* COleControl::GetAmbientDispatchDriver()
  920. {
  921.     if (m_ambientDispDriver.m_lpDispatch == NULL)
  922.     {
  923.         // Initialize pointer to ambient property dispinterface.
  924.         IDispatch* pDispatch = NULL;
  925.  
  926.         if (m_pClientSite != NULL &&
  927.             SUCCEEDED(m_pClientSite->QueryInterface(IID_IDispatch,
  928.                 reinterpret_cast<void**>(&pDispatch))))
  929.         {
  930.             ASSERT(pDispatch != NULL);
  931.             m_ambientDispDriver.AttachDispatch(pDispatch);
  932.         }
  933.     }
  934.  
  935.     return &m_ambientDispDriver;
  936. }
  937.  
  938. AFX_STATIC_DATA const DWORD _afxAmbientFlags[] =
  939. {
  940.     QACONTAINER_MESSAGEREFLECT,     // -706
  941.     0,                              // -707
  942.     0,                              // -708
  943.     QACONTAINER_USERMODE,           // -709
  944.     QACONTAINER_UIDEAD,             // -710
  945.     QACONTAINER_SHOWGRABHANDLES,    // -711
  946.     QACONTAINER_SHOWHATCHING,       // -712
  947.     QACONTAINER_DISPLAYASDEFAULT,   // -713
  948.     QACONTAINER_SUPPORTSMNEMONICS,  // -714
  949.     QACONTAINER_AUTOCLIP,           // -715
  950. };
  951.  
  952. BOOL COleControl::GetAmbientProperty(DISPID dwDispID, VARTYPE vtProp, void* pvProp)
  953. {
  954.     // First, check whether ambient property can be obtained from cache.
  955.  
  956.     _AFXCTL_AMBIENT_CACHE* pAmbientCache = _afxAmbientCache;
  957.     if (pAmbientCache->m_bValid)
  958.     {
  959.         switch (vtProp)
  960.         {
  961.         // Fetch boolean by selecting appropriate flag in cache.
  962.         case VT_BOOL:
  963.             {
  964.                 int nIndex = DISPID_AMBIENT_MESSAGEREFLECT - dwDispID;
  965.                 DWORD dwFlag;
  966.                 if (nIndex >= 0 && nIndex < _countof(_afxAmbientFlags) &&
  967.                     (dwFlag = _afxAmbientFlags[nIndex]) != 0)
  968.                 {
  969.                     *(BOOL*)pvProp =
  970.                         ((pAmbientCache->m_dwAmbientFlags & dwFlag) != 0);
  971.                     return TRUE;
  972.                 }
  973.             }
  974.             break;
  975.  
  976.         // Fetch color, appearance, or font from corresponding cache entry.
  977.         case VT_I4:
  978.             switch (dwDispID)
  979.             {
  980.             case DISPID_AMBIENT_FORECOLOR:
  981.                 *(DWORD*)pvProp = pAmbientCache->m_colorFore;
  982.                 return TRUE;
  983.  
  984.             case DISPID_AMBIENT_BACKCOLOR:
  985.                 *(DWORD*)pvProp = pAmbientCache->m_colorBack;
  986.                 return TRUE;
  987.  
  988.             case DISPID_AMBIENT_APPEARANCE:
  989.                 *(DWORD*)pvProp = pAmbientCache->m_dwAppearance;
  990.                 return TRUE;
  991.             }
  992.             break;
  993.  
  994.         case VT_I2:
  995.             if (dwDispID == DISPID_AMBIENT_APPEARANCE)
  996.             {
  997.                 *(short*)pvProp = (short)pAmbientCache->m_dwAppearance;
  998.                 return TRUE;
  999.             }
  1000.             break;
  1001.  
  1002.         case VT_DISPATCH:
  1003.             if ((dwDispID == DISPID_AMBIENT_FONT) &&
  1004.                 (pAmbientCache->m_pFont != NULL) &&
  1005.                 SUCCEEDED(pAmbientCache->m_pFont->QueryInterface(IID_IFontDisp,
  1006.                     reinterpret_cast<void**>(pvProp))))
  1007.             {
  1008.                 return TRUE;
  1009.             }
  1010.             break;
  1011.         }
  1012.     }
  1013.  
  1014.     // If there's no ambient dispatch interface available, then fail.
  1015.  
  1016.     COleDispatchDriver* pDispDriver = GetAmbientDispatchDriver();
  1017.     if (pDispDriver->m_lpDispatch == NULL)
  1018.         return FALSE;
  1019.  
  1020.     // If requested property is of type VT_I4, use optimized function.
  1021.  
  1022.     if (vtProp == VT_I4)
  1023.         return _GetI4Property(pDispDriver->m_lpDispatch, dwDispID,
  1024.             (DWORD*)pvProp);
  1025.  
  1026.     // If none of the above apply, just use the dispatch driver.
  1027.  
  1028.     BOOL bSuccess = FALSE;
  1029.     TRY
  1030.     {
  1031.         pDispDriver->GetProperty(dwDispID, vtProp, pvProp);
  1032.         bSuccess = TRUE;
  1033.     }
  1034.     END_TRY
  1035.  
  1036.     return bSuccess;
  1037. }
  1038.  
  1039. short COleControl::AmbientAppearance()
  1040. {
  1041.     DWORD dwAppearance;
  1042.     if (!GetAmbientProperty(DISPID_AMBIENT_APPEARANCE, VT_I4, &dwAppearance))
  1043.         dwAppearance = 0;
  1044.     return (short)dwAppearance;
  1045. }
  1046.  
  1047. OLE_COLOR COleControl::AmbientBackColor()
  1048. {
  1049.     OLE_COLOR clrBackColor;
  1050.     if (!GetAmbientProperty(DISPID_AMBIENT_BACKCOLOR, VT_I4, &clrBackColor))
  1051.         clrBackColor = GetSysColor(COLOR_WINDOW);
  1052.     return clrBackColor;
  1053. }
  1054.  
  1055. CString COleControl::AmbientDisplayName()
  1056. {
  1057.     CString strDisplayName;
  1058.     GetAmbientProperty(DISPID_AMBIENT_DISPLAYNAME, VT_BSTR, &strDisplayName);
  1059.     return strDisplayName;
  1060. }
  1061.  
  1062. LPFONTDISP COleControl::AmbientFont()
  1063. {
  1064.     // Note: Caller MUST Release the font!
  1065.     LPFONTDISP pDisp;
  1066.     if (!GetAmbientProperty(DISPID_AMBIENT_FONT, VT_DISPATCH, &pDisp))
  1067.         pDisp = NULL;
  1068.     return pDisp;
  1069. }
  1070.  
  1071. OLE_COLOR COleControl::AmbientForeColor()
  1072. {
  1073.     OLE_COLOR clrForeColor;
  1074.     if (!GetAmbientProperty(DISPID_AMBIENT_FORECOLOR, VT_I4, &clrForeColor))
  1075.         clrForeColor = GetSysColor(COLOR_WINDOWTEXT);
  1076.     return clrForeColor;
  1077. }
  1078.  
  1079. LCID COleControl::AmbientLocaleID()
  1080. {
  1081.     LCID lcid;
  1082.     if (!GetAmbientProperty(DISPID_AMBIENT_LOCALEID, VT_I4, &lcid))
  1083.         lcid = 0;
  1084.     return lcid;
  1085. }
  1086.  
  1087. CString COleControl::AmbientScaleUnits()
  1088. {
  1089.     CString strScaleUnits;
  1090.     GetAmbientProperty(DISPID_AMBIENT_SCALEUNITS, VT_BSTR, &strScaleUnits);
  1091.     return strScaleUnits;
  1092. }
  1093.  
  1094. short COleControl::AmbientTextAlign()
  1095. {
  1096.     short iTextAlign;
  1097.     if (!GetAmbientProperty(DISPID_AMBIENT_TEXTALIGN, VT_I2, &iTextAlign))
  1098.         iTextAlign = 0;
  1099.     return iTextAlign;
  1100. }
  1101.  
  1102. BOOL COleControl::AmbientUserMode()
  1103. {
  1104.     BOOL bUserMode;
  1105.     if (!GetAmbientProperty(DISPID_AMBIENT_USERMODE, VT_BOOL, &bUserMode))
  1106.         bUserMode = TRUE;
  1107.     return bUserMode;
  1108. }
  1109.  
  1110. BOOL COleControl::AmbientUIDead()
  1111. {
  1112.     BOOL bUIDead;
  1113.     if (!GetAmbientProperty(DISPID_AMBIENT_UIDEAD, VT_BOOL, &bUIDead))
  1114.         bUIDead = FALSE;
  1115.     return bUIDead;
  1116. }
  1117.  
  1118. BOOL COleControl::AmbientShowGrabHandles()
  1119. {
  1120.     BOOL bShowGrab;
  1121.     if (!GetAmbientProperty(DISPID_AMBIENT_SHOWGRABHANDLES, VT_BOOL, &bShowGrab))
  1122.         bShowGrab = TRUE;
  1123.     return bShowGrab;
  1124. }
  1125.  
  1126. BOOL COleControl::AmbientShowHatching()
  1127. {
  1128.     BOOL bShowHatch;
  1129.     if (!GetAmbientProperty(DISPID_AMBIENT_SHOWHATCHING, VT_BOOL, &bShowHatch))
  1130.         bShowHatch = TRUE;
  1131.     return bShowHatch;
  1132. }
  1133.  
  1134. /////////////////////////////////////////////////////////////////////////////
  1135. // Calls to IPropertyNotifySink
  1136.  
  1137. void COleControl::BoundPropertyChanged(DISPID dispid)
  1138. {
  1139.     POSITION pos = m_xPropConnPt.GetStartPosition();
  1140.     LPPROPERTYNOTIFYSINK pPropNotifySink;
  1141.  
  1142.     while (pos != NULL)
  1143.     {
  1144.         pPropNotifySink =
  1145.             (LPPROPERTYNOTIFYSINK)m_xPropConnPt.GetNextConnection(pos);
  1146.         ASSERT(pPropNotifySink != NULL);
  1147.         pPropNotifySink->OnChanged(dispid);
  1148.     }
  1149. }
  1150.  
  1151. BOOL COleControl::BoundPropertyRequestEdit(DISPID dispid)
  1152. {
  1153.     POSITION pos = m_xPropConnPt.GetStartPosition();
  1154.     LPPROPERTYNOTIFYSINK pPropNotifySink;
  1155.  
  1156.     while (pos != NULL)
  1157.     {
  1158.         pPropNotifySink =
  1159.             (LPPROPERTYNOTIFYSINK)m_xPropConnPt.GetNextConnection(pos);
  1160.         ASSERT(pPropNotifySink != NULL);
  1161.         if (pPropNotifySink->OnRequestEdit(dispid) != S_OK)
  1162.             return FALSE;
  1163.     }
  1164.  
  1165.     // All of the sinks said yes, so it's ok.
  1166.     return TRUE;
  1167. }
  1168.  
  1169. /////////////////////////////////////////////////////////////////////////////
  1170. // Function to call when BoundPropertyRequestEdit fails
  1171.  
  1172. void COleControl::SetNotPermitted()
  1173. {
  1174.     ThrowError(CTL_E_SETNOTPERMITTED, AFX_IDP_E_SETNOTPERMITTED);
  1175. }
  1176.  
  1177. /////////////////////////////////////////////////////////////////////////////
  1178. // Placeholder functions for read-only or write-only properties
  1179.  
  1180. void COleControl::SetNotSupported()
  1181. {
  1182.     ThrowError(CTL_E_SETNOTSUPPORTED, AFX_IDP_E_SETNOTSUPPORTED);
  1183. }
  1184.  
  1185. void COleControl::GetNotSupported()
  1186. {
  1187.     ThrowError(CTL_E_GETNOTSUPPORTED, AFX_IDP_E_GETNOTSUPPORTED);
  1188. }
  1189.  
  1190. /////////////////////////////////////////////////////////////////////////////
  1191. // COleControl::XPerPropertyBrowsing
  1192.  
  1193. STDMETHODIMP_(ULONG) COleControl::XPerPropertyBrowsing::AddRef()
  1194. {
  1195.     METHOD_PROLOGUE_EX_(COleControl, PerPropertyBrowsing)
  1196.     return (ULONG)pThis->ExternalAddRef();
  1197. }
  1198.  
  1199. STDMETHODIMP_(ULONG) COleControl::XPerPropertyBrowsing::Release()
  1200. {
  1201.     METHOD_PROLOGUE_EX_(COleControl, PerPropertyBrowsing)
  1202.     return (ULONG)pThis->ExternalRelease();
  1203. }
  1204.  
  1205. STDMETHODIMP COleControl::XPerPropertyBrowsing::QueryInterface(
  1206.     REFIID iid, LPVOID* ppvObj)
  1207. {
  1208.     METHOD_PROLOGUE_EX_(COleControl, PerPropertyBrowsing)
  1209.     return (HRESULT)pThis->ExternalQueryInterface(&iid, ppvObj);
  1210. }
  1211.  
  1212. STDMETHODIMP COleControl::XPerPropertyBrowsing::GetDisplayString(
  1213.     DISPID dispid, BSTR* lpbstr)
  1214. {
  1215.     METHOD_PROLOGUE_EX(COleControl, PerPropertyBrowsing)
  1216.  
  1217.     ASSERT_NULL_OR_POINTER(lpbstr, BSTR);
  1218.  
  1219.     CString strValue;
  1220.     BOOL bSuccess = pThis->OnGetDisplayString(dispid, strValue);
  1221.  
  1222.     if (lpbstr != NULL)
  1223.         *lpbstr = (bSuccess ? strValue.AllocSysString() : NULL);
  1224.  
  1225.     return bSuccess ? S_OK : S_FALSE;
  1226. }
  1227.  
  1228. BOOL COleControl::OnGetDisplayString(DISPID dispid, CString& strValue)
  1229. {
  1230.     TRY
  1231.     {
  1232.         switch (dispid)
  1233.         {
  1234.         case DISPID_FONT:
  1235.             return m_font.GetDisplayString(strValue);
  1236.  
  1237.         case DISPID_BORDERSTYLE:
  1238.             return strValue.LoadString(m_sBorderStyle == 0 ?
  1239.                 AFX_IDS_BORDERSTYLE_0 : AFX_IDS_BORDERSTYLE_1);
  1240.         }
  1241.     }
  1242.     END_TRY
  1243.  
  1244.     return FALSE;
  1245. }
  1246.  
  1247. STDMETHODIMP COleControl::XPerPropertyBrowsing::MapPropertyToPage(
  1248.     DISPID dispid, LPCLSID lpclsid)
  1249. {
  1250.     METHOD_PROLOGUE_EX(COleControl, PerPropertyBrowsing)
  1251.  
  1252.     ASSERT_NULL_OR_POINTER(lpclsid, CLSID);
  1253.  
  1254.     CLSID clsid = GUID_NULL;
  1255.     BOOL bPageOptional = FALSE;
  1256.     BOOL bSuccess = pThis->OnMapPropertyToPage(dispid, &clsid, &bPageOptional);
  1257.  
  1258.     if (lpclsid != NULL)
  1259.         *lpclsid = (bSuccess ? clsid : GUID_NULL);
  1260.  
  1261.     return bSuccess ? (bPageOptional ? S_OK : S_FALSE) :
  1262.         PERPROP_E_NOPAGEAVAILABLE;
  1263. }
  1264.  
  1265. BOOL COleControl::OnMapPropertyToPage(DISPID dispid, LPCLSID lpclsid,
  1266.     BOOL* pbPageOptional)
  1267. {
  1268.     switch (dispid)
  1269.     {
  1270.     case DISPID_FONT:
  1271.         *lpclsid = CLSID_CFontPropPage;
  1272.         *pbPageOptional = TRUE;
  1273.         return TRUE;
  1274.  
  1275.     case DISPID_BACKCOLOR:
  1276.     case DISPID_FORECOLOR:
  1277.         *lpclsid = CLSID_CColorPropPage;
  1278.         *pbPageOptional = TRUE;
  1279.         return TRUE;
  1280.     }
  1281.  
  1282.     return FALSE;
  1283. }
  1284.  
  1285. inline LPOLESTR AFXAPI _AfxCopyString(LPCTSTR psz)
  1286. {
  1287.     if (psz == NULL)
  1288.         return NULL;
  1289.  
  1290.     int cch = lstrlen(psz) + 1;
  1291.     LPOLESTR pszCopy = NULL;
  1292.  
  1293.     if ((pszCopy = (LPOLESTR)CoTaskMemAlloc(cch * sizeof(OLECHAR))) != NULL)
  1294.     {
  1295. #ifdef _UNICODE
  1296.         wcscpy(pszCopy, psz);
  1297. #elif !defined(OLE2ANSI)
  1298.         MultiByteToWideChar(CP_ACP, 0, psz, -1, pszCopy, cch);
  1299. #else
  1300.         lstrcpy(pszCopy, psz);
  1301. #endif
  1302.     }
  1303.  
  1304.     return pszCopy;
  1305. }
  1306.  
  1307. STDMETHODIMP COleControl::XPerPropertyBrowsing::GetPredefinedStrings(
  1308.     DISPID dispid, CALPOLESTR* lpcaStringsOut, CADWORD* lpcaCookiesOut)
  1309. {
  1310.     METHOD_PROLOGUE_EX(COleControl, PerPropertyBrowsing)
  1311.  
  1312.     if ((lpcaStringsOut == NULL) || (lpcaCookiesOut == NULL))
  1313.         return E_POINTER;
  1314.  
  1315.     ASSERT_POINTER(lpcaStringsOut, CALPOLESTR);
  1316.     ASSERT_POINTER(lpcaCookiesOut, CADWORD);
  1317.  
  1318.     CStringArray stringArray;
  1319.     CDWordArray cookieArray;
  1320.  
  1321.     BOOL bSuccess = pThis->OnGetPredefinedStrings(dispid, &stringArray,
  1322.         &cookieArray);
  1323.  
  1324.     if (bSuccess)
  1325.     {
  1326.         // Allocate and fill arrays to return.
  1327.  
  1328.         ASSERT(stringArray.GetSize() == cookieArray.GetSize());
  1329.  
  1330.         int iElem = 0;
  1331.         LPOLESTR lpszCopy;
  1332.         ULONG cElems = stringArray.GetSize();
  1333.  
  1334.         lpcaStringsOut->pElems = (LPOLESTR*)CoTaskMemAlloc(
  1335.             sizeof(LPOLESTR) * cElems);
  1336.  
  1337.         if (lpcaStringsOut->pElems == NULL)
  1338.             return E_OUTOFMEMORY;
  1339.  
  1340.         lpcaCookiesOut->pElems = (DWORD*)CoTaskMemAlloc(
  1341.             sizeof(DWORD*) * cElems);
  1342.  
  1343.         if (lpcaCookiesOut->pElems == NULL)
  1344.         {
  1345.             CoTaskMemFree(lpcaStringsOut->pElems);
  1346.             return E_OUTOFMEMORY;
  1347.         }
  1348.  
  1349.         lpcaStringsOut->cElems = cElems;
  1350.         lpcaCookiesOut->cElems = cElems;
  1351.  
  1352.         for (iElem = 0; iElem < (int)cElems; iElem++)
  1353.         {
  1354.             lpszCopy = _AfxCopyString(stringArray.GetAt(iElem));
  1355.  
  1356.             if (lpszCopy == NULL)
  1357.             {
  1358.                 // cleanup everything allocated so far...
  1359.                 while (--iElem >= 0)
  1360.                     CoTaskMemFree(lpcaStringsOut->pElems[iElem]);
  1361.  
  1362.                 CoTaskMemFree(lpcaCookiesOut->pElems);
  1363.                 CoTaskMemFree(lpcaStringsOut->pElems);
  1364.  
  1365.                 return E_OUTOFMEMORY;
  1366.             }
  1367.  
  1368.             lpcaStringsOut->pElems[iElem] = lpszCopy;
  1369.             lpcaCookiesOut->pElems[iElem] = cookieArray.GetAt(iElem);
  1370.         }
  1371.     }
  1372.  
  1373.     return bSuccess ? S_OK : S_FALSE;
  1374. }
  1375.  
  1376. BOOL COleControl::OnGetPredefinedStrings(DISPID dispid,
  1377.     CStringArray* pStringArray, CDWordArray* pCookieArray)
  1378. {
  1379.     BOOL bResult = FALSE;
  1380.  
  1381.     switch (dispid)
  1382.     {
  1383.     case DISPID_BORDERSTYLE:
  1384.         TRY
  1385.         {
  1386.             CString str;
  1387.             str.LoadString(AFX_IDS_BORDERSTYLE_0);
  1388.             pStringArray->Add(str);
  1389.             pCookieArray->Add(0);
  1390.             str.LoadString(AFX_IDS_BORDERSTYLE_1);
  1391.             pStringArray->Add(str);
  1392.             pCookieArray->Add(1);
  1393.             bResult = TRUE;
  1394.         }
  1395.         CATCH (CException, e)
  1396.         {
  1397.             pStringArray->RemoveAll();
  1398.             pCookieArray->RemoveAll();
  1399.             bResult = FALSE;
  1400.         }
  1401.         END_CATCH
  1402.         break;
  1403.     }
  1404.  
  1405.     return bResult;
  1406. }
  1407.  
  1408. STDMETHODIMP COleControl::XPerPropertyBrowsing::GetPredefinedValue(
  1409.     DISPID dispid, DWORD dwCookie, VARIANT* lpvarOut)
  1410. {
  1411.     METHOD_PROLOGUE_EX(COleControl, PerPropertyBrowsing)
  1412.  
  1413.     ASSERT_POINTER(lpvarOut, VARIANT);
  1414.  
  1415.     return pThis->OnGetPredefinedValue(dispid, dwCookie, lpvarOut) ?
  1416.         S_OK : E_FAIL;
  1417. }
  1418.  
  1419. BOOL COleControl::OnGetPredefinedValue(DISPID dispid, DWORD dwCookie,
  1420.     VARIANT* lpvarOut)
  1421. {
  1422.     switch (dispid)
  1423.     {
  1424.     case DISPID_BORDERSTYLE:
  1425.         if ((dwCookie == 0) || (dwCookie == 1))
  1426.         {
  1427.             VariantClear(lpvarOut);
  1428.             V_VT(lpvarOut) = VT_I4;
  1429.             V_I4(lpvarOut) = dwCookie;
  1430.             return TRUE;
  1431.         }
  1432.         break;
  1433.     }
  1434.  
  1435.     return FALSE;
  1436. }
  1437.  
  1438. void COleControl::Load(LPCTSTR strNewPath, CDataPathProperty& prop)
  1439. {
  1440.     prop.SetControl(this);
  1441.     prop.Open(strNewPath);
  1442. }
  1443.  
  1444. /////////////////////////////////////////////////////////////////////////////
  1445. // CDataPathProperty implementation
  1446.  
  1447. BOOL CDataPathProperty::Open(CFileException* pError)
  1448. {
  1449.     return CAsyncMonikerFile::Open(m_strPath, m_pControl ? m_pControl->GetClientSite() : NULL, pError);
  1450. }
  1451.  
  1452. BOOL CDataPathProperty::Open(LPCTSTR lpszPath, CFileException* pError)
  1453. {
  1454.     SetPath(lpszPath);
  1455.     return Open(pError);
  1456. }
  1457.  
  1458. BOOL CDataPathProperty::Open(COleControl* pControl, CFileException* pError)
  1459. {
  1460.     SetControl(pControl);
  1461.     return Open(pError);
  1462. }
  1463.  
  1464. BOOL CDataPathProperty::Open(LPCTSTR lpszPath, COleControl* pControl, CFileException* pError)
  1465. {
  1466.     SetControl(pControl);
  1467.     SetPath(lpszPath);
  1468.     return Open(pError);
  1469. }
  1470.  
  1471. void CDataPathProperty::ResetData()
  1472. {
  1473. }
  1474.  
  1475. #ifdef _DEBUG
  1476. void CDataPathProperty::AssertValid() const
  1477. {
  1478.     CAsyncMonikerFile::AssertValid();
  1479. }
  1480.  
  1481. void CDataPathProperty::Dump(CDumpContext& dc) const
  1482. {
  1483.     CAsyncMonikerFile::Dump(dc);
  1484.  
  1485.     dc << "\nm_pControl = " << m_pControl;
  1486.     dc << "\nm_strPath = \"" << m_strPath;
  1487.     dc << "\"\n";
  1488. }
  1489. #endif //_DEBUG
  1490.  
  1491. /////////////////////////////////////////////////////////////////////////////
  1492. // CCachedDataPathProperty implementation
  1493.  
  1494. AFX_STATIC inline DWORD _AfxTransferFileContent(CFile* pFrom, CFile* pTo)
  1495. {
  1496.     BYTE buff[1024];
  1497.     DWORD dwRead = 0;
  1498.     DWORD dwActual;
  1499.     do
  1500.     {
  1501.         dwActual = pFrom->Read(buff, 1024);
  1502.         pTo->Write(buff, dwActual);
  1503.  
  1504.         dwRead += dwActual;
  1505.     }
  1506.     while (dwActual > 0);
  1507.     return dwRead;
  1508. }
  1509.  
  1510. void CCachedDataPathProperty::OnDataAvailable(DWORD dwSize, DWORD bscfFlag)
  1511. {
  1512.     UNUSED_ALWAYS(bscfFlag);
  1513.     UNUSED_ALWAYS(dwSize);
  1514.     DWORD dwPos = m_Cache.GetPosition();
  1515.     TRY
  1516.     {
  1517.         // Cache the data in our mem file.
  1518.         m_Cache.SeekToEnd();
  1519.         _AfxTransferFileContent(this, &m_Cache);
  1520.     }
  1521.     CATCH_ALL(e)
  1522.     {
  1523.         m_Cache.Seek(dwPos, CFile::begin);
  1524.         THROW_LAST();
  1525.     }
  1526.     END_CATCH_ALL
  1527.     m_Cache.Seek(dwPos, CFile::begin);
  1528. }
  1529.  
  1530. void CCachedDataPathProperty::Close()
  1531. {
  1532.     m_Cache.SetLength(0);
  1533.     CDataPathProperty::Close();
  1534. }
  1535.  
  1536. void CCachedDataPathProperty::ResetData()
  1537. {
  1538.     m_Cache.SetLength(0);
  1539. }
  1540. #ifdef _DEBUG
  1541. void CCachedDataPathProperty::AssertValid() const
  1542. {
  1543.     CDataPathProperty::AssertValid();
  1544.     m_Cache.AssertValid();
  1545. }
  1546.  
  1547. void CCachedDataPathProperty::Dump(CDumpContext& dc) const
  1548. {
  1549.     CDataPathProperty::Dump(dc);
  1550.  
  1551.     m_Cache.Dump(dc);
  1552. }
  1553. #endif //_DEBUG
  1554.  
  1555. /////////////////////////////////////////////////////////////////////////////
  1556. // Force any extra compiler-generated code into AFX_INIT_SEG
  1557.  
  1558. #ifdef AFX_INIT_SEG
  1559. #pragma code_seg(AFX_INIT_SEG)
  1560. #endif
  1561.  
  1562. IMPLEMENT_DYNAMIC(CDataPathProperty, CAsyncMonikerFile)
  1563. IMPLEMENT_DYNAMIC(CCachedDataPathProperty, CDataPathProperty)
  1564.