[Example][ScaleRichView] How to draw line numbers

Demos, code samples. Only questions related to the existing topics are allowed here.
Post Reply
Sergey Tkachenko
Site Admin
Posts: 16238
Joined: Sat Aug 27, 2005 10:28 am
Contact:

[Example][ScaleRichView] How to draw line numbers

Post 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
Last edited by Sergey Tkachenko on Fri Oct 02, 2015 9:42 am, edited 1 time in total.
Jim Knopf
Posts: 205
Joined: Mon Dec 30, 2013 10:07 pm
Location: Austria
Contact:

Post by Jim Knopf »

Oh, I saw it only now - thank you!
Sergey Tkachenko
Site Admin
Posts: 16238
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

I think we will add line numbers as a built-in option in TSRichViewEdit.
11111
Posts: 27
Joined: Sat Feb 07, 2015 12:02 pm

Post by 11111 »

Is it possible to do the same for TRichViewEdit ?
Sergey Tkachenko
Site Admin
Posts: 16238
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post 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.
Sergey Tkachenko
Site Admin
Posts: 16238
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Line numbers is a standard feature since ScaleRichView 6.5 (available for registered users)
11111
Posts: 27
Joined: Sat Feb 07, 2015 12:02 pm

Post 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.
Sergey Tkachenko
Site Admin
Posts: 16238
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post 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.
standay
Posts: 111
Joined: Fri Jun 18, 2021 3:07 pm

Re: Line Numbers in TRVPrint?

Post 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...
Sergey Tkachenko
Site Admin
Posts: 16238
Joined: Sat Aug 27, 2005 10:28 am
Contact:

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

Post by Sergey Tkachenko »

I'll make an example later in this week, sorry for the delay.
standay
Posts: 111
Joined: Fri Jun 18, 2021 3:07 pm

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

Post 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.
Post Reply