FormattedTextDraw.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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.h : Declaration of the CFormattedTextDraw
  5. #ifndef __FORMATTEDTEXTDRAW_H_
  6. #define __FORMATTEDTEXTDRAW_H_
  7. #include "resource.h" // main symbols
  8. // be sure to link this project with riched20.lib
  9. #include <richedit.h>
  10. #include <textserv.h>
  11. #ifndef LY_PER_INCH
  12. #define LY_PER_INCH 1440
  13. #define HOST_BORDER 0
  14. #endif
  15. typedef struct tagCOOKIE
  16. {
  17. BSTR bstrText;
  18. DWORD dwSize;
  19. DWORD dwCount;
  20. } COOKIE, *PCOOKIE;
  21. // These definitions might give you problems. If they do, open your
  22. // textserv.h and comment the following lines out. They are right at the top
  23. // EXTERN_C const IID IID_ITextServices;
  24. // EXTERN_C const IID IID_ITextHost;
  25. const IID IID_ITextServicesEx = { // 8d33f740-cf58-11ce-a89d-00aa006cadc5
  26. 0x8d33f740,
  27. 0xcf58,
  28. 0x11ce,
  29. {0xa8, 0x9d, 0x00, 0xaa, 0x00, 0x6c, 0xad, 0xc5}
  30. };
  31. //const IID IID_ITextHosts = { // c5bdd8d0-d26e-11ce-a89e-00aa006cadc5
  32. // 0xc5bdd8d0,
  33. // 0xd26e,
  34. // 0x11ce,
  35. // {0xa8, 0x9e, 0x00, 0xaa, 0x00, 0x6c, 0xad, 0xc5}
  36. // };
  37. /////////////////////////////////////////////////////////////////////////////
  38. // IFormatttedTextDraw
  39. interface IFormattedTextDraw
  40. {
  41. public:
  42. virtual ~IFormattedTextDraw() {};
  43. virtual HRESULT get_NaturalHeight(long Width, /*[out, retval]*/ long *pVal) = 0;
  44. virtual HRESULT get_NaturalWidth(long Height, /*[out, retval]*/ long *pVal) = 0;
  45. virtual HRESULT Create() = 0;
  46. virtual HRESULT Draw(void *hdcDraw, RECT *prc) = 0;
  47. virtual HRESULT get_RTFText(/*[out, retval]*/ BSTR *pVal) = 0;
  48. virtual HRESULT put_RTFText(/*[in]*/ BSTR newVal) = 0;
  49. // COM-like functions
  50. virtual ULONG STDMETHODCALLTYPE AddRef(void) = 0;
  51. virtual ULONG STDMETHODCALLTYPE Release(void) = 0;
  52. };
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CFormattedTextDraw
  55. class CFormattedTextDraw :
  56. public ITextHost,
  57. public IFormattedTextDraw
  58. {
  59. public:
  60. CFormattedTextDraw()
  61. {
  62. HDC hdcScreen;
  63. m_RTFText = NULL;
  64. hdcScreen = GetDC(NULL);
  65. nPixelsPerInchX = GetDeviceCaps(hdcScreen, LOGPIXELSX);
  66. nPixelsPerInchY = GetDeviceCaps(hdcScreen, LOGPIXELSY);
  67. ReleaseDC(NULL, hdcScreen);
  68. SetRectEmpty(&m_rcClient);
  69. SetRectEmpty(&m_rcViewInset);
  70. m_pCF = (CHARFORMAT2W*) malloc(sizeof(CHARFORMAT2W));
  71. InitDefaultCharFormat();
  72. InitDefaultParaFormat();
  73. m_spTextServices = NULL;
  74. m_spTextDocument = NULL;
  75. m_dwPropertyBits = TXTBIT_RICHTEXT | TXTBIT_MULTILINE | TXTBIT_WORDWRAP | TXTBIT_USECURRENTBKG;
  76. m_dwScrollbar = 0;
  77. m_dwMaxLength = INFINITE;
  78. }
  79. ~CFormattedTextDraw()
  80. {
  81. if (m_RTFText != NULL)
  82. SysFreeString(m_RTFText);
  83. free(m_pCF);
  84. if (m_spTextServices != NULL)
  85. m_spTextServices->Release();
  86. if (m_spTextDocument != NULL)
  87. m_spTextDocument->Release();
  88. }
  89. // Minimal COM functionality
  90. HRESULT STDMETHODCALLTYPE QueryInterface(
  91. /* [in] */ REFIID riid,
  92. /* [iid_is][out] */ void __RPC_FAR *__RPC_FAR *ppvObject)
  93. {
  94. *ppvObject = NULL;
  95. return S_FALSE;
  96. }
  97. ULONG STDMETHODCALLTYPE AddRef(void)
  98. {
  99. return 0;
  100. }
  101. ULONG STDMETHODCALLTYPE Release(void)
  102. {
  103. return 0;
  104. }
  105. // IFormattedTextDraw
  106. public:
  107. HRESULT get_NaturalHeight(long Width, /*[out, retval]*/ long *pVal);
  108. HRESULT get_NaturalWidth(long Height, /*[out, retval]*/ long *pVal);
  109. HRESULT Create();
  110. HRESULT Draw(void *hdcDraw, RECT *prc);
  111. HRESULT get_RTFText(/*[out, retval]*/ BSTR *pVal);
  112. HRESULT put_RTFText(/*[in]*/ BSTR newVal);
  113. // ITextHost
  114. HDC TxGetDC();
  115. INT TxReleaseDC(HDC hdc);
  116. BOOL TxShowScrollBar(INT fnBar, BOOL fShow);
  117. BOOL TxEnableScrollBar(INT fuSBFlags, INT fuArrowflags);
  118. BOOL TxSetScrollRange(INT fnBar, LONG nMinPos, INT nMaxPos, BOOL fRedraw);
  119. BOOL TxSetScrollPos(INT fnBar, INT nPos, BOOL fRedraw);
  120. void TxInvalidateRect(LPCRECT prc, BOOL fMode);
  121. void TxViewChange(BOOL fUpdate);
  122. BOOL TxCreateCaret(HBITMAP hbmp, INT xWidth, INT yHeight);
  123. BOOL TxShowCaret(BOOL fShow);
  124. BOOL TxSetCaretPos(INT x, INT y);
  125. BOOL TxSetTimer(UINT idTimer, UINT uTimeout);
  126. void TxKillTimer(UINT idTimer);
  127. void TxScrollWindowEx(INT dx, INT dy, LPCRECT lprcScroll, LPCRECT lprcClip, HRGN hrgnUpdate, LPRECT lprcUpdate, UINT fuScroll);
  128. void TxSetCapture(BOOL fCapture);
  129. void TxSetFocus();
  130. void TxSetCursor(HCURSOR hcur, BOOL fText);
  131. BOOL TxScreenToClient(LPPOINT lppt);
  132. BOOL TxClientToScreen(LPPOINT lppt);
  133. HRESULT TxActivate(LONG * plOldState);
  134. HRESULT TxDeactivate(LONG lNewState);
  135. HRESULT TxGetClientRect(LPRECT prc);
  136. HRESULT TxGetViewInset(LPRECT prc);
  137. HRESULT TxGetCharFormat(const CHARFORMATW **ppCF);
  138. HRESULT TxGetParaFormat(const PARAFORMAT **ppPF);
  139. COLORREF TxGetSysColor(int nIndex);
  140. HRESULT TxGetBackStyle(TXTBACKSTYLE *pstyle);
  141. HRESULT TxGetMaxLength(DWORD *plength);
  142. HRESULT TxGetScrollBars(DWORD *pdwScrollBar);
  143. HRESULT TxGetPasswordChar(TCHAR *pch);
  144. HRESULT TxGetAcceleratorPos(LONG *pcp);
  145. HRESULT TxGetExtent(LPSIZEL lpExtent);
  146. HRESULT OnTxCharFormatChange(const CHARFORMATW * pcf);
  147. HRESULT OnTxParaFormatChange(const PARAFORMAT * ppf);
  148. HRESULT TxGetPropertyBits(DWORD dwMask, DWORD *pdwBits);
  149. HRESULT TxNotify(DWORD iNotify, void *pv);
  150. HIMC TxImmGetContext();
  151. void TxImmReleaseContext(HIMC himc);
  152. HRESULT TxGetSelectionBarWidth(LONG *lSelBarWidth);
  153. // Custom functions
  154. HRESULT CharFormatFromHFONT(CHARFORMAT2W* pCF, HFONT hFont);
  155. HRESULT InitDefaultCharFormat();
  156. HRESULT InitDefaultParaFormat();
  157. HRESULT CreateTextServicesObject();
  158. // Variables
  159. RECT m_rcClient; // Client Rect
  160. RECT m_rcViewInset; // view rect inset
  161. SIZEL m_sizelExtent; // Extent array
  162. int nPixelsPerInchX; // Pixels per logical inch along width
  163. int nPixelsPerInchY; // Pixels per logical inch along height
  164. CHARFORMAT2W *m_pCF;
  165. PARAFORMAT2 m_PF;
  166. DWORD m_dwScrollbar; // Scroll bar style
  167. DWORD m_dwPropertyBits; // Property bits
  168. DWORD m_dwMaxLength;
  169. COOKIE m_editCookie;
  170. ITextServices *m_spTextServices;
  171. ITextDocument *m_spTextDocument;
  172. BSTR m_RTFText;
  173. };
  174. #endif //__FORMATTEDTEXTDRAW_H_