Page 1 of 1

[Example][ScaleRichView] How to draw line numbers

Posted: Sun Feb 01, 2015 9:28 pm
by Sergey Tkachenko
Update: this code is not needed in new version of ScaleRichView, because this is a standard feature, see TSRichViewEdit.LineNumberProperty

Assign this code to TSRichViewEdit's OnPaintPage event.

The code draws line numbers at the left margin.
Numbering is restarted on each page.

Code: Select all

procedure TForm1.SRichViewEdit1PaintPage(Sender: TObject; PageNo: Integer;
  PageRect, R: TRect; Canvas: TCanvas; Prepaint, Printing: Boolean);
const
  PageNumberDX = 5; // distance between line numbers and text
var
  i: Integer;
  FirstItemNo, LastItemNo, Offs, ItemPart: Integer;
  FirstPageNo, LastPageNo : Integer;
  ItemRect: TRect;
  srv: TSRichViewEdit;
  TextHeight, LineNumber, Left, Top, LeftMargin: Integer;
begin

  // line number font
  Canvas.Font.Name := 'Tahoma';
  Canvas.Font.Size := 6;
  Canvas.Font.Style := [];
  Canvas.Font.Color := clBtnShadow;
  Canvas.Brush.Style := bsClear;
  
  TextHeight := Canvas.TextHeight('0');
  srv := Sender as TSRichViewEdit;
  srv.GetPageStartItemNo(PageNo, FirstItemNo, Offs);
  srv.GetPageLastItemNo(PageNo, LastItemNo, Offs);
  LineNumber := 1;
  LeftMargin := srv.GetLeftMargin100Pix(PageNo);

  for i := FirstItemNo to LastItemNo do begin
    if srv.RichViewEdit.GetItemStyle(i)=rvsTable then
      continue;
    ItemPart := 0;
    while srv.GetItemBounds100(
      srv.RichViewEdit.RVData, i, ItemRect,
      FirstPageNo, LastPageNo, ItemPart) do begin
      if (FirstPageNo = PageNo) and
         ((ItemPart>0) or srv.RichViewEdit.IsFromNewLine(i)) then begin
          Left := PageRect.Left + LeftMargin - PageNumberDX -
            Canvas.TextWidth(IntToStr(LineNumber));
          Top := PageRect.Top + (ItemRect.Top+ItemRect.Bottom - TextHeight) div 2;
          Canvas.TextOut(Left, Top, IntToStr(LineNumber));
        inc(LineNumber);
      end;
      inc(ItemPart);
    end;
  end;
end;
Screenshot:
Image

Posted: Sat Feb 21, 2015 6:59 pm
by Jim Knopf
Oh, I saw it only now - thank you!

Posted: Sun Feb 22, 2015 2:02 pm
by Sergey Tkachenko
I think we will add line numbers as a built-in option in TSRichViewEdit.

Posted: Sat Mar 07, 2015 4:01 pm
by 11111
Is it possible to do the same for TRichViewEdit ?

Posted: Tue Mar 10, 2015 3:59 pm
by Sergey Tkachenko
Do you mean line numbers on pages (when printing using TRVPrint) or line numbers directly in the editor?

Drawing line numbers directly in TRichViewEdit would be very inefficient. Line numbers are not stored, they are calculated on drawing. In the ScaleRichView example, numbering are started at the page beginning, so calculation is simple. In TRichViewEdit, we would need to iterate each time from the beginning, it is slow.

In TRVPrint, it is possible, although, unlike ScaleRichView, it does not have documented methods for it.
If you need line numbers in TRVPrint, I can make an example.

Posted: Sat Aug 01, 2015 5:06 pm
by Sergey Tkachenko
Line numbers is a standard feature since ScaleRichView 6.5 (available for registered users)

Posted: Thu Sep 17, 2015 6:27 am
by 11111
Sergey Tkachenko wrote:Do you mean line numbers on pages (when printing using TRVPrint) or line numbers directly in the editor?

Drawing line numbers directly in TRichViewEdit would be very inefficient. Line numbers are not stored, they are calculated on drawing. In the ScaleRichView example, numbering are started at the page beginning, so calculation is simple. In TRichViewEdit, we would need to iterate each time from the beginning, it is slow.

In TRVPrint, it is possible, although, unlike ScaleRichView, it does not have documented methods for it.
If you need line numbers in TRVPrint, I can make an example.
I mean line numbers directly in the editor.
I think it's good feature that makes reading/writing and navigation more comfortable, also it can be optional as custom panel, or just for plaintext mode.

Posted: Thu Sep 17, 2015 9:24 am
by Sergey Tkachenko
To make it a standard feature, line numbers must be stored in a document.
Otherwise, they need to be calculated on painting. For a page, it's not critical, because pages have a limited count of lines.
But for a document containing hundreds of thousands lines, calculating line numbers on painting is unacceptable.

So, until internal representation is changed, this feature will not be added.

Re: Line Numbers in TRVPrint?

Posted: Fri Jul 16, 2021 7:47 pm
by standay
Sergey Tkachenko wrote: Tue Mar 10, 2015 3:59 pm In TRVPrint, it is possible, although, unlike ScaleRichView, it does not have documented methods for it.
If you need line numbers in TRVPrint, I can make an example.
Yes, Sergey, do you have some code for this? I've been trying to figure something out for hours and having no luck at all! Main issues are getting item positions to draw on the print preview canvas to the left of the rv, and getting print preview line numbers correct. I have a little of it working but only on the first page. After that my line numbers are wrong. Any ideas appreciated. I find I can't format the print rv as it garbles everything up, and if I can't format it I get strange results...

Re: [Example][ScaleRichView] How to draw line numbers

Posted: Wed Jul 21, 2021 7:25 am
by Sergey Tkachenko
I'll make an example later in this week, sorry for the delay.

Re: [Example][ScaleRichView] How to draw line numbers

Posted: Thu Jul 22, 2021 8:15 pm
by standay
Sergey Tkachenko wrote: Wed Jul 21, 2021 7:25 am I'll make an example later in this week, sorry for the delay.
No worries. I like to try getting things to work until I run out of things to try. No hurry, when you have time would be great. Thanks.