Controlling and formatting dropped 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:

Controlling and formatting dropped text

Post by Jim Knopf »

Hello Sergey,
referring to the problem of paste and formatting, I have a question how to intercept and control the process in OleDrop.

With OnPaste I can easily redirect the clipboard to the invisible RichViewEdit.
But how can I do that with OnOleDrop?

Greetings
Martin
standay
Posts: 274
Joined: Fri Jun 18, 2021 3:07 pm

Re: Controlling and formatting dropped text

Post by standay »

Martin,

This isn' Sergey but I can tell you what I do. Sergey may have a better answer. I figured you might post about file drops.

I use DropFiles and process things there. This is a part of what I do for file drops (dropping text would be different):

Code: Select all

procedure TForm1.rveDropFiles...

  success := false;
  DoDefault := false;
  
  try
    
    if (ExtractFileExt(Files[0]).ToLower = '.rvf') then
    begin
      Stream := TMemoryStream.Create;
      Stream.LoadFromFile(Files[0]);
      Stream.Position := 0;
      success := ActiveRVE.InsertRVFFromStreamEd(Stream);
      Stream.Free;
      exit;
    end;
    
    //do more file types here as needed...

  finally

    ActiveRVE.ReFormat;

    if success then
      ActiveRVE.Modified := true;

  end;
I use DropFiles rather than DropFile so I can handle more than one file being dropped. You'd have to add in code similar to what is in your other related forum post to reformat things the way you want.

Dropping text will probably need to use OleDrop and the reformat code from your other post.

Stan
Jim Knopf
Posts: 241
Joined: Mon Dec 30, 2013 10:07 pm
Location: Austria
Contact:

Re: Controlling and formatting dropped text

Post by Jim Knopf »

Hi Stan,

thank you very much for your answer.
But unfortunately it is about dropping text from for example Word into a TRichViewEdit or TSRichViewEdit respectively. Similar to my question with dropping text, where the text should be adapted to the target formatting, but markups (bold, italic etc.) should be taken over.
The paste problem is now solved, but now someone pointed out to me that there is also drag'n'drop ...

Greetings
Martin
standay
Posts: 274
Joined: Fri Jun 18, 2021 3:07 pm

Re: Controlling and formatting dropped text

Post by standay »

Martin,

Yes, but you may want to handle a file getting dropped as well.

For text out of Word, I'd guess using the one of the OleDrop functions will be what you'd want to use. I use AfterOleDrop to resize images that get dropped into my app so I tried that and it worked. I created some styled text in Worpad and Word:

Testing one, two, three.

I selected that and dragged it into my rve. I tried it from both Wordpad and Word. The text dropped in my rve was styled but in a different font than what my app is set to use.

The text I dropped into the rve was already selected after the drop, so in AfterOleDrop I applied the font name my app is set to (Georgia) and it changed the font name and left the styled words.

So in your case, after dragging in the text, you could copy the dropped text in the rve, run it through the format code you have in the other post, then paste it back in and that should do it.

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

Re: Controlling and formatting dropped text

Post by Sergey Tkachenko »

There is OnOleDrop event.
But it's not so simple because you need to get data from IDataObject (there is no public method of TRichViewEdit that can insert data from IDataObject).
There is an additional difficulty when moving selection within the same editor (you need to delete selection and keep the insertion position while deleting).
It makes custom processing of drag&drop a non-trivial task.
Jim Knopf
Posts: 241
Joined: Mon Dec 30, 2013 10:07 pm
Location: Austria
Contact:

Re: Controlling and formatting dropped text

Post by Jim Knopf »

Hi Stan,
unfortunately, the text is not selected for me after dropping. Therefore I cannot use this method.

Sergey, could you please tell me which (undocumented) methods are responsible for dropping text?
Jim Knopf
Posts: 241
Joined: Mon Dec 30, 2013 10:07 pm
Location: Austria
Contact:

Re: Controlling and formatting dropped text

Post by Jim Knopf »

Code: Select all

procedure TfMain.rvOleDrop(Sender: TCustomRichView; const DataObject: IDataObject; Shift: TShiftState; X, Y: Integer;
          PossibleDropEffects: TRVOleDropEffects; var DropEffect: TRVOleDropEffect; var DoDefault: Boolean);
var
  EnumFormatEtc: IEnumFormatEtc;
  FormatEtc: TFormatEtc;
  Medium: TStgMedium;
  PData: PChar;
  AText: string;
  CF_RTF: uint;
  Stream: TStringStream;
begin
  DoDefault := True;
  AText := '';

  if DataObject.EnumFormatEtc(DATADIR_GET, EnumFormatEtc) = S_OK then
  begin
    CF_RTF := RegisterClipboardFormat('Rich Text Format');
    while (EnumFormatEtc.Next(1, FormatEtc, nil) = S_OK) and DoDefault do
    begin
      if FormatEtc.cfFormat = CF_RTF then
      begin
        if DataObject.GetData(FormatEtc, Medium) = S_OK then
        begin
          PData := GlobalLock(Medium.hGlobal);
          AText := PData;
          GlobalUnlock(Medium.hGlobal);
          DoDefault := False;

          STream := TStringStream.Create(AText);
          try
            Stream.Position := 0;
            rvH.InsertRTFFromStreamEd(Stream);
            rvH.Format;
            ConvertRichtext(True);
          finally
            Stream.Free;
          end;
        end;
      end;
    end;
  end;
end;
This routine basically works fine. My problem, however, is that the next time I drag'n'drop, the text is doubled. This is probably because I am not emptying the interface. How can I do this?
standay
Posts: 274
Joined: Fri Jun 18, 2021 3:07 pm

Re: Controlling and formatting dropped text

Post by standay »

Maybe:

Code: Select all

ReleaseStgMedium(Medium);
?

Not sure but that might do it.

Stan
Jim Knopf
Posts: 241
Joined: Mon Dec 30, 2013 10:07 pm
Location: Austria
Contact:

Re: Controlling and formatting dropped text

Post by Jim Knopf »

Hi Stan,

Unfortunately this does not help either.

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

Re: Controlling and formatting dropped text

Post by Sergey Tkachenko »

There is no a separate method that loads from IDataObject.
TCustomRichViewEdit.OleDrop is called when dropping data to TRichViewEdit; it calls OnOleDrop (also BeforeOleDrop, AfterOleDrop, and maybe special events related to dropping files), reads data from IDataObject, inserts data, selects the inserted fragment.

I thought about this problem. Probably, the best solution would be adding a new event OnDropFromStream, which will be called when dropping RVF or RTF format. You will be able to replace content in this stream.
Would this solution be ok?
Jim Knopf
Posts: 241
Joined: Mon Dec 30, 2013 10:07 pm
Location: Austria
Contact:

Re: Controlling and formatting dropped text

Post by Jim Knopf »

Thanks for your thoughts. I have given up on a solution in the meantime and exclude rvddrRTF at AcceptedDragDropFormats. So you can only drop from RVF to RVF or unformatted text. If a user wants to paste from Word, then he just has to do it via copy&paste.

But I have another problem I've been experimenting with for a while and can't get it to work. I'll post it in a new thread.

Edit - problem solved
Post Reply