RichEditCtrlEx.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. // AutoRichEditCtrl.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "RichEditCtrlEx.h"
  5. #ifdef _DEBUG
  6. #define new DEBUG_NEW
  7. #undef THIS_FILE
  8. static char THIS_FILE[] = __FILE__;
  9. #endif
  10. /////////////////////////////////////////////////////////////////////////////
  11. // CAutoRichEditCtrl
  12. CRichEditCtrlEx::CRichEditCtrlEx()
  13. {
  14. }
  15. CRichEditCtrlEx::~CRichEditCtrlEx()
  16. {
  17. }
  18. BEGIN_MESSAGE_MAP(CRichEditCtrlEx, CRichEditCtrl)
  19. //{{AFX_MSG_MAP(CRichEditCtrlEx)
  20. // NOTE - the ClassWizard will add and remove mapping macros here.
  21. //}}AFX_MSG_MAP
  22. END_MESSAGE_MAP()
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CRichEditCtrlEx message handlers
  25. CString CRichEditCtrlEx::GetRTF()
  26. {
  27. // Return the RTF string of the text in the control.
  28. // Stream out here.
  29. EDITSTREAM es;
  30. es.dwError = 0;
  31. es.pfnCallback = CBStreamOut; // Set the callback
  32. CString sRTF = "";
  33. es.dwCookie = (DWORD) &sRTF; // so sRTF receives the string
  34. StreamOut(SF_RTF, es); // Call CRichEditCtrl::StreamOut to get the string.
  35. ///
  36. return sRTF;
  37. }
  38. void CRichEditCtrlEx::SetRTF(CString sRTF)
  39. {
  40. // Put the RTF string sRTF into the rich edit control.
  41. // Read the text in
  42. EDITSTREAM es;
  43. es.dwError = 0;
  44. es.pfnCallback = CBStreamIn;
  45. es.dwCookie = (DWORD) &sRTF;
  46. StreamIn(SF_RTF, es); // Do it.
  47. }
  48. CString CRichEditCtrlEx::GetText()
  49. {
  50. // Return the RTF string of the text in the control.
  51. // Stream out here.
  52. EDITSTREAM es;
  53. es.dwError = 0;
  54. es.pfnCallback = CBStreamOut; // Set the callback
  55. CString sText = "";
  56. es.dwCookie = (DWORD) &sText; // so sRTF receives the string
  57. StreamOut(SF_TEXT, es); // Call CRichEditCtrl::StreamOut to get the string.
  58. return sText;
  59. }
  60. void CRichEditCtrlEx::SetText(CString sText)
  61. {
  62. // Put the RTF string sRTF into the rich edit control.
  63. // Read the text in
  64. EDITSTREAM es;
  65. es.dwError = 0;
  66. es.pfnCallback = CBStreamIn;
  67. es.dwCookie = (DWORD) &sText;
  68. StreamIn(SF_TEXT, es); // Do it.
  69. }
  70. /*
  71. Callback function to stream an RTF string into the rich edit control.
  72. */
  73. DWORD CALLBACK CRichEditCtrlEx::CBStreamIn(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
  74. {
  75. // We insert the rich text here.
  76. /*
  77. This function taken from CodeGuru.com
  78. http://www.codeguru.com/richedit/rtf_string_streamin.shtml
  79. Zafir Anjum
  80. */
  81. CString *pstr = (CString *) dwCookie;
  82. if (pstr->GetLength() < cb)
  83. {
  84. *pcb = pstr->GetLength();
  85. memcpy(pbBuff, (LPCSTR) *pstr, *pcb);
  86. pstr->Empty();
  87. }
  88. else
  89. {
  90. *pcb = cb;
  91. memcpy(pbBuff, (LPCSTR) *pstr, *pcb);
  92. *pstr = pstr->Right(pstr->GetLength() - cb);
  93. }
  94. ///
  95. return 0;
  96. }
  97. /*
  98. Callback function to stream the RTF string out of the rich edit control.
  99. */
  100. DWORD CALLBACK CRichEditCtrlEx::CBStreamOut(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
  101. {
  102. // Address of our string var is in psEntry
  103. CString *psEntry = (CString*) dwCookie;
  104. CString tmpEntry = "";
  105. tmpEntry = (CString) pbBuff;
  106. // And write it!!!
  107. *psEntry += tmpEntry.Left(cb);
  108. return 0;
  109. }
  110. bool CRichEditCtrlEx::SelectionIsBold()
  111. {
  112. CHARFORMAT cf = GetCharFormat();
  113. if (cf.dwEffects & CFM_BOLD)
  114. return true;
  115. else
  116. return false;
  117. }
  118. bool CRichEditCtrlEx::SelectionIsItalic()
  119. {
  120. CHARFORMAT cf = GetCharFormat();
  121. if (cf.dwEffects & CFM_ITALIC)
  122. return true;
  123. else
  124. return false;
  125. }
  126. bool CRichEditCtrlEx::SelectionIsUnderlined()
  127. {
  128. CHARFORMAT cf = GetCharFormat();
  129. if (cf.dwEffects & CFM_UNDERLINE)
  130. return true;
  131. else
  132. return false;
  133. }
  134. CHARFORMAT CRichEditCtrlEx::GetCharFormat(DWORD dwMask)
  135. {
  136. CHARFORMAT cf;
  137. cf.cbSize = sizeof(CHARFORMAT);
  138. cf.dwMask = dwMask;
  139. GetSelectionCharFormat(cf);
  140. return cf;
  141. }
  142. void CRichEditCtrlEx::SetCharStyle(int MASK, int STYLE, int nStart, int nEnd)
  143. {
  144. CHARFORMAT cf;
  145. cf.cbSize = sizeof(CHARFORMAT);
  146. //cf.dwMask = MASK;
  147. GetSelectionCharFormat(cf);
  148. if (cf.dwMask & MASK) // selection is all the same
  149. {
  150. cf.dwEffects ^= STYLE;
  151. }
  152. else
  153. {
  154. cf.dwEffects |= STYLE;
  155. }
  156. cf.dwMask = MASK;
  157. SetSelectionCharFormat(cf);
  158. }
  159. void CRichEditCtrlEx::SetSelectionBold()
  160. {
  161. long start=0, end=0;
  162. GetSel(start, end); // Get the current selection
  163. SetCharStyle(CFM_BOLD, CFE_BOLD, start, end); // Make it bold
  164. }
  165. void CRichEditCtrlEx::SetSelectionItalic()
  166. {
  167. long start=0, end=0;
  168. GetSel(start, end);
  169. SetCharStyle(CFM_ITALIC, CFE_ITALIC, start, end);
  170. }
  171. void CRichEditCtrlEx::SetSelectionUnderlined()
  172. {
  173. long start=0, end=0;
  174. GetSel(start, end);
  175. SetCharStyle(CFM_UNDERLINE, CFE_UNDERLINE, start, end);
  176. }
  177. void CRichEditCtrlEx::SetParagraphCenter()
  178. {
  179. PARAFORMAT paraFormat;
  180. paraFormat.cbSize = sizeof(PARAFORMAT);
  181. paraFormat.dwMask = PFM_ALIGNMENT;
  182. paraFormat.wAlignment = PFA_CENTER;
  183. SetParaFormat(paraFormat); // Set the paragraph.
  184. }
  185. void CRichEditCtrlEx::SetParagraphLeft()
  186. {
  187. PARAFORMAT paraFormat;
  188. paraFormat.cbSize = sizeof(PARAFORMAT);
  189. paraFormat.dwMask = PFM_ALIGNMENT;
  190. paraFormat.wAlignment = PFA_LEFT;
  191. SetParaFormat(paraFormat);
  192. }
  193. void CRichEditCtrlEx::SetParagraphRight()
  194. {
  195. PARAFORMAT paraFormat;
  196. paraFormat.cbSize = sizeof(PARAFORMAT);
  197. paraFormat.dwMask = PFM_ALIGNMENT;
  198. paraFormat.wAlignment = PFA_RIGHT;
  199. SetParaFormat(paraFormat);
  200. }
  201. bool CRichEditCtrlEx::ParagraphIsCentered()
  202. {
  203. PARAFORMAT pf = GetParagraphFormat();
  204. if (pf.wAlignment == PFA_CENTER)
  205. return true;
  206. else
  207. return false;
  208. }
  209. bool CRichEditCtrlEx::ParagraphIsLeft()
  210. {
  211. PARAFORMAT pf = GetParagraphFormat();
  212. if (pf.wAlignment == PFA_LEFT)
  213. return true;
  214. else
  215. return false;
  216. }
  217. bool CRichEditCtrlEx::ParagraphIsRight()
  218. {
  219. PARAFORMAT pf = GetParagraphFormat();
  220. if (pf.wAlignment == PFA_RIGHT)
  221. return true;
  222. else
  223. return false;
  224. }
  225. PARAFORMAT CRichEditCtrlEx::GetParagraphFormat()
  226. {
  227. PARAFORMAT pf;
  228. pf.cbSize = sizeof(PARAFORMAT);
  229. pf.dwMask = PFM_ALIGNMENT | PFM_NUMBERING;
  230. GetParaFormat(pf);
  231. return pf;
  232. }
  233. void CRichEditCtrlEx::SetParagraphBulleted()
  234. {
  235. PARAFORMAT paraformat = GetParagraphFormat();
  236. if ( (paraformat.dwMask & PFM_NUMBERING) && (paraformat.wNumbering == PFN_BULLET) )
  237. {
  238. paraformat.wNumbering = 0;
  239. paraformat.dxOffset = 0;
  240. paraformat.dxStartIndent = 0;
  241. paraformat.dwMask = PFM_NUMBERING | PFM_STARTINDENT | PFM_OFFSET;
  242. }
  243. else
  244. {
  245. paraformat.wNumbering = PFN_BULLET;
  246. paraformat.dwMask = PFM_NUMBERING;
  247. if (paraformat.dxOffset == 0)
  248. {
  249. paraformat.dxOffset = 4;
  250. paraformat.dwMask = PFM_NUMBERING | PFM_STARTINDENT | PFM_OFFSET;
  251. }
  252. }
  253. SetParaFormat(paraformat);
  254. }
  255. bool CRichEditCtrlEx::ParagraphIsBulleted()
  256. {
  257. PARAFORMAT pf = GetParagraphFormat();
  258. if (pf.wNumbering == PFN_BULLET)
  259. return true;
  260. else
  261. return false;
  262. }
  263. void CRichEditCtrlEx::SelectColor()
  264. {
  265. CColorDialog dlg;
  266. CHARFORMAT cf = GetCharFormat();
  267. if (cf.dwEffects & CFE_AUTOCOLOR) cf.dwEffects -= CFE_AUTOCOLOR;
  268. // Get a color from the common color dialog.
  269. if( dlg.DoModal() == IDOK )
  270. {
  271. cf.crTextColor = dlg.GetColor();
  272. }
  273. cf.dwMask = CFM_COLOR;
  274. SetSelectionCharFormat(cf);
  275. }
  276. void CRichEditCtrlEx::SetFontName(CString sFontName)
  277. {
  278. CHARFORMAT cf = GetCharFormat();
  279. // Set the font name.
  280. for (int i = 0; i <= sFontName.GetLength()-1; i++)
  281. cf.szFaceName[i] = sFontName[i];
  282. cf.dwMask = CFM_FACE;
  283. SetSelectionCharFormat(cf);
  284. }
  285. void CRichEditCtrlEx::SetFontSize(int nPointSize)
  286. {
  287. CHARFORMAT cf = GetCharFormat();
  288. nPointSize *= 20; // convert from to twips
  289. cf.yHeight = nPointSize;
  290. cf.dwMask = CFM_SIZE;
  291. SetSelectionCharFormat(cf);
  292. }
  293. void CRichEditCtrlEx::GetSystemFonts(CStringArray &saFontList)
  294. {
  295. CDC *pDC = GetDC ();
  296. EnumFonts (pDC->GetSafeHdc(),NULL,(FONTENUMPROC) CBEnumFonts,(LPARAM)&saFontList);//Enumerate
  297. }
  298. BOOL CALLBACK CRichEditCtrlEx::CBEnumFonts(LPLOGFONT lplf, LPTEXTMETRIC lptm, DWORD dwType, LPARAM lpData)
  299. {
  300. // This function was written with the help of CCustComboBox, by Girish Bharadwaj.
  301. // Available from Codeguru.
  302. if (dwType == TRUETYPE_FONTTYPE)
  303. {
  304. ((CStringArray *) lpData)->Add( lplf->lfFaceName );
  305. }
  306. return true;
  307. }
  308. CString CRichEditCtrlEx::GetSelectionFontName()
  309. {
  310. CHARFORMAT cf = GetCharFormat();
  311. CString sName = cf.szFaceName;
  312. return sName;
  313. }
  314. long CRichEditCtrlEx::GetSelectionFontSize()
  315. {
  316. CHARFORMAT cf = GetCharFormat();
  317. long nSize = cf.yHeight/20;
  318. return nSize;
  319. }