FormattedTextDraw.cpp 8.9 KB

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