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.
LVL 1
quiqueAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
jecksomConnect With a Mentor Commented:
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
 
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
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
 
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
 
quiqueAuthor Commented:
Thanks very much, it works fine!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.