Manipulate pasted text

General TRichView support forum. Please post your questions here
Post Reply
Jim Knopf
Posts: 241
Joined: Mon Dec 30, 2013 10:07 pm
Location: Austria
Contact:

Manipulate pasted text

Post by Jim Knopf »

I want to check and eventually change styles of pasted text. But I can't find a way to find out which part of the text is the just pasted.
Sergey Tkachenko
Site Admin
Posts: 17560
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

OnPaste event occurs before pasting.
In this event, you can get data from the Clipboard, process it and insert it yourself.
If you want to modify RTF or RVF, the simplest way is to paste it in a hidden TRichViewEdit, modify it, save to TMemoryStream as RVF, and insert in the main editor using InsertRVFFromStreamEd.
Jim Knopf
Posts: 241
Joined: Mon Dec 30, 2013 10:07 pm
Location: Austria
Contact:

Post by Jim Knopf »

Thank you Sergey. I worried no more simple way is available.
Sergey Tkachenko
Site Admin
Posts: 17560
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

It's depend on what do you want to do.
If you want, for example, changing a text style for inserted plain text, simply set CurTextStyleNo property in OnPaste and leave DoDefault = True.

More complex manipulation requires parsing of the inserted content, and the simplest way to do it is to loading it in a separate control.
Jim Knopf
Posts: 241
Joined: Mon Dec 30, 2013 10:07 pm
Location: Austria
Contact:

Post by Jim Knopf »

I want to allow only certain para- and textstyles if somebody pastes - or drops - text from other software as e.g. Word or browser, but not completely plain; bold, italic, underline, strikeout is ok. In my opinion it's one of the most confusing experiences of users if they paste or drop text to their document and get a patchwork of styles as a result. Exactly that i prevent, allowing only bold, italic, ... but keep the basic 'normal' style (font, size ...).

I thougt there might be a trick, something as OnAfterPaste where pastetd text remains selected. So I could do it simply by the usual way via ApplyStyleConversion.

But I will try it out CurTextStyleNo and report my insight.
Sergey Tkachenko
Site Admin
Posts: 17560
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

CurTextStyleNo works only if you paste a plain text.
For not allowing some styles to be inserted, I am afraid only a hidden editor can be used.
Jim Knopf
Posts: 241
Joined: Mon Dec 30, 2013 10:07 pm
Location: Austria
Contact:

Post by Jim Knopf »

There are two situations where I have to check the inserted text: pasted and dropped.

In case of paste I did it with a hiden editor and it works simply and fine.

A little more extensive is after dropping. I solved it with a self made Windows message:

Code: Select all

procedure KMAfterOleDrop(var Msg:TMessage); message KM_AfterOleDrop;

...

procedure TfMain.rvOleDrop(Sender: T...);
begin
  PostMessage(Handle, KM_AfterOleDrop, 0 , 0);
end;

...

procedure TfMain.KMAfterOleDrop(var Msg: TMessage);
var S: string;
begin
  rv.ApplyStyleConversion(NORMALIZE_TEXT);
end;
It works because fortunately the dropped text is selected after dropping

In each case an event (OnAfterPasted and OnAfterDropped) would make it more comfortable and simple. It would be optimal if these events would return the pasted/dropped range of items/offsets.
Post Reply