Prev: Task
Next: compile error note
From: JY on 27 Apr 2010 04:18 Hi, I have a Wizard based application, in which I have some property pages. In one of the pages, I have a CRichEditCtrl, and I try to populate its contents from a RTF file. The porblem is, it works correctly if the RTF file is small (about 4-5 KB), but when I use a larger file, its contents either don't display at all, or gets truncated. The code is shown below. What can I do to show the entire contents of the RTF file in the control? Also, it should work for all languages - I have UNICODE defined in the project. m_RECtrl is the rich edit control. static DWORD CALLBACK MyStreamInCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb) { CFile* pFile = (CFile*)(DWORD_PTR)dwCookie; *pcb = pFile->Read(pbBuff, cb); return 0; } BOOL CREPage::OnInitDialog() { CBasePage::OnInitDialog(); if (m_strRTFFilePath.GetLength()) { m_RECtrl.ShowScrollBar(SB_VERT, TRUE); CFile eulaFile(m_strRTFFilePath, CFile::modeRead); EDITSTREAM es; es.dwCookie = (DWORD_PTR)&eulaFile; es.pfnCallback = (EDITSTREAMCALLBACK)MyStreamInCallback; m_RECtrl.StreamIn(SF_RTF, es); } return TRUE; } int CREPage::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CBasePage::OnCreate(lpCreateStruct) == -1) return -1; CRect rect; GetClientRect(&rect); rect.bottom -= 25; m_RECtrl.Create(WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE, rect, this, IDC_RICHEDIT_EULA); m_RECtrl.SetOptions(ECOOP_OR, ECO_AUTOVSCROLL | ECO_AUTOHSCROLL | ECO_READONLY); m_RECtrl.ModifyStyleEx(0, WS_EX_CLIENTEDGE, SWP_FRAMECHANGED); return 0; } TIA, JY
From: Joseph M. Newcomer on 27 Apr 2010 08:48 See below... On Tue, 27 Apr 2010 01:18:08 -0700, JY <sd(a)nospamgroup.com> wrote: >Hi, > >I have a Wizard based application, in which I have some property pages. In >one of the pages, I have a CRichEditCtrl, and I try to populate its contents >from a RTF file. The porblem is, it works correctly if the RTF file is small >(about 4-5 KB), but when I use a larger file, its contents either don't >display at all, or gets truncated. > >The code is shown below. What can I do to show the entire contents of the >RTF file in the control? Also, it should work for all languages - I have >UNICODE defined in the project. m_RECtrl is the rich edit control. > >static DWORD CALLBACK MyStreamInCallback(DWORD dwCookie, LPBYTE pbBuff, LONG **** Why did you declare the cookie as a DWORD and not a DWORD_PTR? This is incorrect, although in 32-bit Windows it would not have any impact **** >cb, LONG *pcb) >{ > CFile* pFile = (CFile*)(DWORD_PTR)dwCookie; > *pcb = pFile->Read(pbBuff, cb); **** Note that the Read should be contained in a try/catch(CFileException * e) structure if you plan to detect errors correctly **** > > return 0; >} > >BOOL CREPage::OnInitDialog() >{ > CBasePage::OnInitDialog(); > > if (m_strRTFFilePath.GetLength()) > { > m_RECtrl.ShowScrollBar(SB_VERT, TRUE); > CFile eulaFile(m_strRTFFilePath, CFile::modeRead); **** Note that this constructor must be in a try/catch(CFileException * e) block, since if there is a problem (such as the file does not exist along the path) then it will throw an exception **** > EDITSTREAM es; > > es.dwCookie = (DWORD_PTR)&eulaFile; > es.pfnCallback = (EDITSTREAMCALLBACK)MyStreamInCallback; > m_RECtrl.StreamIn(SF_RTF, es); > } > > return TRUE; >} > >int CREPage::OnCreate(LPCREATESTRUCT lpCreateStruct) >{ > if (CBasePage::OnCreate(lpCreateStruct) == -1) > return -1; > > CRect rect; > GetClientRect(&rect); > rect.bottom -= 25; > > m_RECtrl.Create(WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE, rect, >this, IDC_RICHEDIT_EULA); > m_RECtrl.SetOptions(ECOOP_OR, ECO_AUTOVSCROLL | ECO_AUTOHSCROLL | >ECO_READONLY); > m_RECtrl.ModifyStyleEx(0, WS_EX_CLIENTEDGE, SWP_FRAMECHANGED); > > return 0; >} > >TIA, >JY **** Here's the stream callback from my handler: class StreamInCookie { public: CFile * file; DWORD_PTR remaining; DWORD err; }; **** This is retrieving data from a precomputed cache which is in rtf format, and if there is any timestamp change on an input, it resets all the content and will recompute it from scratch, ignoring the cache. The cache starts with a binary timestamp and a binary length value. It also marks the hyperlinks. If you want to see the hyperlink code, I've attached it ***** BOOL CIndex::RetrieveFromCache() { CString filename; if(!GetCacheFile(filename)) return FALSE; CFile f; if(!f.Open(filename, CFile::modeRead)) return FALSE; // open failed try { /* try read */ StreamInCookie streamIn; // streamIn.file = &f; //**************************************************************** // [Timestamp] //**************************************************************** TimeStamp ft; if(f.Read(&ft, sizeof(TimeStamp)) == 0) { /* failed */ ResetAllContent(); f.Close(); return FALSE; } /* failed */ //**************************************************************** // [eod] End of data position for RTF data //**************************************************************** if(f.Read(&streamIn.remaining, sizeof(DWORD)) == 0) { /* failed */ ResetAllContent(); f.Close(); return FALSE; } /* failed */ // Now convert from offset to length streamIn.remaining -= f.GetPosition(); //**************************************************************** // <...> RTF data //**************************************************************** EDITSTREAM es; es.dwCookie = (DWORD_PTR)&streamIn; es.pfnCallback = StreamInCallback; c_Index.StreamIn(SF_RTF, es); if(streamIn.err != ERROR_SUCCESS) { /* failed */ f.Close(); ResetAllContent(); return FALSE; } /* failed */ //**************************************************************** // Read the hyperlink length //**************************************************************** DWORD len; if(f.Read(&len, sizeof(DWORD)) == 0) { /* failed to get links */ f.Close(); ResetAllContent(); return FALSE; } /* failed to get links */ hyperlinks.SetSize(len); //**************************************************************** // Read the hyperlink data //**************************************************************** for(DWORD i = 0; i < len; i++) { /* read each */ hyperlinks[i] = new Reference; if(!hyperlinks[i]->Read(f)) { /* failed */ if(::GetLastError() == ERROR_HANDLE_EOF) break; // "failure" is EOF (see spec on Reference::Read) f.Close(); ResetAllContent(); return FALSE; } /* failed */ MarkLink(c_Index, hyperlinks[i]->range); } /* read each */ //**************************************************************** // [eod] Read EOD for Fastlink RTF data //**************************************************************** if(f.Read(&streamIn.remaining, sizeof(DWORD)) == 0) { /* failed */ ResetAllContent(); f.Close(); return FALSE; } /* failed */ streamIn.remaining -= f.GetPosition(); // convert from offset to length //**************************************************************** // <...> RTF data //**************************************************************** c_FastIndex.StreamIn(SF_RTF, es); if(streamIn.err != ERROR_SUCCESS) { /* failed */ ResetAllContent(); f.Close(); return FALSE; } /* failed */ //**************************************************************** // Read the hyperlink length //**************************************************************** if(f.Read(&len, sizeof(DWORD)) == 0) { /* failed to get links */ ResetAllContent(); f.Close(); return FALSE; } /* failed to get links */ fastlinks.SetSize(len); //**************************************************************** // Read the hyperlink data //**************************************************************** for(i = 0; i < len; i++) { /* read each */ if(!fastlinks[i].Read(f)) { /* failed */ if(::GetLastError() == ERROR_HANDLE_EOF) break; f.Close(); ResetAllContent(); return FALSE; } /* failed */ MarkLink(c_FastIndex, fastlinks[i].range); } /* read each */ //**************************************************************** } /* try read */ catch(CFileException * e) { /* read error */ e->Delete(); ResetAllContent(); f.Close(); return FALSE; } /* read error */ f.Close(); c_Index.SetSel(0,0); c_FastIndex.SetSel(0,0); return TRUE; } // CIndex::RetrieveFromCache void CIndex::MarkLink(CRichEditCtrlEx & ctl, CHARRANGE & range) { CHARFORMAT2 linkfmt; ctl.SetSel(range); linkfmt.cbSize = sizeof(CHARFORMAT2); linkfmt.dwMask = CFM_LINK; linkfmt.dwEffects = CFE_LINK; linkfmt.dwMask = CFM_LINK; ctl.SetSelectionCharFormat(linkfmt); } // CIndex::MarkLink /* static */ DWORD CALLBACK CIndex::StreamInCallback(DWORD_PTR cookie, LPBYTE buffer, LONG count, LONG * pcb) { StreamInCookie * streamIn = (StreamInCookie *)cookie; if(streamIn->remaining == 0) { /* all done */ *pcb = 0; streamIn->err = ERROR_SUCCESS; return 1; // nonzero value terminates read } /* all done */ DWORD bytesToRead = min(streamIn->remaining, (DWORD)count); UINT bytesRead; try { bytesRead = streamIn->file->Read(buffer, bytesToRead); } catch(CFileException * e) { /* catch */ streamIn->err = e->m_lOsError; e->Delete(); streamIn->remaining = 0; return 1; } /* catch */ if(bytesRead == 0) { /* read error */ streamIn->err = ::GetLastError(); *pcb = 0; return 1; // return nonzero to stop operation } /* read error */ streamIn->remaining -= bytesRead; *pcb = bytesRead; streamIn->err = ERROR_SUCCESS; return 0; } // CIndex::StreamInCallback Joseph M. Newcomer [MVP] email: newcomer(a)flounder.com Web: http://www.flounder.com MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Goran on 27 Apr 2010 09:54 On Apr 27, 10:18 am, JY <s...(a)nospamgroup.com> wrote: > Hi, > > I have a Wizard based application, in which I have some property pages. In > one of the pages, I have a CRichEditCtrl, and I try to populate its contents > from a RTF file. The porblem is, it works correctly if the RTF file is small > (about 4-5 KB), but when I use a larger file, its contents either don't > display at all, or gets truncated. > > The code is shown below. What can I do to show the entire contents of the > RTF file in the control? Also, it should work for all languages - I have > UNICODE defined in the project. m_RECtrl is the rich edit control. void CMyDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); DDX_Control(pDX, IDC_RICHEDIT21, m_Edit); } static DWORD CALLBACK MyStreamInCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb) { CFile* pFile = (CFile*)(DWORD_PTR)dwCookie; *pcb = pFile->Read(pbBuff, cb); return 0; } BOOL CMyDlg::OnInitDialog() { CDialog::OnInitDialog(); m_Edit.SetOptions(ECOOP_OR, ECO_AUTOVSCROLL | ECO_AUTOHSCROLL | ECO_READONLY); m_Edit.ModifyStyleEx(0, WS_EX_CLIENTEDGE, SWP_FRAMECHANGED); m_Edit.ShowScrollBar(SB_VERT, TRUE); CFile f(RTF_PATH_HERE, CFile::modeRead); EDITSTREAM es; es.dwCookie = (DWORD_PTR)&f; es.pfnCallback = (EDITSTREAMCALLBACK)MyStreamInCallback; long l = m_Edit.StreamIn(SF_RTF, es); return TRUE; // return TRUE unless you set the focus to a control }
From: Goran on 27 Apr 2010 10:12 Whoops! That went out sooner than I wanted... The above worked for me, regardless of the file size, using unicode in project settings, and using rtf in two rather distinctive languages. I don't know why you use OnCreate. It should work, your code, but... DDX_Control in DoDataExchange is easier. Other than that, note a canonical form for any non-trivial OnInitDialog is e.g: bool CXDialog::OnInitDialog() { CDialog::OnInitDialog(); TRY { InitBabyInit(); } CATCH(CException* pe) { ReportErrorSomehow(pe); EndDialog(PROBABLY_ID_CANCEL); // But it's up to you. } } It's a bad idea to let exception escape your callback (possible due to use of MFC in it), so you should e.g. do: struct RichEditStreamInContext { RichEditStreamInContext(const CString& csFileName) : m_File(csFileName, CFile::modeRead), m_pError(NULL) {} CFile m_File; CException* m_pError; }; static DWORD CALLBACK MyStreamInCallback(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb) { RichEditStreamInContext& Ctx = *reinterpret_cast<RichEditStreamInContext*>(dwCookie); try { *pcb = Ctx.m_File.Read(pbBuff, cb); return 0; } catch (CException* p) { ASSERT(FALSE); Ctx.m_pError = p; return -1; } } and in OnInitDialog: .... RichEditStreamInContext Ctx(RTF_PATH_HERE); EDITSTREAM es = { (DWORD_PTR)&Ctx, 0, &MyStreamInCallback }; MCALLBACK)MyStreamInCallback; m_Edit.StreamIn(SF_RTF, es); if (es.dwError) HandleError(Ctx); // Including looking at Ctx.m_pError; HTH, Goran.
From: Hector Santos on 27 Apr 2010 10:20
The problem with the particular code you have is that it doesn't support reading a RTF file saved as UNI-GARBAGE A.K.A UNICODE. It appears you need to convert each block read in into multi-byte array. It works find when the RTF file is not stored in unicode. JY wrote: > Hi, > > I have a Wizard based application, in which I have some property pages. In > one of the pages, I have a CRichEditCtrl, and I try to populate its contents > from a RTF file. The porblem is, it works correctly if the RTF file is small > (about 4-5 KB), but when I use a larger file, its contents either don't > display at all, or gets truncated. > > The code is shown below. What can I do to show the entire contents of the > RTF file in the control? Also, it should work for all languages - I have > UNICODE defined in the project. m_RECtrl is the rich edit control. > > static DWORD CALLBACK MyStreamInCallback(DWORD dwCookie, LPBYTE pbBuff, LONG > cb, LONG *pcb) > { > CFile* pFile = (CFile*)(DWORD_PTR)dwCookie; > *pcb = pFile->Read(pbBuff, cb); > > return 0; > } > > BOOL CREPage::OnInitDialog() > { > CBasePage::OnInitDialog(); > > if (m_strRTFFilePath.GetLength()) > { > m_RECtrl.ShowScrollBar(SB_VERT, TRUE); > CFile eulaFile(m_strRTFFilePath, CFile::modeRead); > EDITSTREAM es; > > es.dwCookie = (DWORD_PTR)&eulaFile; > es.pfnCallback = (EDITSTREAMCALLBACK)MyStreamInCallback; > m_RECtrl.StreamIn(SF_RTF, es); > } > > return TRUE; > } > > int CREPage::OnCreate(LPCREATESTRUCT lpCreateStruct) > { > if (CBasePage::OnCreate(lpCreateStruct) == -1) > return -1; > > CRect rect; > GetClientRect(&rect); > rect.bottom -= 25; > > m_RECtrl.Create(WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE, rect, > this, IDC_RICHEDIT_EULA); > m_RECtrl.SetOptions(ECOOP_OR, ECO_AUTOVSCROLL | ECO_AUTOHSCROLL | > ECO_READONLY); > m_RECtrl.ModifyStyleEx(0, WS_EX_CLIENTEDGE, SWP_FRAMECHANGED); > > return 0; > } > > TIA, > JY -- HLS |