Goto same line on next page

General TRichView support forum. Please post your questions here
Post Reply
JDommi
Posts: 6
Joined: Sat Aug 17, 2013 12:34 pm

Goto same line on next page

Post by JDommi »

I'm trying to add text to the same line on multiple pages.
At the moment I'm realizing this by searching the first text occurence on each page as it always is the same word, count the difference between this item and the current item and repeat these steps for every page. But sometimes the item count differs from one page to another.
The better way would be to get the current line number of the first page and then scroll to the same line on the next page.
How can I perform this?

JDommi
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

But adding text in the middle of document may affect pagination?
JDommi
Posts: 6
Joined: Sat Aug 17, 2013 12:34 pm

Post by JDommi »

Okay, in general I have to agree. But in my case it does not.
I have a numbered list with a name and have to add "only" a short phrase that will not affect the pagination.
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

The better way would be to get the current line number of the first page and then scroll to the same line on the next page.
I assume by "line number" you assume "paragraph number", because line number depends on word wrapping and can be different on the screen and on paper (in TRichViewEdit+TRVPrint; in ScaleRichView, lines are the same on the screen and on paper).

The requested functions are below.
This function returns the current number of paragraph (more exactly, of paragraph section; not only paragraph breaks (added by Enter key) but also line breaks added by Shift+Enter are taken into account).
For the first paragraph, it returns 0, for the second - 1, and so on.

Code: Select all

function GetCurrentParagraphIndex(rve: TCustomRichViewEdit): Integer;
var i: Integer;
begin
   Result := 0;
   for i := rve.CurItemNo downto 1 do
     if rve.IsFromNewLine(i) then
       inc(Result);
end;
The next function moves the caret down by the specified number of paragraphs (more exactly, paragraph sections). ParaCount=0 is ignored. ParaCount=1 moves to the beginning of the next paragraph; ParaCount=2 moves to the beginning of next after next paragraph, and so on:

Code: Select all

function MoveDown(rve: TCustomRichViewEdit; ParaCount: Integer): Boolean;
var
  i: Integer;
begin
  Result := False;
  if ParaCount<=0 then
    exit;
  for i := rve.CurItemNo+1 to rve.ItemCount-1 do
    if rve.IsFromNewLine(i) then begin
      dec(ParaCount);
      if ParaCount=0 then begin
        rve.SetSelectionBounds(i, rve.GetOffsBeforeItem(i), i, rve.GetOffsBeforeItem(i));
        Result := True;
        exit;
      end;
    end;
end;
Place the caret on the desired line of the first page. Store the result of GetCurrentParagraphIndex.
Move the caret to the first line of the second page. Then call MoveDown, using the stored value as a parameter.
JDommi
Posts: 6
Joined: Sat Aug 17, 2013 12:34 pm

Post by JDommi »

Simply great :)
Looks as it will do the thing. Tomorrow I will test it in my source.
JDommi
Posts: 6
Joined: Sat Aug 17, 2013 12:34 pm

Post by JDommi »

I just have updated my source with these two functions and they work like a charm :D
Big thanks to you :!:
Post Reply