/** * @file XTPPropertyGridItemFont.cpp * * @copyright * (c) 1998-2025 Codejock Software, All Rights Reserved. * * This source file is the property of Codejock Software and must not be * redistributed by any means without the explicit written permission of * Codejock Software. * * The use of this source code is governed by the terms and conditions specified * in the Toolkit Pro license agreement. Codejock Software grants you, as a * single software developer, the limited right to use this software on one * computer only. * * Contact Information: * support@codejock.com * http://www.codejock.com * */ #include "stdafx.h" #include "Common/XTPTypeId.h" #include "Common/XTPFramework.h" #include "Common/XTPVC80Helpers.h" #include "Common/XTPSystemHelpers.h" #include "Common/XTPSynchro.h" #include "Common/XTPApplication.h" #include "Common/XTPSingleton.h" #include "Common/XTPGdiObjects.h" #include "Common/XTPDrawHelpers.h" #include "Common/XTPMacros.h" #include "PropertyGrid/XTPPropertyGridDefines.h" #include "PropertyGrid/XTPPropertyGridItem.h" #include "PropertyGrid/XTPPropertyGridItemFont.h" #include "PropertyGrid/XTPPropertyGridToolTip.h" #include "PropertyGrid/XTPPropertyGridView.h" #include "PropertyGrid/XTPPropertyGridIIDs.h" #include "Common/Base/Diagnostic/XTPDisableNoisyWarnings.h" #ifdef _DEBUG # define new DEBUG_NEW # undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CXTPPropertyGridItemFont IMPLEMENT_DYNAMIC(CXTPPropertyGridItemFont, CXTPPropertyGridItem) CXTPPropertyGridItemFont::CXTPPropertyGridItemFont(LPCTSTR strCaption, LOGFONT& font) : CXTPPropertyGridItem(strCaption) { SetFont(font); m_nFlags = xtpPropertyGridItemHasExpandButton; m_clrValue = (COLORREF)-1; EnableAutomation(); m_strDefaultValue = m_strValue; } CXTPPropertyGridItemFont::CXTPPropertyGridItemFont(UINT nID, LOGFONT& font) : CXTPPropertyGridItem(nID) { SetFont(font); m_nFlags = xtpPropertyGridItemHasExpandButton; m_clrValue = (COLORREF)-1; EnableAutomation(); m_strDefaultValue = m_strValue; } CXTPPropertyGridItemFont::~CXTPPropertyGridItemFont() { } ///////////////////////////////////////////////////////////////////////////// // void CXTPPropertyGridItemFont::SetFont(LOGFONT& font) { MEMCPY_S(&m_lfValue, &font, sizeof(LOGFONT)); m_strValue = FontToString(m_lfValue); } CString CXTPPropertyGridItemFont::FontToString(const LOGFONT& lfValue) { CWindowDC dc(CWnd::GetDesktopWindow()); int nHeight = -MulDiv(lfValue.lfHeight, 72, dc.GetDeviceCaps(LOGPIXELSY)); CString strValue; strValue.Format(_T("%s; %ipt"), lfValue.lfFaceName, nHeight); if (lfValue.lfWeight == FW_BOLD) strValue += _T("; bold"); return strValue; } BOOL CXTPPropertyGridItemFont::OnDrawItemValue(CDC& dc, CRect rcValue) { if (m_clrValue == (COLORREF)-1) return CXTPPropertyGridItem::OnDrawItemValue(dc, rcValue); COLORREF clr = dc.GetTextColor(); CRect rcSample(rcValue.left - XTP_DPI_X(2), rcValue.top + XTP_DPI_Y(1), rcValue.left + XTP_DPI_X(18), rcValue.bottom - XTP_DPI_Y(1)); CXTPPenDC pen(dc, clr); CXTPBrushDC brush(dc, m_clrValue); dc.Rectangle(rcSample); CRect rcText(rcValue); rcText.left += XTP_DPI_X(25); dc.DrawText(m_strValue, rcText, DT_SINGLELINE | DT_VCENTER); return TRUE; } CRect CXTPPropertyGridItemFont::GetValueRect() { CRect rcValue(CXTPPropertyGridItem::GetValueRect()); if (m_clrValue != (COLORREF)-1) rcValue.left += XTP_DPI_X(25); return rcValue; } UINT_PTR CALLBACK CXTPPropertyGridItemFont::FontDlgProc(HWND hWnd, UINT message, WPARAM, LPARAM) { // special case for WM_INITDIALOG if (message == WM_INITDIALOG) { HWND hWndCombo = GetDlgItem(hWnd, 1139); if (hWndCombo) EnableWindow(hWndCombo, FALSE); CDialog* pDlg = DYNAMIC_DOWNCAST(CDialog, CWnd::FromHandlePermanent(hWnd)); if (pDlg != NULL) return (UINT_PTR)pDlg->OnInitDialog(); else return 1; } return 0; } void CXTPPropertyGridItemFont::OnInplaceButtonDown(CXTPPropertyGridInplaceButton* pButton) { if (m_pGrid->SendNotifyMessage(XTP_PGN_INPLACEBUTTONDOWN, (LPARAM)pButton) == TRUE) return; if (!OnRequestEdit()) return; InternalAddRef(); CFontDialog dlg(&m_lfValue, CF_EFFECTS | CF_SCREENFONTS, NULL, m_pGrid); if (m_clrValue == (COLORREF)-1) { dlg.m_cf.lpfnHook = FontDlgProc; } else { dlg.m_cf.rgbColors = m_clrValue; } if (dlg.DoModal() == IDOK) { LOGFONT lf; dlg.GetCurrentFont(&lf); CString strValue = FontToString(lf); if (OnAfterEdit(strValue)) { SetFont(lf); if (m_clrValue != (COLORREF)-1) m_clrValue = dlg.GetColor(); OnValueChanged(strValue); SAFE_INVALIDATE(m_pGrid); } } else { OnCancelEdit(); } InternalRelease(); } void CXTPPropertyGridItemFont::SetColor(COLORREF clr) { m_clrValue = clr; SAFE_INVALIDATE(m_pGrid); } COLORREF CXTPPropertyGridItemFont::GetColor() { return m_clrValue; } void CXTPPropertyGridItemFont::GetFont(LOGFONT* lf) { _ASSERTE(lf != NULL); MEMCPY_S(lf, &m_lfValue, sizeof(LOGFONT)); } #ifdef _XTP_ACTIVEX BEGIN_DISPATCH_MAP(CXTPPropertyGridItemFont, CXTPPropertyGridItem) DISP_PROPERTY_EX_ID(CXTPPropertyGridItemFont, "Color", 50, GetColor, SetColor, VT_I4) DISP_PROPERTY_EX_ID(CXTPPropertyGridItemFont, "FontFaceName", 51, OleGetFaceName, OleSetFaceName, VT_BSTR) DISP_PROPERTY_EX_ID(CXTPPropertyGridItemFont, "FontSize", 52, OleGetSize, OleSetSize, VT_I4) DISP_PROPERTY_EX_ID(CXTPPropertyGridItemFont, "FontBold", 53, OleGetBold, OleSetBold, VT_BOOL) DISP_PROPERTY_EX_ID(CXTPPropertyGridItemFont, "FontItalic", 54, OleGetItalic, OleSetItalic, VT_BOOL) DISP_PROPERTY_EX_ID(CXTPPropertyGridItemFont, "FontUnderline", 55, OleGetUnderline, OleSetUnderline, VT_BOOL) DISP_PROPERTY_EX_ID(CXTPPropertyGridItemFont, "FontStrikeOut", 56, OleGetStrikeOut, OleSetStrikeOut, VT_BOOL) DISP_PROPERTY_EX_ID(CXTPPropertyGridItemFont, "FontWeight", 57, OleGetWeight, OleSetWeight, VT_I4) END_DISPATCH_MAP() BEGIN_INTERFACE_MAP(CXTPPropertyGridItemFont, CXTPPropertyGridItem) INTERFACE_PART(CXTPPropertyGridItemFont, XTPDIID_IPropertyGridItemFont, Dispatch) END_INTERFACE_MAP() IMPLEMENT_OLETYPELIB_EX(CXTPPropertyGridItemFont, XTPDIID_IPropertyGridItemFont) BSTR CXTPPropertyGridItemFont::OleGetFaceName() { return CString(m_lfValue.lfFaceName).AllocSysString(); } void CXTPPropertyGridItemFont::OleSetFaceName(LPCTSTR lpszFaceName) { STRCPY_S(m_lfValue.lfFaceName, LF_FACESIZE, lpszFaceName); SetFont(m_lfValue); } int CXTPPropertyGridItemFont::OleGetSize() { CWindowDC dc(CWnd::GetDesktopWindow()); int nHeight = -MulDiv(m_lfValue.lfHeight, 72, dc.GetDeviceCaps(LOGPIXELSY)); return nHeight; } void CXTPPropertyGridItemFont::OleSetSize(int nHeigth) { CWindowDC dc(CWnd::GetDesktopWindow()); m_lfValue.lfHeight = -MulDiv(nHeigth, dc.GetDeviceCaps(LOGPIXELSY), 72); SetFont(m_lfValue); } int CXTPPropertyGridItemFont::OleGetWeight() { return m_lfValue.lfWeight; } void CXTPPropertyGridItemFont::OleSetWeight(int nWeight) { m_lfValue.lfWeight = nWeight; SetFont(m_lfValue); } BOOL CXTPPropertyGridItemFont::OleGetBold() { return m_lfValue.lfWeight == FW_BOLD; } void CXTPPropertyGridItemFont::OleSetBold(BOOL bBold) { m_lfValue.lfWeight = bBold ? FW_BOLD : FW_NORMAL; SetFont(m_lfValue); } BOOL CXTPPropertyGridItemFont::OleGetItalic() { return m_lfValue.lfItalic; } void CXTPPropertyGridItemFont::OleSetItalic(BOOL bItalic) { m_lfValue.lfItalic = bItalic; SetFont(m_lfValue); } BOOL CXTPPropertyGridItemFont::OleGetUnderline() { return m_lfValue.lfUnderline; } void CXTPPropertyGridItemFont::OleSetUnderline(BOOL bUnderline) { m_lfValue.lfUnderline = bUnderline; SetFont(m_lfValue); } BOOL CXTPPropertyGridItemFont::OleGetStrikeOut() { return m_lfValue.lfStrikeOut; } void CXTPPropertyGridItemFont::OleSetStrikeOut(BOOL bStrikeOut) { m_lfValue.lfStrikeOut = bStrikeOut; SetFont(m_lfValue); } void CXTPPropertyGridItemFont::OleSetValue(const VARIANT* varValue) { if (varValue->vt == (VT_VARIANT | VT_BYREF)) { OleSetValue(varValue->pvarVal); return; } if (varValue->vt == VT_I4) { HGDIOBJ hFont = reinterpret_cast((LONG_PTR)varValue->lVal); if (::GetObjectType(hFont) == OBJ_FONT) { LOGFONT lf; ::GetObject(hFont, sizeof(LOGFONT), &lf); SetFont(lf); } } else if (varValue->vt == VT_DISPATCH) { CComQIPtr lpFont(varValue->pdispVal); if (lpFont) { HFONT hFont; LOGFONT lf; lpFont->get_hFont(&hFont); ::GetObject(hFont, sizeof(LOGFONT), &lf); SetFont(lf); } } } const VARIANT CXTPPropertyGridItemFont::OleGetValue() { USES_CONVERSION; FONTDESC fd; fd.cbSizeofstruct = sizeof(FONTDESC); fd.lpstrName = T2OLE(m_lfValue.lfFaceName); fd.sWeight = (short)m_lfValue.lfWeight; fd.sCharset = m_lfValue.lfCharSet; fd.fItalic = m_lfValue.lfItalic; fd.fUnderline = m_lfValue.lfUnderline; fd.fStrikethrough = m_lfValue.lfStrikeOut; long lfHeight = m_lfValue.lfHeight; if (lfHeight < 0) lfHeight = -lfHeight; CWindowDC dc(CWnd::GetDesktopWindow()); int ppi = dc.GetDeviceCaps(LOGPIXELSY); fd.cySize.Lo = lfHeight * 720000 / ppi; fd.cySize.Hi = 0; IFontDisp* lpFont = 0; if (FAILED(::OleCreateFontIndirect(&fd, IID_IFontDisp, (void**)&lpFont))) lpFont = NULL; return CComVariant(lpFont); } #endif