[Lazarus] RE : RE : RE : Displaying text with colour and insertionpoint

Mark Morgan Lloyd markMLl.lazarus at telemetry.co.uk
Sun May 20 10:12:29 CEST 2012

Ludo Brands wrote:

>> Or put another way, in the simplest case where I want to change the 
>> colour of the most-recently-added character, what parameters 
>> should I be using?
> Don't know what function you are using to add the characters but here is a
> routine that will paste from the clipboard in red at the cursor position:
> procedure TForm1.Button1Click(Sender: TObject);
> var
>   selbegin,sellen:integer;
> begin
>   selbegin:=RichMemo1.SelStart;
>   RichMemo1.PasteFromClipboard; 
>   sellen:=RichMemo1.SelStart-selbegin;
>   RichMemo1.SetRangeColor(selbegin,sellen,clred);
> end;
> Tried this on windows by pasting chinese characters with the cursor in the
> middle of an existing text: only the pasted characters are colored red. 
> So the trick is to get the cursor position before and after the insertion
> and take these values to define the range, then color that range. Whatever
> the characters inserted, single byte or multibyte, the cursor position
> reflects this correctly.

Thanks. I didn't get this working reliably but I'm fairly sure that's 
self-inflicted: I'm dropping it for the moment but will go into a bit 
more detail in case it's relevant to anybody else considering terminal 
emulators etc.

I started this off by outputting to a TListBox, appending text directly 
to the underlying strings. This worked, but line-spacing was a bit 
unnatural and there was no cursor support. I then added output via a 
TMemo- reusing as much of the existing code as possible, the result of 
this is that I'm not going via any of the "obvious" ways of adding text 
so had to fudge cursor handling- which I suspect I have done imperfectly.

Revisiting this to look at adding colour was originally done because I 
wanted to familiarise myself with TCmdBox for another job, however while 
it's obviously good for handling local commands etc. I don't think it's 
appropriate for character-by-character terminal operation. For the 
latter, in cases where "paper roll" operation is required (as distinct 
from a terminal with cursor positioning etc.) I think that 
TMemo/TRichMemo are probably the tools for the job, in particular my 
code to intercept keydown events works well enough that I can simulate 
an APL keyboard. I suspect that the "correct" way of displaying received 
characters is by pushing them through simulated keypress events- I've 
certainly been able to force UTF8 into a SynEdit in the past by using 
IntfUTF8KeyPress() but I believe that LCLSendUTF8KeyPress() may be 
preferred (albeit a recent addition).

I don't know what the best choice would be for writing a terminal 
emulator that required accurate cursor positioning, i.e. a VT-52/100 
emulation with "go to X,Y" capability rather than simply stepping 
between adjacent cells. As things stand at present, I suspect that it 
would require a purpose-built component- which is something that I have 
neither the skills nor time to contemplate. Fortunately, I rarely need 
that sort of thing for the equipment I need to talk to, and when I do 
using Teraterm (Windows) or gtkterm (Linux) or a Lazarus hack based on a 
DrawGrid is generally adequate.

Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]

More information about the Lazarus mailing list