Lazarus
Programming => Packages and Libraries => RichMemo => Topic started 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!
-
I have not used Richmemo very often, but this (in my eyes terribly dirty) coding works to get lines in different colors:
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;
-
Thank you for your replay, it works. :D
I have updated the example a bit, it's a little bit faster:
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;
-
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.
-
you are right, here is the updated example:
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:
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.
-
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)?
-
From mdalacu's sample:
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?
-
From mdalacu's sample:
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
-
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