Page 1 of 1

Manipulate pasted text

Posted: Wed Apr 13, 2016 4:57 pm
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.

Posted: Thu Apr 14, 2016 6:43 am
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.

Posted: Thu Apr 14, 2016 9:28 am
by Jim Knopf
Thank you Sergey. I worried no more simple way is available.

Posted: Thu Apr 14, 2016 11:12 am
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.

Posted: Thu Apr 14, 2016 11:49 am
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.

Posted: Fri Apr 15, 2016 7:28 am
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.

Posted: Fri Apr 15, 2016 8:39 am
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.