Lazarus

Programming => Packages and Libraries => RichMemo => Topic started by: mdalacu on September 14, 2011, 12:42:47 pm

Title: TRichMemo - add line by line
Post by: mdalacu on September 14, 2011, 12:42:47 pm
Hi, i need a simple example or a hint regarding adding lines in a TRichMemo with different styles or colors.
I have tried everyting..it is such a simple task and i can't do it.. :'(
I need something like this (pseudocode):
 RichMemo.Lines.Clear;
 RichMemo1.SetTextAttributes(style1);
 RichMemo.Lines.('Sample text 1');
 RichMemo1.SetTextAttributes(style2);
 RichMemo.Lines.('Sample text 2');
 .. and so on...

Thank you!

Title: Re: TRichMemo - add line by line
Post by: Arbee on September 14, 2011, 01:44:06 pm
I have not used Richmemo very often, but this (in my eyes terribly dirty) coding works to get lines in different colors:

Code: [Select]
  with Richmemo1 do begin
     clear;
     SetRangeColor(0,1000,clRed);
     Append('Line in red');
     SelStart := 999999;
     SetRangeColor(SelStart,1000,clBlue);
     Append('Line in blue');
     SelStart := 999999;
     SetRangeColor(SelStart,1000,clGreen);
     Append('Line in green');
   end;
Title: Re: TRichMemo - add line by line with different colors
Post by: mdalacu on September 14, 2011, 03:45:13 pm
Thank you for your replay, it works. :D
 I have updated the example a bit, it's a little bit faster:
Code: [Select]
    with RichMemo1 do
      begin
        Clear;
        Lines.Add('Line in red');
        SetRangeColor(Length(RichMemo1.Lines.Text)-Length(RichMemo1.Lines[RichMemo1.Lines.Count-1])-RichMemo1.Lines.Count-1,Length(RichMemo1.Lines[RichMemo1.Lines.Count-1]),clRed);

        Lines.Add('Line in black ');
        SetRangeColor(Length(RichMemo1.Lines.Text)-Length(RichMemo1.Lines[RichMemo1.Lines.Count-1])-RichMemo1.Lines.Count-1,Length(RichMemo1.Lines[RichMemo1.Lines.Count-1]),clBlack);

        Lines.Add('Line in blue ');
        SetRangeColor(Length(RichMemo1.Lines.Text)-Length(RichMemo1.Lines[RichMemo1.Lines.Count-1])-RichMemo1.Lines.Count-1,Length(RichMemo1.Lines[RichMemo1.Lines.Count-1]),clBlue);

        Lines.Add('Line in green ');
        SetRangeColor(Length(RichMemo1.Lines.Text)-Length(RichMemo1.Lines[RichMemo1.Lines.Count-1])-RichMemo1.Lines.Count-1,Length(RichMemo1.Lines[RichMemo1.Lines.Count-1]),clGreen);

        Lines.Add('Line in yellow ');
        SetRangeColor(Length(RichMemo1.Lines.Text)-Length(RichMemo1.Lines[RichMemo1.Lines.Count-1])-RichMemo1.Lines.Count-1,Length(RichMemo1.Lines[RichMemo1.Lines.Count-1]),clYellow);

        Lines.Add('Normal line.');
        Lines.Add('Normal line.');
        Lines.Add('Line in gray');
        SetRangeColor(Length(RichMemo1.Lines.Text)-Length(RichMemo1.Lines[RichMemo1.Lines.Count-1])-RichMemo1.Lines.Count-1,Length(RichMemo1.Lines[RichMemo1.Lines.Count-1]),clGray);
      end;
Title: Re: TRichMemo - add line by line
Post by: User137 on September 15, 2011, 11:14:17 am
Just a sidenote, you have the whole code in "with RichMemo1 do", so you don't need to call RichMemo1 inside it  :)

If you only need to color 10 or less lines with the code its fast enough, but overuse of length() should be avoided. You can use variables to save last index position.
Title: Re: TRichMemo - add line by line
Post by: mdalacu on June 19, 2012, 12:12:17 pm
you are right, here is the updated example:
Code: [Select]
with RichMemo1 do
      begin
        Clear;
        Lines.Add('Line in red');
        SetRangeColor(Length(Lines.Text)-Length(Lines[Lines.Count-1])-Lines.Count-1,Length(Lines[Lines.Count-1]),clRed);

        Lines.Add('Line in black ');
        SetRangeColor(Length(Lines.Text)-Length(Lines[Lines.Count-1])-Lines.Count-1,Length(Lines[Lines.Count-1]),clBlack);

        Lines.Add('Line in blue ');
        SetRangeColor(Length(Lines.Text)-Length(Lines[Lines.Count-1])-Lines.Count-1,Length(Lines[Lines.Count-1]),clBlue);

        Lines.Add('Line in green ');
        SetRangeColor(Length(Lines.Text)-Length(Lines[Lines.Count-1])-Lines.Count-1,Length(Lines[Lines.Count-1]),clGreen);

        Lines.Add('Line in yellow ');
        SetRangeColor(Length(Lines.Text)-Length(Lines[Lines.Count-1])-Lines.Count-1,Length(Lines[Lines.Count-1]),clYellow);

        Lines.Add('Normal line.');
        Lines.Add('Normal line.');
        Lines.Add('Line in gray');
        SetRangeColor(Length(Lines.Text)-Length(Lines[Lines.Count-1])-Lines.Count-1,Length(Lines[Lines.Count-1]),clGray);
      end;             

For Linux you have to modify the line like this:
Code: [Select]
SetRangeColor(Length(Lines.Text)-Length(Lines[Lines.Count-1])-1,Length(Lines[Lines.Count-1]),clYellow);And also for the component to compile under Ubuntu you have to replace gtk2richmemo with the one attached.

Anyway, Lazarus really needs an updated component capable of displaying formatted text.
Title: Re: TRichMemo - add line by line
Post by: evoshroom on July 08, 2012, 11:24:17 pm
Quote
Anyway, Lazarus really needs an updated component capable of displaying formatted text.

I wholeheartedly agree.

I've found Richmemo to be great on Mac, but on Windows is where it seems lacking and it needs a way to add links and pics.

Is there a way to not count carriage returns as characters on Windows?  It seems the default behavior is the carriage returns don't count on Mac but do count on Windows, requiring separate $IFDEF'ed codebases.

Also, have you found any way to add line-by-line on Windows using more than one color per line and where it doesn't first add the line in one color and then change it (this looks horrible for users, but only seems to affect Windows)?
Title: Re: TRichMemo - add line by line
Post by: Lagavulin16 on December 03, 2012, 04:26:44 pm
From mdalacu's sample:

Code: [Select]
SetRangeColor(Length(Lines.Text)-Length(Lines[Lines.Count-1])-Lines.Count-1,Length(Lines[Lines.Count-1]),clRed);
Excuse my ignorance but what is this "-Lines.Count-1" for?
Title: Re: TRichMemo - add line by line
Post by: taazz on December 07, 2012, 09:53:50 pm
From mdalacu's sample:

Code: [Select]
SetRangeColor(Length(Lines.Text)-Length(Lines[Lines.Count-1])-Lines.Count-1,Length(Lines[Lines.Count-1]),clRed);
Excuse my ignorance but what is this "-Lines.Count-1" for?
assumption warning

the number of LF chars?
its heavily OS depended since in windows this should multiplied by 2
Title: Re: TRichMemo - add line by line
Post by: p95x on January 10, 2013, 07:42:48 pm
Hello,

as a solution for your issue, you can look at my procedure I have written and added to RichMemo.pas

Here, I have posted the functions:
http://www.lazarus.freepascal.org/index.php/topic,19446.0.html

TinyPortal © 2005-2018