Can't load RTF in a table cell programmatically

General TRichView support forum. Please post your questions here
Post Reply
merovingian
Posts: 2
Joined: Tue Apr 10, 2007 4:39 pm
Location: London / Italy

Can't load RTF in a table cell programmatically

Post by merovingian »

Hi to everybody,
I've a dummy problem i can't solve.
I need to put RTF text in a table cell programmatically, so i'm trying to obtain the in place editor, for having the InsertRTFFromStreamEd method,
but i'm not able to obtain a GetRVData as TRichViewRVData.
This should-be-simple-code desn't work, probably i've missed something important...

Code: Select all

procedure TForm1.BitBtn3Click(Sender: TObject);

const RTFString='{\rtf1\ansi\ansicpg1252\deff0\deflang1040{\fonttbl{\f0\fswiss\fprq2\fcharset0 Arial;}{\f1\fswiss\fcharset0 Arial;}}{\*\generator Msftedit 5.41.15.1507;}\viewkind4\uc1\pard\ul\b\f0\fs24 RTF Text\ulnone\b0\f1\fs20\par}';

var table: TRVTableItemInfo;
    stringStream: TStringStream;
    RVData: TCustomRVData;

begin
    RichViewEdit1.Clear;

    stringStream := TStringStream.Create(RTFString);
    try
      RichViewEdit1.InsertRTFFromStreamEd(stringStream);
      RichViewEdit1.InsertText(#13#13);

      table := TRVTableItemInfo.CreateEx(3,3, RichViewEdit1.RVData);
      try
        table.EditCell(1,1);
        RVData:=table.Cells[1,1].GetRVData;
        stringStream.WriteString(RTFString);

        if RVData is TRVEditRVData then
          (TRVEditRVData(RVData).RichView as TCustomRichViewEdit).InsertRTFFromStreamEd(stringStream);

      finally
        RichViewEdit1.InsertItem('', table)
      end;

    finally
      stringStream.Free;
    end;
end;
I'm using Delphi 2006 and TRichView 1.9.24.
Thank you very much for any advice.
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

You can load RTF directly in table cell before inserting table in the editor:

Code: Select all

procedure TForm3.ToolButton64Click(Sender: TObject);

const RTFString='{\rtf1\ansi\ansicpg1252\deff0\deflang1040{\fonttbl{\f0\fswiss\fprq2\fcharset0 Arial;}{\f1\fswiss\fcharset0 Arial;}}{\*\generator Msftedit 5.41.15.1507;}\viewkind4\uc1\pard\ul\b\f0\fs24 RTF Text\ulnone\b0\f1\fs20\par}';

var table: TRVTableItemInfo;
    stringStream: TStringStream;

begin
    RichViewEdit1.Clear;

    stringStream := TStringStream.Create(RTFString);
    try
      RichViewEdit1.InsertRTFFromStreamEd(stringStream);
      RichViewEdit1.InsertText(#13#13);

      table := TRVTableItemInfo.CreateEx(3,3, RichViewEdit1.RVData);
      stringStream.Position := 0;
      table.Cells[1,1].Clear;
      table.Cells[1,1].LoadRTFFromStream(stringStream);
      RichViewEdit1.InsertItem('', table)

    finally
      stringStream.Free;
    end;
end;
Last edited by Sergey Tkachenko on Thu Apr 12, 2007 7:09 pm, edited 1 time in total.
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Or you can use cell editing. But you can edit table cell only AFTER inserting the table in editor:

Code: Select all

procedure TForm3.ToolButton64Click(Sender: TObject);

const RTFString='{\rtf1\ansi\ansicpg1252\deff0\deflang1040{\fonttbl{\f0\fswiss\fprq2\fcharset0 Arial;}{\f1\fswiss\fcharset0 Arial;}}{\*\generator Msftedit 5.41.15.1507;}\viewkind4\uc1\pard\ul\b\f0\fs24 RTF Text\ulnone\b0\f1\fs20\par}';

var table: TRVTableItemInfo;
    stringStream: TStringStream;

begin
    RichViewEdit1.Clear;

    stringStream := TStringStream.Create(RTFString);
    try
      RichViewEdit1.InsertRTFFromStreamEd(stringStream);
      RichViewEdit1.InsertText(#13#13);
      table := TRVTableItemInfo.CreateEx(3,3, RichViewEdit1.RVData);
      RichViewEdit1.InsertItem('', table);
      table.EditCell(1,1);
      stringStream.Position := 0;
      RichViewEdit1.InsertRTFFromStreamEd(stringStream);

    finally
      stringStream.Free;
    end;
end;
This code and the code above insert exactly the same content.
The difference is in undo (the first example inserts table with content as one operation; the second example uses 2 operations for this) and in ending caret position.
merovingian
Posts: 2
Joined: Tue Apr 10, 2007 4:39 pm
Location: London / Italy

Thank you very much!

Post by merovingian »

I was sure the solution was as simple as it seemed... but i wasn't able to find it! :-)
I prefer the first solution you proposed.
Thank you again for your support, and congratulations for your work. It's a week I'm evaluating TRichView, and i found it very interesting.

byebye
Post Reply