Forum > LCL

Clipboard formats

(1/1)

commodianus:
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:

--- Quote from: commodianus on March 31, 2010, 07:40:03 am ---I realize I wouldn't see the same thing, but the tags would be there right?

--- End quote ---

This works on my System (Linux):


--- Code: ---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;

--- End code ---

commodianus:
Well done theo. Much appreciated.

commodianus:
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: ---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;
 
--- End code ---

Ideas?

commodianus:
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;    
--- End quote ---

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

Navigation

[0] Message Index

Go to full version