Access objects from rvf

General TRichView support forum. Please post your questions here
Post Reply
haidomingo
Posts: 16
Joined: Tue Nov 10, 2009 7:27 pm

Access objects from rvf

Post by haidomingo »

Hi,
if I create objects like:

Code: Select all

SRichViewEdit1.RichViewEdit.InsertItem('Table1',Table1);
(Table1 : TRVTableItemInfo;)
And save the file as rvf.
When I load the rvf file, how do I access the Table1 object, and assign to Table1 var?
(By the name 'Table1'?).

Another questions: how to delete and free Table1?
Thanks for this great support.
Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Access objects from rvf

Post by Sergey Tkachenko »

Here is the function retuning table having the specified name.

Code: Select all

function FindTable(RVData: TCustomRVData; const Name: String): TRVTableItemInfo;
var
  i, r, c: Integer;
  Table: TRVTableItemInfo;
begin
  for i := 0 to RVData.ItemCount - 1 do
    if RVData.GetItem(i) is TRVTableItemInfo then
    begin
      Table := TRVTableItemInfo(RVData.GetItem(i));
      if RVData.GetItemText(i) = Name then
      begin
        Result := Table;
        exit;
      end;
      for r := 0 to Table.RowCount - 1 do
        for c := 0 to Table.ColCount - 1 do
          if Table.Cells[r, c] <> nil then
          begin
            Result := FindTable(Table.Cells[r, c].GetRVData, Name);
            if Result <> nil then
              exit;
          end;
    end;
  Result := nil;
end;
Call:

Code: Select all

Table := FindTable(SRichViewEdit1.RichViewEdit.RVData, 'Table1');
This code could be simple if we were sure that the table is inserted directly in the editor, not in a cell of another table.
Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Access objects from rvf

Post by Sergey Tkachenko »

As for deleting: do you need to delete it as an editing (undoable) operation?
If yes, then

Code: Select all

procedure DeleteTable(rve: TCustomRichViewEdit; Table: TRVTableItemInfo);
var
  RVData: TCustomRVData;
  ItemNo: Integer;
begin
  RVData := Table.Cells[0,0].GetAbsoluteParentData;
  ItemNo := Table.GetMyItemNo;
  // Table is the ItemNo-th item in RVData.
  RVData := RVData.Edit;
  TCustomRVFormattedData(RVData).SetSelectionBounds(ItemNo, 0, ItemNo, 1);
  rve.DeleteSelection;
end;
Call:

Code: Select all

DeleteTable(SRichViewEdit1.RichViewEdit, Table);
This code does not free the table immediately, it moves it to the undo buffer. The table can be returned back by Undo command, or freed when the the undo buffer is cleared (when ClearUndo or Clear is called, or when SRichViewEdit1 is destroyed)
haidomingo
Posts: 16
Joined: Tue Nov 10, 2009 7:27 pm

Re: Access objects from rvf

Post by haidomingo »

Sergey Tkachenko wrote: Mon May 08, 2017 7:39 pm ...
Call:

Code: Select all

DeleteTable(SRichViewEdit1.RichViewEdit, Table);
...
Thanks, works.
But

Code: Select all

DeleteTable(DeleteTable(SRichViewEdit1.[b]RVHeader[/b],Table);
It does not work well.
Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Access objects from rvf

Post by Sergey Tkachenko »

This code must work for the header only if the header is being edited. Otherwise the content of RVHeader is undefined, and the actual header is stored in SubDocuments[] property.
So, before calling DeleteTable, call

Code: Select all

SRichViewEdit1.StartEditing(srvrveHeader)
Post Reply