/** * @file XTPMarkupRender.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 "GraphicLibrary/GdiPlus/XTPGdiPlus.h" #include "Common/XTPTypeId.h" #include "Common/XTPFramework.h" #include "Common/XTPSystemHelpers.h" #include "Common/XTPSynchro.h" #include "Common/XTPApplication.h" #include "Common/XTPSingleton.h" #include "Common/XTPGdiObjects.h" #include "Common/XTPXMLHelpers.h" #include "Common/XTPMarkupRender.h" #include "Common/Base/Types/XTPSize.h" #include #include "Markup/XTPMarkupContext.h" #include "Markup/XTPMarkupObject.h" #include "Markup/XTPMarkupInputElement.h" #include "Markup/XTPMarkupUIElement.h" #include "Markup/XTPMarkupDrawingContext.h" #include "Markup/XTPMarkupBuilder.h" #include "Markup/XTPMarkupParser.h" #include "Common/Base/Diagnostic/XTPDisableNoisyWarnings.h" #ifdef _DEBUG # define new DEBUG_NEW # undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif CXTPMarkupContext* AFX_CDECL XTPMarkupCreateContext(HWND hWnd /*= NULL*/, BOOL bDpiAware /*= FALSE*/) { return XTPMarkupCreateContext(hWnd, CRect(0, 0, 0, 0), bDpiAware); } CXTPMarkupContext* AFX_CDECL XTPMarkupCreateContext(HWND hWnd, RECT rcUpdate, BOOL bDpiAware) { CXTPMarkupContext* pContext = new CXTPMarkupContext(); pContext->SetContextWindow(hWnd, &rcUpdate); pContext->SetDpiAware(bDpiAware, hWnd); return pContext; } void AFX_CDECL XTPMarkupAssignHandle(CXTPMarkupContext* pContext, HWND hWnd) { XTPMarkupAssignHandle(pContext, hWnd, CRect(0, 0, 0, 0)); } void AFX_CDECL XTPMarkupAssignHandle(CXTPMarkupContext* pContext, HWND hWnd, CRect rcUpdate) { _ASSERTE(NULL != pContext); if (NULL == pContext) return; pContext->SetContextWindow(hWnd, rcUpdate); } void AFX_CDECL XTPMarkupReleaseContext(CXTPMarkupContext*& pContext) { XTPMarkupReleaseContext(pContext, FALSE); } void AFX_CDECL XTPMarkupReleaseContext(CXTPMarkupContext*& pContext, BOOL bShutdown) { if (NULL == pContext) return; if (bShutdown) pContext->Shutdown(); pContext->Release(); pContext = NULL; } void AFX_CDECL XTPMarkupReleaseElement(CXTPMarkupUIElement*& pElement) { if (NULL == pElement) return; pElement->Release(); pElement = NULL; } CXTPMarkupUIElement* AFX_CDECL XTPMarkupParseText(CXTPMarkupContext* pContext, LPCSTR lpszMarkup) { _ASSERTE(NULL != pContext); _ASSERTE(NULL != lpszMarkup); if (NULL == pContext || NULL == lpszMarkup) return NULL; CXTPMarkupParser sc; sc.SetBuffer(lpszMarkup, lpszMarkup + strlen(lpszMarkup)); CXTPMarkupBuilder builder(pContext); return builder.Parse(&sc); } CXTPMarkupUIElement* AFX_CDECL XTPMarkupParseText(CXTPMarkupContext* pContext, LPCWSTR lpszMarkup) { _ASSERTE(NULL != pContext); _ASSERTE(NULL != lpszMarkup); if (NULL == pContext || NULL == lpszMarkup) return NULL; CXTPMarkupParser sc; sc.SetBuffer(lpszMarkup, lpszMarkup + wcslen(lpszMarkup)); CXTPMarkupBuilder builder(pContext); return builder.Parse(&sc); } CXTPMarkupUIElement* AFX_CDECL XTPMarkupParseText(CXTPMarkupContext* pContext, LPCSTR lpszMarkup, INT_PTR nLength) { _ASSERTE(NULL != pContext); _ASSERTE(NULL != lpszMarkup); if (NULL == pContext || NULL == lpszMarkup) return NULL; CXTPMarkupParser sc; sc.SetBuffer(lpszMarkup, lpszMarkup + nLength); CXTPMarkupBuilder builder(pContext); return builder.Parse(&sc); } CXTPMarkupUIElement* AFX_CDECL XTPMarkupParseText(CXTPMarkupContext* pContext, LPCWSTR lpszMarkup, INT_PTR nLength) { _ASSERTE(NULL != pContext); _ASSERTE(NULL != lpszMarkup); if (NULL == pContext || NULL == lpszMarkup) return NULL; CXTPMarkupParser sc; sc.SetBuffer(lpszMarkup, lpszMarkup + nLength); CXTPMarkupBuilder builder(pContext); return builder.Parse(&sc); } CXTPMarkupUIElement* AFX_CDECL XTPMarkupParseText(CXTPMarkupContext* pContext, IStream* pStream) { _ASSERTE(NULL != pContext); _ASSERTE(NULL != pStream); if (NULL == pContext || NULL == pStream) return NULL; CXTPMarkupParser sc; sc.SetDataStream(pStream); CXTPMarkupBuilder builder(pContext); return builder.Parse(&sc); } CXTPMarkupContext* AFX_CDECL XTPMarkupElementContext(CXTPMarkupUIElement* pElement) { _ASSERTE(NULL != pElement); if (NULL == pElement) return NULL; return pElement->GetMarkupContext(); } CSize AFX_CDECL XTPMarkupMeasureElement(CXTPMarkupUIElement* pElement, int cxAvail /*= INT_MAX*/, int cyAvail /*= INT_MAX*/) { _ASSERTE(NULL != pElement); if (NULL == pElement) return CSize(0, 0); CXTPMarkupDrawingContext dc(pElement->GetMarkupContext()); pElement->Measure(&dc, CSize(cxAvail, cyAvail)); return pElement->GetDesiredSize(); } void AFX_CDECL XTPMarkupRenderElement(CXTPMarkupUIElement* pElement, HDC hDC, LPCRECT lpRect) { _ASSERTE(NULL != pElement); _ASSERTE(NULL != hDC); _ASSERTE(NULL != lpRect); if (NULL == pElement || NULL == hDC || NULL == lpRect) return; CRect rc(lpRect); CXTPMarkupDrawingContext dc(pElement->GetMarkupContext(), hDC); pElement->Measure(&dc, rc.Size()); pElement->Arrange(rc); pElement->Render(&dc); } void AFX_CDECL XTPMarkupSetDefaultFont(CXTPMarkupContext* pContext, HFONT hFont, COLORREF clrText) { _ASSERTE(NULL != pContext); if (NULL == pContext) return; if (NULL != hFont) pContext->SetDefaultFont(hFont); if (clrText != (COLORREF)-1) pContext->SetDefaultTextColor(clrText); } BOOL AFX_CDECL XTPMarkupRelayMessage(CXTPMarkupUIElement* pElement, UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult) { _ASSERTE(NULL != pElement); if (NULL == pElement) return FALSE; _ASSERTE(NULL != pElement->GetMarkupContext()); return pElement->GetMarkupContext()->OnWndMsg(pElement, message, wParam, lParam, pResult); } BOOL AFX_CDECL XTPMarkupRelayMessage(CXTPMarkupContext* pContext, UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult) { _ASSERTE(NULL != pContext); if (NULL == pContext) return FALSE; return pContext->OnWndMsg(NULL, message, wParam, lParam, pResult); } void AFX_CDECL XTPMarkupDrawText(CXTPMarkupContext* pContext, HDC hDC, LPCTSTR lpszText, LPRECT lpRect, UINT nFormat) { CXTPMarkupUIElement* pElement = pContext ? XTPMarkupParseText(pContext, lpszText) : NULL; if (NULL == pElement) { ::DrawText(hDC, lpszText, (int)_tcslen(lpszText), lpRect, nFormat); return; } XTPMarkupSetDefaultFont(pContext, (HFONT)::GetCurrentObject(hDC, OBJ_FONT), ::GetTextColor(hDC)); CSize sz = XTPMarkupMeasureElement(pElement, lpRect->right - lpRect->left, lpRect->bottom - lpRect->top); int nLeft = lpRect->left; int nTop = lpRect->top; if (nFormat & DT_CENTER) nLeft = (lpRect->right + lpRect->left - sz.cx) / 2; else if (nFormat & DT_RIGHT) nLeft = lpRect->right - sz.cx; if (nFormat & DT_VCENTER) nTop = (lpRect->top + lpRect->bottom - sz.cy) / 2; else if (nFormat & DT_BOTTOM) nTop = lpRect->bottom - sz.cy; XTPMarkupRenderElement(pElement, hDC, CRect(nLeft, nTop, nLeft + sz.cx, nTop + sz.cy)); XTPMarkupReleaseElement(pElement); }