Recent

Author Topic: TSynEdit beginner question  (Read 1537 times)

Gerold_R

  • New Member
  • *
  • Posts: 12
TSynEdit beginner question
« on: February 22, 2021, 02:01:16 pm »
Hi

I am quite new to Lazarus and now I am using TSynEdit for the first time to replace a TMemo which seems to have problems with long lines.

In my program I have a log window (something like the "Messages" window in Lazarus) which shows status information from the program itself and incomming messages from a connected server.

I add new messages using
SynEdit.Append(S + LineEnding);

What do I have to do that I always see the new added line in the window? I searched the forum and used google but did not find an easy solution. The window does not scroll down automatically like the TMemo.

I also used
SynEdit.InsertTextAtCaret(S+LineEnding, scamEnd);

This worked a little bit better, but sometimes I saw a line with the same text and after I clicked on the scrollbar the second line disappeared.

BTW: Is there an easy way to add a new line with an other font color (line contains as first word "ERROR" and than is appended in RED for error messages from the server)

Thanks in advance
Gerold

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 7516
  • Debugger - SynEdit - and more
    • wiki
Re: TSynEdit beginner question
« Reply #1 on: February 22, 2021, 02:38:25 pm »
Code: Pascal  [Select][+][-]
  1. SynEdit.TopLine := SynEdit.Lines.Count;
should probably do it.

Couple of notes:

1) Make sure eoScrollPastEof is NOT in SynEdit.options.

2) SynEdit.Append / Synedit.Lines.*** versus SynEdit.InsertText***
I assume you display data only? (ReadOnly)?
The user does not edit the text via the Keyboard?

If the user can edit the text, then you may want Undo/Redo to work?
- Undo/Redo does not work with SynEdit.Append / Synedit.Lines.*** .
- In that case you need SynEdit.InsertAtCaret / SynEdit.TextBetweenPoints ....
If you do not need undo/redo SynEdit.Append / Synedit.Lines.*** is simpler and faster. (and saves memory on not adding undo/redo)

3) If you do not need the blinking caret visible:
Add eoNoCaret to SynEdit.Options


For the font color, you need a highlighter or markup.

There is a ready made Highlighter that can take a list of words. IIRC SynAnySyn... Not sure.

Or you can add  (you can add more than one)
Code: Pascal  [Select][+][-]
  1.     M := TSynEditMarkupHighlightAllMulti.Create(SynEdit);
  2.     SynEdit.MarkupMgr.AddMarkUp(M); // To be owned and destroyed by SynEdit
  3.  

TSynEditMarkupHighlightAll   // Can Highlight all occurrences of one given word
TSynEditMarkupHighlightAllMulti  // Can highlight several words (all in the same color)
Both have a property MarkupColor with several setting.

However Highlighter and Markup will all find the word anywhere in the text, not just at the start of line.

--
But if you look at TSynEditMarkupHighlightAll you can probably modify it to "start of line" only.

There is nothing that stores colors for a specific line or specific place.
So all of those will find the term to highlight by searching for it in the entire text.

Based on the above you can write code for X,Y locations. But then if you delete lines, you need to update all X/Y.
« Last Edit: February 22, 2021, 02:40:43 pm by Martin_fr »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 7516
  • Debugger - SynEdit - and more
    • wiki
Re: TSynEdit beginner question
« Reply #2 on: February 22, 2021, 02:46:28 pm »
There is a tutorial to write your own Highlighter
https://wiki.lazarus.freepascal.org/SynEdit_Highlighter

Note that SynEdit can only have ONE highlighter. So if you need the HL for other stuff, then you should use "Markups".

But as for Highlighting at specific locations: If you use a HL, you could use ("misuse") the "Ranges" (see wiki) to store that...
 

Gerold_R

  • New Member
  • *
  • Posts: 12
Re: TSynEdit beginner question
« Reply #3 on: February 22, 2021, 07:48:12 pm »
Thank you for your answer Martin.

SynEdit.TopLine := SynEdit.Lines.Count;

This works.

This window is ReadOnly. No need to Undo etc.

I already found the way to disable the caret.

Thanks again
Gerold

 

TinyPortal © 2005-2018