Page 1 of 1

Access objects from rvf

Posted: Mon May 08, 2017 6:31 am
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.

Re: Access objects from rvf

Posted: Mon May 08, 2017 7:32 pm
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.

Re: Access objects from rvf

Posted: Mon May 08, 2017 7:39 pm
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)

Re: Access objects from rvf

Posted: Tue May 09, 2017 10:39 am
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.

Re: Access objects from rvf

Posted: Tue May 09, 2017 11:31 am
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)