Intercepting pasted content

General TRichView support forum. Please post your questions here
Post Reply
aoven
Posts: 45
Joined: Wed Nov 09, 2005 7:28 pm

Intercepting pasted content

Post by aoven »

Is there a way to intercept content that is being pasted and modify it just before it integrates with the document?

The problem I'm facing is as follows:

User can mark a few text blocks and attach string tags to them. Each block receives a unique tag by which program can identify the block later on. Now, if user were to select one or more tagged blocks and copy&paste them back into document, they'd get multiple blocks with the same tag, which I need to avoid somehow.

What can I do?

Aleksander
Sergey Tkachenko
Site Admin
Posts: 17566
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

OnPaste event.
In this event, you can insert data in your own format, and set DoDefault parameter to False to prevent default pasting.
aoven
Posts: 45
Joined: Wed Nov 09, 2005 7:28 pm

Post by aoven »

I managed to solve the problem by creating a temporary TRichView in the OnPaste event and process the pasted RVF before inserting it into the target editor.

Thanks for the tip!

Aleksander
geurt
Posts: 1
Joined: Mon Sep 12, 2005 11:07 am
Location: Deventer, The Netherlands
Contact:

Intercepting pasted content

Post by geurt »

aoven wrote:I managed to solve the problem by creating a temporary TRichView in the OnPaste event and process the pasted RVF before inserting it into the target editor.

Thanks for the tip!

Aleksander
I have the same problem. Could you post a sample of how to paste the clipboard contents into a tempory editor?

Thanks,

Geurt Lagemaat
aoven
Posts: 45
Joined: Wed Nov 09, 2005 7:28 pm

Post by aoven »

Sure, this is how I did it:

Code: Select all

procedure TSourceDocDesigner.rvEditPaste(Sender: TCustomRichViewEdit;
  var DoDefault: Boolean);
var
  RV: TRichView;
  RS: TRVStyle;

  MS: TMemoryStream;
  hMem: Cardinal;
  P: Pointer;
  Size: Integer;
begin
  if Clipboard.HasFormat(CFRV_RVF) then begin
    RV := TRichView.Create(nil);
    RS := TRVStyle.Create(nil);
    RV.Style := RS;
    try
      RV.Options := RV.Options + [rvoTagsArePChars];
      RV.RTFReadProperties.ParaStyleMode := rvrsAddIfNeeded;
      RV.RTFReadProperties.TextStyleMode := rvrsAddIfNeeded;
      RV.RTFReadProperties.UnicodeMode := rvruOnlyUnicode;
      RV.RVFOptions := RV.RVFOptions + [rvfoSaveTextStyles, rvfoSaveParaStyles];

      Clipboard.Open;
      MS := TMemoryStream.Create;
      try
        hMem := Clipboard.GetAsHandle(CFRV_RVF);
        Size := GlobalSize(hMem);
        MS.SetSize(Size);
        P := GlobalLock(hMem);
        Move(P^, MS.Memory^, Size);
        GlobalUnlock(hMem);
        MS.Seek(0, soBeginning);
        MS.ReadBuffer(Size, SizeOf(Size));

        MS.SetSize(SizeOf(Size) + Size);

        MS.Seek(SizeOf(Size), soBeginning);
        RV.LoadRVFFromStream(MS);
      finally
        MS.Free;
        Clipboard.Close;
      end;

      // do something with RVF in temp editor

      // insert the modified temp RVF to target editor at caret
      MS := TMemoryStream.Create;
      try
        RV.SaveRVFToStream(MS, False);
        MS.Seek(0, soBeginning); 
        rvEdit.InsertRVFFromStreamEd(MS);
      finally
        MS.Free;
      end;
    finally
      RV.Style := nil;
      RS.Free;
      RV.Free;
    end;

    DoDefault := False;
  end;
end;
Sergey Tkachenko
Site Admin
Posts: 17566
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

This code may be simplified, if you create a TRichViewEdit, assign its parent and paste RVF using PasteRVF method.

But your code is faster because it does not perform document formatting.
aoven
Posts: 45
Joined: Wed Nov 09, 2005 7:28 pm

Post by aoven »

I did consider using PasteRVF, but instead chose to go this way because I didn't need formatting.

Aleksander
Post Reply