Position cursor programatically

General TRichView support forum. Please post your questions here
Post Reply
didierh
Posts: 1
Joined: Fri Apr 24, 2020 5:01 pm

Position cursor programatically

Post by didierh »

Hi
I'm trying to re-position the cursor at the last position after a table edition.

Practically my code is similar to this:
TRVTableItemInfo(Item).InsertColsRight(1);
RVE.Format;
After this the cursor is put just before the table.
I can (before inserting a column) retrieve the current cell position via the TRVTableInplaceEdit object but i'm unable to move my cursor to the previous position.
Is this behavior possible?

Thanks a lot fro your help.

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

Re: Position cursor programatically

Post by Sergey Tkachenko »

Do you really need to return the caret back to the place before the column insertion? This is not a normal behavior in most word processors. Normally, after insertion, an editor selects the inserted cells.

You should not call Format after InsertColRight. Format always moves the caret to the beginning of the document.
Instead of format, use BeginItemModify and EndItemModify.

Code: Select all

var item: TCustomRVItemInfo;
    table: TRVTableItemInfo;
    Data: Integer;
    rve: TCustomRichViewEdit;
    ItemNo: Integer;
begin
  if not RichViewEdit1.CanChange or
     not RichViewEdit1.GetCurrentItemEx(TRVTableItemInfo, rve, item) then
    exit;
  table := TRVTableItemInfo(item);
  ItemNo := rve.GetItemNo(table);
  rve.BeginItemModify(ItemNo, Data);
  table.InsertColsRight(1);
  rve.EndItemModify(ItemNo, Data);
  rve.Change;
end;
Sergey Tkachenko
Site Admin
Posts: 17291
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Position cursor programatically

Post by Sergey Tkachenko »

As for the caret positioning.

1) Solution 1: remember position as a count of characters from the beginning. You can use functions from RVLineaer: RVGetSelection/RVSetSelection, RVGetSelectionEx/RVSetSelectionEx, RVGetLinearCaretPos/RVSetLinearCaretPost.
However, these functions are only useful if the count of characters is not changed. InsertColLeft inserts empty cells before the caret position, they are counted as line break characters by the functions from RVLinear, so these functions cannot be used.

2) You can remember the current position as (RVData, position in this rvdata).
uses
CRVData, CRVFData;
var
RVData: TCustomRVData;
ItemNo1, Offs1, ItemNo2, Offs2: Integer;

store:
RVData := RichViewEdit1.TopLevelEditor.RVData.GetSourceRVData;
TCustomRVFormattedData(RVData).GetSelectionBoundsEx(ItemNo1, Off1, ItemNo2, Offs2, False);
restore:
RVData := RVData.Edit;
TCustomRVFormattedData(RVData).SetSelectionBounds(ItemNo1, Off1, ItemNo2, Offs2);
The second method is useful if the cell is not deleted between store and restore.
Post Reply