Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

RichEdit Component

136 views
Skip to first unread message

Graham Powell

unread,
Jun 20, 2005, 1:25:06 PM6/20/05
to
Having loaded an RTF file into a RichEdit component I am now trying to
determine the Font Style of each character. I set the SelStart to the next
character and the SelLength to 1. The Delphi RichEdit seems to not give the
Font Style at all and the TntRichEdit component only gives one of the
attributes. If it is Bold and Italics, it only tells me it is Bold.

Code looks like this

var
t_style : TFontStyles;
t_str : WideString;

begin
RichEdit1.Lines.LoadFromFile('c:\hello.rtf');
t_str := RichEdit1.Lines.Text;

while n <= Length (t_str) do
begin
RichEdit1.SelStart := n;
RichEdit1.SelLength := 1;
t_style := RichEdit.SelAttributes.Style;

if t_style = [fsBold] then
bla bla
else
do something else;

n := n + 1;
end; //while

I'm obviously doing something wrong here, any clues much appreciated.

Graham


Mike Williams (TeamB)

unread,
Jun 20, 2005, 1:40:41 PM6/20/05
to
Graham Powell wrote:

> if t_style = [fsBold] then
> bla bla
> else
> do something else;

That will only tell you if the style is bold and only bold. Since
style is a set you need to test the set for inclusion of a specific
element:

if fsBold in t_style then

--
-Mike (TeamB)

Remy Lebeau (TeamB)

unread,
Jun 20, 2005, 2:25:22 PM6/20/05
to

"Graham Powell" <gra...@deephaven.co.uk> wrote in message
news:42b6...@newsgroups.borland.com...

> t_str := RichEdit1.Lines.Text;
>
> while n <= Length (t_str) do

That is not a very efficient way to loop through the characters. Edit
controls always know how many characters they contain at all times, so you
should use the RichEdit's GetTextLen() method instead, ie:

var
t_style : TFontStyles;
n, len : Integer;


begin
RichEdit1.Lines.LoadFromFile('c:\hello.rtf');

len := RichEdit1.GetTextLen;

For n := 0 to len-1 do


begin
RichEdit1.SelStart := n;
RichEdit1.SelLength := 1;
t_style := RichEdit.SelAttributes.Style;

if fsBold in t_style then
// style includes bold
else
// style does not include bold
end;
end;


Gambit


Graham Powell

unread,
Jun 21, 2005, 4:23:47 AM6/21/05
to
Thanks for that. Also SelStart starts from 0, something I did not spot.

If I need to get the character and the attribute, do I have to revert to my
scheme or is there a neater way to get at each character.

And finally, I understand that these RichEdit controls are wrappers around
the Windows control, so as Windows upgrades, and possibly changes its
RichEdit control, does the Delphi control also update? Or are the Delphi
RichEdit and the TntRichEdit based on a fixed version.

Just so you know what I am trying to achieve:
Basically I have my own editor which works in Unicode. It has a simple RTF
import mechanism which could be better. Using the RichEdit control seems a
good way to decode the RTF file into characters and attributes to import
into my editor.

Regards
Graham

"Remy Lebeau (TeamB)" <no....@no.spam.com> wrote in message
news:42b70bb0$1...@newsgroups.borland.com...

Remy Lebeau (TeamB)

unread,
Jun 21, 2005, 2:05:37 PM6/21/05
to

"Graham Powell" <gra...@deephaven.co.uk> wrote in message
news:42b7ce8e$1...@newsgroups.borland.com...

> If I need to get the character and the attribute, do I have to revert
> to my scheme or is there a neater way to get at each character.

There is no way (that I know of, anyway) to get both the character and its
attributes in a single operation. You have to retreive them separately.

> And finally, I understand that these RichEdit controls are wrappers
> around the Windows control, so as Windows upgrades, and possibly
> changes its RichEdit control, does the Delphi control also update? Or
> are the Delphi RichEdit and the TntRichEdit based on a fixed version.

I cannot comment on TntRichEdit, but TRichEdit always uses v1.0 of the
RichEdit control (Microsoft is up to v4.1 now). To take advantage of newer
versions, you would have to write a custom descendant class, or use a
third-party one, that loads the desired version internally.

> Basically I have my own editor which works in Unicode. It has a simple
> RTF import mechanism which could be better. Using the RichEdit control
> seems a good way to decode the RTF file into characters and attributes to
> import into my editor.

I disagree. It sounds like you are trying to use one visual control to
parse content that is meant for another visual control. You would be better
off writing/finding an actual RTF parser instead. Then you can get the RTF
content directly into your editor without using another visual control to do
the work.


Gambit


Iain Macmillan

unread,
Jun 22, 2005, 11:33:18 AM6/22/05
to

In article <42b856f8$1...@newsgroups.borland.com>, "Remy Lebeau \(TeamB\)"
<no....@no.spam.com> wrote:

>> If I need to get the character and the attribute, do I have to revert
>> to my scheme or is there a neater way to get at each character.
>
> There is no way (that I know of, anyway) to get both the character and its
> attributes in a single operation. You have to retreive them separately.

However you may want to look at ConsistentAttributes.
It may be more efficient to extend the SelLenght until ConsistentAttributes
changes and then deal with a whole block of characters known to have the
same atttibs.
(But there is no other way basically to get attribs, and its slow slow
slow!)

>> And finally, I understand that these RichEdit controls are wrappers
>> around the Windows control, so as Windows upgrades, and possibly
>> changes its RichEdit control, does the Delphi control also update? Or
>> are the Delphi RichEdit and the TntRichEdit based on a fixed version.
>
> I cannot comment on TntRichEdit,

I think its a wrapper for v3.


> but TRichEdit always uses v1.0 of the

.. That's not the whole story, as with the later implementations, as the v1
becomes a stub, which calls into the other DLL and gets it to create a v2
with restricted functionality..

> I disagree. It sounds like you are trying to use one visual control to
> parse content that is meant for another visual control. You would be better
> off writing/finding an actual RTF parser instead.

Writing one of those is no mean task!

Remy Lebeau (TeamB)

unread,
Jun 22, 2005, 5:02:22 PM6/22/05
to

"Iain Macmillan" <he...@ariesps.co.uk> wrote in message
news:42b9...@newsgroups.borland.com...

> Writing one of those is no mean task!

Which is why I also mentioned *finding* a third-party pre-written parser.


Gambit


Graham Powell

unread,
Jun 23, 2005, 10:35:27 AM6/23/05
to
I have done my own parser for RTF files and it basically works to extract
text and simple attributes such as Bold and Underline etc. However life
suddenly gets more complicated when you start talking about Japanese Ruby
characters embedded in the text. With XML and HTML life is a bit more sane.

I realize that would need a RichEdit control that supports V4 to solve this
anyway.

Thanks for all your thoughts though.

Regards
Graham

"Remy Lebeau (TeamB)" <no....@no.spam.com> wrote in message

news:42b9d3da$1...@newsgroups.borland.com...

0 new messages