Page 1 of 1

position of caret after marking a word/area

Posted: Sat Sep 27, 2008 10:12 am
by j&b
After marking a word (sentence).
How can I find out whether the caret stands at the beginning or end of a selected word (sentence).

Posted: Sat Sep 27, 2008 11:25 am
by Sergey Tkachenko
See GetSelectionBounds method.
Call RichViewEdit.TopLevelEditor.GetSelectionBounds(..., False), and check the returned values.

Posted: Sat Sep 27, 2008 12:18 pm
by j&b
Hello Sergey,
thank you for your fast help.

memo.GetSelectionBounds(StartItemNo, StartItemOffs, EndItemNo, EndItemOffs, true);
memoPos:=RVGetLinearCaretPos(memo);
laenge:= length(memo.getseltext);

ShowMessage(intToStr(StartItemNo)+' * '+intToStr(StartItemOffs)+' * '+intToStr(EndItemNo)+' * '+intToStr(EndItemOffs)+#13#10+
intToStr(memoPos)+' * '+intToStr(memoPos+laenge));


I have problems with memo.GetSelectionBounds(StartItemNo, ...) and RVGetLinearCaretPos(memo)
If I stand at the beginning of a marked text I want to set caret at the end of the marked text.

3 * 6 * 4 * 18 // GetSelectionBounds
95 * 112 // memoPos * memoPos+laenge


Background:

I mark a text. Then I change font.color to blue and I underline the marked text by clicking a button (or F11).

if I mark the text from left to right I stand at the end of the marked text.
If I press RETURN I change to the next line, BUT the new text has the attributes (blue and underlined) of the marked text.

This I can change:
//look some lines below //<<<<-------------------------------
cd.color:= 0; Memo.ApplyStyleConversion(rv_COLOR);
Memo.ApplyStyleConversion(rv_CLEARUNDERLINE);
if query1.state in [dsEdit, dsInsert] then query1.Post;


But if caret stands at the beginning of marked text I first have to set caret at the end of the marked text. Therfore I must know if I stand at the beginning or end of the marked text

...
memo.GetSelectionBounds(StartItemNo, StartItemOffs, EndItemNo, EndItemOffs, true);
memoPos:=RVGetLinearCaretPos(memo); laenge:= length(memo.getseltext); memo.deselect;

//ShowMessage(intToStr(StartItemNo)+' * '+intToStr(StartItemOffs)+' * '+intToStr(EndItemNo)+' * '+intToStr(EndItemOffs)+#13#10+
// intToStr(memoPos)+' * '+intToStr(memoPos+laenge));

//if caret stands at the beginning then
RVSetLinearCaretPos(memo,memoPos+laenge); // <<<<------------------------

cd.color:= 0; Memo.ApplyStyleConversion(rv_COLOR);
Memo.ApplyStyleConversion(rv_CLEARUNDERLINE);
if query1.state in [dsEdit, dsInsert] then query1.Post;

Posted: Sat Sep 27, 2008 2:10 pm
by Sergey Tkachenko
If you call GetSelectionBounds with the last parameter = True, the selection will be returned in "normalized form", i.e. as if the caret is at the end of selection.

This code moves the caret to the end of selection:

Code: Select all

memo.TopLevelEditor.GetSelectionBounds(StartItemNo, StartItemOffs, EndItemNo, EndItemOffs, true); 
if StartItemNo>=0 then // selection exists
  memo.TopLevelEditor.SetSelectionBounds(StartItemNo, StartItemOffs, EndItemNo, EndItemOffs); 

Posted: Sat Sep 27, 2008 3:05 pm
by j&b
Thank you.

In the meantime I have found following solution

memoPos:=RVGetLinearCaretPos(memo);
laenge:= length(memo.getseltext);
if memo.OffsetInCurItem>laenge+1 then RVSetLinearCaretPos(memo,memoPos+laenge);

Posted: Sat Sep 27, 2008 3:27 pm
by Sergey Tkachenko
It's not a solution, because comparing offset of the caret position in item and text selection length gives nothing.

Posted: Sat Sep 27, 2008 4:01 pm
by j&b
It works correct. 34x tested.

Why ?

Posted: Sat Sep 27, 2008 6:08 pm
by Sergey Tkachenko
Selection may include several items. In this case OffsetInCurItem may be small and laenge may be large, regardless of the caret position.

If you want to use functions from RVLinear.pas, you can use RVGetSelection. If the caret is before the selection, SelLength is negative:

Code: Select all

var SelStart, SelLength: Integer;
begin
  RVGetSelection(RichViewEdit1, SelStart, SelLength);
  if SelLength<0 then
    RVSetSelection(RichViewEdit1, SelStart+SelLength, -SelLength);
end;
Note that GetSelectionBounds and SetSelectionBounds work faster than RVGetSelection and RVSetSelection.

Posted: Sun Sep 28, 2008 6:20 am
by j&b
Thanks.

Your help is wonderful :P