Entering text and seeking information

General TRichView support forum. Please post your questions here
Post Reply
Ceprotec
Posts: 259
Joined: Thu Oct 28, 2010 6:09 pm
Contact:

Entering text and seeking information

Post by Ceprotec »

Hello Sergey.
Everything good?

I'm having a hard time and need your help. First explain what happens. Srichviewedit I use as a component for printing from a register, or seek information from a database and insert the srichview in the form of a text, ok. So I need to know:

1) What is the position of the last valid character (blank space can not count) in row and column?
2) How many characters still fit within that page?
3) How many lines there are still empty from the last character to the end of the page?
4) How to get text input from the line (x) and column (y)?

I need these data apart from the cursor position. I need this information in time for the automatic insertion of text in srichviewedit.

Why is this .. I will use a method where I will give a "InsertText" from "x" and "y " and need to know how many characters can I enter to the final page.

Thank you for listening.
Graciously Ceprotec.
Sergey Tkachenko
Site Admin
Posts: 17566
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

I am afraid these questions cannot be answered.
Each character has different width (unless you use a monospace font), so you cannot tell how many characters will fit.
Lines can have different heights as well.
Ceprotec
Posts: 259
Joined: Thu Oct 28, 2010 6:09 pm
Contact:

Post by Ceprotec »

Ok so let's go party.
1) You can find the position of the last valid character?
Sergey Tkachenko
Site Admin
Posts: 17566
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Do you need a position as Line, Column?

What if the document is ended with a picture, should I ignore it and process text before it?
What is the last character is a table? In this case, Line/Column does not make too much sense. Or should I ignore this table as well?
Ceprotec
Posts: 259
Joined: Thu Oct 28, 2010 6:09 pm
Contact:

Post by Ceprotec »

Yes, I need the position in row / column. You can skip images, and tables because at the time table will not have nor images, text only.
Sergey Tkachenko
Site Admin
Posts: 17566
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Code: Select all

var i, j, l, c: Integer;
    rve: TCustomRichViewEdit;
    s: String;
begin
  rve := SRichViewEdit1.RichViewEdit;
  for i := rve.ItemCount-1 downto 0 do
    if (rve.GetItemStyle(i)>=0) then begin
      s := rve.GetItemText(i);
      for j := Length(s) downto 1 do
        if s[j]<>' ' then begin
          rve.SetSelectionBounds(i, j, i, j);
          rve.GetCurrentLineCol(l, c);
          Caption := Format('(%d %d)', [l, c]);
          exit;
        end;
    end;
  Caption := 'no characters';
end;
This code returns the global line, not the line on the page.
It moves the caret before this character. If you need version without moving the caret, let me know.
Ceprotec
Posts: 259
Joined: Thu Oct 28, 2010 6:09 pm
Contact:

Post by Ceprotec »

Ok I got with this formula. Thanks.
Now the next step.

2) How to insert text from a line (l) and column (c)?
Sergey Tkachenko
Site Admin
Posts: 17566
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

I can write this function, but it would require using some undocumented methods.
But do you really need using (line, column) kind of coordinates for defining position in the document? They are not native for TRichViewEdit/TSRichViewEdit, so using them require additional efforts that better to be avoided.

The most simple and efficient for TRichViewEdit is using (Item index, offset in item) pair of coordinate.
Or there are functions allowing working with "position from the beginning" coordinate (i.e. richedit-like SelStart).
Ceprotec
Posts: 259
Joined: Thu Oct 28, 2010 6:09 pm
Contact:

Post by Ceprotec »

Actually I only need the line, it will always be in column 0 (zero).
Example:

With the formula above I know that line is the last character. Now I need a formula to insert text from the line (l). Like a InsertText ('', l). Would do that?

That also would direct the cursor to the line (l) / column (c) and use a InsertText () normal.
Sergey Tkachenko
Site Admin
Posts: 17566
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Lines depend on word wrapping. When you insert something at the beginning of line that is not the beginning of paragraph, this line may be wrapped differently. You may insert at the beginning of line, but after the insertion it may become not a beginning.
Are you sure that you mean "lines", not "paragraphs"?
Ceprotec
Posts: 259
Joined: Thu Oct 28, 2010 6:09 pm
Contact:

Post by Ceprotec »

Yesterday you gave me a formula that took the position of the last character, right?
Suppose that the last character is on the line ( 18 ) and column ( 58 ), right?
so I need a command that will insert a string line (19) and column (0), which would be the beginning of the paragraph within the margin set, for example.
Sergey Tkachenko
Site Admin
Posts: 17566
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Than function returns the position of the last non-space character. So there may be some lines after this line (empty text or spaces, or pictures), or may be not.
So, in your example, the line #19 may exist or may not exist.

Ok, below is the function that works in all cases:

Code: Select all

uses DLines;
procedure GoToLine(LineNo: Integer; rve: TCustomRichViewEdit);
var i: Integer;
begin
  // moving before existing line, if possible
  for i := 0 to rve.RVData.DrawItems.Count-1 do
    if rve.RVData.DrawItems[i].FromNewLine then begin
      dec(LineNo);
      if LineNo=0 then begin
        rve.SetSelectionBounds(rve.RVData.DrawItems[i].ItemNo,
          rve.RVData.DrawItems[i].Offs,
          rve.RVData.DrawItems[i].ItemNo,
          rve.RVData.DrawItems[i].Offs);
        exit;
      end;
    end;
  // adding new lines
  i := rve.ItemCount-1;
  rve.SetSelectionBounds(i, rve.GetOffsAfterItem(i), i, rve.GetOffsAfterItem(i));
  while LineNo>0 do begin
    rve.InsertText(#13#10);
    dec(LineNo);
  end;
end;
...
GoToLine(SRichViewEdit1.RichViewEdit, 19);
SRichViewEdit1.RichViewEdit.InsertText('Hi!');

Ceprotec
Posts: 259
Joined: Thu Oct 28, 2010 6:09 pm
Contact:

Post by Ceprotec »

Thank you Sergey, was exactly what I needed. Thanks.
:D :P :o
Post Reply