Recent

Author Topic: Clipboard formats  (Read 8317 times)

commodianus

  • New Member
  • *
  • Posts: 18
Clipboard formats
« on: March 31, 2010, 07:40:03 am »
Hi.

Say I copy some paragraphs of text to the clipboard in FireFox.

Now if I go into a program like Kompozer (wysiwyg HTML editor) and paste my text, it will retain the HTML formatting I copied from the page.

However, if I were to paste that text into a Memo for example, it just pastes plain text.

My question is, how can I make (for example) a Popup Menu Item to the effect of "Paste HTML" to do what the wysiwyg does in the Memo?

I realize I wouldn't see the same thing, but the tags would be there right?

Thanks.

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1927
Re: Clipboard formats
« Reply #1 on: March 31, 2010, 10:41:13 am »
I realize I wouldn't see the same thing, but the tags would be there right?

This works on my System (Linux):

Code: [Select]
procedure TForm1.Button1Click(Sender:TObject);
var Fid:integer;
Strm:TMemoryStream;
Str:WideString;
begin
 Fid:=Clipboard.FindFormatID('text/html');
 if Fid<>0 then
  begin
   Strm:=TMemoryStream.Create;
   if Clipboard.GetFormat(Fid,Strm) then
    begin
     Strm.Position:=0;
     Str:=PWideChar(Strm.Memory);
     Str[(Strm.Size div 2)+1]:=#0;
     Memo1.Text:=UTF8Encode(Str);
    end;
   Strm.free;
  end;
end;

commodianus

  • New Member
  • *
  • Posts: 18
Re: Clipboard formats
« Reply #2 on: March 31, 2010, 10:54:15 am »
Well done theo. Much appreciated.

commodianus

  • New Member
  • *
  • Posts: 18
Re: Clipboard formats
« Reply #3 on: March 31, 2010, 11:15:59 am »
It indeed does paste, but I'm having some odd behavior. With the modified code here, inserting the string seems to delete everything in the memo after the HTML pasted.

See:

Code: [Select]
var
s: string;
p: Integer;
Fid:integer;
Strm:TMemoryStream;
Str:WideString;
HTML: String;
begin


 Fid:=Clipboard.FindFormatID('text/html');
 if Fid<>0 then
  begin
   Strm:=TMemoryStream.Create;
   if Clipboard.GetFormat(Fid,Strm) then
    begin
     Strm.Position:=0;
     Str:=PWideChar(Strm.Memory);
     Str[(Strm.Size div 2)+1]:=#0;
     HTML := UTF8Encode(Str);
  s := Memo1.Lines.Text;
  p :=  Memo1.SelStart + Length(HTML);
  system.Insert(HTML, s, Memo1.SelStart+1);
  Memo1.Lines.Text := s;
  Memo1.SelStart := p;
    end;
   Strm.free;
  end;
 

Ideas?

commodianus

  • New Member
  • *
  • Posts: 18
Re: Clipboard formats
« Reply #4 on: April 01, 2010, 07:45:50 am »
Solution:

Quote
procedure TForm1.PasteHTMLExecute(Sender: TObject);
var
s, HTML: String;
p, Fid: Integer;
Strm: TMemoryStream;
Str: WideString;
begin


 Fid:=Clipboard.FindFormatID('text/html');
 if Fid<>0 then
  begin
   Strm:=TMemoryStream.Create;
   if Clipboard.GetFormat(Fid,Strm) then
    begin


  p := 0;
  Strm.Write(p, 2);
  Strm.Position := 0;
  Str:=PWideChar(Strm.Memory);

     HTML := UTF8Encode(Str);
    end;
   Strm.free;
  s := Memo1.Lines.Text;
  p :=  Memo1.SelStart + Length(HTML);


  System.Insert(HTML, s, Memo1.SelStart+1);
  Memo1.Lines.Text := s;
  Memo1.SelStart := p-2;
  end;

end;    

I've been told this code doesn't work under Windows, though that's ok for me at this time.

 

TinyPortal © 2005-2018