Board index » delphi » Copying RTF from one Richedit to another

Copying RTF from one Richedit to another

OK, I'm writing a word processing application that uses 2 richedits - one
visible and the other hidden. At various times I need to copy the complete
RTF data from the visible to the invisible edit. Now although this is easy
using a memory stream, the problem is that as soon as I do that it seems to
empty the Undo list! I'm using the new RichEdit 3.0 dll by the way.

So, anyone know a) Why does the Undo memory empty? and b) any other way
anyone knows to get the complete RTF data out of a RichEdit, other than
SaveToStream?

On a similar subject, it's reported that the new Richedit 3.0 can turn undo
memorizing on and off as desired. Anyone know how?

If worst comes to worst I'll have to write my own undo / redo code, which is
a bit of a shame...

Many thanks for any help!

Martin Coster

 

Re:Copying RTF from one Richedit to another


I can only answer question a):

Remember what a word processor (like Word) does when you save your document.
It clears the undo buffer ! Why ? Because 'Undo' means you want to undo an
action that has been performed since the last saved version. The Delphi
editor has the possibility to change the save/undo behaviour with Tools |
Environment Options... | Editor | Undo after save. Maybe you can find
something in the RichEdit 3.0 specs that does the same thing...

"Martin R. Coster" <m...@pacific.net.sg> schreef in bericht
news:8k4erf$2dj$1@newton.pacific.net.sg...

Quote
> OK, I'm writing a word processing application that uses 2 richedits - one
> visible and the other hidden. At various times I need to copy the complete
> RTF data from the visible to the invisible edit. Now although this is easy
> using a memory stream, the problem is that as soon as I do that it seems
to
> empty the Undo list! I'm using the new RichEdit 3.0 dll by the way.

> So, anyone know a) Why does the Undo memory empty? and b) any other way
> anyone knows to get the complete RTF data out of a RichEdit, other than
> SaveToStream?

> On a similar subject, it's reported that the new Richedit 3.0 can turn
undo
> memorizing on and off as desired. Anyone know how?

> If worst comes to worst I'll have to write my own undo / redo code, which
is
> a bit of a shame...

> Many thanks for any help!

> Martin Coster

Re:Copying RTF from one Richedit to another


Im Artikel <8k4erf$2d...@newton.pacific.net.sg>, "Martin R. Coster"
<m...@pacific.net.sg> schreibt:

Quote
>b) any other way
>anyone knows to get the complete RTF data out of a RichEdit, other than
>SaveToStream?

Why not assign the Text property to the other control?
Perhaps you must set PlainText to False before!

DoDi

Re:Copying RTF from one Richedit to another


Quote
"Martin R. Coster" wrote:
> OK, I'm writing a word processing application that uses 2 richedits - one
> visible and the other hidden. At various times I need to copy the complete
> RTF data from the visible to the invisible edit. Now although this is easy
> using a memory stream, the problem is that as soon as I do that it seems to
> empty the Undo list! I'm using the new RichEdit 3.0 dll by the way.

Sigh ... I already replied to this, and Netscape crashed while sending
it.

Quote
> So, anyone know a) Why does the Undo memory empty?

"By design", presumably. ;-) It does it w/ 2.0, too.

Quote
> b) any other way
> anyone knows to get the complete RTF data out of a RichEdit, other than
> SaveToStream?

Haven't tried it, but it's *possible* that EM_STREAMOUT with the
SFF_SELECTION flag will not wipe out undo.

Quote
> On a similar subject, it's reported that the new Richedit 3.0 can turn undo
> memorizing on and off as desired. Anyone know how?

EM_EMPTYUNDOBUFFER and EM_SETUNDOLIMIT.

--

http://www.midnightbeach.com    - Me, my work, my writing, and
http://www.midnightbeach.com/hs - my homeschool resource pages

Re:Copying RTF from one Richedit to another


Problem is that will not copy the RTF formatting - such as indents and
paragraph styles etc...

Thanks anyway!

Martin.

Quote
VBDis wrote in message <20000709142550.15680.00006...@nso-bj.aol.com>...
>Im Artikel <8k4erf$2d...@newton.pacific.net.sg>, "Martin R. Coster"
><m...@pacific.net.sg> schreibt:

>>b) any other way
>>anyone knows to get the complete RTF data out of a RichEdit, other than
>>SaveToStream?

>Why not assign the Text property to the other control?
>Perhaps you must set PlainText to False before!

>DoDi

Re:Copying RTF from one Richedit to another


Hello Martin,
downwards You will find some function to do the Copy task. The function
Copys the *selected* textparts only!
So if You want to copy the whole stuff You have to do something like
this
      BufEdit.SelectAll;
      CopyRTF(BufEdit,ResultEdit);

where BufEdit and ResultEdit a TRichEdit component.
The Source is not developed by me, but it was placed in a NewsGroup some
month ago.
I dont know if it work with all Richedit librarys - have a test!
Best regards
-=-=-=Source starts
type TEditStreamCallBack = function (dwCookie: Longint; pbBuff: PByte;
cb: Longint; var pcb: Longint): DWORD; stdcall;

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

function EditStreamInCallback(dwCookie: Longint; pbBuff: PByte;
cb: Longint; var pcb: Longint): DWORD; Stdcall;
var
  theStream: TStream;
  dataAvail: LongInt;
begin
  theStream := TStream(dwCookie);
  with theStream do
    begin
      dataAvail := Size - Position;
      Result := 0; {assume everything is ok}
      if dataAvail <= cb then
        begin
          pcb := Read(pbBuff^, dataAvail);
          if pcb <> dataAvail then {couldn't read req. amount of bytes}
            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): DWORD; 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 GetRTFSelection( aRichEdit: TRichEdit; intoStream: TStream );
var editstream: TEditStream;
begin
  with editstream do
    begin
      dwCookie:= Longint(intoStream);
      dwError:= 0;
      pfnCallback:= EditStreamOutCallBack;
    end;
  aRichedit.Perform( EM_STREAMOUT, SF_RTF or SFF_SELECTION,
longint(@editstream));
end;

procedure PutRTFSelection( aRichEdit: TRichEdit; sourceStream: TStream
);
var editstream: TEditStream;
begin
  with editstream do
    begin
      dwCookie:= Longint(sourceStream);
      dwError:= 0;
      pfnCallback:= EditStreamInCallBack;
    end;
  aRichedit.Perform( EM_STREAMIN, SF_RTF or SFF_SELECTION,
  longint(@editstream));
end;

procedure InsertRTF(aRichEdit:TRichEdit; s : String);
var aMemStream: TMemoryStream;
begin
  if Length(s) > 0 then
    begin
      aMemStream := TMemoryStream.Create;
      try
        aMemStream.Write(s[1],length(s));
        aMemStream.Position := 0;
        PutRTFSelection( aRichEdit, aMemStream );
      finally
        aMemStream.Free;
      end;
    end;
end;

procedure CopyRTF(aSource, aDest:TRichEdit);
var aMemStream: TMemoryStream;
begin
  aMemStream := TMemoryStream.Create;
  try
    GetRTFSelection(aSource, aMemStream );
    aMemStream.Position := 0;
    PutRTFSelection(aDest, aMemStream );
  finally
    aMemStream.Free;
  end;
end;
-=-=-=-=Source ends

Quote
"Martin R. Coster" wrote:

> OK, I'm writing a word processing application that uses 2 richedits - one
> visible and the other hidden. At various times I need to copy the complete
> RTF data from the visible to the invisible edit. Now although this is easy
> using a memory stream, the problem is that as soon as I do that it seems to
> empty the Undo list! I'm using the new RichEdit 3.0 dll by the way.

> So, anyone know a) Why does the Undo memory empty? and b) any other way
> anyone knows to get the complete RTF data out of a RichEdit, other than
> SaveToStream?

> On a similar subject, it's reported that the new Richedit 3.0 can turn undo
> memorizing on and off as desired. Anyone know how?

> If worst comes to worst I'll have to write my own undo / redo code, which is
> a bit of a shame...

> Many thanks for any help!

> Martin Coster

--
DomIS Internet Solution - Ekkehard Domning
Spahner Str. 11 - D-49751 S?gel
Tel: +49-5952-9889-5 Fax: +49-5952-9889-6 Mobil: +49-172-8327978
Mail: e...@domis.de Internet: http://www.domis.de

Re:Copying RTF from one Richedit to another


Martin R. Coster wrote

Quote
> OK, I'm writing a word processing application that uses 2 richedits - one
> visible and the other hidden. At various times I need to copy the complete
> RTF data from the visible to the invisible edit. Now although this is easy
> using a memory stream, the problem is that as soon as I do that it seems
to
> empty the Undo list! I'm using the new RichEdit 3.0 dll by the way.

I'm probably getting confused, but why not:

    hiddenRichEdit.Lines.Assign(visibleRichEdit.Lines);

Or are you talking about a different component?

Other Threads