IonicWind Software

IWBasic => General Questions => Topic started by: Andy on March 25, 2020, 06:04:06 AM

Title: Speeding up rich edit - but why
Post by: Andy on March 25, 2020, 06:04:06 AM
Well,

I've spent a long time on rich edits now, and I had a sneaking suspicion about them.

When I colour 2,000 lines of code, it takes my program around 0.75 seconds.

When I made the code 4,000 lines (same code added to the end) I would have expected it would be take double the time i.e. 1.5 seconds - but no, it was around 2.25 seconds.

So I had a sneaking suspicion that the colouring of a rich edit control had to be "interrupted" every so often to speed it up.

I tested this by setting the focus to another control every X number of lines, like this:


    for a = 0 to MaxLines - 1
        CONTROLCMD(w1,1,@RTGETLINE,a,line$,255)
        WordPerLine2(w1,1,a+1,Line$)
        lc = ++
        If lc = 25
           lc = 0
           setfocus w1,STATIC_4
        endif
     next a

Using this "interrupt", I reduced the time taken for 4,085 lines of code from 2.25 to 1.80 saving nearly half a second.

But why?

Anyone got any answers please?

Thanks,
Andy.
 :)
Title: Re: Speeding up rich edit - but why
Post by: Brian on March 25, 2020, 06:14:05 AM
Andy,

Why don't you try Fletchie's ConDrawOff and ConDrawOn? Might make a difference

Brian
Title: Re: Speeding up rich edit - but why
Post by: h3kt0r on March 25, 2020, 06:33:08 AM
Brian is right, by turning off the control WM_PAINT message, you speed things up a bit.
I suspect that whenever the control does not have the focus, the WM_PAINT message is
probably processed fewer times, so this is the reason why you get to speed things up roughly
by half a sec.
Something you might want to use for increased speed : turning off the event mask.
Instead of using Fletchie's ConDrawOff command, you can use WM_SETREDRAW.
A snippet (Rich Edit has control ID 1 = hEdit) :
': Disable the event mask, for increased speed
 mask = CONTROLCMD (win, 1, @RTGETEVENTMASK)
 controlcmd win, 1, @rtseteventmask, 0

 SendMessage(hEdit, WM_SETREDRAW, 0, 0)
 FormatCode(0,-1) :' Call the routine for syntax highlighting
 SendMessage(hEdit, WM_SETREDRAW, 1, 0)

 ': Restore the event mask
 controlcmd win, 1, @rtseteventmask, mask
Title: Re: Speeding up rich edit - but why
Post by: Andy on March 25, 2020, 08:13:40 AM
Thanks guys,

Will have a look!

Andy.
 :)
Title: Re: Speeding up rich edit - but why
Post by: Andy on March 26, 2020, 02:09:22 AM
Tried Brian's suggestion,

That didn't make a difference here.

Hecktor's suggestion is correct.

I've been using this for the past 2 / 3 weeks.

Setting the event mask to zero and WM_SETREDRAW really speeds things up.

My time to colour 4,085 lines this morning is down to 1.75 seconds.
Without turning the above off it takes 4.9 seconds - so well worth doing.

Another thing I have noticed:

I changed the @RTSETSELSELECTION to Win32 API command EM_SETSEL - this too made things quicker.

SendMessage(rHandle,EM_SETSEL,(#i[GetLine].StartPos[c1]+Offset),(#i[GetLine].EndPos[c1]+Offset))
'CONTROLCMD WinIn,ControlIn,@RTSETSELECTION,(#i[GetLine].StartPos[c1]+Offset),(#i[GetLine].EndPos[c1]+Offset)
CONTROLCMD WinIn,ControlIn,@RTSETSELCOLOR,#h[GetLine].Colour[c1]

So I'm now thinking about other Win32 API commands instead of @RTSETSELCOLOR etc - but not sure which messages to send?

Andy.
Title: Re: Speeding up rich edit - but why
Post by: h3kt0r on March 26, 2020, 06:18:19 AM
From the Win32 Programmer's Reference (http://idquodnescis.we.bs/public/Win32.chm) :
QuoteText Formatting

An application can send messages to a rich edit control to format characters and paragraphs and to retrieve formatting information. Paragraph formatting attributes include alignment, tabs, indents, and numbering. For characters, you can specify typeface, size, color, and effects such as bold, italic, and protected.

You can apply paragraph formatting by using the EM_SETPARAFORMAT message. To determine the current paragraph formatting for the selected text, use the EM_GETPARAFORMAT message. The PARAFORMAT structure is used with both messages to specify paragraph formatting attributes.

You can apply character formatting by using the EM_SETCHARFORMAT message. To determine the current character formatting for the selected text, you can use the EM_GETCHARFORMAT message. The CHARFORMAT structure is used with both messages to specify character attributes.

You can also use EM_SETCHARFORMAT and EM_GETCHARFORMAT messages to set and retrieve the default character formatting, which is the formatting applied to any subsequently inserted characters. For example, if an application sets the default character formatting to bold and the user then types a character, that character is bold.

The default character formatting is applied to newly inserted text only if the current selection is empty. Otherwise, the new text assumes the character formatting of the text it replaces. If the selection changes, the default character formatting changes to match the first character in the new selection.

Title: Re: Speeding up rich edit - but why
Post by: Andy on March 26, 2020, 10:27:02 AM
Thanks Hecktor,

That's useful to know.

Got EM_SETCHARFORMAT working, made a little difference to the timing.

Thanks again,
Andy.