| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 |
- // Feel free to use this code in your own applications.
- // The Author does not guarantee anything about this code.
- // Author : Yves Maurer
- // FormattedTextDraw.cpp : Implementation of CFormattedTextDraw
- #include "stdafx.h"
- #include "FormattedTextDraw.h"
- /////////////////////////////////////////////////////////////////////////////
- // CallBack functions
- DWORD CALLBACK EditStreamInCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
- {
- PCOOKIE pCookie = (PCOOKIE) dwCookie;
- if(pCookie->dwSize - pCookie->dwCount < (DWORD) cb)
- *pcb = pCookie->dwSize - pCookie->dwCount;
- else
- *pcb = cb;
- CopyMemory(pbBuff, pCookie->bstrText, *pcb);
- pCookie->dwCount += *pcb;
- return 0; // callback succeeded - no errors
- }
- /////////////////////////////////////////////////////////////////////////////
- // CFormattedTextDraw
- HRESULT CFormattedTextDraw::get_RTFText(BSTR *pVal)
- {
- *pVal = SysAllocStringLen(m_RTFText, SysStringLen(m_RTFText));
- return S_OK;
- }
- HRESULT CFormattedTextDraw::put_RTFText(BSTR newVal)
- {
- HRESULT hr;
- long len;
- LRESULT lResult = 0;
- EDITSTREAM editStream;
- if (m_RTFText != NULL)
- SysFreeString(m_RTFText);
- len = SysStringLen(newVal);
- m_RTFText = SysAllocStringLen(newVal, len);
- if (!m_spTextServices)
- return S_FALSE;
- m_editCookie.bstrText = (BSTR) malloc(len + 1);
- WideCharToMultiByte(CP_ACP, 0, m_RTFText, len, (char *) m_editCookie.bstrText, len, NULL, NULL);
- m_editCookie.dwSize = lstrlenA((LPSTR) m_editCookie.bstrText);
- m_editCookie.dwCount = 0;
- editStream.dwCookie = (DWORD_PTR) &m_editCookie;
- editStream.dwError = 0;
- editStream.pfnCallback = EditStreamInCallback;
- hr = m_spTextServices->TxSendMessage(EM_STREAMIN, (WPARAM)(SF_RTF | SF_UNICODE), (LPARAM)&editStream, &lResult);
- return S_OK;
- }
- HRESULT CFormattedTextDraw::Draw(void *hdcDraw, RECT *prc)
- {
- if (!m_spTextServices)
- return S_FALSE;
- m_spTextServices->TxDraw(
- DVASPECT_CONTENT, // Draw Aspect
- 0, // Lindex
- NULL, // Info for drawing optimization
- NULL, // target device information
- (HDC) hdcDraw, // Draw device HDC
- NULL, // Target device HDC
- (RECTL *) prc, // Bounding client rectangle
- NULL, // Clipping rectangle for metafiles
- (RECT *) NULL, // Update rectangle
- NULL, // Call back function
- NULL, // Call back parameter
- TXTVIEW_INACTIVE); // What view of the object could be TXTVIEW_ACTIVE
- return S_OK;
- }
- HRESULT CFormattedTextDraw::Create()
- {
- return CreateTextServicesObject();
- }
- HRESULT CFormattedTextDraw::get_NaturalWidth(long Height, long *pVal)
- {
- long lWidth;
- SIZEL szExtent;
- HDC hdcDraw;
- if (!m_spTextServices)
- return S_FALSE;
- hdcDraw = GetDC(NULL);
- szExtent.cy = Height;
- szExtent.cx = 10000;
- lWidth = 10000;
- m_spTextServices->TxGetNaturalSize(DVASPECT_CONTENT,
- hdcDraw,
- NULL,
- NULL,
- TXTNS_FITTOCONTENT,
- &szExtent,
- &lWidth,
- &Height);
- ReleaseDC(NULL, hdcDraw);
- *pVal = lWidth;
- return S_OK;
- }
- HRESULT CFormattedTextDraw::get_NaturalHeight(long Width, long *pVal)
- {
- long lHeight;
- SIZEL szExtent;
- HDC hdcDraw;
- if (!m_spTextServices)
- return S_FALSE;
- hdcDraw = GetDC(NULL);
- szExtent.cx = Width;
- szExtent.cy = 10000;
- lHeight = 10000;
- m_spTextServices->TxGetNaturalSize(DVASPECT_CONTENT,
- hdcDraw,
- NULL,
- NULL,
- TXTNS_FITTOCONTENT,
- &szExtent,
- &Width,
- &lHeight);
- ReleaseDC(NULL, hdcDraw);
- *pVal = lHeight;
- return S_OK;
- }
- /////////////////////////////////////////////////////////////////////////////
- // ITextHost functions
- HDC CFormattedTextDraw::TxGetDC()
- {
- return NULL;
- }
- INT CFormattedTextDraw::TxReleaseDC(HDC hdc)
- {
- return 1;
- }
- BOOL CFormattedTextDraw::TxShowScrollBar(INT fnBar, BOOL fShow)
- {
- return FALSE;
- }
- BOOL CFormattedTextDraw::TxEnableScrollBar(INT fuSBFlags, INT fuArrowflags)
- {
- return FALSE;
- }
- BOOL CFormattedTextDraw::TxSetScrollRange(INT fnBar, LONG nMinPos, INT nMaxPos, BOOL fRedraw)
- {
- return FALSE;
- }
- BOOL CFormattedTextDraw::TxSetScrollPos(INT fnBar, INT nPos, BOOL fRedraw)
- {
- return FALSE;
- }
- void CFormattedTextDraw::TxInvalidateRect(LPCRECT prc, BOOL fMode)
- {
- }
- void CFormattedTextDraw::TxViewChange(BOOL fUpdate)
- {
- }
- BOOL CFormattedTextDraw::TxCreateCaret(HBITMAP hbmp, INT xWidth, INT yHeight)
- {
- return FALSE;
- }
- BOOL CFormattedTextDraw::TxShowCaret(BOOL fShow)
- {
- return FALSE;
- }
- BOOL CFormattedTextDraw::TxSetCaretPos(INT x, INT y)
- {
- return FALSE;
- }
- BOOL CFormattedTextDraw::TxSetTimer(UINT idTimer, UINT uTimeout)
- {
- return FALSE;
- }
- void CFormattedTextDraw::TxKillTimer(UINT idTimer)
- {
- }
- void CFormattedTextDraw::TxScrollWindowEx(INT dx, INT dy, LPCRECT lprcScroll, LPCRECT lprcClip, HRGN hrgnUpdate, LPRECT lprcUpdate, UINT fuScroll)
- {
- }
- void CFormattedTextDraw::TxSetCapture(BOOL fCapture)
- {
- }
- void CFormattedTextDraw::TxSetFocus()
- {
- }
- void CFormattedTextDraw::TxSetCursor(HCURSOR hcur, BOOL fText)
- {
- }
- BOOL CFormattedTextDraw::TxScreenToClient(LPPOINT lppt)
- {
- return FALSE;
- }
- BOOL CFormattedTextDraw::TxClientToScreen(LPPOINT lppt)
- {
- return FALSE;
- }
- HRESULT CFormattedTextDraw::TxActivate(LONG * plOldState)
- {
- return S_OK;
- }
- HRESULT CFormattedTextDraw::TxDeactivate(LONG lNewState)
- {
- return S_OK;
- }
- HRESULT CFormattedTextDraw::TxGetClientRect(LPRECT prc)
- {
- *prc = m_rcClient;
- return S_OK;
- }
- HRESULT CFormattedTextDraw::TxGetViewInset(LPRECT prc)
- {
- *prc = m_rcViewInset;
- return S_OK;
- }
- HRESULT CFormattedTextDraw::TxGetCharFormat(const CHARFORMATW **ppCF)
- {
- *ppCF = m_pCF;
- return S_OK;
- }
- HRESULT CFormattedTextDraw::TxGetParaFormat(const PARAFORMAT **ppPF)
- {
- *ppPF = &m_PF;
- return S_OK;
- }
- COLORREF CFormattedTextDraw::TxGetSysColor(int nIndex)
- {
- return GetSysColor(nIndex);
- }
- HRESULT CFormattedTextDraw::TxGetBackStyle(TXTBACKSTYLE *pstyle)
- {
- *pstyle = TXTBACK_TRANSPARENT;
- return S_OK;
- }
- HRESULT CFormattedTextDraw::TxGetMaxLength(DWORD *plength)
- {
- *plength = m_dwMaxLength;
- return S_OK;
- }
- HRESULT CFormattedTextDraw::TxGetScrollBars(DWORD *pdwScrollBar)
- {
- *pdwScrollBar = m_dwScrollbar;
- return S_OK;
- }
- HRESULT CFormattedTextDraw::TxGetPasswordChar(TCHAR *pch)
- {
- return S_FALSE;
- }
- HRESULT CFormattedTextDraw::TxGetAcceleratorPos(LONG *pcp)
- {
- *pcp = -1;
- return S_OK;
- }
- HRESULT CFormattedTextDraw::TxGetExtent(LPSIZEL lpExtent)
- {
- return E_NOTIMPL;
- }
- HRESULT CFormattedTextDraw::OnTxCharFormatChange(const CHARFORMATW * pcf)
- {
- memcpy(m_pCF, pcf, pcf->cbSize);
- return S_OK;
- }
- HRESULT CFormattedTextDraw::OnTxParaFormatChange(const PARAFORMAT * ppf)
- {
- memcpy(&m_PF, ppf, ppf->cbSize);
- return S_OK;
- }
- HRESULT CFormattedTextDraw::TxGetPropertyBits(DWORD dwMask, DWORD *pdwBits)
- {
- *pdwBits = m_dwPropertyBits;
- return S_OK;
- }
- HRESULT CFormattedTextDraw::TxNotify(DWORD iNotify, void *pv)
- {
- return S_OK;
- }
- HIMC CFormattedTextDraw::TxImmGetContext()
- {
- return NULL;
- }
- void CFormattedTextDraw::TxImmReleaseContext(HIMC himc)
- {
- }
- HRESULT CFormattedTextDraw::TxGetSelectionBarWidth(LONG *lSelBarWidth)
- {
- *lSelBarWidth = 100;
- return S_OK;
- }
- /////////////////////////////////////////////////////////////////////////////
- // custom functions
- HRESULT CFormattedTextDraw::CharFormatFromHFONT(CHARFORMAT2W* pCF, HFONT hFont)
- // Takes an HFONT and fills in a CHARFORMAT2W structure with the corresponding info
- {
- HWND hWnd;
- LOGFONT lf;
- HDC hDC;
- LONG yPixPerInch;
- // Get LOGFONT for default font
- if (!hFont)
- hFont = (HFONT) GetStockObject(SYSTEM_FONT);
- // Get LOGFONT for passed hfont
- if (!GetObject(hFont, sizeof(LOGFONT), &lf))
- return E_FAIL;
- // Set CHARFORMAT structure
- memset(pCF, 0, sizeof(CHARFORMAT2W));
- pCF->cbSize = sizeof(CHARFORMAT2W);
- hWnd = GetDesktopWindow();
- hDC = GetDC(hWnd);
- yPixPerInch = GetDeviceCaps(hDC, LOGPIXELSY);
- pCF->yHeight = -lf.lfHeight * LY_PER_INCH / yPixPerInch;
- ReleaseDC(hWnd, hDC);
- pCF->yOffset = 0;
- pCF->crTextColor = 0;
- pCF->dwEffects = CFM_EFFECTS | CFE_AUTOBACKCOLOR;
- pCF->dwEffects &= ~(CFE_PROTECTED | CFE_LINK | CFE_AUTOCOLOR);
- if(lf.lfWeight < FW_BOLD)
- pCF->dwEffects &= ~CFE_BOLD;
- if(!lf.lfItalic)
- pCF->dwEffects &= ~CFE_ITALIC;
- if(!lf.lfUnderline)
- pCF->dwEffects &= ~CFE_UNDERLINE;
- if(!lf.lfStrikeOut)
- pCF->dwEffects &= ~CFE_STRIKEOUT;
- pCF->dwMask = CFM_ALL | CFM_BACKCOLOR | CFM_STYLE;
- pCF->bCharSet = lf.lfCharSet;
- pCF->bPitchAndFamily = lf.lfPitchAndFamily;
- #ifdef UNICODE
- _tcscpy(pCF->szFaceName, lf.lfFaceName);
- #else
- MultiByteToWideChar(CP_ACP, 0, lf.lfFaceName, LF_FACESIZE, pCF->szFaceName, LF_FACESIZE);
- #endif
- return S_OK;
- }
- HRESULT CFormattedTextDraw::InitDefaultCharFormat()
- {
- return CharFormatFromHFONT(m_pCF, NULL);
- }
- HRESULT CFormattedTextDraw::InitDefaultParaFormat()
- {
- memset(&m_PF, 0, sizeof(PARAFORMAT2));
- m_PF.cbSize = sizeof(PARAFORMAT2);
- m_PF.dwMask = PFM_ALL;
- m_PF.wAlignment = PFA_LEFT;
- m_PF.cTabCount = 1;
- m_PF.rgxTabs[0] = lDefaultTab;
- return S_OK;
- }
- HRESULT CFormattedTextDraw::CreateTextServicesObject()
- {
- HRESULT hr;
- IUnknown *spUnk;
- hr = CreateTextServices(NULL, static_cast<ITextHost*>(this), &spUnk);
- if (hr == S_OK) {
- hr = spUnk->QueryInterface(IID_ITextServicesEx, (void**)&m_spTextServices);
- hr = spUnk->QueryInterface(IID_ITextDocument, (void**)&m_spTextDocument);
- spUnk->Release();
- }
- return hr;
- }
|