Recent

Author Topic: [Solved] Colored Text in TMemo  (Read 7092 times)

OLLI_S

  • Full Member
  • ***
  • Posts: 119
[Solved] Colored Text in TMemo
« on: November 15, 2018, 11:40:58 am »
Hello,

I am using Lazarus 1.8.4 in Windows 10 Professional x64.

In my application I have a TMemo control that I use to write some logging information (not visible for the user, just for me):
Code: Pascal  [Select][+][-]
  1. mmoLogging.Lines.Add(aText);

When an error occurs I currently log a text like:
Code: Pascal  [Select][+][-]
  1.     Log('Check if Folder Exists  : Folder Not Found!');
  2.     Log('Open Log Folder         : EXIT WITH ERROR');

Now I want to format the error text in red font, so I see at once that there is an error.

I think a TMemo can not perform this, so I played a bit with the TIpHtmlPanel.
I can use IpHtmlPanel2.SetHtmlFromStr() to set the HTML Text of the control.
But how can I add a single line to an existing text?
Or is the TIpHtmlPanel not the right component to dynamically add text?

Best regards

OLLI
« Last Edit: November 17, 2018, 03:58:01 pm by OLLI_S »

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Colored Text in TMemo
« Reply #1 on: November 15, 2018, 12:04:31 pm »
No. A standard Tmemo knows only one text color. You need one of the rich** or a kmemo or syn*** or any other to do that.
Or extend tmemo yourself.
Specialize a type, not a var.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9792
  • Debugger - SynEdit - and more
    • wiki
Re: Colored Text in TMemo
« Reply #2 on: November 15, 2018, 12:45:03 pm »
How important is it to see the log within the application?

If you where logging to a file (debugln => console or file), then you could use tail and color the output in the console. There are tail for windows that include coloring.

debugln is provided by LazLogger, and has dummy replacements for release builds.
See http://wiki.lazarus.freepascal.org/LazLogger

OLLI_S

  • Full Member
  • ***
  • Posts: 119
Re: Colored Text in TMemo
« Reply #3 on: November 15, 2018, 12:55:16 pm »
How important is it to see the log within the application?

In the case of an error I want to tell the user to double-click on the version number in the status bar and then he will see a new tab with the logging information.
So I want to leave it in the application so users can send me errors they have (and I can explain why they have the errors).

No. A standard Tmemo knows only one text color. You need one of the rich** or a kmemo or syn*** or any other to do that.

Thank you, I am having a TRichMemo now in my application.
And I also found an article how to format the Text in RichMemo: http://wiki.freepascal.org/RichMemo#Append_mixed_color_text_at_the_end_of_the_RichMemo

Best regards

OLLI
« Last Edit: November 15, 2018, 01:00:27 pm by OLLI_S »

OLLI_S

  • Full Member
  • ***
  • Posts: 119
Re: Colored Text in TMemo
« Reply #4 on: November 15, 2018, 02:59:12 pm »
I have a last question about the TRichMemo:
How can I adjust the scrolling speed?

On the form I had a TIpHtmlPanel and when I scroll with the mouse wheel one step, then a full line is scrolled (the first line in the TIpHtmlPanel is the second line with text).
When I scroll in the TRichMemo then only a part of the line is scrolled.
I have to scroll 5-6 steps to scroll one complete line.

So how can I increase the scrolling speed of the TRichMemo?
Thank you!

Best regards

OLLI

wp

  • Hero Member
  • *****
  • Posts: 11855
Re: Colored Text in TMemo
« Reply #5 on: November 15, 2018, 03:44:30 pm »
I can use IpHtmlPanel2.SetHtmlFromStr() to set the HTML Text of the control.
But how can I add a single line to an existing text?
Or is the TIpHtmlPanel not the right component to dynamically add text?
You must store the string to be displayed outside the HTML panel and add the new line to it. Then you put this into the SetHtmlFromStr again.

But there are several issues:
  • Using a "mini-browser" like IpHtmlPanel just for displaying some red words here and there looks like overkill to me.
  • The IpHtmlPanel adds some margin at the top and bottom of each paragraph which makes the lines look too much apart. I don't know how to turn this off, css margin does not seem to work here.
  • And there does not seem to be a way to autoscroll the panel when a line is added.

Therefore, I played with the TJvHTLabel which belongs to the JVCL library and can display a very simple html, but just enough to change the font and font color. Having its AutoSize set to true and putting it into a TScrollbox you can achieve also autoscrolling to the end of the list.

Just look at the attached demo.

The easiest way to get TJvHTLabel is to use Online Package Manager: Scroll down to the JVCLLaz packages and check the package JvCtrlsLazD (only this one if you don't need the others; note the "D" at the end, for "design-time package"!). Click Install and confirm the questions to resolve dependencies (i.e. the JVCL packages on which JvCtrlsLazD depends will be installed, too).

All the html solutions have the disadvantage that
  • the storage string will have to be copied to another location when another message is added.
  • the html string will have to be re-parsed every time a new message is added.
Both points will have an effect on speed, in particular if there are "many" messages. If speed becomes an issue you should write your own LogMessageViewer which should use more clever storage and should paint and parse the color codes only for the visible lines.

Josh

  • Hero Member
  • *****
  • Posts: 1271
Re: Colored Text in TMemo
« Reply #6 on: November 15, 2018, 03:59:04 pm »
Hi

Justa thought.

WHen you click the version number to display a tab, could this tab have 2 options.
Log    Error Log.

If the log is large, it could take some time to scan this memo to find the error. SO having an errror log window would make it easier.

The error would still be in the main log, and the error log window could also preceed the error with the line number of error in the log window.

Just an idea.
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: Colored Text in TMemo
« Reply #7 on: November 16, 2018, 12:01:36 am »
You can use A Syn Editor and configure it to work with a particular language and then you can use comment-like logs
for instance

Code: C  [Select][+][-]
  1. 15.26.34.410 - [FINAL.] TSchedulerThread.Execute;  Thread Name = "Scheduler"; Thread ID = 16136.
  2. 15.26.34.426 - [INICIO] Forzar envio transacciones el dia 15/11/2018 15:26:34
  3. 15.26.34.457 - TOutboxProcessDirect.ProcessingOutBox: enviando 1 de 1
  4. 15.26.34.548 - ERROR: [MENSAJE: "Exception: Access violation"; EXTRA: "TMyClass.SomeMethod"]
  5. 15.26.34.548 - // ERROR: [MENSAJE: "Exception: Access violation"; EXTRA: "TMyClass.SomeMethod"]
  6. 15.26.34.548 - # ERROR: [MENSAJE: "Exception: Access violation"; EXTRA: "TMyClass.SomeMethod"]
  7.  
And you can put very spotty colors to make it more visible


OFF-TOPIC:
I used to view the log files with Lazarus it is Lightning fast and efficient (I really admire the people involved in the highlighter). If you don't believe me, then open a really huge log file (500 K lines) and activate a highlighter for that extension (C++ for instance) and do same with Notepad++ and then you'll see a difference. Notepad++ at 80K lines starts to hiccup, and have a real bad time at scrolling...

I stopped using Lazarus to view log files because when I'm debugging is a real pain especially when stepping line by line you get the dialog "the file xxxx has changed..." and I don't know how to configure it to not showing up for this or that  file only, or this or that file extension: It's a friking log file!!! Reload it silently already!!!! that's what I thought and started using Notepad++

« Last Edit: November 16, 2018, 12:07:35 am by garlar27 »

OLLI_S

  • Full Member
  • ***
  • Posts: 119
Re: Colored Text in TMemo
« Reply #8 on: November 17, 2018, 03:57:13 pm »
Hello,

I am using a TRichMemo control now and this is perfectly working.
So I also had to change just a little bit of code to make it work like the old TMemo control.
The issue is solved, thank you for your help!

OLLI

 

TinyPortal © 2005-2018