FormattedTextDraw.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. // Feel free to use this code in your own applications.
  2. // The Author does not guarantee anything about this code.
  3. // Author : Yves Maurer
  4. // FormattedTextDraw.cpp : Implementation of CFormattedTextDraw
  5. #include "stdafx.h"
  6. #include "FormattedTextDraw.h"
  7. /////////////////////////////////////////////////////////////////////////////
  8. // CallBack functions
  9. DWORD CALLBACK EditStreamInCallback(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
  10. {
  11. PCOOKIE pCookie = (PCOOKIE) dwCookie;
  12. if(pCookie->dwSize - pCookie->dwCount < (DWORD) cb)
  13. *pcb = pCookie->dwSize - pCookie->dwCount;
  14. else
  15. *pcb = cb;
  16. CopyMemory(pbBuff, pCookie->bstrText, *pcb);
  17. pCookie->dwCount += *pcb;
  18. return 0; // callback succeeded - no errors
  19. }
  20. /////////////////////////////////////////////////////////////////////////////
  21. // CFormattedTextDraw
  22. HRESULT CFormattedTextDraw::get_RTFText(BSTR *pVal)
  23. {
  24. *pVal = SysAllocStringLen(m_RTFText, SysStringLen(m_RTFText));
  25. return S_OK;
  26. }
  27. HRESULT CFormattedTextDraw::put_RTFText(BSTR newVal)
  28. {
  29. HRESULT hr;
  30. long len;
  31. LRESULT lResult = 0;
  32. EDITSTREAM editStream;
  33. if (m_RTFText != NULL)
  34. SysFreeString(m_RTFText);
  35. len = SysStringLen(newVal);
  36. m_RTFText = SysAllocStringLen(newVal, len);
  37. if (!m_spTextServices)
  38. return S_FALSE;
  39. m_editCookie.bstrText = (BSTR) malloc(len + 1);
  40. WideCharToMultiByte(CP_ACP, 0, m_RTFText, len, (char *) m_editCookie.bstrText, len, NULL, NULL);
  41. m_editCookie.dwSize = lstrlenA((LPSTR) m_editCookie.bstrText);
  42. m_editCookie.dwCount = 0;
  43. editStream.dwCookie = (DWORD_PTR) &m_editCookie;
  44. editStream.dwError = 0;
  45. editStream.pfnCallback = EditStreamInCallback;
  46. hr = m_spTextServices->TxSendMessage(EM_STREAMIN, (WPARAM)(SF_RTF | SF_UNICODE), (LPARAM)&editStream, &lResult);
  47. free(m_editCookie.bstrText);
  48. return S_OK;
  49. }
  50. HRESULT CFormattedTextDraw::Draw(void *hdcDraw, RECT *prc)
  51. {
  52. if (!m_spTextServices)
  53. return S_FALSE;
  54. m_spTextServices->TxDraw(
  55. DVASPECT_CONTENT, // Draw Aspect
  56. 0, // Lindex
  57. NULL, // Info for drawing optimization
  58. NULL, // target device information
  59. (HDC) hdcDraw, // Draw device HDC
  60. NULL, // Target device HDC
  61. (RECTL *) prc, // Bounding client rectangle
  62. NULL, // Clipping rectangle for metafiles
  63. (RECT *) NULL, // Update rectangle
  64. NULL, // Call back function
  65. NULL, // Call back parameter
  66. TXTVIEW_INACTIVE); // What view of the object could be TXTVIEW_ACTIVE
  67. return S_OK;
  68. }
  69. HRESULT CFormattedTextDraw::Create()
  70. {
  71. return CreateTextServicesObject();
  72. }
  73. HRESULT CFormattedTextDraw::get_NaturalWidth(long Height, long *pVal)
  74. {
  75. long lWidth;
  76. SIZEL szExtent;
  77. HDC hdcDraw;
  78. if (!m_spTextServices)
  79. return S_FALSE;
  80. hdcDraw = GetDC(NULL);
  81. szExtent.cy = Height;
  82. szExtent.cx = 10000;
  83. lWidth = 10000;
  84. m_spTextServices->TxGetNaturalSize(DVASPECT_CONTENT,
  85. hdcDraw,
  86. NULL,
  87. NULL,
  88. TXTNS_FITTOCONTENT,
  89. &szExtent,
  90. &lWidth,
  91. &Height);
  92. ReleaseDC(NULL, hdcDraw);
  93. *pVal = lWidth;
  94. return S_OK;
  95. }
  96. HRESULT CFormattedTextDraw::get_NaturalHeight(long Width, long *pVal)
  97. {
  98. long lHeight;
  99. SIZEL szExtent;
  100. HDC hdcDraw;
  101. if (!m_spTextServices)
  102. return S_FALSE;
  103. hdcDraw = GetDC(NULL);
  104. szExtent.cx = Width;
  105. szExtent.cy = 10000;
  106. lHeight = 10000;
  107. m_spTextServices->TxGetNaturalSize(DVASPECT_CONTENT,
  108. hdcDraw,
  109. NULL,
  110. NULL,
  111. TXTNS_FITTOCONTENT,
  112. &szExtent,
  113. &Width,
  114. &lHeight);
  115. ReleaseDC(NULL, hdcDraw);
  116. *pVal = lHeight;
  117. return S_OK;
  118. }
  119. /////////////////////////////////////////////////////////////////////////////
  120. // ITextHost functions
  121. HDC CFormattedTextDraw::TxGetDC()
  122. {
  123. return NULL;
  124. }
  125. INT CFormattedTextDraw::TxReleaseDC(HDC hdc)
  126. {
  127. return 1;
  128. }
  129. BOOL CFormattedTextDraw::TxShowScrollBar(INT fnBar, BOOL fShow)
  130. {
  131. return FALSE;
  132. }
  133. BOOL CFormattedTextDraw::TxEnableScrollBar(INT fuSBFlags, INT fuArrowflags)
  134. {
  135. return FALSE;
  136. }
  137. BOOL CFormattedTextDraw::TxSetScrollRange(INT fnBar, LONG nMinPos, INT nMaxPos, BOOL fRedraw)
  138. {
  139. return FALSE;
  140. }
  141. BOOL CFormattedTextDraw::TxSetScrollPos(INT fnBar, INT nPos, BOOL fRedraw)
  142. {
  143. return FALSE;
  144. }
  145. void CFormattedTextDraw::TxInvalidateRect(LPCRECT prc, BOOL fMode)
  146. {
  147. }
  148. void CFormattedTextDraw::TxViewChange(BOOL fUpdate)
  149. {
  150. }
  151. BOOL CFormattedTextDraw::TxCreateCaret(HBITMAP hbmp, INT xWidth, INT yHeight)
  152. {
  153. return FALSE;
  154. }
  155. BOOL CFormattedTextDraw::TxShowCaret(BOOL fShow)
  156. {
  157. return FALSE;
  158. }
  159. BOOL CFormattedTextDraw::TxSetCaretPos(INT x, INT y)
  160. {
  161. return FALSE;
  162. }
  163. BOOL CFormattedTextDraw::TxSetTimer(UINT idTimer, UINT uTimeout)
  164. {
  165. return FALSE;
  166. }
  167. void CFormattedTextDraw::TxKillTimer(UINT idTimer)
  168. {
  169. }
  170. void CFormattedTextDraw::TxScrollWindowEx(INT dx, INT dy, LPCRECT lprcScroll, LPCRECT lprcClip, HRGN hrgnUpdate, LPRECT lprcUpdate, UINT fuScroll)
  171. {
  172. }
  173. void CFormattedTextDraw::TxSetCapture(BOOL fCapture)
  174. {
  175. }
  176. void CFormattedTextDraw::TxSetFocus()
  177. {
  178. }
  179. void CFormattedTextDraw::TxSetCursor(HCURSOR hcur, BOOL fText)
  180. {
  181. }
  182. BOOL CFormattedTextDraw::TxScreenToClient(LPPOINT lppt)
  183. {
  184. return FALSE;
  185. }
  186. BOOL CFormattedTextDraw::TxClientToScreen(LPPOINT lppt)
  187. {
  188. return FALSE;
  189. }
  190. HRESULT CFormattedTextDraw::TxActivate(LONG * plOldState)
  191. {
  192. return S_OK;
  193. }
  194. HRESULT CFormattedTextDraw::TxDeactivate(LONG lNewState)
  195. {
  196. return S_OK;
  197. }
  198. HRESULT CFormattedTextDraw::TxGetClientRect(LPRECT prc)
  199. {
  200. *prc = m_rcClient;
  201. return S_OK;
  202. }
  203. HRESULT CFormattedTextDraw::TxGetViewInset(LPRECT prc)
  204. {
  205. *prc = m_rcViewInset;
  206. return S_OK;
  207. }
  208. HRESULT CFormattedTextDraw::TxGetCharFormat(const CHARFORMATW **ppCF)
  209. {
  210. *ppCF = m_pCF;
  211. return S_OK;
  212. }
  213. HRESULT CFormattedTextDraw::TxGetParaFormat(const PARAFORMAT **ppPF)
  214. {
  215. *ppPF = &m_PF;
  216. return S_OK;
  217. }
  218. COLORREF CFormattedTextDraw::TxGetSysColor(int nIndex)
  219. {
  220. return GetSysColor(nIndex);
  221. }
  222. HRESULT CFormattedTextDraw::TxGetBackStyle(TXTBACKSTYLE *pstyle)
  223. {
  224. *pstyle = TXTBACK_TRANSPARENT;
  225. return S_OK;
  226. }
  227. HRESULT CFormattedTextDraw::TxGetMaxLength(DWORD *plength)
  228. {
  229. *plength = m_dwMaxLength;
  230. return S_OK;
  231. }
  232. HRESULT CFormattedTextDraw::TxGetScrollBars(DWORD *pdwScrollBar)
  233. {
  234. *pdwScrollBar = m_dwScrollbar;
  235. return S_OK;
  236. }
  237. HRESULT CFormattedTextDraw::TxGetPasswordChar(TCHAR *pch)
  238. {
  239. return S_FALSE;
  240. }
  241. HRESULT CFormattedTextDraw::TxGetAcceleratorPos(LONG *pcp)
  242. {
  243. *pcp = -1;
  244. return S_OK;
  245. }
  246. HRESULT CFormattedTextDraw::TxGetExtent(LPSIZEL lpExtent)
  247. {
  248. return E_NOTIMPL;
  249. }
  250. HRESULT CFormattedTextDraw::OnTxCharFormatChange(const CHARFORMATW * pcf)
  251. {
  252. memcpy(m_pCF, pcf, pcf->cbSize);
  253. return S_OK;
  254. }
  255. HRESULT CFormattedTextDraw::OnTxParaFormatChange(const PARAFORMAT * ppf)
  256. {
  257. memcpy(&m_PF, ppf, ppf->cbSize);
  258. return S_OK;
  259. }
  260. HRESULT CFormattedTextDraw::TxGetPropertyBits(DWORD dwMask, DWORD *pdwBits)
  261. {
  262. *pdwBits = m_dwPropertyBits;
  263. return S_OK;
  264. }
  265. HRESULT CFormattedTextDraw::TxNotify(DWORD iNotify, void *pv)
  266. {
  267. return S_OK;
  268. }
  269. HIMC CFormattedTextDraw::TxImmGetContext()
  270. {
  271. return NULL;
  272. }
  273. void CFormattedTextDraw::TxImmReleaseContext(HIMC himc)
  274. {
  275. }
  276. HRESULT CFormattedTextDraw::TxGetSelectionBarWidth(LONG *lSelBarWidth)
  277. {
  278. *lSelBarWidth = 100;
  279. return S_OK;
  280. }
  281. /////////////////////////////////////////////////////////////////////////////
  282. // custom functions
  283. HRESULT CFormattedTextDraw::CharFormatFromHFONT(CHARFORMAT2W* pCF, HFONT hFont)
  284. // Takes an HFONT and fills in a CHARFORMAT2W structure with the corresponding info
  285. {
  286. HWND hWnd;
  287. LOGFONT lf;
  288. HDC hDC;
  289. LONG yPixPerInch;
  290. // Get LOGFONT for default font
  291. if (!hFont)
  292. hFont = (HFONT) GetStockObject(SYSTEM_FONT);
  293. // Get LOGFONT for passed hfont
  294. if (!GetObject(hFont, sizeof(LOGFONT), &lf))
  295. return E_FAIL;
  296. // Set CHARFORMAT structure
  297. memset(pCF, 0, sizeof(CHARFORMAT2W));
  298. pCF->cbSize = sizeof(CHARFORMAT2W);
  299. hWnd = GetDesktopWindow();
  300. hDC = GetDC(hWnd);
  301. yPixPerInch = GetDeviceCaps(hDC, LOGPIXELSY);
  302. pCF->yHeight = -lf.lfHeight * LY_PER_INCH / yPixPerInch;
  303. ReleaseDC(hWnd, hDC);
  304. pCF->yOffset = 0;
  305. pCF->crTextColor = 0;
  306. pCF->dwEffects = CFM_EFFECTS | CFE_AUTOBACKCOLOR;
  307. pCF->dwEffects &= ~(CFE_PROTECTED | CFE_LINK | CFE_AUTOCOLOR);
  308. if(lf.lfWeight < FW_BOLD)
  309. pCF->dwEffects &= ~CFE_BOLD;
  310. if(!lf.lfItalic)
  311. pCF->dwEffects &= ~CFE_ITALIC;
  312. if(!lf.lfUnderline)
  313. pCF->dwEffects &= ~CFE_UNDERLINE;
  314. if(!lf.lfStrikeOut)
  315. pCF->dwEffects &= ~CFE_STRIKEOUT;
  316. pCF->dwMask = CFM_ALL | CFM_BACKCOLOR | CFM_STYLE;
  317. pCF->bCharSet = lf.lfCharSet;
  318. pCF->bPitchAndFamily = lf.lfPitchAndFamily;
  319. #ifdef UNICODE
  320. wcscpy(pCF->szFaceName, lf.lfFaceName);
  321. #else
  322. MultiByteToWideChar(CP_ACP, 0, lf.lfFaceName, LF_FACESIZE, pCF->szFaceName, LF_FACESIZE);
  323. #endif
  324. return S_OK;
  325. }
  326. HRESULT CFormattedTextDraw::InitDefaultCharFormat()
  327. {
  328. return CharFormatFromHFONT(m_pCF, NULL);
  329. }
  330. HRESULT CFormattedTextDraw::InitDefaultParaFormat()
  331. {
  332. memset(&m_PF, 0, sizeof(PARAFORMAT2));
  333. m_PF.cbSize = sizeof(PARAFORMAT2);
  334. m_PF.dwMask = PFM_ALL;
  335. m_PF.wAlignment = PFA_LEFT;
  336. m_PF.cTabCount = 1;
  337. m_PF.rgxTabs[0] = lDefaultTab;
  338. return S_OK;
  339. }
  340. HRESULT CFormattedTextDraw::CreateTextServicesObject()
  341. {
  342. HRESULT hr = S_OK;
  343. #ifdef _M_IX86
  344. IUnknown *spUnk;
  345. hr = CreateTextServices(NULL, static_cast<ITextHost*>(this), &spUnk);
  346. if (hr == S_OK) {
  347. hr = spUnk->QueryInterface(IID_ITextServicesEx, (void**)&m_spTextServices);
  348. hr = spUnk->QueryInterface(IID_ITextDocument, (void**)&m_spTextDocument);
  349. spUnk->Release();
  350. }
  351. #endif
  352. return hr;
  353. }