How does the cursor jump to the starting point

General TRichView support forum. Please post your questions here
Post Reply
LuckySpacy
Posts: 7
Joined: Fri Nov 22, 2013 7:45 am

How does the cursor jump to the starting point

Post by LuckySpacy »

I would like to change a color in a text and then the cursor should be return on the starting point.

see the follow simple example.

In the example the cursor starting point is behind "u" from the word "jump".
Then I click on the 'btn_SetCursorToStart".
The cursor appear bevor "T".
Then I click on the "btn_ReturnCursor", the cursor appear on the starting point.

So far, so good.

Now, I would change the color to green in the word "quick".

I click on the "btn_SetCursorToStart", "btn_SetWordGreen" and then "btn_ReturnCursor".
It doesn't work.
I do understand the reason, but how can i solve it.
The cursor should be return on the starting point.

Code: Select all

procedure TForm16.FormCreate(Sender: TObject);
var
  f: TFontInfo;
begin
  RVStyle.TextStyles.Clear;
  f := RvStyle.TextStyles.Add;
  f.FontName := 'Courier new';
  f.Size := 9;
  f.StyleName := 'Normal';

  f := RvStyle.TextStyles.Add;
  f.FontName := 'Courier new';
  f.Size := 9;
  f.Color := clGreen;
  f.StyleName := 'Green';

  rv.InsertText('The quick brown fox jumps over the lazy dog');

end;


procedure TForm16.btn_SetCursorToStartClick(Sender: TObject);
begin
  FCurItemNo := rv.CurItemNo;
  FCurOff    := TRVEditRVData(rv.RVData).GetOffsetInCurItem;
  rv.SetSelectionBounds(0, 0, 0, 0);
  rv.SetFocus;
end;


procedure TForm16.btn_ReturnCursorClick(Sender: TObject);
begin
  rv.SetSelectionBounds(FCurItemNo, FCurOff, FCurItemNo, FCurOff);
  rv.SetFocus;
end;

procedure TForm16.btn_SetWordGreenClick(Sender: TObject);
begin
  rv.SearchText('quick', [rvseoDown]);
  rv.ApplyTextStyle(1);
  rv.Invalidate;
end;
Sergey Tkachenko
Site Admin
Posts: 17564
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Initially, the document has 1 item:
- 'The quick brown fox jumps over the lazy dog'.
After changing color, the document has 3 items:
- 'The ',
- 'quick',
- ' brown fox jumps over the lazy dog'.
So you cannot use the stored ItemNo and Offs to return to the starting point.

You can use the function from RVLinear.pas: http://www.trichview.com/help/idh_rvlinear.html
To store and restore the caret position, use RVGetLinearCaretPos and RVSetLinearCaretPos.
You can also store not only the caret position, but selection (the caret is always at the end of a selection, so storing a selection stores a caret position as well).
Post Reply