| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542 | 
							- // AutoRichEditCtrl.cpp : implementation file
 
- //
 
- #include "stdafx.h"
 
- #include "RichEditCtrlEx.h"
 
- #include "shared/TextConvert.h"
 
- #ifdef _DEBUG
 
- #define new DEBUG_NEW
 
- #undef THIS_FILE
 
- static char THIS_FILE[] = __FILE__;
 
- #endif
 
- /////////////////////////////////////////////////////////////////////////////
 
- // CAutoRichEditCtrl
 
- _AFX_RICHEDITEX_STATE _afxRichEditStateEx ;
 
- BOOL PASCAL AfxInitRichEditEx()
 
- {
 
-     if( ! ::AfxInitRichEdit() )
 
-     {
 
-         return FALSE ;
 
-     }
 
-     _AFX_RICHEDITEX_STATE* l_pState = &_afxRichEditStateEx ;
 
-     if( l_pState->m_hInstRichEdit20 == NULL )
 
-     {
 
- #ifdef _UNICODE 
 
-         l_pState->m_hInstRichEdit20 = LoadLibraryW(_T("RICHED20.DLL"));
 
- #else
 
- 		l_pState->m_hInstRichEdit20 = LoadLibraryA(_T("RICHED20.DLL"));
 
- #endif
 
-     }
 
-     return l_pState->m_hInstRichEdit20 != NULL ;
 
- }
 
- CRichEditCtrlEx::CRichEditCtrlEx()
 
- {
 
- }
 
- CRichEditCtrlEx::~CRichEditCtrlEx()
 
- {
 
- }
 
- BEGIN_MESSAGE_MAP(CRichEditCtrlEx, CRichEditCtrl)
 
- 	//{{AFX_MSG_MAP(CRichEditCtrlEx)
 
- 	ON_WM_CREATE()
 
- 	//}}AFX_MSG_MAP
 
- END_MESSAGE_MAP()
 
- /////////////////////////////////////////////////////////////////////////////
 
- // CRichEditCtrlEx message handlers
 
- CString CRichEditCtrlEx::GetRTF()
 
- {
 
- 	// Return the RTF string of the text in the control.
 
- 	
 
- 	// Stream out here.
 
- 	EDITSTREAM es;
 
- 	es.dwError = 0;
 
- 	es.pfnCallback = CBStreamOut;		// Set the callback
 
- 	CString sRTF = "";
 
- 	es.dwCookie = (DWORD) &sRTF;	// so sRTF receives the string
 
- 	
 
- 	StreamOut(SF_RTF, es);			// Call CRichEditCtrl::StreamOut to get the string.
 
- 	///
 
- 	return sRTF;
 
- }
 
- void CRichEditCtrlEx::SetRTF(const char *pRTF)
 
- {
 
- 	// Put the RTF string sRTF into the rich edit control.
 
- 	// Read the text in
 
- 	EDITSTREAM es;
 
- 	es.dwError = 0;
 
- 	es.pfnCallback = CBStreamIn;
 
- #ifdef _UNICODE
 
- 	CString cs;
 
- 	es.dwCookie = (DWORD) &cs;
 
- #else
 
- 	CString cs(pRTF);
 
- 	es.dwCookie = (DWORD) &cs;
 
- #endif
 
- 	StreamIn(SF_RTF, es);	// Do it.
 
- #ifdef _UNICODE
 
- 	SETTEXTEX stex;
 
- 	stex.flags = ST_SELECTION | ST_KEEPUNDO;
 
- 	SendMessage(EM_SETTEXTEX, (WPARAM)&stex, (LPARAM)pRTF); 
 
- #endif
 
- }
 
- void CRichEditCtrlEx::SetRTF(CStringA sRTF)
 
- {
 
- 	// Put the RTF string sRTF into the rich edit control.
 
- 	// Read the text in
 
- 	EDITSTREAM es;
 
- 	es.dwError = 0;
 
- 	es.pfnCallback = CBStreamIn;
 
- #ifdef _UNICODE
 
- 	CString cs;
 
- 	es.dwCookie = (DWORD) &cs;
 
- #else
 
- 	es.dwCookie = (DWORD) &sRTF;
 
- #endif
 
- 	StreamIn(SF_RTF, es);	// Do it.
 
- #ifdef _UNICODE
 
- 	SETTEXTEX stex;
 
-     stex.flags = ST_SELECTION | ST_KEEPUNDO;
 
-     SendMessage(EM_SETTEXTEX, (WPARAM)&stex, (LPARAM)sRTF.GetBuffer(sRTF.GetLength())); 
 
- #endif
 
- }
 
- CString CRichEditCtrlEx::GetText()
 
- {
 
- 	CString sText;
 
- 	
 
- #ifdef _UNICODE
 
- 	GETTEXTEX stex;
 
- 	stex.codepage = 1200;  // Unicode code page(set SETTEXTEX documentation)
 
- 	int nSize = GetTextLength();
 
- 	//increase the size incase of unicode text
 
- 	nSize++;
 
- 	nSize = nSize * 2;
 
- 	stex.cb = nSize;
 
- 	TCHAR *pText = new TCHAR[nSize];
 
- 	if(pText)
 
- 	{
 
- 		SendMessage(EM_GETTEXTEX, (WPARAM)&stex, (LPARAM)pText); 
 
- 		sText = pText;
 
- 		delete []pText;
 
- 		pText = NULL;
 
- 	}
 
- #else
 
- 	// Stream out here.
 
- 	EDITSTREAM es;
 
- 	es.dwError = 0;
 
- 	es.pfnCallback = CBStreamOut;		// Set the callback
 
- 	es.dwCookie = (DWORD) &sText;	// so sRTF receives the string
 
- 	StreamOut(SF_TEXT, es);			// Call CRichEditCtrl::StreamOut to get the string.
 
- #endif
 
- 	return sText;
 
- }
 
- void CRichEditCtrlEx::SetText(CString sText)
 
- {
 
- 	// Put the RTF string sRTF into the rich edit control.
 
- 	// Read the text in
 
- 	EDITSTREAM es;
 
- 	es.dwError = 0;
 
- 	es.pfnCallback = CBStreamIn;
 
- #ifdef _UNICODE
 
- 	CString cs;
 
- 	es.dwCookie = (DWORD) &cs;
 
- #else
 
- 	es.dwCookie = (DWORD) &sText;
 
- #endif
 
- 	StreamIn(SF_TEXT, es);	// Do it.
 
- #ifdef _UNICODE
 
- 	SETTEXTEX stex;
 
-     stex.flags = ST_SELECTION | ST_KEEPUNDO;
 
-     stex.codepage = 1200;  // Unicode code page(set SETTEXTEX documentation)
 
-     SendMessage(EM_SETTEXTEX, (WPARAM)&stex, (LPARAM)sText.GetBuffer(sText.GetLength())); 
 
- 	sText.ReleaseBuffer();
 
- #endif
 
- }
 
- /*
 
- 	Callback function to stream an RTF string into the rich edit control.
 
- */
 
- DWORD CALLBACK CRichEditCtrlEx::CBStreamIn(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
 
- {
 
- 	// We insert the rich text here.
 
- /*	
 
- 	This function taken from CodeGuru.com
 
- 	http://www.codeguru.com/richedit/rtf_string_streamin.shtml
 
- 	Zafir Anjum
 
- */
 
- 	CString *pstr = (CString *) dwCookie;
 
- 	if (pstr->GetLength() < cb)
 
- 	{
 
- 		*pcb = pstr->GetLength();
 
- 		memcpy(pbBuff, (LPCTSTR) *pstr, *pcb);
 
- 		pstr->Empty();
 
- 	}
 
- 	else
 
- 	{
 
- 		*pcb = cb;
 
- 		memcpy(pbBuff, (LPCTSTR) *pstr, *pcb);
 
- 		*pstr = pstr->Right(pstr->GetLength() - cb);
 
- 	}
 
- 	///
 
- 	return 0;
 
- }
 
- /*
 
- 	Callback function to stream the RTF string out of the rich edit control.
 
- */
 
- DWORD CALLBACK CRichEditCtrlEx::CBStreamOut(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
 
- {
 
- 	// Address of our string var is in psEntry
 
- 	CString *psEntry = (CString*) dwCookie;
 
- 	
 
- 	CString tmpEntry = "";
 
- 	tmpEntry = (CString) pbBuff;
 
- 	// And write it!!!
 
- 	*psEntry += tmpEntry.Left(cb);
 
- 	return 0;
 
- }
 
- bool CRichEditCtrlEx::SelectionIsBold()
 
- {
 
- 	CHARFORMAT cf = GetCharFormat();	
 
- 	
 
- 	if (cf.dwEffects & CFM_BOLD)
 
- 		return true;
 
- 	else
 
- 		return false;
 
- }
 
- bool CRichEditCtrlEx::SelectionIsItalic()
 
- {
 
- 	CHARFORMAT cf = GetCharFormat();	
 
- 	
 
- 	if (cf.dwEffects & CFM_ITALIC)
 
- 		return true;
 
- 	else
 
- 		return false;
 
- }
 
- bool CRichEditCtrlEx::SelectionIsUnderlined()
 
- {
 
- 	CHARFORMAT cf = GetCharFormat();	
 
- 	
 
- 	if (cf.dwEffects & CFM_UNDERLINE)
 
- 		return true;
 
- 	else
 
- 		return false;
 
- }
 
- CHARFORMAT CRichEditCtrlEx::GetCharFormat(DWORD dwMask)
 
- {
 
- 	CHARFORMAT cf;
 
- 	cf.cbSize = sizeof(CHARFORMAT);
 
- 	cf.dwMask = dwMask;
 
- 	GetSelectionCharFormat(cf);
 
- 	return cf;
 
- }
 
- void CRichEditCtrlEx::SetCharStyle(int MASK, int STYLE, int nStart, int nEnd)
 
- {
 
- 	CHARFORMAT cf;
 
- 	cf.cbSize = sizeof(CHARFORMAT);
 
- 	//cf.dwMask = MASK;
 
- 	
 
- 	GetSelectionCharFormat(cf);
 
- 	
 
- 	if (cf.dwMask & MASK)	// selection is all the same
 
- 	{
 
- 		cf.dwEffects ^= STYLE; 
 
- 	}
 
- 	else
 
- 	{
 
- 		cf.dwEffects |= STYLE;
 
- 	}
 
- 	
 
- 	cf.dwMask = MASK;
 
- 	SetSelectionCharFormat(cf);
 
- }
 
- void CRichEditCtrlEx::SetSelectionBold()
 
- {
 
- 	long start=0, end=0;
 
- 	GetSel(start, end);		// Get the current selection
 
- 	SetCharStyle(CFM_BOLD, CFE_BOLD, start, end);	// Make it bold
 
- }
 
- void CRichEditCtrlEx::SetSelectionItalic()
 
- {
 
- 	long start=0, end=0;
 
- 	GetSel(start, end);
 
- 	SetCharStyle(CFM_ITALIC, CFE_ITALIC, start, end);
 
- }
 
- void CRichEditCtrlEx::SetSelectionUnderlined()
 
- {
 
- 	long start=0, end=0;
 
- 	GetSel(start, end);
 
- 	SetCharStyle(CFM_UNDERLINE, CFE_UNDERLINE, start, end);
 
- }
 
- void CRichEditCtrlEx::SetParagraphCenter()
 
- {
 
- 	PARAFORMAT paraFormat;    
 
- 	paraFormat.cbSize = sizeof(PARAFORMAT);
 
- 	paraFormat.dwMask = PFM_ALIGNMENT;    
 
- 	paraFormat.wAlignment = PFA_CENTER;
 
- 	
 
- 	SetParaFormat(paraFormat);	// Set the paragraph.
 
- }
 
- void CRichEditCtrlEx::SetParagraphLeft()
 
- {
 
- 	PARAFORMAT paraFormat;
 
- 	paraFormat.cbSize = sizeof(PARAFORMAT);
 
- 	paraFormat.dwMask = PFM_ALIGNMENT;    
 
- 	paraFormat.wAlignment = PFA_LEFT;
 
- 	
 
- 	SetParaFormat(paraFormat);
 
- }
 
- void CRichEditCtrlEx::SetParagraphRight()
 
- {
 
- 	PARAFORMAT paraFormat;
 
- 	paraFormat.cbSize = sizeof(PARAFORMAT);
 
- 	paraFormat.dwMask = PFM_ALIGNMENT;    
 
- 	paraFormat.wAlignment = PFA_RIGHT;
 
- 	
 
- 	SetParaFormat(paraFormat);
 
- }
 
- bool CRichEditCtrlEx::ParagraphIsCentered()
 
- {
 
- 	PARAFORMAT pf = GetParagraphFormat();
 
- 	if (pf.wAlignment == PFA_CENTER)
 
- 		return true;
 
- 	else
 
- 		return false;
 
- }
 
- bool CRichEditCtrlEx::ParagraphIsLeft()
 
- {
 
- 	PARAFORMAT pf = GetParagraphFormat();
 
- 	if (pf.wAlignment == PFA_LEFT)
 
- 		return true;
 
- 	else
 
- 		return false;
 
- }
 
- bool CRichEditCtrlEx::ParagraphIsRight()
 
- {
 
- 	PARAFORMAT pf = GetParagraphFormat();
 
- 	if (pf.wAlignment == PFA_RIGHT)
 
- 		return true;
 
- 	else
 
- 		return false;
 
- }
 
- PARAFORMAT CRichEditCtrlEx::GetParagraphFormat()
 
- {
 
- 	PARAFORMAT pf;
 
- 	pf.cbSize = sizeof(PARAFORMAT);
 
- 	pf.dwMask = PFM_ALIGNMENT | PFM_NUMBERING;    	
 
- 	GetParaFormat(pf);
 
- 	return pf;
 
- }
 
- void CRichEditCtrlEx::SetParagraphBulleted()
 
- {
 
- 	PARAFORMAT paraformat = GetParagraphFormat();
 
- 	if ( (paraformat.dwMask & PFM_NUMBERING) && (paraformat.wNumbering == PFN_BULLET) )
 
- 	{
 
- 		paraformat.wNumbering = 0;
 
- 		paraformat.dxOffset = 0;
 
- 		paraformat.dxStartIndent = 0;
 
- 		paraformat.dwMask = PFM_NUMBERING | PFM_STARTINDENT | PFM_OFFSET;
 
- 	}
 
- 	else
 
- 	{
 
- 		paraformat.wNumbering = PFN_BULLET;
 
- 		paraformat.dwMask = PFM_NUMBERING;
 
- 		if (paraformat.dxOffset == 0)
 
- 		{
 
- 			paraformat.dxOffset = 4;
 
- 			paraformat.dwMask = PFM_NUMBERING | PFM_STARTINDENT | PFM_OFFSET;
 
- 		}
 
- 	}
 
- 	
 
- 	SetParaFormat(paraformat);
 
- }
 
- bool CRichEditCtrlEx::ParagraphIsBulleted()
 
- {
 
- 	PARAFORMAT pf = GetParagraphFormat();
 
- 	if (pf.wNumbering == PFN_BULLET)
 
- 		return true;
 
- 	else
 
- 		return false;
 
- }
 
- void CRichEditCtrlEx::SelectColor()
 
- {
 
- 	CColorDialog dlg;
 
- 	CHARFORMAT cf = GetCharFormat();
 
- 	if (cf.dwEffects & CFE_AUTOCOLOR) cf.dwEffects -= CFE_AUTOCOLOR;
 
- 	// Get a color from the common color dialog.
 
- 	if( dlg.DoModal() == IDOK )
 
- 	{	
 
- 		cf.crTextColor = dlg.GetColor();
 
- 	}
 
- 	cf.dwMask = CFM_COLOR;
 
- 	SetSelectionCharFormat(cf);
 
- }
 
- void CRichEditCtrlEx::SetFontName(CString sFontName)
 
- {
 
- 	CHARFORMAT cf = GetCharFormat();
 
- 	// Set the font name.
 
- 	for (int i = 0; i <= sFontName.GetLength()-1; i++)
 
- 		cf.szFaceName[i] = (char)sFontName[i];
 
- 	cf.dwMask = CFM_FACE;
 
- 	SetSelectionCharFormat(cf);
 
- }
 
- void CRichEditCtrlEx::SetFontSize(int nPointSize)
 
- {
 
- 	CHARFORMAT cf = GetCharFormat();
 
- 	nPointSize *= 20;	// convert from to twips
 
- 	cf.yHeight = nPointSize;
 
- 	
 
- 	cf.dwMask = CFM_SIZE;
 
- 	SetSelectionCharFormat(cf);
 
- }
 
- void CRichEditCtrlEx::GetSystemFonts(CStringArray &saFontList)
 
- {
 
- 	CDC *pDC = GetDC ();
 
- 	EnumFonts (pDC->GetSafeHdc(),NULL,(FONTENUMPROC) CBEnumFonts,(LPARAM)&saFontList);//Enumerate
 
- }
 
- BOOL CALLBACK CRichEditCtrlEx::CBEnumFonts(LPLOGFONT lplf, LPTEXTMETRIC lptm, DWORD dwType, LPARAM lpData)
 
- {
 
- 	// This function was written with the help of CCustComboBox, by Girish Bharadwaj.
 
- 	// Available from Codeguru.
 
- 	if (dwType == TRUETYPE_FONTTYPE) 
 
- 	{
 
- 		((CStringArray *) lpData)->Add( lplf->lfFaceName );
 
- 	}
 
- 	return true;
 
- }
 
- CString CRichEditCtrlEx::GetSelectionFontName()
 
- {
 
- 	CHARFORMAT cf = GetCharFormat();
 
- 	CString sName = cf.szFaceName;
 
- 	return sName;
 
- }
 
- long CRichEditCtrlEx::GetSelectionFontSize()
 
- {
 
- 	CHARFORMAT cf = GetCharFormat();
 
- 	long nSize = cf.yHeight/20;
 
- 	return nSize;
 
- }
 
- int CRichEditCtrlEx::OnCreate(LPCREATESTRUCT lpCreateStruct) 
 
- {
 
- 	if (CRichEditCtrl::OnCreate(lpCreateStruct) == -1)
 
- 		return -1;
 
- 	
 
- 	// TODO: Add your specialized creation code here
 
- 	
 
- 	return 0;
 
- }
 
- BOOL CRichEditCtrlEx::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
 
- {
 
- #ifdef _UNICODE
 
- 	return CWnd::Create(_T("RichEdit20W"), lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
 
- #else
 
- 	return CWnd::Create(_T("RichEdit20A"), lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
 
- #endif
 
- }
 
 
  |