GetSelectionBounds and CurItemNo

General TRichView support forum. Please post your questions here
Post Reply
jgkoehn
Posts: 303
Joined: Thu Feb 20, 2020 9:32 pm

GetSelectionBounds and CurItemNo

Post by jgkoehn »

I am using
RichViewEdit.GetSelectionBounds(PsiNo, PsiOffs, PeiNo, PeiOffs, True);
RichViewEdit.CurItemNo

However I am needing help understanding why PsiNo is not equal to CurItemNo.
This happens when the selection bound is around a single item. Is it because the selection technically starts before the 1st character of an item?

I can use CurItemNo but I would rather not do double work if I can just get it from GetSelectionBounds.
Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: GetSelectionBounds and CurItemNo

Post by Sergey Tkachenko »

The caret is always at the end of selection, so CurItemNo is either PsiNo or PeiNo.
If you call GetSelectionBounds(... False), it is always equal to PeiNo.
However, it is possible that in case of no selection GetSelectionBounds returns -1 in item indexes.
jgkoehn
Posts: 303
Joined: Thu Feb 20, 2020 9:32 pm

Re: GetSelectionBounds and CurItemNo

Post by jgkoehn »

If I use GetSelectionBounds(... True) should PsiNo be the CurItemNo?
jgkoehn
Posts: 303
Joined: Thu Feb 20, 2020 9:32 pm

Re: GetSelectionBounds and CurItemNo

Post by jgkoehn »

What I am doing is combining Regex Search with Item Format settings. Is the regex a bold item etc.
Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: GetSelectionBounds and CurItemNo

Post by Sergey Tkachenko »

If I use GetSelectionBounds(... True) should PsiNo be the CurItemNo?
No, it should not.
The caret is always at the end of the selected fragment. "The end" depends on the order how this selection was made. If it was made from top to bottom (PsiNo <= PeiNo), the end is closer to the bottom of the document. If it was made from bottom to top (PsiNo >= PeiNo), the end is closer to the top of the document.

GetSelectionBounds(... False) returns the selection bounds as they are, so CurItemNo = PeiNo.

GetSelectionBounds(... True) provides that PsiNo <= PeiNo, so CurItemNo is either PeiNo (if the selection was made from top to bottom) or PsiNo (if the selection was made from bottom to top).
jgkoehn
Posts: 303
Joined: Thu Feb 20, 2020 9:32 pm

Re: GetSelectionBounds and CurItemNo

Post by jgkoehn »

Thank you for taking time to explain that! That really helps.
jgkoehn
Posts: 303
Joined: Thu Feb 20, 2020 9:32 pm

Re: GetSelectionBounds and CurItemNo

Post by jgkoehn »

I think I am seeing part of what is going on
Imagine a word like this
word Nextword
Which is 5 bold characters because the space is included (1 item in RichView)
The "Nextword" is italics (1 item in RichView)
When I run RichViewEdit.GetSelectionBounds(PsiNo,PsiOffs, PeiNo, PeiOffs, False)
If I've selected the entire first Item using: TCustomRVFormattedData(RVData1).SetSelectionBounds(ItemNo1, Offs1, ItemNo2, Offs2)
Visually in RichViewEdit I see just "word " is selected.
I expect
PsiNo = 1
PeiNo = 1
PsiOffs = 1
PeiOffs = 5 (it is 6, I don't know why but doesn't affect me for now)

2nd Item selection what I expect
PsiNo = 2 (but it is 1??? this is really messing me up, how can I account for this. (It is not always this way.))
PsiOffs = 1 (but it is 10 probably due to item1)
PseiNo = 2
PseiOffs = 8 (it is 9, I don't know why but doesn't affect me for now)
jgkoehn
Posts: 303
Joined: Thu Feb 20, 2020 9:32 pm

Re: GetSelectionBounds and CurItemNo

Post by jgkoehn »

I think I must be doing something wrong here:

StartIndex := M.Index-1;
EndIndex := StartIndex + M.Length;
if LinearToRichView(MainForm.RichViewEdit, MainForm.RichViewEdit.RVData, StartIndex, RVData1, ItemNo1, Offs1) and
LinearToRichView(MainForm.RichViewEdit, MainForm.RichViewEdit.RVData, EndIndex, RVData2, ItemNo2, Offs2) and
(RVData1 = RVData2) then
begin
RVData1 := RVData1.Edit;
TCustomRVFormattedData(RVData1).SetSelectionBounds(ItemNo1, Offs1, ItemNo2, Offs2);
jgkoehn
Posts: 303
Joined: Thu Feb 20, 2020 9:32 pm

Re: GetSelectionBounds and CurItemNo

Post by jgkoehn »

I think I see what is going on just not sure how to fix.
I need the selection to go to the closet item and not just use offs.
jgkoehn
Posts: 303
Joined: Thu Feb 20, 2020 9:32 pm

Re: GetSelectionBounds and CurItemNo

Post by jgkoehn »

I found a fix, this brings the selection up to the present item if they are identically linearly.
A similar function may be nice to add to the LinearToRichView. It adjusts the Item and the Offs to the Items.

Code: Select all

       RichViewEdit.GetSelectionBounds(PsiNo, PsiOffs, PeiNo, PeiOffs, False);
       //Selection was made PsiNo------>PeiNo in SelectNext, Replace, ReplaceAll

       //Test two linear positions
       if PsiOffs <> 1 then
          begin
          RichViewToLinear(RichViewEdit, RichViewEdit.RVData, RVData1,  PsiNo, PsiOffs, PosA);
          RichViewToLinear(RichViewEdit, RichViewEdit.RVData, RVData2,  PsiNo+1, 1, PosB);
          if PosA = PosB then
             begin
             PsiNo := PsiNo+1;
             PsiOffs := 1;
          end;
        end;
Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: GetSelectionBounds and CurItemNo

Post by Sergey Tkachenko »

Sorry, I do not understand what your code does.
jgkoehn
Posts: 303
Joined: Thu Feb 20, 2020 9:32 pm

Re: GetSelectionBounds and CurItemNo

Post by jgkoehn »

Ah basically the idea is to check if caret positions are identical if so move to the updated item.
However, I wonder if I am misunderstanding something.
Why is APos and BPos identical? I don't think it should be.

RichViewToLinear(GRichViewEdit, GRichViewEdit.RVData, RVData1, 1, 9, PosA);
RichViewToLinear(GRichViewEdit, GRichViewEdit.RVData, RVData2, 2, 1, PosB);

Is it because I have not supplied data to RVData1, RVData2: TCustomRVData; ??
jgkoehn
Posts: 303
Joined: Thu Feb 20, 2020 9:32 pm

Re: GetSelectionBounds and CurItemNo

Post by jgkoehn »

My Fault I am sorry
I didn't do it right
RichViewToLinear(GRichViewEdit, GRichViewEdit.RVData, GRichViewEdit.RVData, PsiNo, PsiOffs, PosA);
RichViewToLinear(GRichViewEdit, GRichViewEdit.RVData, GRichViewEdit.RVData, PsiNo+1, 1, PosB);
Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: GetSelectionBounds and CurItemNo

Post by Sergey Tkachenko »

Problems:
1) The item with index PsiNo+1 does not necessary exist (check GRichViewEdit.ItemCount)
2) 1 is the position before the item only if this is a text item. For non-text item, the position before item is 0. Universal way: comparison with GRichViewEdit.GetOffsetBeforeItem(PsiNo_1)

But as I understand, you simply want to know if the position (PsiNo, PsiOffs) is at the end of the item.
You can check it simpler and much faster: if PsiOffs = GRichViewEdit.GetOffAferItem(PsiNo).

However, I am still not sure what you need. Your comparison of linear position works only if the item PsiNo and PsiOffs are at the same paragraph. Otherwise, they will be different by the size of line break (by default, by 2).
And my comparison (PsiOffs = GRichViewEdit.GetOffAferItem(PsiNo)) works when the next item is at the same paragraph, in the new paragraph, or even if PsiNo is the last item.
I am not sure what you need.
Post Reply