Page 1 of 1

Position cursor programatically

Posted: Fri Apr 24, 2020 5:14 pm
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

Re: Position cursor programatically

Posted: Sat Apr 25, 2020 10:13 am
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;

Re: Position cursor programatically

Posted: Sat Apr 25, 2020 10:34 am
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.