Recent

Author Topic: A minor issue with the TRichMemo operation.  (Read 3805 times)

jianwt

  • Full Member
  • ***
  • Posts: 164
A minor issue with the TRichMemo operation.
« on: July 15, 2025, 04:36:26 am »
I want to manipulate text strings in RichMemo1 and have two small questions to ask everyone:

When richmemo1.scrollbars =ssBoth, how can the total number of paragraphs in RichMemo1 be obtained instead of richmemo1.lines.count? In the following routine, there are 4 paragraphs instead of 6.

2. There are 4 paragraphs in the routine. How can the text of odd-numbered paragraphs be displayed in red and that of even-numbered paragraphs in green?

I have no idea how to handle the above-mentioned issues.

Thank you all for your help.

rvk

  • Hero Member
  • *****
  • Posts: 7045
Re: A minor issue with the TRichMemo operation.
« Reply #1 on: July 15, 2025, 09:24:30 am »
1) For the number of paragraphs...
You can put the content of the rtf in a string, which eliminates the linewrap, and then count the linebreaks.
Code: Pascal  [Select][+][-]
  1. var
  2.   ParaCount: Integer;
  3.   TextContent: string;
  4. begin
  5.   TextContent := RichMemo1.Text;
  6.   ParaCount := 0;
  7.   if TextContent <> '' then
  8.     ParaCount := Length(SplitString(TextContent, sLineBreak));
Keep in mind that this can count the last linebreak as a paragraph.
Also... if you have blanc lines you would need to change sLineBreak + sLineBreak to a single sLineBreak first.

2) For coloring the odd numbered paragraphs you would really need to parse through the code and apply coloring to the paragraph yourself. There is no standard way of doing this in RichMemo/RichEdit.

paule32

  • Hero Member
  • *****
  • Posts: 646
  • One in all. But, not all in one.
Re: A minor issue with the TRichMemo operation.
« Reply #2 on: July 15, 2025, 10:13:48 am »
isn't TRICHMemo not bound to win32API ?

there could be give a API Function for it ...
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

Thaddy

  • Hero Member
  • *****
  • Posts: 19268
  • Glad to be alive.
objects are fine constructs. You can even initialize them with constructors.

jianwt

  • Full Member
  • ***
  • Posts: 164
Re: A minor issue with the TRichMemo operation.
« Reply #4 on: July 15, 2025, 10:39:48 am »
@rvk
Thank you for your answer. However, it seems that the code does not take the total number of paragraphs and returns 1

Code: Pascal  [Select][+][-]
  1. var
  2.   ParaCount: Integer;
  3.   TextContent: string;
  4. begin
  5.   TextContent := RichMemo1.Text;
  6.   ParaCount := 0;
  7.   if TextContent <> '' then
  8.     ParaCount := Length(SplitString(TextContent, sLineBreak));
  9.  

rvk

  • Hero Member
  • *****
  • Posts: 7045
Re: A minor issue with the TRichMemo operation.
« Reply #5 on: July 15, 2025, 11:02:26 am »
Thank you for your answer. However, it seems that the code does not take the total number of paragraphs and returns 1
On what platform?
Maybe change the sLineBreak in #13.
Could be that on Windows RichEd20 does #13#10 as linebreak while Linux only does #13.
In both cases will counting just the #13 work.

Code: Pascal  [Select][+][-]
  1. ParaCount := Length(SplitString(TextContent, #13));

CM630

  • Hero Member
  • *****
  • Posts: 1699
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: A minor issue with the TRichMemo operation.
« Reply #6 on: July 15, 2025, 11:12:33 am »
Yes, it is only #13 for tRichMemo even on Windows.
But also ParaCount := Length(SplitString(TextContent, #13)) -1; because TRichMemo also has a parasite end line.
Лазар 4,4 32 bit (sometimes 64 bit); FPC3,2,2

rvk

  • Hero Member
  • *****
  • Posts: 7045
Re: A minor issue with the TRichMemo operation.
« Reply #7 on: July 15, 2025, 11:19:18 am »
Yes, it is only #13 for tRichMemo even on Windows.
But also ParaCount := Length(SplitString(TextContent, #13)) -1; because TRichMemo also has a parasite end line.
Grr  :-\ Yes. SplitString splits on ALL delimiters/characters. The parameter is not a splitstring. So passing sLineBreak (#13#10) will split on both #13#10. I missed that  :) (that's why it worked here when I tested it)

CM630

  • Hero Member
  • *****
  • Posts: 1699
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: A minor issue with the TRichMemo operation.
« Reply #8 on: July 15, 2025, 11:42:12 am »
As for colouring, I use:

Code: Pascal  [Select][+][-]
  1. procedure AppendMemo(RichMemo: TRichMemo; aText: String; Colour: TColor = clDefault);
  2. var
  3.   prms : TFontParams;
  4. begin
  5.   prms := GetFontParams(RichMemo.Font);
  6.   if (Colour <> clDefault) then prms.Color := Colour;
  7.   InsertFontText(RichMemo,aText, prms);
  8.   //Next line is optional
  9.   //SendMessage(RichMemo.handle, WM_VSCROLL, SB_BOTTOM, 0);
  10. end;


EDIT: Actually, this seems to return the number of the paragraphs. I wonder why no -1 is required.
Code: Pascal  [Select][+][-]
  1. ParaCount := string(RichMemo1.Text).CountChar(#13);
« Last Edit: July 15, 2025, 12:33:00 pm by CM630 »
Лазар 4,4 32 bit (sometimes 64 bit); FPC3,2,2

jianwt

  • Full Member
  • ***
  • Posts: 164
Re: A minor issue with the TRichMemo operation.
« Reply #9 on: July 15, 2025, 03:22:39 pm »
Thank you to all the friends above for your help. I have basically achieved my requirements. Thank you again

paule32

  • Hero Member
  • *****
  • Posts: 646
  • One in all. But, not all in one.
Re: A minor issue with the TRichMemo operation.
« Reply #10 on: July 15, 2025, 03:43:53 pm »
on Windows the Line-End-Mark is #13#10
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

rvk

  • Hero Member
  • *****
  • Posts: 7045
Re: A minor issue with the TRichMemo operation.
« Reply #11 on: July 15, 2025, 04:25:22 pm »
on Windows the Line-End-Mark is #13#10
Not for riched20 and thus TRichMemo on Windows.
That's because the .Text property gets it's string from TRichEditManager.GetTextW().
In Windows this returns default #13, not #13#10.

I have looked into this problem and found the cause.
Although the TRichMemo.Lines is a TStrings it gets its .Text property from another class.
It uses TRichEditManager.GetTextW() to retrieve the complete .Text.

There a EM_GETTEXTEX is issued to get the text from the RichEdit (Windows) component.
There is a flag setting possible for getting the text with #13#10 instead of the default #13 (GTL_USECRLF).
But it isn't used (and that's the reason .Text has #13 instead of #13#10).

I find this very confusing and is definitely not like it is in the TRichEdit in Delphi.

If TStringList has only #13 on Linux systems (I don't know) then this could also be for TRichMemo.Lines.
But the TRichMemo.Lines should give #13#10 on Windows systems (and be the same as TStringList and Delphi).

As a test I edited TRichEditManager.GetTextW to include GTL_USECRLF (and that works) but I see that it must be implemented in a lot of other places in win32richmemoproc.pas too (everywhere where the TGETTEXTEX.flag is used).

I not sure why TRichMemo.Text doesn't just use TStrings.GetTextStr like Delphi does.
https://forum.lazarus.freepascal.org/index.php/topic,60956.msg457944.html#msg457944

paule32

  • Hero Member
  • *****
  • Posts: 646
  • One in all. But, not all in one.
Re: A minor issue with the TRichMemo operation.
« Reply #12 on: July 15, 2025, 05:08:12 pm »
is the Text a PChar ?

Because, W-Functions: #13#00#10#00

So, PChar is null-terminated ... ?
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

Thaddy

  • Hero Member
  • *****
  • Posts: 19268
  • Glad to be alive.
Re: A minor issue with the TRichMemo operation.
« Reply #13 on: July 15, 2025, 05:10:19 pm »
Pascal strings are also #0 terminated, because of interoperability with PChars.
(Except shortstrings)
objects are fine constructs. You can even initialize them with constructors.

paule32

  • Hero Member
  • *****
  • Posts: 646
  • One in all. But, not all in one.
Re: A minor issue with the TRichMemo operation.
« Reply #14 on: July 15, 2025, 08:50:14 pm »
what are the relevant Part of ShortStrings ?
PChar is relevant if you use Library Calls that was build with a C/C++ Compiler where the Strings can have more Characters than the 255 Limit - and terminated by #0.

So, if you use {$mode FPCOBJ}
you have to explicit add {$H+} for Huge String's...

In {$mode DELPHI}
you don't need to explicit add {$H+} which is by default.

And when H+ Option is on, FPC use AnsiString instead, which are RefCount'ed.

For me I get the Opinion, that PChar and AnsiString are nearly the same (if we don't Focus the Encoding) ?
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

 

TinyPortal © 2005-2018