[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: 13576
Joined: Sat Aug 27, 2005 10:28 am
Contact:

[Example][ScaleRichView] How to draw line numbers

Post by Sergey Tkachenko » Sun Feb 01, 2015 9:28 pm

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: 161
Joined: Mon Dec 30, 2013 10:07 pm
Location: Austria
Contact:

Post by Jim Knopf » Sat Feb 21, 2015 6:59 pm

Oh, I saw it only now - thank you!

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

Post by Sergey Tkachenko » Sun Feb 22, 2015 2:02 pm

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 » Sat Mar 07, 2015 4:01 pm

Is it possible to do the same for TRichViewEdit ?

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

Post by Sergey Tkachenko » Tue Mar 10, 2015 3:59 pm

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: 13576
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko » Sat Aug 01, 2015 5:06 pm

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 » Thu Sep 17, 2015 6:27 am

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: 13576
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko » Thu Sep 17, 2015 9:24 am

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.

Post Reply