Page 1 of 1
Intercepting pasted content
Posted: Fri Feb 03, 2006 10:07 pm
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
Posted: Sat Feb 04, 2006 8:40 am
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.
Posted: Mon Feb 06, 2006 1:10 pm
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
Intercepting pasted content
Posted: Mon Feb 06, 2006 2:51 pm
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
Posted: Mon Feb 06, 2006 3:40 pm
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;
Posted: Mon Feb 06, 2006 5:22 pm
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.
Posted: Tue Feb 07, 2006 10:40 am
by aoven
I did consider using PasteRVF, but instead chose to go this way because I didn't need formatting.
Aleksander