• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 240
  • Last Modified:

RichEdit inside RIchEdit

I have a RichEdit1 component with some text in it, and I want to insert in some position of it the contents of another RichEdit2. If I use Streams and LoadFromStream, then all text in RichEdit1 gets overwritten by text in RichEdit2. And if I use RichEdit.Seltext = Richedit2.Seltext then I get RichEdit2's text, but without format identifiers as color, bold, underline. etc.

So my question is:

How can I put a Stream inside another Stream in order to put A Richedit2's text inside another richedit1's text without loosing format information?

Thanks ver much.
0
quique
Asked:
quique
  • 9
  • 6
  • 2
  • +1
1 Solution
 
jecksomCommented:
Hi quique!

Just save to stream richedit1 then save richedit2 to save stream  and then load from
stream richedit1 , is that solution ?

Best Regards ,
             Jecksom

0
 
jecksomCommented:
sorry,  "to save stream" = to same stream" :)

0
 
jecksomCommented:
Hi quiue!

Hm , it's not so easy as i tough before, after:

richedit1.Lines.SaveToStream(stream1);
richedit1.Lines.SaveToStream(stream2);
stream2.position:=stream2.position-1;
stream1.copyfrom(stream2,stream2.size);
stream1.position:=0;
memo1.lines.loadfromstream(stream1);

then in memo1 you'll get nice piece of two RTF-format fragments.
Here you can start parse them ! ;)

Best Regards ,
             Jecksom.

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
javiertbCommented:
I tried that code and didn't work. I got a 'Stream read error' at line stream1.copyfrom(stream2, stream2.size)
0
 
quiqueAuthor Commented:
jecksom, look at javiertb's comment to see if you can solve it
0
 
jecksomCommented:
procedure TForm1.Button1Click(Sender: TObject);
var
stream1:tmemorystream;
stream2:tmemorystream;
begin
stream1:=tmemorystream.create;
stream2:=tmemorystream.create;
richedit1.Lines.LoadFromFile('c:\test.rtf'); //my RTF file
richedit1.Lines.SaveToStream(stream1);
richedit1.Lines.SaveToStream(stream2);
stream1.position:=stream1.position-1;
stream2.Position:=0;
stream1.copyfrom(stream2,stream2.size);
stream1.position:=0;
memo1.lines.loadfromstream(stream1);
stream1.destroy;
stream2.destroy;
end;

Sorry , just place richedit ,memo and button on form and then link onbutton click with this event.

Jecksom

0
 
quiqueAuthor Commented:
No way. I also tried that, but it seems that I can only copy from one richedit to another but not to insert.
I'll explain better the background:

I have a little text editor with a RichEdit component. Then I have an option in this editor to insert templates. These templates are stored in a FormattedField (for the richedit component) of a table. What I'm trying to do is to insert this template right in the cursor position of the text in the editor.
Hope this helps a little more.

0
 
jecksomCommented:
Hi quique!

Hm , maybe OLE automation would be answer ? Weakness of this , you must have
MSWord installed ...

Jecksom

0
 
ThaddyCommented:
Why not use the clipboard?
Richedit1.CopyToClipBoard
Richedit2.pastefromclipboard

Delphi does all the streaming for you....
0
 
jecksomCommented:
Thaddy:

you right:

procedure TForm1.Button1Click(Sender: TObject);
begin
richedit1.Lines.LoadFromFile('c:\test1.rtf');
richedit1.SelectAll;
richedit1.CopyToClipboard;
richedit2.PasteFromClipboard;
end;

Works , i've tryed this before , but didn't get success , was little sleeping i guess :)

Jecksom

0
 
javiertbCommented:
The problem is that quique (I'm a friend of him) doesn't wan't to use the clipboard (maybe the user has something in it) and doesn't have msword installed, nor the final users. So the only way is streaming, but he can't get to make it work.
0
 
quiqueAuthor Commented:
Look at javiertb comment
0
 
jecksomCommented:
Streaming only , well , i have no idea right now , i'll let you know if  i'll get any success.
btw , you always can presave and then restore back buffer before merging two richedits.

Jecksom

0
 
jecksomCommented:
i'm talking about clipboard buffer saving and restoring.

0
 
quiqueAuthor Commented:
I don't want to use the clipboard. I want to use Streams!
0
 
quiqueAuthor Commented:
Well, I desist of streaming, but another problem is how can I copy a dbrichedit to the clipboard? because the TableField.CopyToClipboard doesn't work.
0
 
jecksomCommented:
Hi quique!

here is what you want using streaming :

Type
  TEditStreamCallBack = function (dwCookie: Longint; pbBuff: PByte;
    cb: Longint; var pcb: Longint): Longint; stdcall;

  TEditStream = record
    dwCookie: Longint;
    dwError: Longint;
    pfnCallback: TEditStreamCallBack;
  end;

function EditStreamInCallback(dwCookie: Longint; pbBuff: PByte;
 cb: Longint; var pcb: Longint): Longint; Stdcall;
var
  theStream: TStream;
  dataAvail: LongInt;
begin
  theStream := TStream(dwCookie);
  with theStream do begin
    dataAvail := Size - Position;
    Result := 0;
    if dataAvail <= cb then begin
      pcb := Read(pbBuff^, dataAvail);
      if pcb <> dataAvail then
        result := E_FAIL;
    end
    else begin
      pcb := Read(pbBuff^, cb);
      if pcb <> cb then
        result := E_FAIL;
    end;
  end;
end;


Function EditStreamOutCallback(dwCookie: Longint; pbBuff: PByte;
    cb: Longint; var pcb: Longint): Longint; stdcall;
 var
   theStream: TStream;
 begin
   theStream := TStream(dwCookie);

   with theStream do begin
     If cb > 0 Then
       pcb := Write(pbBuff^, cb);
     Result := 0;
   end;
 end;

procedure TForm1.Button2Click(Sender: TObject);
Var
  st: TMemoryStream;
  editstream: TEditStream;
begin
  richedit1.lines.loadfromfile('c:\test1.rtf');
  st := TMemoryStream.Create;
  try
  editstream.dwCookie:= Longint(st);
  editstream.dwError:= 0;
  editstream.pfnCallback:= EditStreamOutCallBack;
  richedit1.selectall;
  Richedit1.Perform( EM_STREAMOUT, SF_RTF or SFF_SELECTION,
                     longint(@editstream));
  st.Position := 0;
  editstream.dwCookie:= Longint(st);
  editstream.dwError:= 0;
  editstream.pfnCallback:= EditStreamInCallBack;
  Richedit2.Perform( EM_STREAMIN, SF_RTF or SFF_SELECTION,
                     longint(@editstream));
  finally
    st.Free;
  end;
end;

Place 2 richedits and one button , don't forget uses richedit and comctrls.

Best Regards,
Jecksom
0
 
quiqueAuthor Commented:
Thanks very much, it works fine!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 9
  • 6
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now