Click to See Complete Forum and Search --> : Serialization of CRichEditCtrl


June 3rd, 1999, 12:40 PM
Help!! I need to know how I can serialize a CRichEditCtrl!!! Can anyone help me?

David Giovannini
June 3rd, 1999, 04:44 PM
Below is a set of code that I use for file/memory IO. A similar set can be used for views. Hope this is useful.

Since you are using a CRichEditCtrl, maybe you could help me :-) I am trying to embed one in a dialog. The CRichEditCtrl will throw on create if I specify one in the resource editor. I can create one through code but then I loose all formatting in the streamed text. The text's format goes the parent dialog's font setting. How do I set things up correctly so I can properly use the resource specifications and have a CRichEditCtrl that doesn't default to its parent's font settings?

ARichEditControl is derived from CRichEditCtrl
APath is derived from CString
The callbacks are declared as static


void ARichEditControl::ExtractRTFString(CString &outString, int options)
{
this->LockWindowUpdate();
EDITSTREAM es = {(DWORD)&outString, 0, ARichEditControl::OutStringCallBack};
CRichEditCtrl &cntl = *this;
SInt32 start = 0, end = 0;
cntl.GetSel(start, end);
cntl.SetSel(0, cntl.GetTextLength());
options = options | SFF_SELECTION | SF_RTFNOOBJS | SFF_PLAINRTF;
cntl.StreamOut(options, es);
cntl.SetSel(start, end);
this->UnlockWindowUpdate();
}

void ARichEditControl::InsertRTFString(CString &inString)
{
this->LockWindowUpdate();
EDITSTREAM es = {(DWORD)&inString, 0, ARichEditControl::InStringCallBack};
CRichEditCtrl &cntl = *this;
SInt32 start = 0, end = 0;
cntl.GetSel(start, end);
cntl.SetSel(0, cntl.GetTextLength());
cntl.StreamIn(SF_RTF | SFF_PLAINRTF | SFF_SELECTION, es);
cntl.SetSel(start, end);
this->UnlockWindowUpdate();
}

void ARichEditControl::InsertRTFFile(const APath &inPath, CRichEditCtrl &inCntrl)
{
CFile file(inPath, CFile::modeRead);

// this->LockWindowUpdate();
EDITSTREAM es = {(DWORD)&file, 0, ARichEditControl::InFileCallBack};
SInt32 start = 0, end = 0;
inCntrl.GetSel(start, end);
inCntrl.SetSel(0, inCntrl.GetTextLength());
inCntrl.StreamIn(SF_RTF | SFF_PLAINRTF | SFF_SELECTION, es);
inCntrl.SetSel(start, end);
// this->UnlockWindowUpdate();
}

DWORD CALLBACK ARichEditControl::InFileCallBack(DWORD dwCookie, LPBYTE pbBuff, LONG maxCount, LONG *pcb)
{
if (dwCookie)
{
// make this readable
// CString &source = *(CString *)dwCookie;
CFile &file = *(CFile *)dwCookie;
long &actuallCount = *pcb;
// if the string can fit into the buffer
if( file.GetLength() < maxCount )
{
// the bytes moved will be equal to the string's length
// actuallCount = source.GetLength();
actuallCount = file.GetLength();
// move the data into the buffer
// ::memcpy(pbBuff, (LPCSTR)source, actuallCount );
file.Read( pbBuff, actuallCount );
// treat the string like a stream and flush to all that has been read
// source.Empty();
// no, do not do that
}
else // the string is larger than the buffer
{
// we hit a limit at the buffer
actuallCount = maxCount;
// move the stuff over
// ::memcpy(pbBuff, (LPCSTR)source, actuallCount );
file.Read( pbBuff, actuallCount );
// eliminate the left side characters that have neen read
// source = source.Right( source.GetLength() - maxCount );
}
}
return 0;
}

DWORD CALLBACK ARichEditControl::InStringCallBack(DWORD dwCookie, LPBYTE pbBuff, LONG maxCount, LONG *pcb)
{
if (dwCookie)
{
// make this readable
CString &source = *(CString *)dwCookie;
long &actuallCount = *pcb;
// if the string can fit into the buffer
if( source.GetLength() < maxCount )
{
// the bytes moved will be equal to the string's length
actuallCount = source.GetLength();
// move the data into the buffer
::memcpy(pbBuff, (LPCSTR)source, actuallCount );
// treat the string like a stream and flush to all that has been read
source.Empty();
}
else // the string is larger than the buffer
{
// we hit a limit at the buffer
actuallCount = maxCount;
// move the stuff over
::memcpy(pbBuff, (LPCSTR)source, actuallCount );
// eliminate the left side characters that have neen read
source = source.Right( source.GetLength() - maxCount );
}
}
return 0;
}

DWORD CALLBACK ARichEditControl::OutStringCallBack(DWORD dwCookie, LPBYTE pbBuff, LONG maxCount, LONG *pcb)
{
if (dwCookie)
{
CString &destination = *(CString *)dwCookie;
long &actuallCount = *pcb;
actuallCount = destination.GetLength();
// use the constructor to copy in the data
CString source((LPCSTR)pbBuff, maxCount);
destination += source;
actuallCount = destination.GetLength() - actuallCount;
}
}

June 3rd, 1999, 08:08 PM
If you are using CRichEditView, set the CRichEditDoc::m_bRTF flag to FALSE
in your derived document class's constructor.