Re: RichEdit syntax hilighting??
- From: Randy Widell <NOSPAM_BornAgainSlakr@xxxxxxxxxxxxxxxxxx>
- Date: Mon, 06 Jun 2005 23:00:54 -0400
Well, I wrote a small test program and used a ~1MB XML (a good 25000 lines of text) document as the input. It took about 71.9 seconds to search through the doc and highlight each XML tag blue.
Now, it originally exhibited the behavior you are seeing and took exponentially longer with the length of the file. I used the EM_SETUNDOLIMIT message to set the undo limit to zero. That sped it up quite a bit.
Here's the code:
//// CODE FRAGMENT ////
CFileDialog dlg(
TRUE,
NULL,
NULL,
0,
_T("All Files (*.*)|*.*||"),
this);
if(dlg.DoModal()!=IDCANCEL){
CFile file;
if(file.Open(dlg.GetPathName(), CFile::modeRead)){
file.SeekToBegin(); EDITSTREAM es={0};
es.dwCookie=reinterpret_cast<DWORD>(&file);
es.pfnCallback=StreamInCallback;m_Edit.SendMessage( EM_STREAMIN, SF_TEXT, reinterpret_cast<LPARAM>(&es));
file.Close(); } }
int nTabSize=8;
m_Edit.SendMessage( EM_SETTABSTOPS, 1, reinterpret_cast<LPARAM>(&nTabSize));
LockWindowUpdate();
FINDTEXTEX find; find.chrg.cpMin=0; find.chrg.cpMax=-1;
m_Edit.SendMessage( EM_SETUNDOLIMIT, 0, 0);
clock_t s=clock(), e=0;
for( ; ; ){
CHARRANGE fmt={0}; find.lpstrText=_T("<");int nRet=m_Edit.SendMessage( EM_FINDTEXTEX, FR_DOWN, reinterpret_cast<LPARAM>(&find));
if(nRet==-1) break;
fmt.cpMin=find.chrgText.cpMin;
find.chrg.cpMin=find.chrgText.cpMax;
find.lpstrText=_T(">");nRet=m_Edit.SendMessage( EM_FINDTEXTEX, FR_DOWN, reinterpret_cast<LPARAM>(&find));
if(nRet==-1) break;
fmt.cpMax=find.chrgText.cpMax; find.chrg.cpMin=find.chrgText.cpMax; find.chrg.cpMax=-1;
m_Edit.SendMessage( EM_SETSEL, fmt.cpMin, fmt.cpMax);
CHARFORMAT c={0};
c.cbSize=sizeof(c);
c.dwMask=CFM_COLOR;
c.crTextColor=RGB(0, 0, 255);m_Edit.SendMessage( EM_SETCHARFORMAT, SCF_SELECTION, reinterpret_cast<LPARAM>(&c)); }
e=clock();
TRACE(_T(">> %.1fs\n"), double(e-s)/CLOCKS_PER_SEC);UnlockWindowUpdate();
//// END FRAGMENT ////
bartt wrote:
Thanks for the reply Randy-
I am trying to highlight the entire contents of the control. But it seems to take geometrically longer the larger the file gets( eg. <1sec for about a hundred chars, ~10 sec for 800 chars, and >5min for 32k chars). It took so long for the last test that I thought it had broken.
I am trying to highlight XML, to differentiate the values from the tags.
So I am just walking the contents looking for the < and > chars, and changing the font color on the value not the tags. To do this I have to set the selected text for each occurance and change the font color.
My loop is pretty tight and I have examined it in detail to make sure that there isn't something obviously wrong.
Aside from only hightlighting the displayed portion of the control, is there another way to do this that might be faster.? Is using the selected text an expensive way to approach this? Perhaps I could try a different technique besides a color change?
Any ideas are appreciated.
Randy Widell wrote:
bartt wrote:
I am using a control that is derived from RichEdit to display some file contents and allow edits. So far so good.
I added some code to do Syntax Hilighting and found that it is terribly slow.
I am running XP pro with the latest SP's..
There is a riched20.dll (8/4/2004) file in my system32 dir which I am assuming is where the control code comes from.
Can anyone point me to a newer dll or some example code on how to make this fly..?
TIA..
Are you sure it is the RichEditCtrl and not the highlighting algorithm or whatever you are using to find tokens?
I wrote a class derived from CRichEditCtrl to syntax highlight mathematical expressions. One thing I thought of doing, but decided not to since my expected input is usually < 256 chars, is localizing the highlighting updates. You can try taking advantage of assumptions that can be made about your input to determine the smallest amount of text that needs to be updated around the cursor position when the user edits the text.
Also, if you have a lot of text, you should only update the visible text, only text that is revealed by scrolling, etc.
Oh, also try putting LockWindowUpdate() and UnlockWindowUpdate() around your highlighting code to keep the control from updating at the same time you are setting text styles.
.
- References:
- RichEdit syntax hilighting??
- From: bartt
- Re: RichEdit syntax hilighting??
- From: Randy Widell
- Re: RichEdit syntax hilighting??
- From: bartt
- RichEdit syntax hilighting??
- Prev by Date: Re: Ownerdraw tabitem no longer works when run from 64-bit Windows
- Next by Date: What's the standard cursor for DROPEFFECT_COPY?
- Previous by thread: Re: RichEdit syntax hilighting??
- Next by thread: Large graphic area change issue
- Index(es):
Relevant Pages
|